package org.kne.cloud.network.klalb; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; 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.UUID; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; public class KLALBClient { private List tls=new ArrayList<>(); private TCPListener tcpl; 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); 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(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)); kcp.handleSocket(tc); 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; } } try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } } public void open() throws IOException { tcpl.open(); } public List getTls() { return tls; } public static List services=new ArrayList(); 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(); for (int i = 0; i < s; i++) { services.add(new ServiceElement(tcpc.getDin().readUTF())); } int s2=tcpc.getDin().readInt(); for (int i = 0; i < s2; i++) { tls.add(new Tunnel(tcpc.getDin().readUTF())); } System.out.println(services); System.out.println(tls); }finally { if(tcpc!=null) { tcpc.close(); } } } public static void main(String[] args) throws IOException { KLALBClient kc=new KLALBClient(4568); kc.searchTunnels("153.36.240.12:65529"); //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 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);*/ kc.open(); } }