Files
KLALB/src/org/kne/cloud/network/klalb/KLALBClient.java
T
Administrator 0499ecb34d 修复长时间使用导致连接卡死的故障
优化性能,减少内存占用
提高断线切换的响应速度
2023-04-05 08:44:37 +08:00

152 lines
4.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package org.kne.cloud.network.klalb;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringReader;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import javax.swing.text.html.HTMLDocument.HTMLReader.PreAction;
import org.kne.cloud.network.mport.IPPort;
import org.kne.cloud.network.mport.ServiceElement;
import org.kne.cloud.network.mport.ThreadTool;
import com.google.gson.stream.JsonReader;
public class KLALBClient {
private UUID suid=UUID.randomUUID();
private KLALBController iom=new KLALBController();
private List<Tunnel> tls=new ArrayList<>();
private List<TCPListener> tcpl=new ArrayList<>();
private List<ProxyElement> pet=new ArrayList<>();
private void open(ProxyElement pe) throws IOException {
TCPListener tl=new TCPListener(pe.getIpport().getPort(),pe.getIpport().getIp());
tl.setCon((s)->{
LocalTCPConnection tcc = null;
try {
tcc = new LocalTCPConnection(s);
tcc.setServiceElement(pe.getSe());
//System.out.println("TCP"+tcc.getCuid()+"已连接");
iom.handleLocal(tcc,true);
//System.out.println("TCP"+tcc.getCuid()+"已关闭");
} catch (IOException e) {
e.printStackTrace();
}finally {
if(tcc!=null) {
tcc.close();
}
}
});
tl.open();
tcpl.add(tl);
System.out.println(pe.getSe().name+"映射已启动 端口:"+pe.getIpport().getPort()+" 绑定地址:"+pe.getIpport().getIp());
}
public void listenProxy() {
for (int i = 0; i < pet.size(); i++) {
ProxyElement pe=pet.get(i);
if(pe.getIpport().getPort()>0&&pe.getIpport().getPort()<65536)
try {
open(pe);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void connectTunnel() {
for (int i = 0; i < tls.size(); i++) {
Tunnel tll=tls.get(i);
ThreadTool.makeVThreadIfSupport("隧道监视线程", ()->{
int re=0;
RemoteTCPConnection tc=null;
while(iom.isOpen()) {
try {
re++;
tc=new RemoteTCPConnection(tll);
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();
tc.handshake(10);
re=0;
System.out.println(tll+":连接成功");
tll.setState(true);
iom.handleRemote(tc);
System.out.println(tll+":连接断开");
tll.setState(false);
} catch (UnknownHostException e) {
e.printStackTrace();
}catch(ConnectException|SocketTimeoutException e) {
}catch(EOFException e) {
} catch (IOException e) {
e.printStackTrace();
}finally {
if(tc!=null)
tc.close();
}
try {
//System.out.println(re);
Thread.sleep(5000*(1<<Math.min(re,3)));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
public List<Tunnel> getTls() {
return tls;
}
public List<ProxyElement> getPet() {
return pet;
}
public List<ServiceElement> services=new ArrayList<ServiceElement>();
public List<ServiceElement> getServices() {
return services;
}
public void close() {
iom.close();
for (Iterator<TCPListener> iterator = tcpl.iterator(); iterator.hasNext();) {
TCPListener tl = (TCPListener) iterator.next();
tl.close();
}
tls.forEach((v)->{
v.destroyFrpc();
});
}
}