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
@@ -8,7 +8,7 @@ import java.util.List;
import java.util.Vector;
public class IOThreadManager {
private KLALBCore klc=new KLALBCore(500);
private KLALBCore klc=new KLALBCore(200);
private List<TCPConnection>tcps=new Vector<>();
@@ -35,6 +35,10 @@ public class IPPort implements Serializable{
ip=InetAddress.getByName(ip2);
port=port2;
}
public IPPort(InetSocketAddress remoteSocketAddress) {
ip=remoteSocketAddress.getAddress();
port=remoteSocketAddress.getPort();
}
public InetAddress getIp() {
return ip;
}
@@ -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) {
@@ -76,7 +76,7 @@ public class KLALBCore {
}
}
if (b != null) {
//System.out.println("\tPROCESS:" + inputcache.size());
System.out.println("\tPROCESS:" + inputcache.size());
inputcount++;
return b;
}
@@ -119,14 +119,14 @@ public class KLALBCore {
} else {
if (kd.thread.isAlive()) {
long timex = (System.nanoTime() - kd.time) / 1000000;
if (timex > 10000) {
//System.out.println("³¬Ê±ÖØ´«£º"+kd);
if (timex > 200000) {
System.out.println("³¬Ê±ÖØ´«£º"+kd);
kd.time = System.nanoTime();
kd.thread = Thread.currentThread();
ks = kd;
}
} else {
//System.out.println("µôÏßÖØ´«£º"+kd);
System.out.println("µôÏßÖØ´«£º"+kd);
kd.time = System.nanoTime();
kd.thread = Thread.currentThread();
ks = kd;
@@ -159,6 +159,9 @@ public class KLALBCore {
KLALBBlock klb=new KLALBBlock(null, 0, -x.number);
ackq.add(klb);
klb=new KLALBBlock(null, 0, -x.number);
ackq.add(klb);
/*BlockingQueue<KLALBBlock> bq=acks.get(in);
if(bq==null) {
BlockingQueue<KLALBBlock> bqt=new LinkedBlockingQueue<>();
@@ -220,7 +223,7 @@ public class KLALBCore {
out.flush();
}
//System.out.println("SEND:" + kd);
System.out.println(tcp.getTunnel().getName()+" SEND:" + kd);
}
private KLALBBlock receive0(TCPConnection tcp) throws IOException {
@@ -238,7 +241,7 @@ public class KLALBCore {
kb.time=in.readLong();
}
}
//System.out.println("RECEIVE:" + kb);
System.out.println(tcp.getTunnel().getName()+" RECEIVE:" + kb);
return kb;
}
@@ -3,6 +3,7 @@ package org.kne.cloud.network.klalb;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Collection;
import java.util.Collections;
@@ -65,13 +66,13 @@ public class KLALBServer {
UUID uid=new UUID(din.readLong(),din.readLong());
System.out.println("隧道连接:"+tll);
System.out.println(new IPPort((InetSocketAddress)s.getRemoteSocketAddress())+"->"+tll.getIpport()+"->"+eas.ipport);
IOThreadManager nx = null;
if(whm.containsKey(uid)) {
nx=whm.get(uid);
}else {
nx=new IOThreadManager();
Socket soc=new Socket("192.168.1.233",eas.ipport.getPort());
Socket soc=new Socket(eas.ipport.getIp(),eas.ipport.getPort());
nx.setLocal(new TCPConnection(null, soc));
nx.startLocal();
whm.put(uid, nx);
@@ -70,13 +70,13 @@ public class TCPConnection {
if(tunnel!=null) {
tunnel.setDelay(delay);
}
if(connect!=null) {
/*if(connect!=null) {
try {
connect.setSoTimeout((int) (delay/100000));
} catch (SocketException e) {
e.printStackTrace();
}
}
}*/
}
public void setTunnel(Tunnel tunnel) {
@@ -19,6 +19,9 @@ import java.util.concurrent.atomic.AtomicInteger;
public class Tunnel {
private String name;
private IPPort ipport;
public IPPort getIpport() {
return ipport;
}
private long delay=-1;
public long getDelay() {
return delay;