Files
KLALB/src/org/kne/cloud/network/klalb/KLALBProxySystem.java
T
SerinaNya 61868fe93e feat(config): switch webPort to URI-style webListen address
- replace integer webPort with MultiProtocolSocketAddress webListen
  (default: http://0.0.0.0:4665) in controller config
- rename Swing UI item to "Web API 监听地址" and validate as full URI
- update WebServer binding to honor configured host and port
- normalize legacy webPort integers to http://0.0.0.0:<port> on load
  and keep JSON API backward compatibility
- update i18n keys in zh_CN and en_US resource bundles
- update root klalb-config.json and AGENTS.md documentation
2026-08-26 19:42:26 +08:00

268 lines
7.6 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.lang.reflect.Type;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.HashSet;
import org.kne.cloud.network.*;
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.klalb.web.KLALBWebServer;
import java.util.Set;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonParser;
public class KLALBProxySystem {
private Set<Proxy> proxys=new HashSet<>();
private KLALBController klalbController;
private KLALBWebServer webServer;
private KLALBConfig config;
private Gson gson;
private File jsonFile;
{
GsonBuilder gb=new GsonBuilder().setPrettyPrinting();
MultiProtocolSocketAddress.registerToGsonBuilder(gb);
KLALBConfigItem.registerToGsonBuilder(gb);
gb.registerTypeAdapter(InetAddress.class, new JsonSerializer<InetAddress>() {
@Override
public JsonElement serialize(InetAddress src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.getHostAddress());
}
});
gb.registerTypeAdapter(InetAddress.class, new JsonDeserializer<InetAddress>() {
@Override
public InetAddress deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
try {
return InetAddress.getByName(json.getAsString());
} catch (Exception e) {
throw new JsonParseException(e);
}
}
});
gson=gb.create();
}
public Gson getGson() {
return gson;
}
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 enableWebServer() throws IOException {
KLALBControllerConfigItem cci = getControllerConfig();
MultiProtocolSocketAddress listen = cci == null || cci.getWebListen() == null
? new MultiProtocolSocketAddress("http", "0.0.0.0", 4665) : cci.getWebListen();
enableWebServer(listen);
}
public void enableWebServer(int port) throws IOException {
enableWebServer(new MultiProtocolSocketAddress("http", "0.0.0.0", port));
}
public void enableWebServer(MultiProtocolSocketAddress listen) throws IOException {
if (webServer == null) {
webServer = new KLALBWebServer(this, listen);
webServer.start();
} else {
throw new IllegalStateException("Web server already enabled!");
}
}
public boolean isWebServerEnabled() {
return webServer != null && webServer.isRunning();
}
public KLALBWebServer getWebServer() {
return webServer;
}
public void disableWebServer() {
if (webServer != null) {
webServer.stop();
webServer = null;
}
}
public void loadConfigJson(File jsonFile) throws IOException {
this.jsonFile=jsonFile;
if(jsonFile.exists()) {
FileReader fr = null;
try {
fr = new FileReader(jsonFile);
loadConfigJson(fr);
} finally {
if (fr != null)
fr.close();
}
}else{
loadConfig(KLALBConfig.getDefault());
}
}
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);
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();
}
}
}
klalbController=new KLALBController(kcci);
String vsne=kcci.getVirtualSocketName();
//if(vsne!=null) {
MultiProtocolSocketAddress.getSocketTypeRegister().put(vsne, klalbController.getStreamSocketType());
//}
MultiProtocolSocketAddress tcple=kcci.getTCPListen();
if(tcple!=null) {
SocketChannelListener tcpl = null;
try {
tcpl = new SocketChannelListener(tcple);
tcpl.setCon((soc)->{
KLALBRemoteLink krs=null;
try {
krs = new KLALBRemoteLink(klalbController,new StreamChannelKLALBPacketLink(soc));
klalbController.addRemoteLine(krs);
} catch (IOException e) {
e.printStackTrace();
}
});
MultiProtocolSocketAddress mpsa2=new MultiProtocolSocketAddress(tcple.getProtocol(), tcple.getHost(),((InetSocketAddress)tcpl.getServerSocketChannel().getLocalAddress()).getPort());
klalbController.getListenSocketAddress().add(mpsa2);
} catch (IOException e1) {
e1.printStackTrace();
}
}
MultiProtocolSocketAddress udple=kcci.getUDPListen();
if(udple!=null) {
DatagramSocketListener udpl = null;
try {
udpl = new DatagramSocketListener(udple);
udpl.setCon((soc)->{
KLALBRemoteLink krs=null;
try {
krs = new KLALBRemoteLink(klalbController,new SplitedDatagramKLALBPacketLink(soc));
klalbController.addRemoteLine(krs);
} catch (IOException e) {
e.printStackTrace();
}
});
MultiProtocolSocketAddress mpsau2=new MultiProtocolSocketAddress(udple.getProtocol(), udple.getHost(),((InetSocketAddress)(udpl.getDatagramServerSocket().getLocalSocketAddress())).getPort());
//klalbController.getListenSocketAddress().add(mpsau2);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}else if(item instanceof SocketBridgeConfigItem) {
SocketBridgeConfigItem scci=(SocketBridgeConfigItem) item;
try {
proxys.add(scci.createProxy(klalbController));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void saveConfigToFile() {
if (jsonFile != null && config != null) {
String json = gson.toJson(config);
try (FileWriter fw = new FileWriter(jsonFile)) {
fw.write(json);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private KLALBStateGUI3 kgui;
public KLALBStateGUI3 getKLALBGUI() {
if(kgui==null) {
kgui=new KLALBStateGUI3(klalbController);
kgui.loadConfig(config);
kgui.setSaveComsumer((cfg)->{
saveConfigToFile();
});
}
return kgui;
}
public KLALBConfig getConfig() {
return config;
}
public KLALBControllerConfigItem getControllerConfig() {
for(KLALBConfigItem item:config) {
if(item instanceof KLALBControllerConfigItem)
return (KLALBControllerConfigItem) item;
}
return null;
}
}