KLALB2.3
This commit is contained in:
@@ -38,6 +38,8 @@ public class FrpcProcess extends Process{
|
||||
private File base;
|
||||
private File exe;
|
||||
private Process theProcess;
|
||||
|
||||
private File config_file;
|
||||
|
||||
public FrpcProcess(String ini) throws IOException {
|
||||
this(defaultBase,ini);
|
||||
@@ -50,11 +52,14 @@ public class FrpcProcess extends Process{
|
||||
}else {
|
||||
exe=new File(base,"frpc_linux_amd64");
|
||||
}
|
||||
File f=new File(base,"frpc_conf.ini");
|
||||
f.createNewFile();
|
||||
AtomicReference<IOException >arex=new AtomicReference<IOException >();
|
||||
synchronized (getClass()) {
|
||||
config_file = new File(base,"frpc_conf_"+UUID.randomUUID()+".ini");
|
||||
config_file.createNewFile();
|
||||
config_file.deleteOnExit();
|
||||
PrintWriter ps=null;
|
||||
try {
|
||||
ps=new PrintWriter(f);
|
||||
ps=new PrintWriter(config_file);
|
||||
ps.println(ini);
|
||||
}finally {
|
||||
if(ps!=null)
|
||||
@@ -63,12 +68,23 @@ public class FrpcProcess extends Process{
|
||||
List<String>lstc=new ArrayList<>();
|
||||
lstc.add(exe.getAbsolutePath());
|
||||
lstc.add("-c");
|
||||
lstc.add(f.getName());
|
||||
lstc.add(config_file.getName());
|
||||
ProcessBuilder psb=new ProcessBuilder(lstc);
|
||||
psb.directory(base);
|
||||
theProcess=psb.start();
|
||||
AtomicReference<IOException >arex=new AtomicReference<IOException >();
|
||||
Thread t=Thread.currentThread();
|
||||
|
||||
hook=new Thread(()->{
|
||||
try {
|
||||
hook=null;
|
||||
finalize();
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
Runtime.getRuntime().addShutdownHook(hook);
|
||||
|
||||
CountDownLatch cdl=new CountDownLatch(1);
|
||||
new Thread(()->{
|
||||
BufferedReader br=new BufferedReader(new InputStreamReader(theProcess.getInputStream()));
|
||||
|
||||
@@ -78,6 +94,7 @@ public class FrpcProcess extends Process{
|
||||
if(s==null) {
|
||||
break;
|
||||
}
|
||||
cdl.countDown();
|
||||
System.out.println (s);
|
||||
if(s.contains("start proxy success")) {
|
||||
t.interrupt();
|
||||
@@ -93,6 +110,9 @@ public class FrpcProcess extends Process{
|
||||
}else if(s.contains("already exists")&&s.contains("proxy")) {
|
||||
arex.set(new BindException("proxy already exists"));
|
||||
t.interrupt();
|
||||
}else if(s.contains("已经存在, 请检查是否重复开启")&&s.contains("隧道")) {
|
||||
arex.set(new BindException("proxy already exists"));
|
||||
t.interrupt();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
@@ -106,6 +126,13 @@ public class FrpcProcess extends Process{
|
||||
}
|
||||
|
||||
}).start();
|
||||
try {
|
||||
cdl.await(5000, TimeUnit.MILLISECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if(theProcess.waitFor(20000, TimeUnit.MILLISECONDS)) {
|
||||
destroy();
|
||||
@@ -115,23 +142,17 @@ public class FrpcProcess extends Process{
|
||||
throw new SocketException("Frpc timed out");
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
//destroy();
|
||||
Thread.interrupted();
|
||||
if(arex.get()!=null) {
|
||||
destroy();
|
||||
arex.get().fillInStackTrace();
|
||||
throw arex.get();
|
||||
}
|
||||
}finally {
|
||||
Thread.interrupted();
|
||||
}
|
||||
hook=new Thread(()->{
|
||||
try {
|
||||
hook=null;
|
||||
finalize();
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
Runtime.getRuntime().addShutdownHook(hook);
|
||||
|
||||
|
||||
|
||||
}
|
||||
public File getBase() {
|
||||
return base;
|
||||
@@ -163,6 +184,9 @@ public class FrpcProcess extends Process{
|
||||
if(hook!=null) {
|
||||
Runtime.getRuntime().removeShutdownHook(hook);
|
||||
}
|
||||
if(config_file!=null) {
|
||||
config_file.delete();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public boolean waitFor(long timeout, TimeUnit unit) throws InterruptedException {
|
||||
|
||||
Reference in New Issue
Block a user