2024-05-13 14:41:54 +08:00
|
|
|
package org.aohe;
|
|
|
|
|
2024-05-14 15:12:21 +08:00
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2024-05-21 16:52:55 +08:00
|
|
|
import org.aohe.core.sane.utils.SaneSessionUtils;
|
2024-05-15 20:24:05 +08:00
|
|
|
import org.aohe.core.utils.CommandUtils;
|
2024-05-14 15:12:21 +08:00
|
|
|
import org.aohe.core.utils.SystemUtils;
|
2024-05-15 20:24:05 +08:00
|
|
|
import org.aohe.core.web.SocketServer;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.InputStreamReader;
|
2024-05-21 16:52:55 +08:00
|
|
|
import java.net.UnknownHostException;
|
2024-05-14 15:12:21 +08:00
|
|
|
|
|
|
|
@Slf4j
|
2024-05-13 14:41:54 +08:00
|
|
|
public class Main {
|
2024-05-21 16:52:55 +08:00
|
|
|
|
|
|
|
private static SocketServer socketServer;
|
|
|
|
|
2024-05-15 20:24:05 +08:00
|
|
|
public static void main(String[] args) throws IOException, InterruptedException {
|
2024-05-14 15:12:21 +08:00
|
|
|
log.info(" 当前系统类型: {}", SystemUtils.getOsName());
|
2024-05-15 20:24:05 +08:00
|
|
|
//初始化Sane Socket 守护进程
|
2024-05-21 16:52:55 +08:00
|
|
|
init();
|
2024-05-15 20:24:05 +08:00
|
|
|
BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
|
|
|
|
while (true) {
|
|
|
|
String in = sysin.readLine();
|
2024-05-21 16:52:55 +08:00
|
|
|
socketServer.broadcast(in);
|
2024-05-15 20:24:05 +08:00
|
|
|
if (in.equals("exit")) {
|
2024-05-21 16:52:55 +08:00
|
|
|
socketServer.stop(1000);
|
2024-05-15 20:24:05 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-05-13 14:41:54 +08:00
|
|
|
}
|
2024-05-21 16:52:55 +08:00
|
|
|
|
|
|
|
public static void init(){
|
|
|
|
log.info("程序初始化开始...");
|
|
|
|
CommandUtils.initSanedSocket();
|
|
|
|
log.info("Saned Socket 检查完成...");
|
|
|
|
initSocket();
|
|
|
|
log.info("Socket 服务启动成功...");
|
|
|
|
stopGracefully();
|
|
|
|
log.info("Hook注册成功...");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void initSocket() {
|
|
|
|
int port = 8997; // 8997 is must port
|
|
|
|
|
|
|
|
try {
|
|
|
|
socketServer = new SocketServer(port);
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
log.error("socket 服务启动失败, 服务关闭", e);
|
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
socketServer.start();
|
|
|
|
log.info("scan-service started on port: {}", socketServer.getPort());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void stopGracefully() {
|
|
|
|
log.info("优雅退出Hook注册");
|
|
|
|
//优雅退出,在程序停止时,销毁关键资源占用
|
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
|
|
try {
|
|
|
|
log.info("开始资源释放");
|
|
|
|
//释放sane资源
|
|
|
|
SaneSessionUtils.resource();
|
|
|
|
if (socketServer != null) {
|
|
|
|
//关闭socket绑定程序
|
|
|
|
socketServer.stop(1000);
|
|
|
|
log.info("socket 服务已关闭 ");
|
|
|
|
}
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
2024-05-13 14:41:54 +08:00
|
|
|
}
|