parent
c5b9c59524
commit
c35289b93c
|
@ -1,36 +1,76 @@
|
|||
package org.aohe;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aohe.core.sane.utils.SaneSessionUtils;
|
||||
import org.aohe.core.utils.CommandUtils;
|
||||
import org.aohe.core.utils.SystemUtils;
|
||||
import org.aohe.core.web.SocketServer;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
@Slf4j
|
||||
public class Main {
|
||||
|
||||
private static SocketServer socketServer;
|
||||
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
log.info(" 当前系统类型: {}", SystemUtils.getOsName());
|
||||
//初始化Sane Socket 守护进程
|
||||
CommandUtils.initSanedSocket();
|
||||
int port = 8997; // 843 flash policy port
|
||||
|
||||
SocketServer s = new SocketServer(port);
|
||||
s.start();
|
||||
System.out.println("scan-service started on port: " + s.getPort());
|
||||
|
||||
init();
|
||||
BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
|
||||
while (true) {
|
||||
String in = sysin.readLine();
|
||||
s.broadcast(in);
|
||||
socketServer.broadcast(in);
|
||||
if (in.equals("exit")) {
|
||||
s.stop(1000);
|
||||
socketServer.stop(1000);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
|
@ -11,6 +11,10 @@ public class RemoteConfig {
|
|||
|
||||
RemoteConfig() {
|
||||
}
|
||||
|
||||
private static Setting getSetting(){
|
||||
return new Setting("config.setting");
|
||||
}
|
||||
|
||||
RemoteConfig(String ip) {
|
||||
this.ip = ip;
|
||||
|
@ -19,7 +23,7 @@ public class RemoteConfig {
|
|||
public static RemoteConfig getInstance(){
|
||||
String setIp;
|
||||
try {
|
||||
setIp = new Setting("config.setting").get("ip");
|
||||
setIp = getSetting().getStr("ip");
|
||||
}catch (Exception e){
|
||||
setIp = "127.0.0.1";
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@ import cn.hutool.core.util.RuntimeUtil;
|
|||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
@ -20,7 +19,13 @@ public class CommandUtils {
|
|||
String status = CommandUtils.getSanedSocketStatus(cmdResult);
|
||||
log.info("saned.socket status:{}", status);
|
||||
if (StrUtil.isNotEmpty(status) && "dead".equals(status)) {
|
||||
|
||||
execForStrUtf8("systemctl start saned.socket");
|
||||
status = CommandUtils.getSanedSocketStatus(execForStrUtf8("systemctl status saned.socket"));
|
||||
if("dead".equals(status)){
|
||||
cmdResult = execForStrUtf8Sudo("systemctl start saned.socket");
|
||||
}
|
||||
log.info("start command {}", cmdResult);
|
||||
log.info("saned.socket successfully");
|
||||
}
|
||||
}
|
||||
|
@ -30,6 +35,10 @@ public class CommandUtils {
|
|||
return RuntimeUtil.execForStr(StandardCharsets.UTF_8,command);
|
||||
}
|
||||
|
||||
public static String execForStrUtf8Sudo(String command){
|
||||
return RuntimeUtil.execForStr(StandardCharsets.UTF_8,"sudo " + command);
|
||||
}
|
||||
|
||||
public static String getSanedSocketStatus(String commandResult) {
|
||||
String result = "";
|
||||
String[] resultArray = commandResult.split("\n");
|
||||
|
|
|
@ -16,7 +16,6 @@ import org.java_websocket.server.WebSocketServer;
|
|||
@Slf4j
|
||||
public class SocketServer extends WebSocketServer {
|
||||
|
||||
|
||||
public SocketServer(int port) throws UnknownHostException {
|
||||
super(new InetSocketAddress(port));
|
||||
}
|
||||
|
@ -38,7 +37,8 @@ public class SocketServer extends WebSocketServer {
|
|||
@Override
|
||||
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
|
||||
log.info("client is closed , client system is : {}", SystemUtils.getOsName());
|
||||
//这里执行关闭扫描仪连接
|
||||
//这里执行关闭连接
|
||||
conn.close();
|
||||
SaneSessionUtils.resource();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue