Server local command handling from sockets

This commit is contained in:
Anuken
2019-05-04 12:36:24 -04:00
parent a4fd88a440
commit b2b8cf2357

View File

@ -23,8 +23,8 @@ import io.anuke.mindustry.net.Packets.KickReason;
import io.anuke.mindustry.type.Item; import io.anuke.mindustry.type.Item;
import io.anuke.mindustry.type.ItemType; import io.anuke.mindustry.type.ItemType;
import java.io.IOException; import java.io.*;
import java.net.BindException; import java.net.*;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Scanner; import java.util.Scanner;
@ -36,6 +36,7 @@ public class ServerControl implements ApplicationListener{
private static final int roundExtraTime = 12; private static final int roundExtraTime = 12;
//in bytes: 512 kb is max //in bytes: 512 kb is max
private static final int maxLogLength = 1024 * 512; private static final int maxLogLength = 1024 * 512;
private static final int commandSocketPort = 6859;
private final CommandHandler handler = new CommandHandler(""); private final CommandHandler handler = new CommandHandler("");
private final FileHandle logFolder = Core.files.local("logs/"); private final FileHandle logFolder = Core.files.local("logs/");
@ -44,6 +45,9 @@ public class ServerControl implements ApplicationListener{
private boolean inExtraRound; private boolean inExtraRound;
private Task lastTask; private Task lastTask;
private Thread socketThread;
private PrintWriter socketOutput;
public ServerControl(String[] args){ public ServerControl(String[] args){
Core.settings.defaults( Core.settings.defaults(
"shufflemode", "normal", "shufflemode", "normal",
@ -52,7 +56,8 @@ public class ServerControl implements ApplicationListener{
"shuffle", true, "shuffle", true,
"crashreport", false, "crashreport", false,
"port", port, "port", port,
"logging", true "logging", true,
"socket", false
); );
Log.setLogger(new LogHandler(){ Log.setLogger(new LogHandler(){
@ -86,6 +91,14 @@ public class ServerControl implements ApplicationListener{
if(Core.settings.getBool("logging")){ if(Core.settings.getBool("logging")){
logToFile("[" + dateTime.format(LocalDateTime.now()) + "] " + format(text + "&fr", false, args)); logToFile("[" + dateTime.format(LocalDateTime.now()) + "] " + format(text + "&fr", false, args));
} }
if(socketOutput != null){
try{
socketOutput.println("[" + dateTime.format(LocalDateTime.now()) + "] " + format(text + "&fr", false, args));
}catch(Throwable e){
err("Error occurred logging to socket: {0}", e.getClass().getSimpleName());
}
}
} }
}); });
@ -161,6 +174,10 @@ public class ServerControl implements ApplicationListener{
info("&lcServer loaded. Type &ly'help'&lc for help."); info("&lcServer loaded. Type &ly'help'&lc for help.");
System.out.print("> "); System.out.print("> ");
if(Core.settings.getBool("socket")){
toggleSocket(true);
}
} }
private void registerCommands(){ private void registerCommands(){
@ -360,6 +377,19 @@ public class ServerControl implements ApplicationListener{
info("Strict mode is now {0}.", netServer.admins.getStrict() ? "on" : "off"); info("Strict mode is now {0}.", netServer.admins.getStrict() ? "on" : "off");
}); });
handler.register("socketinput", "[on/off]", "Disables or enables a local TCP socket at port "+commandSocketPort+" to recieve commands from other applications", arg -> {
if(arg.length == 0){
info("Socket input is currently &lc{0}.", Core.settings.getBool("socket") ? "on" : "off");
return;
}
boolean value = arg[0].equalsIgnoreCase("on");
toggleSocket(value);
Core.settings.put("socket", value);
Core.settings.save();
info("Socket input is now &lc{0}.", value ? "on" : "off");
});
handler.register("allow-custom-clients", "[on/off]", "Allow or disallow custom clients.", arg -> { handler.register("allow-custom-clients", "[on/off]", "Allow or disallow custom clients.", arg -> {
if(arg.length == 0){ if(arg.length == 0){
info("Custom clients are currently &lc{0}.", netServer.admins.allowsCustomClients() ? "allowed" : "disallowed"); info("Custom clients are currently &lc{0}.", netServer.admins.allowsCustomClients() ? "allowed" : "disallowed");
@ -615,8 +645,11 @@ public class ServerControl implements ApplicationListener{
Scanner scan = new Scanner(System.in); Scanner scan = new Scanner(System.in);
while(scan.hasNext()){ while(scan.hasNext()){
String line = scan.nextLine(); String line = scan.nextLine();
Core.app.post(() -> handleCommandString(line));
}
}
Core.app.post(() -> { private void handleCommandString(String line){
Response response = handler.handleMessage(line); Response response = handler.handleMessage(line);
if(response.type == ResponseType.unknownCommand){ if(response.type == ResponseType.unknownCommand){
@ -644,8 +677,6 @@ public class ServerControl implements ApplicationListener{
} }
System.out.print("> "); System.out.print("> ");
});
}
} }
private void play(boolean wait, Runnable run){ private void play(boolean wait, Runnable run){
@ -723,4 +754,39 @@ public class ServerControl implements ApplicationListener{
currentLogFile.writeString(text + "\n", true); currentLogFile.writeString(text + "\n", true);
} }
private void toggleSocket(boolean on){
if(on && socketThread == null){
socketThread = new Thread(() -> {
try{
try(ServerSocket socket = new ServerSocket()){
socket.bind(new InetSocketAddress("localhost", commandSocketPort));
while(true){
Socket client = socket.accept();
info("&lmRecieved command socket connection: &lb{0}", socket.getLocalSocketAddress());
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
socketOutput = new PrintWriter(client.getOutputStream(), true);
String line;
while(client.isConnected() && (line = in.readLine()) != null){
info("&lmRecieved remote command: &lb'{0}'", line);
String result = line;
Core.app.post(() -> handleCommandString(result));
}
info("&lmLost command socket connection: &lb{0}", socket.getLocalSocketAddress());
socketOutput = null;
}
}
}catch(IOException e){
err("Terminating socket.");
e.printStackTrace();
}
});
socketThread.setDaemon(true);
socketThread.start();
}else if(socketThread != null){
socketThread.interrupt();
socketThread = null;
socketOutput = null;
}
}
} }