[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,5 +1,6 @@
package io.wdd.common.utils; package io.wdd.common.utils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
@@ -17,7 +18,7 @@ public class OctopusObjectMapperConfig {
return jacksonObjectMapperBuilder -> { return jacksonObjectMapperBuilder -> {
//若POJO对象的属性值为null序列化时不进行显示 //若POJO对象的属性值为null序列化时不进行显示
//jacksonObjectMapperBuilder.serializationInclusion(JsonInclude.Include.NON_NULL); jacksonObjectMapperBuilder.serializationInclusion(JsonInclude.Include.NON_NULL);
//针对于Date类型文本格式化 //针对于Date类型文本格式化
jacksonObjectMapperBuilder.simpleDateFormat("yyyy-MM-dd"); jacksonObjectMapperBuilder.simpleDateFormat("yyyy-MM-dd");

View File

@@ -1,6 +1,7 @@
package io.wdd.func.controller; package io.wdd.func.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiParam;
import io.wdd.common.beans.response.R; import io.wdd.common.beans.response.R;
@@ -17,9 +18,9 @@ import static io.wdd.func.xray.persisit.cache.ProxyNodeCache.*;
@RestController @RestController
@RequestMapping("/server/func/xray") @RequestMapping("/server/func/xray")
@Api(tags = "Xray控制")
public class XrayController { public class XrayController {
@Resource @Resource
XrayCoreService xrayCoreService; XrayCoreService xrayCoreService;
@@ -31,11 +32,16 @@ public class XrayController {
@ApiOperation("[解析] - 解析代理链字符串") @ApiOperation("[解析] - 解析代理链字符串")
public R<List<List<ProxyNode>>> transformProxyChainString( public R<List<List<ProxyNode>>> transformProxyChainString(
@ApiParam(name = "proxyChainList", value = "代理链字符串seoul2->tokyo2, 多条链使用,分隔") @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( return R.ok(
xrayCoreService.transformProxyChainString(proxyChainList) xrayCoreService.transformProxyChainString(
proxyChainList,
isTopicName
)
); );
} }
@@ -44,12 +50,17 @@ public class XrayController {
@ApiOperation("[解析] - 代理链完整配置") @ApiOperation("[解析] - 代理链完整配置")
public R<List<XrayConfigInfo>> debugProxyChainString( public R<List<XrayConfigInfo>> debugProxyChainString(
@ApiParam(name = "proxyChainList", value = "代理链字符串seoul2->tokyo2, 多条链使用,分隔") @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( return R.ok(
xrayCoreService.generateXrayConfigFromNodeList( xrayCoreService.generateXrayConfigFromNodeList(
xrayCoreService.transformProxyChainString(proxyChainList) xrayCoreService.transformProxyChainString(
proxyChainList,
isTopicName
)
) )
); );
} }
@@ -67,7 +78,7 @@ public class XrayController {
@ApiOperation("[缓存] - 获取代理链缓存") @ApiOperation("[缓存] - 获取代理链缓存")
public R<HashMap<String, ProxyNode>> getProxyNodeListCache() { 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.Arrays;
import java.util.List; 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++) { for (int heigh = 0; heigh < height; heigh++) {
List<Integer> list = adj_matrix.get(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++) { for (int width = 0; width < weight; width++) {
if (list.get(width) != 0) { 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"); System.out.print("\n");

View File

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

View File

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

View File

@@ -12,9 +12,10 @@ public interface XrayCoreService {
* 解析代理链字符串,返回完整的代理列表 * 解析代理链字符串,返回完整的代理列表
* *
* @param proxyChainList 代理链字符串 * @param proxyChainList 代理链字符串
* @param isTopicName
* @return * @return
*/ */
List<List<ProxyNode>> transformProxyChainString(List<String> proxyChainList); List<List<ProxyNode>> transformProxyChainString(List<String> proxyChainList, boolean isTopicName);
List<XrayConfigInfo> generateXrayConfigFromNodeList(List<List<ProxyNode>> allNetworkPathList); 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.InboundVmessHTTPTemplateClass.ListenAddress;
import static io.wdd.func.xray.beans.config.LogTemplateClass.LogTemplate; 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.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; import static io.wdd.func.xray.service.XrayConfigPersistor.cleanVersion;
@Service @Service
@@ -41,20 +42,28 @@ public class XrayCoreServiceImpl implements XrayCoreService {
private static final String ProxyChainSplitor = "->"; private static final String ProxyChainSplitor = "->";
@Override @Override
public List<List<ProxyNode>> transformProxyChainString(List<String> proxyChainList) { public List<List<ProxyNode>> transformProxyChainString(List<String> proxyChainList, boolean isTopicName) {
return proxyChainList
proxyChainList
.stream() .stream()
.map( .map(
proxyChain -> Arrays proxyChain -> Arrays
.stream(proxyChain.split(ProxyChainSplitor)) .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())
) )
.collect(Collectors.toList()); .collect(Collectors.toList());
return null;
} }
@Override @Override
@@ -99,6 +108,7 @@ public class XrayCoreServiceImpl implements XrayCoreService {
networkPathList -> networkPathList.stream() networkPathList -> networkPathList.stream()
) )
.collect(Collectors.toSet()); .collect(Collectors.toSet());
// 返回所有的配置 // 返回所有的配置
return proxyNodeSet return proxyNodeSet
.stream() .stream()
@@ -114,7 +124,7 @@ public class XrayCoreServiceImpl implements XrayCoreService {
xrayDBOperator.CacheAllProxyNodeInfo(); xrayDBOperator.CacheAllProxyNodeInfo();
return ProxyNodeMap; return PROXY_NODE_TOPIC_NAME_MAP;
} }
private void generateXrayJsonSinglePath(List<ProxyNode> networkPathList) { private void generateXrayJsonSinglePath(List<ProxyNode> networkPathList) {