Skip to content

Commit

Permalink
Fixed binding to loopback and parsing of incoming client socket for v…
Browse files Browse the repository at this point in the history
…alidation
  • Loading branch information
mondain committed Jun 1, 2021
1 parent 81a5c81 commit d4d8114
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<red5-io.version>${project.parent.version}</red5-io.version>
<red5-service.version>${project.parent.version}</red5-service.version>
<red5-server-common.version>${project.parent.version}</red5-server-common.version>
<tomcatplugin.version>2.2.8</tomcatplugin.version>
<tomcatplugin.version>2.2.9</tomcatplugin.version>
<cors-filter.version>2.6</cors-filter.version>
</properties>
<repositories>
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/org/red5/server/service/ShutdownServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public void destroy() throws Exception {
* Starts internal server listening for shutdown requests.
*/
public void start() {
log.info("Shutdown server start");
// dump to stdout
System.out.printf("Token: %s%n", token);
// write out the token to a file so that red5 may be shutdown external to this VM instance.
Expand All @@ -147,11 +148,20 @@ public void start() {
log.warn("Exception handling token file", e);
}
final InetAddress loopbackAddr = InetAddress.getLoopbackAddress();
// handle format of localhost/127.0.0.1
final String localAddr = loopbackAddr.getHostAddress();
int pos = localAddr.indexOf('/');
final String localIPAddr = pos >= 0 ? localAddr.substring(pos) : localAddr;
log.info("Starting socket server on {}:{}", localIPAddr, port);
while (!shutdown.get()) {
// server socket on loopback address (127.0.0.1) with given port and a backlog of 8
try (ServerSocket serverSocket = new ServerSocket(port, 8, loopbackAddr); Socket clientSocket = serverSocket.accept(); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));) {
log.info("Connected - local: {} remote: {}", clientSocket.getLocalSocketAddress(), clientSocket.getRemoteSocketAddress());
if (clientSocket.getRemoteSocketAddress().toString().startsWith(loopbackAddr.toString())) {
String remoteAddr = clientSocket.getRemoteSocketAddress().toString();
// handle SocketAddress format of /127.0.0.1:9999
String remoteIPAddr = remoteAddr.substring(1, remoteAddr.indexOf(':'));
log.info("IP addresses - local: {} remote: {}", localIPAddr, remoteIPAddr);
if (localIPAddr.equals(remoteIPAddr)) {
String inputLine = in.readLine();
if (inputLine != null && token.equals(inputLine)) {
log.info("Shutdown request validated using token");
Expand Down

0 comments on commit d4d8114

Please sign in to comment.