Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Red5/red5-server
Browse files Browse the repository at this point in the history
  • Loading branch information
mondain committed Jun 1, 2021
2 parents 62ed61f + 5ffcada commit 81a5c81
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/main/java/org/red5/server/service/ShutdownServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.net.BindException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
Expand Down Expand Up @@ -145,16 +146,22 @@ public void start() {
} catch (Exception e) {
log.warn("Exception handling token file", e);
}
final InetAddress loopbackAddr = InetAddress.getLoopbackAddress();
while (!shutdown.get()) {
try (ServerSocket serverSocket = new ServerSocket(port); Socket clientSocket = serverSocket.accept(); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));) {
// 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());
String inputLine = in.readLine();
if (inputLine != null && token.equals(inputLine)) {
log.info("Shutdown request validated using token");
out.println("Ok");
shutdownOrderly();
if (clientSocket.getRemoteSocketAddress().toString().startsWith(loopbackAddr.toString())) {
String inputLine = in.readLine();
if (inputLine != null && token.equals(inputLine)) {
log.info("Shutdown request validated using token");
out.println("Ok");
shutdownOrderly();
} else {
out.println("Invalid input");
}
} else {
out.println("Bye");
out.println("Invalid requester");
}
} catch (BindException be) {
log.error("Cannot bind to port: {}, ensure no other instances are bound or choose another port", port, be);
Expand Down

0 comments on commit 81a5c81

Please sign in to comment.