[server][ xray]- 完成数据库serverInfo转换ProxyNode;完成代理链配置生成

This commit is contained in:
zeaslity
2023-03-01 16:45:34 +08:00
parent 8a15e3fcf6
commit 55b7f68282
7 changed files with 68 additions and 36 deletions

View File

@@ -1,6 +1,7 @@
package io.wdd.func.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.wdd.common.beans.response.R;
@@ -17,9 +18,9 @@ import static io.wdd.func.xray.persisit.cache.ProxyNodeCache.*;
@RestController
@RequestMapping("/server/func/xray")
@Api(tags = "Xray控制")
public class XrayController {
@Resource
XrayCoreService xrayCoreService;
@@ -31,11 +32,16 @@ public class XrayController {
@ApiOperation("[解析] - 解析代理链字符串")
public R<List<List<ProxyNode>>> transformProxyChainString(
@ApiParam(name = "proxyChainList", value = "代理链字符串seoul2->tokyo2, 多条链使用,分隔")
@RequestParam("proxyChainList") List<String> proxyChainList
@RequestParam("proxyChainList") List<String> proxyChainList,
@ApiParam(name = "isTopicName", value = "是否使用AgentTopicName, 默认为AgentName")
@RequestParam(value = "isTopicName", required = false, defaultValue = "false") boolean isTopicName
) {
return R.ok(
xrayCoreService.transformProxyChainString(proxyChainList)
xrayCoreService.transformProxyChainString(
proxyChainList,
isTopicName
)
);
}
@@ -44,12 +50,17 @@ public class XrayController {
@ApiOperation("[解析] - 代理链完整配置")
public R<List<XrayConfigInfo>> debugProxyChainString(
@ApiParam(name = "proxyChainList", value = "代理链字符串seoul2->tokyo2, 多条链使用,分隔")
@RequestParam("proxyChainList") List<String> proxyChainList
@RequestParam("proxyChainList") List<String> proxyChainList,
@ApiParam(name = "isTopicName", value = "是否使用AgentTopicName, 默认为AgentName")
@RequestParam(value = "isTopicName", required = false, defaultValue = "false") boolean isTopicName
) {
return R.ok(
xrayCoreService.generateXrayConfigFromNodeList(
xrayCoreService.transformProxyChainString(proxyChainList)
xrayCoreService.transformProxyChainString(
proxyChainList,
isTopicName
)
)
);
}
@@ -67,7 +78,7 @@ public class XrayController {
@ApiOperation("[缓存] - 获取代理链缓存")
public R<HashMap<String, ProxyNode>> getProxyNodeListCache() {
return R.ok(ProxyNodeMap);
return R.ok(PROXY_NODE_TOPIC_NAME_MAP);
}

View File

@@ -5,7 +5,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static io.wdd.func.xray.persisit.cache.ProxyNodeCache.ProxyNodeStaticMap;
import static io.wdd.func.xray.persisit.cache.ProxyNodeCache.PROXY_NODE_INDEX_MAP;
/**
@@ -91,10 +91,10 @@ class Graph {
for (int heigh = 0; heigh < height; heigh++) {
List<Integer> list = adj_matrix.get(heigh);
System.out.print("FROM [ " + ProxyNodeStaticMap.get(heigh) + " ] ==> ");
System.out.print("FROM [ " + PROXY_NODE_INDEX_MAP.get(heigh) + " ] ==> ");
for (int width = 0; width < weight; width++) {
if (list.get(width) != 0) {
System.out.print(ProxyNodeStaticMap.get(width) + ": " + list.get(width) + "\t");
System.out.print(PROXY_NODE_INDEX_MAP.get(width) + ": " + list.get(width) + "\t");
}
}
System.out.print("\n");

View File

@@ -15,8 +15,7 @@ import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import static io.wdd.func.xray.persisit.cache.ProxyNodeCache.ProxyNodeMap;
import static io.wdd.func.xray.persisit.cache.ProxyNodeCache.ProxyNodeStaticMap;
import static io.wdd.func.xray.persisit.cache.ProxyNodeCache.*;
import static io.wdd.rpc.init.AcceptAgentInitInfo.ALL_SERVER_CITY_INDEX;
/**
@@ -110,21 +109,26 @@ public class XrayDBOperator {
// 缓存内容
ProxyNodeMap.clear();
ProxyNodeStaticMap.clear();
PROXY_NODE_MAP.clear();
PROXY_NODE_TOPIC_NAME_MAP.clear();
PROXY_NODE_INDEX_MAP.clear();
proxyNodeList
.stream()
.forEach(
proxyNode -> {
ProxyNodeMap.put(
PROXY_NODE_TOPIC_NAME_MAP.put(
proxyNode.getAgentTopicName(),
proxyNode
);
ProxyNodeStaticMap.put(
PROXY_NODE_INDEX_MAP.put(
proxyNode.getNum(),
proxyNode
);
PROXY_NODE_MAP.put(
proxyNode.getAgentName(),
proxyNode
);
}
);

View File

@@ -7,13 +7,18 @@ import java.util.HashMap;
public class ProxyNodeCache {
/**
* 保存 AgentName - ProxyNode 的全局缓存
*/
public static final HashMap<String, ProxyNode> PROXY_NODE_MAP = new HashMap<>();
/**
* 保存 AgentTopicName - ProxyNode 的全局缓存
*/
public static final HashMap<String, ProxyNode> ProxyNodeMap = new HashMap<>();
public static final HashMap<String, ProxyNode> PROXY_NODE_TOPIC_NAME_MAP = new HashMap<>();
public static final HashMap<Integer, ProxyNode> ProxyNodeStaticMap = new HashMap<>();
public static final HashMap<Integer, ProxyNode> PROXY_NODE_INDEX_MAP = new HashMap<>();
public static ProxyNode chengdu;
public static ProxyNode shanghai;
@@ -139,43 +144,43 @@ public class ProxyNodeCache {
.agentTopicName("Seoul-arm64-02-oracle")
.build();
ProxyNodeStaticMap.put(
PROXY_NODE_INDEX_MAP.put(
chengdu.getNum(),
chengdu
);
ProxyNodeStaticMap.put(
PROXY_NODE_INDEX_MAP.put(
hongkong.getNum(),
hongkong
);
ProxyNodeStaticMap.put(
PROXY_NODE_INDEX_MAP.put(
shanghai.getNum(),
shanghai
);
ProxyNodeStaticMap.put(
PROXY_NODE_INDEX_MAP.put(
seoul2.getNum(),
seoul2
);
ProxyNodeStaticMap.put(
PROXY_NODE_INDEX_MAP.put(
tokyo2.getNum(),
tokyo2
);
ProxyNodeStaticMap.put(
PROXY_NODE_INDEX_MAP.put(
phoenix2.getNum(),
phoenix2
);
ProxyNodeStaticMap.put(
PROXY_NODE_INDEX_MAP.put(
london2.getNum(),
london2
);
ProxyNodeStaticMap.put(
PROXY_NODE_INDEX_MAP.put(
chengduAgent.getNum(),
chengduAgent
);
ProxyNodeStaticMap.put(
PROXY_NODE_INDEX_MAP.put(
tokyoDev.getNum(),
tokyoDev
);
ProxyNodeStaticMap.put(
PROXY_NODE_INDEX_MAP.put(
seoul5.getNum(),
seoul5
);

View File

@@ -12,9 +12,10 @@ public interface XrayCoreService {
* 解析代理链字符串,返回完整的代理列表
*
* @param proxyChainList 代理链字符串
* @param isTopicName
* @return
*/
List<List<ProxyNode>> transformProxyChainString(List<String> proxyChainList);
List<List<ProxyNode>> transformProxyChainString(List<String> proxyChainList, boolean isTopicName);
List<XrayConfigInfo> generateXrayConfigFromNodeList(List<List<ProxyNode>> allNetworkPathList);

View File

@@ -25,7 +25,8 @@ import static io.wdd.func.xray.beans.config.InboundVmessHTTPTemplateClass.Inboun
import static io.wdd.func.xray.beans.config.InboundVmessHTTPTemplateClass.ListenAddress;
import static io.wdd.func.xray.beans.config.LogTemplateClass.LogTemplate;
import static io.wdd.func.xray.beans.config.OutboundVmessHTTPTemplateClass.*;
import static io.wdd.func.xray.persisit.cache.ProxyNodeCache.ProxyNodeMap;
import static io.wdd.func.xray.persisit.cache.ProxyNodeCache.PROXY_NODE_MAP;
import static io.wdd.func.xray.persisit.cache.ProxyNodeCache.PROXY_NODE_TOPIC_NAME_MAP;
import static io.wdd.func.xray.service.XrayConfigPersistor.cleanVersion;
@Service
@@ -41,20 +42,28 @@ public class XrayCoreServiceImpl implements XrayCoreService {
private static final String ProxyChainSplitor = "->";
@Override
public List<List<ProxyNode>> transformProxyChainString(List<String> proxyChainList) {
public List<List<ProxyNode>> transformProxyChainString(List<String> proxyChainList, boolean isTopicName) {
proxyChainList
return proxyChainList
.stream()
.map(
proxyChain -> Arrays
.stream(proxyChain.split(ProxyChainSplitor))
.map(
// 此处获取到的就是 每一个节点的名称
// AgentName 或者 AgentTopicName
agentKindOfName -> {
if (isTopicName) {
return PROXY_NODE_TOPIC_NAME_MAP.get(agentKindOfName);
} else {
return PROXY_NODE_MAP.get(agentKindOfName);
}
}
)
.collect(Collectors.toList())
)
.collect(Collectors.toList());
return null;
}
@Override
@@ -99,6 +108,7 @@ public class XrayCoreServiceImpl implements XrayCoreService {
networkPathList -> networkPathList.stream()
)
.collect(Collectors.toSet());
// 返回所有的配置
return proxyNodeSet
.stream()
@@ -114,7 +124,7 @@ public class XrayCoreServiceImpl implements XrayCoreService {
xrayDBOperator.CacheAllProxyNodeInfo();
return ProxyNodeMap;
return PROXY_NODE_TOPIC_NAME_MAP;
}
private void generateXrayJsonSinglePath(List<ProxyNode> networkPathList) {