This commit is contained in:
Administrator
2022-12-01 10:19:48 +08:00
parent 7d90fa17bd
commit 45c369a0a1
7 changed files with 66 additions and 39 deletions
@@ -2,6 +2,7 @@ package org.kne.cloud.network.klalb;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.Socket;
@@ -19,21 +20,7 @@ public class KLALBClient {
private List<Tunnel> tls=new ArrayList<>();
private TCPListener tcpl;
private ServiceElement sel;
public KLALBClient(int port) throws IOException {
tcpl=new TCPListener(port);
tcpl.setCon((s)->{
IOThreadManager kcp=new IOThreadManager();
UUID uid=UUID.randomUUID();
try {
kcp.setLocal(new TCPConnection(null, s));
kcp.startLocal();
runProtocol(kcp,uid);
} catch (IOException e) {
e.printStackTrace();
}
});
}
public void runProtocol(IOThreadManager kcp, UUID uid) {
AtomicBoolean b=new AtomicBoolean(true);
AtomicInteger aig=new AtomicInteger(0);
@@ -57,15 +44,15 @@ public class KLALBClient {
tc.getDout().writeLong(uid.getLeastSignificantBits());
tc.getDout().flush();
aig.incrementAndGet();
//System.out.println("隧道"+tll+"已连接,可用线路数量:"+ (kcp.getTcps().size()+1));
System.out.println("隧道"+tll+"已连接,可用线路数量:"+ (kcp.getTcps().size()+1));
kcp.handleSocket(tc);
tc.close();
int n=kcp.getTcps().size();
//System.out.println("隧道"+tll+"已断开,可用线路数量:"+ n);
System.out.println("隧道"+tll+"已断开,可用线路数量:"+ n);
if(n<=0) {
kcp.closeRemote();
kcp.closeLocal();
//System.out.println("连接已断开");
System.out.println("连接已断开");
return;
}
} catch (IOException e) {
@@ -81,7 +68,7 @@ public class KLALBClient {
}
try {
Thread.sleep(5000);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
@@ -93,7 +80,22 @@ public class KLALBClient {
}
public void open() throws IOException {
public void open(int port) throws IOException {
tcpl=new TCPListener(port);
tcpl.setCon((s)->{
IOThreadManager kcp=new IOThreadManager();
UUID uid=UUID.randomUUID();
try {
kcp.setLocal(new TCPConnection(null, s));
kcp.startLocal();
runProtocol(kcp,uid);
} catch (IOException e) {
e.printStackTrace();
}
});
tcpl.open();
}
public List<Tunnel> getTls() {
@@ -113,15 +115,19 @@ public class KLALBClient {
tcpc.getDout().write(0);
tcpc.getDout().flush();
int s=tcpc.getDin().readInt();
System.out.println("服务列表:");
for (int i = 0; i < s; i++) {
services.add(new ServiceElement(tcpc.getDin().readUTF()));
String str=tcpc.getDin().readUTF();
System.out.println(str);
services.add(new ServiceElement(str));
}
int s2=tcpc.getDin().readInt();
System.out.println("隧道列表:");
for (int i = 0; i < s2; i++) {
tls.add(Tunnel.newTunnel(tcpc.getDin().readUTF()));
String str=tcpc.getDin().readUTF();
System.out.println(str);
tls.add(Tunnel.newTunnel(str));
}
//System.out.println(services);
//System.out.println(tls);
}finally {
if(tcpc!=null) {
tcpc.close();
@@ -137,10 +143,20 @@ public class KLALBClient {
}
public static void main(String[] args) throws IOException {
Scanner scn=new Scanner(System.in);
KLALBClient kc=new KLALBClient(4568);
kc.searchTunnels("153.36.240.12:65529");
kc.setSel(kc.getServices().get(0));
kc.open();
KLALBClient kc=new KLALBClient();
/*File f=new File("klalbc.ini");
if(!f.exists()) {
f.createNewFile();
return;
}
Scanner scn=new scan*/
System.out.println("请输入服务器任意一条线路的地址:");
kc.searchTunnels(scn.next());//"153.36.240.12:65529"
System.out.println("你想要连接哪个服务?请输入序号:");
kc.setSel(kc.getServices().get(scn.nextInt()));
System.out.println("请输入你要映射的本地端口:");
kc.open(scn.nextInt());
while(true) {
String s=scn.next();
switch(s) {