forked from KNEMC/KLALB
227 lines
6.0 KiB
Java
227 lines
6.0 KiB
Java
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;
|
|
import java.net.URI;
|
|
import java.net.URL;
|
|
import java.net.UnknownHostException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Scanner;
|
|
import java.util.UUID;
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
import org.kne.cloud.network.mport.IPPort;
|
|
import org.kne.cloud.network.mport.ServiceElement;
|
|
import org.kne.cloud.network.mport.ThreadTool;
|
|
|
|
public class KLALBClient {
|
|
private static Object olock=new Object();
|
|
static {
|
|
new Thread(()->{
|
|
while(true) {
|
|
try {
|
|
Thread.sleep(10000);
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
synchronized(olock) {
|
|
olock.notifyAll();
|
|
}
|
|
}
|
|
}).start();
|
|
}
|
|
|
|
|
|
private List<Tunnel> tls=new ArrayList<>();
|
|
private TCPListener tcpl;
|
|
private ServiceElement sel;
|
|
|
|
public void runProtocol(IOThreadManager kcp, UUID uid) {
|
|
AtomicBoolean b=new AtomicBoolean(true);
|
|
AtomicInteger aig=new AtomicInteger(0);
|
|
for (int i = 0; i < tls.size(); i++) {
|
|
Tunnel tll=tls.get(i);
|
|
ThreadTool.makeVThreadIfSupport("隧道监视线程",()->{
|
|
while (kcp.isOpen()) {
|
|
try {
|
|
TCPConnection tc=new TCPConnection(tll);
|
|
tc.getDout().writeShort(59649);
|
|
tc.getDout().write(1);
|
|
|
|
tc.getDout().writeUTF(sel.ipport.getIp().getHostAddress());
|
|
tc.getDout().writeInt(sel.ipport.getPort());
|
|
tc.getDout().writeUTF(sel.proc);
|
|
|
|
tc.getDout().writeUTF(tll.getName());
|
|
tc.getDout().writeUTF(tll.getIp());
|
|
tc.getDout().writeInt(tll.getPort());
|
|
tc.getDout().writeLong(uid.getMostSignificantBits());
|
|
tc.getDout().writeLong(uid.getLeastSignificantBits());
|
|
tc.getDout().flush();
|
|
aig.incrementAndGet();
|
|
System.out.println("隧道"+tll+"已连接,可用线路数量:"+ (kcp.getTcps().size()+1));
|
|
try {
|
|
kcp.handleSocket(tc);
|
|
|
|
}catch(IOException e){
|
|
e.printStackTrace();
|
|
}finally {
|
|
tc.close();
|
|
}
|
|
int n=kcp.getTcps().size();
|
|
System.out.println("隧道"+tll+"已断开,可用线路数量:"+ n);
|
|
if(n<=0) {
|
|
kcp.closeRemote();
|
|
kcp.closeLocal();
|
|
System.out.println("连接已断开");
|
|
return;
|
|
}
|
|
} catch (IOException e) {
|
|
if(aig.get()<=0) {
|
|
kcp.closeRemote();
|
|
kcp.closeLocal();
|
|
if(b.get()) {
|
|
System.out.println("隧道连接失败");
|
|
b.set(false);
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
synchronized(olock) {
|
|
try {
|
|
olock.wait();
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}).start();
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
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() {
|
|
return tls;
|
|
}
|
|
public List<ServiceElement> services=new ArrayList<ServiceElement>();
|
|
|
|
public List<ServiceElement> getServices() {
|
|
return services;
|
|
}
|
|
public void searchTunnels(String ipport) throws IOException {
|
|
IPPort u=new IPPort(ipport);
|
|
TCPConnection tcpc=null;
|
|
try {
|
|
tcpc=new TCPConnection(null, new Socket(u.getIp(),u.getPort()));
|
|
tcpc.getDout().writeShort(59649);
|
|
tcpc.getDout().write(0);
|
|
tcpc.getDout().flush();
|
|
int s=tcpc.getDin().readInt();
|
|
System.out.println("服务列表:");
|
|
for (int i = 0; i < s; i++) {
|
|
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++) {
|
|
String str=tcpc.getDin().readUTF();
|
|
System.out.println(str);
|
|
tls.add(Tunnel.newTunnel(str));
|
|
}
|
|
}finally {
|
|
if(tcpc!=null) {
|
|
tcpc.close();
|
|
}
|
|
}
|
|
}
|
|
|
|
public ServiceElement getSel() {
|
|
return sel;
|
|
}
|
|
public void setSel(ServiceElement sel) {
|
|
this.sel = sel;
|
|
}
|
|
public static void main(String[] args) throws IOException {
|
|
Scanner scn=new Scanner(System.in);
|
|
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());
|
|
System.out.println("服务已开启");
|
|
while(true) {
|
|
String s=scn.next();
|
|
switch(s) {
|
|
case "states":
|
|
Tunnel.states();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
//Tunnel t= new Tunnel("Test1", "127.0.0.1", 4569);
|
|
/*Tunnel t= new Tunnel("Test1", "cn-hz-bgp-1.openfrp.top", 65529);
|
|
tls.add(t);
|
|
Tunnel t2=new Tunnel("Test2","cn-bj-bgp-3.openfrp.top",65529);
|
|
tls.add(t2);
|
|
Tunnel t3=new Tunnel("Test3","180.76.147.250",65529);
|
|
tls.add(t3);
|
|
Tunnel t4=new Tunnel("Test4","cn-sx-xa-bgp-1.openfrp.top",65529);
|
|
tls.add(t4);
|
|
*/
|
|
/* List<Tunnel> tls=kc.getTls();
|
|
Tunnel t5=new Tunnel("Test5","la.afrps.cn",49966);
|
|
tls.add(t5);
|
|
Tunnel t6=new Tunnel("Test6","sg.afrps.cn",49966);
|
|
tls.add(t6);
|
|
Tunnel t7=new Tunnel("Test7","ch.afrps.cn",49966);
|
|
tls.add(t7);
|
|
Tunnel t8=new Tunnel("Test8","sj.afrps.cn",49966);
|
|
tls.add(t8);
|
|
Tunnel t9=new Tunnel("Test9","frp.104300.xyz",49965);
|
|
tls.add(t9);
|
|
|
|
Tunnel t11=new Tunnel("Test11","153.36.240.12",65529);
|
|
tls.add(t11);
|
|
Tunnel t12=new Tunnel("Test12","frp.freefrps.com",49965);
|
|
tls.add(t12);*/
|
|
|
|
}
|