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; import java.util.Vector;
public class IOThreadManager { public class IOThreadManager {
private KLALBCore klc=new KLALBCore(500); private KLALBCore klc=new KLALBCore(200);
private List<TCPConnection>tcps=new Vector<>(); private List<TCPConnection>tcps=new Vector<>();
@@ -35,6 +35,10 @@ public class IPPort implements Serializable{
ip=InetAddress.getByName(ip2); ip=InetAddress.getByName(ip2);
port=port2; port=port2;
} }
public IPPort(InetSocketAddress remoteSocketAddress) {
ip=remoteSocketAddress.getAddress();
port=remoteSocketAddress.getPort();
}
public InetAddress getIp() { public InetAddress getIp() {
return ip; return ip;
} }
@@ -2,6 +2,7 @@ package org.kne.cloud.network.klalb;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.Socket; import java.net.Socket;
@@ -19,21 +20,7 @@ public class KLALBClient {
private List<Tunnel> tls=new ArrayList<>(); private List<Tunnel> tls=new ArrayList<>();
private TCPListener tcpl; private TCPListener tcpl;
private ServiceElement sel; 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) { public void runProtocol(IOThreadManager kcp, UUID uid) {
AtomicBoolean b=new AtomicBoolean(true); AtomicBoolean b=new AtomicBoolean(true);
AtomicInteger aig=new AtomicInteger(0); AtomicInteger aig=new AtomicInteger(0);
@@ -57,15 +44,15 @@ public class KLALBClient {
tc.getDout().writeLong(uid.getLeastSignificantBits()); tc.getDout().writeLong(uid.getLeastSignificantBits());
tc.getDout().flush(); tc.getDout().flush();
aig.incrementAndGet(); aig.incrementAndGet();
//System.out.println("隧道"+tll+"已连接,可用线路数量:"+ (kcp.getTcps().size()+1)); System.out.println("隧道"+tll+"已连接,可用线路数量:"+ (kcp.getTcps().size()+1));
kcp.handleSocket(tc); kcp.handleSocket(tc);
tc.close(); tc.close();
int n=kcp.getTcps().size(); int n=kcp.getTcps().size();
//System.out.println("隧道"+tll+"已断开,可用线路数量:"+ n); System.out.println("隧道"+tll+"已断开,可用线路数量:"+ n);
if(n<=0) { if(n<=0) {
kcp.closeRemote(); kcp.closeRemote();
kcp.closeLocal(); kcp.closeLocal();
//System.out.println("连接已断开"); System.out.println("连接已断开");
return; return;
} }
} catch (IOException e) { } catch (IOException e) {
@@ -81,7 +68,7 @@ public class KLALBClient {
} }
try { try {
Thread.sleep(5000); Thread.sleep(1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); 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(); tcpl.open();
} }
public List<Tunnel> getTls() { public List<Tunnel> getTls() {
@@ -113,15 +115,19 @@ public class KLALBClient {
tcpc.getDout().write(0); tcpc.getDout().write(0);
tcpc.getDout().flush(); tcpc.getDout().flush();
int s=tcpc.getDin().readInt(); int s=tcpc.getDin().readInt();
System.out.println("服务列表:");
for (int i = 0; i < s; i++) { 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(); int s2=tcpc.getDin().readInt();
System.out.println("隧道列表:");
for (int i = 0; i < s2; i++) { 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 { }finally {
if(tcpc!=null) { if(tcpc!=null) {
tcpc.close(); tcpc.close();
@@ -137,10 +143,20 @@ public class KLALBClient {
} }
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
Scanner scn=new Scanner(System.in); Scanner scn=new Scanner(System.in);
KLALBClient kc=new KLALBClient(4568); KLALBClient kc=new KLALBClient();
kc.searchTunnels("153.36.240.12:65529");
kc.setSel(kc.getServices().get(0)); /*File f=new File("klalbc.ini");
kc.open(); 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) { while(true) {
String s=scn.next(); String s=scn.next();
switch(s) { switch(s) {
@@ -76,7 +76,7 @@ public class KLALBCore {
} }
} }
if (b != null) { if (b != null) {
//System.out.println("\tPROCESS:" + inputcache.size()); System.out.println("\tPROCESS:" + inputcache.size());
inputcount++; inputcount++;
return b; return b;
} }
@@ -119,14 +119,14 @@ public class KLALBCore {
} else { } else {
if (kd.thread.isAlive()) { if (kd.thread.isAlive()) {
long timex = (System.nanoTime() - kd.time) / 1000000; long timex = (System.nanoTime() - kd.time) / 1000000;
if (timex > 10000) { if (timex > 200000) {
//System.out.println("³¬Ê±ÖØ´«£º"+kd); System.out.println("³¬Ê±ÖØ´«£º"+kd);
kd.time = System.nanoTime(); kd.time = System.nanoTime();
kd.thread = Thread.currentThread(); kd.thread = Thread.currentThread();
ks = kd; ks = kd;
} }
} else { } else {
//System.out.println("µôÏßÖØ´«£º"+kd); System.out.println("µôÏßÖØ´«£º"+kd);
kd.time = System.nanoTime(); kd.time = System.nanoTime();
kd.thread = Thread.currentThread(); kd.thread = Thread.currentThread();
ks = kd; ks = kd;
@@ -159,6 +159,9 @@ public class KLALBCore {
KLALBBlock klb=new KLALBBlock(null, 0, -x.number); KLALBBlock klb=new KLALBBlock(null, 0, -x.number);
ackq.add(klb); ackq.add(klb);
klb=new KLALBBlock(null, 0, -x.number);
ackq.add(klb);
/*BlockingQueue<KLALBBlock> bq=acks.get(in); /*BlockingQueue<KLALBBlock> bq=acks.get(in);
if(bq==null) { if(bq==null) {
BlockingQueue<KLALBBlock> bqt=new LinkedBlockingQueue<>(); BlockingQueue<KLALBBlock> bqt=new LinkedBlockingQueue<>();
@@ -220,7 +223,7 @@ public class KLALBCore {
out.flush(); out.flush();
} }
//System.out.println("SEND:" + kd); System.out.println(tcp.getTunnel().getName()+" SEND:" + kd);
} }
private KLALBBlock receive0(TCPConnection tcp) throws IOException { private KLALBBlock receive0(TCPConnection tcp) throws IOException {
@@ -238,7 +241,7 @@ public class KLALBCore {
kb.time=in.readLong(); kb.time=in.readLong();
} }
} }
//System.out.println("RECEIVE:" + kb); System.out.println(tcp.getTunnel().getName()+" RECEIVE:" + kb);
return kb; return kb;
} }
@@ -3,6 +3,7 @@ package org.kne.cloud.network.klalb;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.ConnectException; import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@@ -65,13 +66,13 @@ public class KLALBServer {
UUID uid=new UUID(din.readLong(),din.readLong()); 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; IOThreadManager nx = null;
if(whm.containsKey(uid)) { if(whm.containsKey(uid)) {
nx=whm.get(uid); nx=whm.get(uid);
}else { }else {
nx=new IOThreadManager(); 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.setLocal(new TCPConnection(null, soc));
nx.startLocal(); nx.startLocal();
whm.put(uid, nx); whm.put(uid, nx);
@@ -70,13 +70,13 @@ public class TCPConnection {
if(tunnel!=null) { if(tunnel!=null) {
tunnel.setDelay(delay); tunnel.setDelay(delay);
} }
if(connect!=null) { /*if(connect!=null) {
try { try {
connect.setSoTimeout((int) (delay/100000)); connect.setSoTimeout((int) (delay/100000));
} catch (SocketException e) { } catch (SocketException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }*/
} }
public void setTunnel(Tunnel tunnel) { public void setTunnel(Tunnel tunnel) {
@@ -19,6 +19,9 @@ import java.util.concurrent.atomic.AtomicInteger;
public class Tunnel { public class Tunnel {
private String name; private String name;
private IPPort ipport; private IPPort ipport;
public IPPort getIpport() {
return ipport;
}
private long delay=-1; private long delay=-1;
public long getDelay() { public long getDelay() {
return delay; return delay;