-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
190 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
LaunchServer/src/main/java/pro/gravit/launchserver/socket/SocketCommandServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package pro.gravit.launchserver.socket; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import pro.gravit.launchserver.config.log4j.LogAppender; | ||
import pro.gravit.utils.command.CommandHandler; | ||
|
||
import java.io.IOException; | ||
import java.net.StandardProtocolFamily; | ||
import java.net.UnixDomainSocketAddress; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.ServerSocketChannel; | ||
import java.nio.channels.SocketChannel; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
public class SocketCommandServer implements Runnable { | ||
private final Logger logger = LogManager.getLogger(SocketCommandServer.class); | ||
private ServerSocketChannel channel; | ||
private Path path; | ||
private UnixDomainSocketAddress address; | ||
private ServerSocketChannel serverChannel; | ||
private CommandHandler commandHandler; | ||
private transient SocketChannel clientChannel; | ||
|
||
public SocketCommandServer(CommandHandler commandHandler, Path path) { | ||
this.commandHandler = commandHandler; | ||
this.path = path; | ||
} | ||
|
||
private void runCommand(SocketChannel channel, String command) { | ||
logger.info("Command '{}' from socket", command); | ||
clientChannel = channel; | ||
try { | ||
commandHandler.evalNative(command, false); | ||
} catch (Throwable e) { | ||
logger.error("Error when execute command", e); | ||
} finally { | ||
clientChannel = null; | ||
|
||
} | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
Files.deleteIfExists(path); | ||
this.address = UnixDomainSocketAddress.of(path); | ||
serverChannel = ServerSocketChannel.open(StandardProtocolFamily.UNIX); | ||
serverChannel.configureBlocking(true); | ||
serverChannel.bind(address); | ||
LogAppender.getInstance().addListener((logEvent -> { | ||
if(clientChannel != null && clientChannel.isOpen()) { | ||
try { | ||
String s = logEvent.getMessage().getFormattedMessage()+"\n"; | ||
byte[] bytes = s.getBytes(StandardCharsets.UTF_8); | ||
ByteBuffer buffer = ByteBuffer.wrap(bytes); | ||
clientChannel.write(buffer); | ||
} catch (Throwable ignored) { | ||
} | ||
} | ||
})); | ||
ByteBuffer buffer = ByteBuffer.allocate(1024); | ||
while (true) { | ||
SocketChannel channel = serverChannel.accept(); | ||
channel.configureBlocking(true); | ||
String command = null; | ||
try { | ||
mark: | ||
while (true) { | ||
int bytesRead = channel.read(buffer); | ||
if (bytesRead < 0) { | ||
break; | ||
} | ||
for (var i=0;i<buffer.limit();i++) { | ||
if(buffer.get(i) == '\n') { | ||
command = new String(buffer.array(), 0, i); | ||
break mark; | ||
} | ||
} | ||
|
||
} | ||
if(command != null) { | ||
runCommand(channel, command); | ||
} | ||
} finally { | ||
buffer.clear(); | ||
channel.close(); | ||
} | ||
} | ||
} catch (Throwable e) { | ||
logger.error("Unix command socket server error", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
project.ext { | ||
verAsm = '9.7' | ||
verNetty = '4.1.111.Final' | ||
verOshiCore = '6.6.4' | ||
verJunit = '5.10.2' | ||
verAsm = '9.7.1' | ||
verNetty = '4.1.114.Final' | ||
verOshiCore = '6.6.5' | ||
verJunit = '5.11.2' | ||
verJansi = '2.4.1' | ||
verJline = '3.26.3' | ||
verJwt = '0.12.5' | ||
verJwt = '0.12.6' | ||
verGson = '2.11.0' | ||
verBcpkix = '1.78.1' | ||
verSlf4j = '2.0.13' | ||
verLog4j = '2.23.1' | ||
verSlf4j = '2.0.16' | ||
verLog4j = '2.24.1' | ||
verMySQLConn = '9.0.0' | ||
verMariaDBConn = '3.4.0' | ||
verMariaDBConn = '3.4.1' | ||
verPostgreSQLConn = '42.7.4' | ||
verH2Conn = '2.3.232' | ||
verProguard = '7.5.0' | ||
verProguard = '7.6.0' | ||
} |