Files
KLALB/src/org/kne/cloud/network/klalb/KLALBProxySystem.java
T
2025-11-15 11:08:16 +08:00

301 lines
8.3 KiB
Java

package org.kne.cloud.network.klalb;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.kne.cloud.network.*;
import org.kne.cloud.network.klalb.ui.KLALBStateGUI2;
import org.kne.cloud.network.klalb.ui.KLALBStateGUI3;
import org.kne.cloud.network.klalb.ui.Language;
import org.kne.cloud.network.klalb.ui.UIEnv;
import org.kne.cloud.network.minecraft.MinecraftSocketBridge;
import java.util.Set;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
public class KLALBProxySystem {
private Set<Proxy> proxys=new HashSet<>();
private KLALBController klalbController;
private KLALBRemoteManagement krm;
private KLALBConfig config;
private Gson gson;
private File jsonFile;
{
GsonBuilder gb=new GsonBuilder().setPrettyPrinting();
MultipurposeSocketAddress.registerToGsonBuilder(gb);
KLALBConfigItem.registerToGsonBuilder(gb);
gson=gb.create();
}
public Set<Proxy> getProxys() {
return proxys;
}
public KLALBController getKlalbController() {
return klalbController;
}
public KLALBProxySystem(Reader json) {
loadConfigJson(json);
}
public KLALBProxySystem(String json) {
loadConfigJson(json);
}
public KLALBProxySystem(JsonElement json) {
loadConfigJson(json);
}
public KLALBProxySystem(File jsonFile) throws IOException {
loadConfigJson(jsonFile);
}
public KLALBProxySystem() {
}
public void enableRemoteManagement() throws IOException {
if(krm==null) {
krm=new KLALBRemoteManagement(this);
}else {
throw new IllegalStateException("Remote Management already enabled!");
}
}
public void enableRemoteManagement(MultipurposeSocketAddress bind) throws IOException {
if(krm==null) {
krm=new KLALBRemoteManagement(this,bind);
}else {
throw new IllegalStateException("Remote Management already enabled!");
}
}
public boolean isRemoteManagementEnabled() {
return krm!=null;
}
public void disableRemoteManagement() {
if(krm!=null) {
krm.close();
krm=null;
}
}
public void loadConfigJson(File jsonFile) throws IOException {
this.jsonFile=jsonFile;
FileReader fr = null;
try {
fr=new FileReader(jsonFile);
loadConfigJson(fr);
}finally {
if(fr!=null)
fr.close();
}
}
public void loadConfigJson(String json) {
loadConfigJson(new JsonParser().parse(json));
}
public void loadConfigJson(Reader json) {
loadConfigJson(new JsonParser().parse(json));
}
public void loadConfigJson(JsonElement json) {
KLALBConfig config= gson.fromJson(json, KLALBConfig.class);
System.out.println(config);
loadConfig(config);
}
public void loadConfig(KLALBConfig config) {
this.config=config;
for(KLALBConfigItem item:config) {
if(item instanceof KLALBControllerConfigItem) {
KLALBControllerConfigItem kcci=(KLALBControllerConfigItem) item;
String lstr=kcci.getLanguage();
if(lstr!=null) {
Language lang=Language.valueOf(lstr);
if(lang!=null) {
try {
UIEnv.setRsb(lang.getFileName());
} catch (IOException e) {
e.printStackTrace();
}
}
}
String vase= kcci.getVirtualAddress();
//System.out.println(vase);
List<InetAddress>daddr=new ArrayList<InetAddress>();
List<InetAddress> vdns= kcci.getDNS();
if(vdns!=null) {
for(InetAddress dnsaddr:vdns) {
daddr.add(dnsaddr);
}
}
if(vase!=null) {
try {
klalbController=new KLALBController((Inet6Address) InetAddress.getByName(vase),daddr);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}else {
klalbController=new KLALBController(daddr);
}
Long vasn= kcci.getVirtualASN();
if(vasn!=null) {
klalbController.getIpv6Router().setASN (vasn);
}
String vsne=kcci.getVirtualSocketName();
//if(vsne!=null) {
MultipurposeSocketAddress.getSocketTypeRegister().put(vsne, klalbController.getStreamSocketType());
//}
MultipurposeSocketAddress tcple=kcci.getTCPListen();
if(tcple!=null) {
SocketChannelListener tcpl = null;
try {
tcpl = new SocketChannelListener(tcple);
tcpl.setCon((soc)->{
KLALBRemoteLine krs=null;
try {
krs = new KLALBRemoteLine(new StreamChannelKLALBPacketLink(soc));
klalbController.addRemoteLine(krs);
} catch (IOException e) {
e.printStackTrace();
}
});
MultipurposeSocketAddress mpsa2=new MultipurposeSocketAddress(tcple.getType(), tcple.getHost(),((InetSocketAddress)tcpl.getServerSocketChannel().getLocalAddress()).getPort());
klalbController.getListenSocketAddress().add(mpsa2);
} catch (IOException e1) {
e1.printStackTrace();
}
}
MultipurposeSocketAddress udple=kcci.getUDPListen();
if(udple!=null) {
DatagramSocketListener udpl = null;
try {
udpl = new DatagramSocketListener(udple);
udpl.setCon((soc)->{
KLALBRemoteLine krs=null;
try {
krs = new KLALBRemoteLine(new SplitedDatagramKLALBPacketLink(soc));
klalbController.addRemoteLine(krs);
} catch (IOException e) {
e.printStackTrace();
}
});
MultipurposeSocketAddress mpsau2=new MultipurposeSocketAddress(udple.getType(), udple.getHost(),((InetSocketAddress)(udpl.getDatagramServerSocket().getLocalSocketAddress())).getPort());
//klalbController.getListenSocketAddress().add(mpsau2);
} catch (IOException e1) {
e1.printStackTrace();
}
}
List<MultipurposeSocketAddress> linele=kcci.getLineTable();
if(linele!=null) {
klalbController.getSelflineTable().addAll(linele);
}
List<MultipurposeSocketAddress> linetoc=kcci.getConnectLineTable();
if(linetoc!=null) {
linetoc.forEach((aline)->{
klalbController.addRemoteLines(aline);
});
}
List<MultipurposeSocketAddress> ntps=kcci.getNtpServerTable();
if(ntps!=null) {
klalbController.getNTPTable().addAll(ntps);
}
List<String> strexc=kcci.getNetworkInterfaceExcepts();
klalbController.getNetworkInterfaceExcept().clear();
if(strexc!=null) {
for(String strexci:strexc) {
try {
klalbController.getNetworkInterfaceExcept().add(NetworkInterface.getByName(strexci));
} catch (SocketException e) {
e.printStackTrace();
}
}
}
}else if(item instanceof SocketBridgeConfigItem) {
SocketBridgeConfigItem scci=(SocketBridgeConfigItem) item;
try {
proxys.add(scci.createProxy(klalbController));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private KLALBStateGUI3 kgui;
public KLALBStateGUI3 getKLALBGUI() {
if(kgui==null) {
kgui=new KLALBStateGUI3(klalbController);
kgui.loadConfig(config);
kgui.setSaveComsumer((cfg)->{
String json=gson.toJson(cfg);
if(jsonFile!=null) {
FileWriter fw = null;
try {
fw=new FileWriter(jsonFile);
fw.write(json);
}catch(IOException e) {
e.printStackTrace();
}finally {
if(fw!=null)
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
return kgui;
}
public KLALBConfig getConfig() {
return config;
}
public KLALBControllerConfigItem getControllerConfig() {
for(KLALBConfigItem item:config) {
if(item instanceof KLALBControllerConfigItem)
return (KLALBControllerConfigItem) item;
}
return null;
}
}