KLALB2.3
This commit is contained in:
@@ -19,31 +19,26 @@ public class FrpcIni extends Ini {
|
||||
|
||||
public FrpcIni(File input) throws IOException, InvalidFileFormatException {
|
||||
super(input);
|
||||
// TODO 自动生成的构造函数存根
|
||||
}
|
||||
|
||||
public FrpcIni(InputStream input) throws IOException, InvalidFileFormatException {
|
||||
super(input);
|
||||
// TODO 自动生成的构造函数存根
|
||||
}
|
||||
|
||||
public FrpcIni(Reader input) throws IOException, InvalidFileFormatException {
|
||||
super(input);
|
||||
// TODO 自动生成的构造函数存根
|
||||
}
|
||||
|
||||
public FrpcIni(URL input) throws IOException, InvalidFileFormatException {
|
||||
super(input);
|
||||
// TODO 自动生成的构造函数存根
|
||||
}
|
||||
|
||||
public FrpcIni(String str) throws InvalidFileFormatException {
|
||||
this();
|
||||
loadFromString(str);
|
||||
public FrpcIni(String str) throws IOException ,InvalidFileFormatException{
|
||||
super(new StringReader(str));
|
||||
}
|
||||
|
||||
public void setServerAddr(String addr) {
|
||||
add("common", "server_addr", addr);
|
||||
put("common", "server_addr", addr);
|
||||
}
|
||||
|
||||
public String getServerAddr() {
|
||||
@@ -51,7 +46,7 @@ public class FrpcIni extends Ini {
|
||||
}
|
||||
|
||||
public void setToken(String addr) {
|
||||
add("common", "token", addr);
|
||||
put("common", "token", addr);
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
@@ -59,7 +54,7 @@ public class FrpcIni extends Ini {
|
||||
}
|
||||
|
||||
public void setProtocol(String protocol) {
|
||||
add("common", "protocol", protocol);
|
||||
put("common", "protocol", protocol);
|
||||
}
|
||||
|
||||
public String getProtocol() {
|
||||
@@ -67,7 +62,7 @@ public class FrpcIni extends Ini {
|
||||
}
|
||||
|
||||
public void setServerPort(int port) {
|
||||
add("common", "server_port", port);
|
||||
put("common", "server_port", port);
|
||||
}
|
||||
|
||||
public int getServerPort() {
|
||||
@@ -75,14 +70,14 @@ public class FrpcIni extends Ini {
|
||||
}
|
||||
|
||||
public void setDnsServer(String dns) {
|
||||
add("common", "dns_server", dns);
|
||||
put("common", "dns_server", dns);
|
||||
}
|
||||
|
||||
public String getDnsServer() {
|
||||
return get("common", "dns_server");
|
||||
}
|
||||
public void setConnectServerLocalIp(String connectserverlocalip) {
|
||||
add("common", "connect_server_local_ip", connectserverlocalip);
|
||||
put("common", "connect_server_local_ip", connectserverlocalip);
|
||||
}
|
||||
|
||||
public String getConnectServerLocalIp() {
|
||||
@@ -90,7 +85,7 @@ public class FrpcIni extends Ini {
|
||||
}
|
||||
|
||||
public void setUser(String user) {
|
||||
add("common", "user", user);
|
||||
put("common", "user", user);
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
@@ -98,7 +93,7 @@ public class FrpcIni extends Ini {
|
||||
}
|
||||
|
||||
public void setTcpMux(boolean tcpmux) {
|
||||
add("common", "tcp_mux", tcpmux);
|
||||
put("common", "tcp_mux", tcpmux);
|
||||
}
|
||||
|
||||
public boolean getTcpMux() {
|
||||
@@ -106,7 +101,7 @@ public class FrpcIni extends Ini {
|
||||
}
|
||||
|
||||
public void setTlsEnable(boolean tlsenable) {
|
||||
add("common", "tls_enable", tlsenable);
|
||||
put("common", "tls_enable", tlsenable);
|
||||
}
|
||||
|
||||
public boolean getTlsEnable() {
|
||||
@@ -125,63 +120,63 @@ public class FrpcIni extends Ini {
|
||||
|
||||
|
||||
public void setType(String tunnel,String type) {
|
||||
add(tunnel, "type", type);
|
||||
put(tunnel, "type", type);
|
||||
}
|
||||
|
||||
public String getType(String tunnel) {
|
||||
return get(tunnel, "type");
|
||||
}
|
||||
public void setLocalIP (String tunnel,String localip) {
|
||||
add(tunnel, "local_ip", localip);
|
||||
put(tunnel, "local_ip", localip);
|
||||
}
|
||||
|
||||
public String getLocalIP(String tunnel) {
|
||||
return get(tunnel, "local_ip");
|
||||
}
|
||||
public void setLocalPort (String tunnel,int localport) {
|
||||
add(tunnel, "local_port", localport);
|
||||
put(tunnel, "local_port", localport);
|
||||
}
|
||||
|
||||
public int getLocalPort(String tunnel) {
|
||||
return get(tunnel, "local_port",int.class);
|
||||
}
|
||||
public void setRemotePort (String tunnel,int remoteport) {
|
||||
add(tunnel, "remote_port", remoteport);
|
||||
put(tunnel, "remote_port", remoteport);
|
||||
}
|
||||
|
||||
public int getRemotePort(String tunnel) {
|
||||
return get(tunnel, "remote_port",int.class);
|
||||
}
|
||||
public void setUseEncryption (String tunnel,boolean useencryption) {
|
||||
add(tunnel, "use_encryption", useencryption);
|
||||
put(tunnel, "use_encryption", useencryption);
|
||||
}
|
||||
|
||||
public boolean getUseEncryption(String tunnel) {
|
||||
return get(tunnel, "use_encryption",boolean.class);
|
||||
}
|
||||
public void setUseCompression (String tunnel,boolean usecompression) {
|
||||
add(tunnel, "use_compression", usecompression);
|
||||
put(tunnel, "use_compression", usecompression);
|
||||
}
|
||||
|
||||
public boolean getUseCompression(String tunnel) {
|
||||
return get(tunnel, "use_compression",boolean.class);
|
||||
}
|
||||
public void setSk(String tunnel,String sk) {
|
||||
add(tunnel, "sk", sk);
|
||||
put(tunnel, "sk", sk);
|
||||
}
|
||||
|
||||
public String getSk(String tunnel) {
|
||||
return get(tunnel, "sk");
|
||||
}
|
||||
public void setRole(String tunnel,String sk) {
|
||||
add(tunnel, "role", sk);
|
||||
put(tunnel, "role", sk);
|
||||
}
|
||||
|
||||
public String getRole(String tunnel) {
|
||||
return get(tunnel, "role");
|
||||
}
|
||||
public void setServerName(String tunnel,String servername) {
|
||||
add(tunnel, "server_name", servername);
|
||||
put(tunnel, "server_name", servername);
|
||||
}
|
||||
|
||||
public String getServerName(String tunnel) {
|
||||
@@ -189,21 +184,21 @@ public class FrpcIni extends Ini {
|
||||
}
|
||||
|
||||
public void setBindAddr(String tunnel,String bindaddr) {
|
||||
add(tunnel, "bind_addr", bindaddr);
|
||||
put(tunnel, "bind_addr", bindaddr);
|
||||
}
|
||||
|
||||
public String getBindAddr(String tunnel) {
|
||||
return get(tunnel, "bind_addr");
|
||||
}
|
||||
public void setBindPort (String tunnel,int bindport) {
|
||||
add(tunnel, "bind_port", bindport);
|
||||
put(tunnel, "bind_port", bindport);
|
||||
}
|
||||
|
||||
public int getBindPort(String tunnel) {
|
||||
return get(tunnel, "bind_port",int.class);
|
||||
}
|
||||
public void setKeepTunnelOpen (String tunnel,boolean keeptunnelopen) {
|
||||
add(tunnel, "keep_tunnel_open", keeptunnelopen);
|
||||
put(tunnel, "keep_tunnel_open", keeptunnelopen);
|
||||
}
|
||||
|
||||
public boolean getKeepTunnelOpen(String tunnel) {
|
||||
@@ -215,6 +210,7 @@ public class FrpcIni extends Ini {
|
||||
StringWriter sw = new StringWriter();
|
||||
try {
|
||||
store(sw);
|
||||
//System.out.println(sw);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -10,6 +10,7 @@ import java.net.SocketImpl;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import org.ini4j.Profile.Section;
|
||||
import org.kne.cloud.network.MultipurposeSocketAddress;
|
||||
@@ -85,7 +86,7 @@ public class FrpcServerSocket extends ServerSocket {
|
||||
public void bind(SocketAddress endpoint, int backlog) throws IOException {
|
||||
if(frpcini!=null) {
|
||||
if(tunnel==null) {
|
||||
var e=frpcini.keySet();
|
||||
Set<String> e=frpcini.keySet();
|
||||
for (Iterator<String> iterator = e.iterator(); iterator.hasNext();) {
|
||||
String key = iterator.next();
|
||||
if(!key.equals("common")) {
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package org.kne.cloud.network.frpc;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.net.URL;
|
||||
|
||||
import org.ini4j.InvalidFileFormatException;
|
||||
|
||||
import com.moandjiezana.toml.Toml;
|
||||
import com.moandjiezana.toml.TomlWriter;
|
||||
public class FrpcToml extends Toml{
|
||||
|
||||
public FrpcToml() {
|
||||
super();
|
||||
}
|
||||
public FrpcToml(File input) throws IOException, InvalidFileFormatException {
|
||||
Toml t=new Toml().read(input);
|
||||
System.out.println(t.toMap());
|
||||
}
|
||||
|
||||
public FrpcToml(InputStream input) throws IOException, InvalidFileFormatException {
|
||||
Toml t=new Toml().read(input);
|
||||
System.out.println(t.toMap());
|
||||
}
|
||||
|
||||
public FrpcToml(Reader input) throws IOException, InvalidFileFormatException {
|
||||
Toml t=new Toml().read(input);
|
||||
System.out.println(t.toMap());
|
||||
}
|
||||
|
||||
public FrpcToml(String input) throws InvalidFileFormatException {
|
||||
Toml t=new Toml().read(input);
|
||||
System.out.println(t.toMap());
|
||||
}
|
||||
public static void main(String[] args) throws IOException {
|
||||
Toml t=new Toml().read(new File("C:\\KNE的文件夹\\coding\\java\\KLALB\\frpc\\frpc.toml"));
|
||||
System.out.println(t.toMap());
|
||||
TomlWriter te=new TomlWriter();
|
||||
System.out.println( te.write(""));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user