forked from KNEMC/KLALB
185 lines
5.2 KiB
Java
185 lines
5.2 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.ConnectException;
|
||
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 {
|
||
|
||
UUID suid=UUID.randomUUID();
|
||
IOThreadManager iom=new IOThreadManager();
|
||
private List<Tunnel> tls=new ArrayList<>();
|
||
private TCPListener tcpl;
|
||
private ServiceElement sel;
|
||
|
||
|
||
|
||
public void open(int port) throws IOException {
|
||
tcpl=new TCPListener(port);
|
||
tcpl.setCon((s)->{
|
||
LocalTCPConnection tcc = null;
|
||
try {
|
||
tcc = new LocalTCPConnection(s);
|
||
System.out.println("TCP:"+tcc.getCuid()+"已连接");
|
||
iom.handleLocal(tcc,sel,true);
|
||
System.out.println("TCP:"+tcc.getCuid()+"已关闭");
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}finally {
|
||
if(tcc!=null) {
|
||
tcc.close();
|
||
}
|
||
}
|
||
});
|
||
tcpl.open();
|
||
for (int i = 0; i < tls.size(); i++) {
|
||
Tunnel tll=tls.get(i);
|
||
ThreadTool.makeVThreadIfSupport("隧道监视线程", ()->{
|
||
RemoteTCPConnection tc=null;
|
||
while(iom.isOpen()) {
|
||
try {
|
||
tc=new RemoteTCPConnection(tll);
|
||
tc.getDout().writeShort(59649);
|
||
tc.getDout().write(1);
|
||
tc.getDout().writeUTF(tll.getName());
|
||
tc.getDout().writeUTF(tll.getIp());
|
||
tc.getDout().writeInt(tll.getPort());
|
||
tc.getDout().writeLong(suid.getMostSignificantBits());
|
||
tc.getDout().writeLong(suid.getLeastSignificantBits());
|
||
tc.getDout().flush();
|
||
System.out.println(tll+":连接成功");
|
||
iom.handleRemote(tc);
|
||
System.out.println(tll+":连接断开");
|
||
} catch (UnknownHostException e) {
|
||
e.printStackTrace();
|
||
}catch(ConnectException e) {
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}finally {
|
||
if(tc!=null)
|
||
tc.close();
|
||
}
|
||
try {
|
||
Thread.sleep(5000);
|
||
} catch (InterruptedException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}).start();
|
||
}
|
||
}
|
||
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 RemoteTCPConnection(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);*/
|
||
|
||
}
|