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; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.UUID; import java.util.WeakHashMap; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Supplier; import org.kne.cloud.network.mport.IPPort; import org.kne.cloud.network.mport.ServiceElement; import org.kne.cloud.network.mport.ThreadTool; public class KLALBServer { Map whm=new ConcurrentHashMap<>(); //Map whm=Collections.synchronizedMap( new WeakHashMap()); public KLALBServer(int port,Supplier gjso) throws IOException { TCPListener tcpl=new TCPListener(port); tcpl.setCon((s)->{ RemoteTCPConnection tcc=null; KLALBController nx=null; UUID uid=null; try { //s.setSoTimeout(10000); tcc=new RemoteTCPConnection(null,s); DataInputStream din=tcc.getDin(); int x=din.read(); if(x==0) { String sj=gjso.get(); tcc.getDout().writeUTF(sj); tcc.getDout().flush(); return; } String name = din.readUTF(); String ip = din.readUTF(); int portx=din.readInt(); Tunnel tll=new Tunnel(name,new IPPort( ip, portx)); tcc.setTunnel(tll); uid=new UUID(din.readLong(),din.readLong()); tcc.handshake(10); synchronized (whm) { nx=whm.computeIfAbsent(uid, (kuid)->{ KLALBController iom=new KLALBController(); iom.getCore().setAcceptSYN((b,bol)->{ try { STJson stj=new STJson(gjso.get()); if(!stj.checkSafety(b.lipport)) { System.out.println("未授权的请求:"+b.lipport); bol.accept(false); return; } LocalTCPConnection ltc=null; try { Socket soc=new Socket(b.lipport.getIp(),b.lipport.getPort()); ltc=new LocalTCPConnection(soc, b.cuid); bol.accept(true); iom.handleLocal(ltc, false); return; } catch (IOException e) { e.printStackTrace(); }finally { if(ltc!=null) ltc.close(); } bol.accept(false); return ; } catch (IOException e) { System.out.println("连接本地服务"+b.lipport+"失败,请检查你的本地服务"); e.printStackTrace(); } bol.accept(false); return; }); return iom; }); } /*if(whm.containsKey(uid)) { nx=whm.get(uid); }else { nx=new IOThreadManager(); IOThreadManager n1=nx; nx.getCore().setAcceptSYN((b)->{ try { STJson stj=new STJson(gjso.get()); if(!stj.checkSafety(b.lipport)) { System.out.println("未授权的请求:"+b.lipport); return false; } Socket soc=new Socket(b.lipport.getIp(),b.lipport.getPort()); LocalTCPConnection ltc=new LocalTCPConnection(soc, b.cuid); ThreadTool.makeVThreadIfSupport("局域网连接监视线程", ()->{ try { n1.handleLocal(ltc, false); }finally { ltc.close(); } }).start(); return true; } catch (IOException e) { System.out.println("连接本地服务"+b.lipport+"失败,请检查你的本地服务"); e.printStackTrace(); } return false; }); whm.put(uid, nx); }*/ nx.handleRemote(tcc); }catch(IOException e) { e.printStackTrace(); }finally { if(tcc!=null) tcc.close(); if(nx.getCore().getRemotetcps().isEmpty()) { nx.close(); if(uid!=null) whm.remove(uid); } } }); tcpl.open(); } /*public static void main(String[] args) throws IOException { KLALBServer kc=new KLALBServer(4569); }*/ }