diff --git a/.run/RunServerToRemote.run.xml b/.run/RunServerToRemote.run.xml new file mode 100644 index 0000000..62af215 --- /dev/null +++ b/.run/RunServerToRemote.run.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.run/SkipTest-Package.run.xml b/.run/SkipTest-Package.run.xml new file mode 100644 index 0000000..ebfa812 --- /dev/null +++ b/.run/SkipTest-Package.run.xml @@ -0,0 +1,31 @@ + + + + + + + + \ No newline at end of file diff --git a/server/Dockerfile-shanghai-remote-idea b/server/Dockerfile-shanghai-remote-idea new file mode 100644 index 0000000..271221e --- /dev/null +++ b/server/Dockerfile-shanghai-remote-idea @@ -0,0 +1,23 @@ + +# Base images that the image needs to depend on +FROM icederce/eclipse-temurin-11-jre-focal + +# Set environment variables +ENV TZ=Asia/Shanghai JAVA_OPTS="-Xms2048m -Xmx2048m -Dfile.encoding=utf-8 -Dspring.profiles.active=k3s -Dspring.cloud.nacos.config.group=k3s -Dspring.cloud.nacos.config.extension-configs[0].dataId=common-k3s.yaml -Dspring.cloud.nacos.config.extension-configs[0].group=k3s -Ddebug=true -Dlogging.level.io.wdd.server=debug" + +# Set time zone +RUN set -eux; \ + ln -snf /usr/share/zoneinfo/$TZ /etc/localtime; \ + echo $TZ > /etc/timezone \ + +# Create Folder +RUN mkdir -p /wdd + +# Define the work dir +WORKDIR /wdd + +# Copy the jar and rename it +COPY ./target/server-*.jar /wdd/server.jar + +# When the docker container starts, run the jar +ENTRYPOINT exec java ${JAVA_OPTS} -jar /wdd/server.jar diff --git a/server/src/main/java/io/wdd/func/xray/service/XrayConfigPersistor.java b/server/src/main/java/io/wdd/func/xray/service/XrayConfigPersistor.java index 0f9b976..3de7fe5 100644 --- a/server/src/main/java/io/wdd/func/xray/service/XrayConfigPersistor.java +++ b/server/src/main/java/io/wdd/func/xray/service/XrayConfigPersistor.java @@ -27,6 +27,7 @@ import java.util.concurrent.atomic.AtomicInteger; /** * 获取rerouces目录 https://blog.csdn.net/pengpengpeng85/article/details/84785575 *

+ *

* 写入文件的教程 https://cloud.tencent.com/developer/article/1895274 */ @Slf4j @@ -45,32 +46,51 @@ public class XrayConfigPersistor { .withLinefeed("\n") )); private static final String XrayResultPathPrefix = "xrayResult/"; - + /** + * XRAY端口的起始位置 + */ + public static final int XRAY_PORT_MAX = 20000; /** * 存储生成的Xray Config的临时文件目录 */ - private static File XrayResultPath; + private static final File XrayResultPath; + /** + * 一个乐观锁,用于标定一次生成过程是否需要进行旧文件的清除 + */ public static AtomicInteger cleanVersion = new AtomicInteger(0); + /** + * Xray的Port的池子, 一直递减 + * 当进行重置的时候,需要进行清空 + */ + public static AtomicInteger XRAY_PORT_POOL = new AtomicInteger(XRAY_PORT_MAX); static { ClassPathResource resource = new ClassPathResource(XrayResultPathPrefix); - if (resource == null) { - // SpringBoot 打了Jar包的形式 - File file = new File("/octopus-server/xray"); - if (!file.exists()) { - file.mkdir(); + + try { + if (resource + .getURL() + .getPath() + .startsWith("file:/wdd/server")) { + // SpringBoot 打了Jar包的形式 + File file = new File("/octopus-server/xray"); + if (!file.exists()) { + file.mkdirs(); + } + + XrayResultPath = file; + } else { + // 本地开发的模式 + try { + XrayResultPath = resource.getFile(); + } catch (IOException e) { + log.error("获取XrayConfig的临时文件目录失败!"); + throw new MyRuntimeException(e); + } } - XrayResultPath = file; - } else { - // 本地开发的模式 - try { - XrayResultPath = resource.getFile(); - - } catch (IOException e) { - log.error("获取XrayConfig的临时文件目录失败!"); - throw new MyRuntimeException(e); - } + } catch (IOException e) { + throw new RuntimeException(e); } } diff --git a/server/src/main/java/io/wdd/func/xray/service/XrayCoreServiceImpl.java b/server/src/main/java/io/wdd/func/xray/service/XrayCoreServiceImpl.java index 3e58be0..6d29651 100644 --- a/server/src/main/java/io/wdd/func/xray/service/XrayCoreServiceImpl.java +++ b/server/src/main/java/io/wdd/func/xray/service/XrayCoreServiceImpl.java @@ -30,7 +30,7 @@ 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.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.*; @Service @Slf4j @@ -98,7 +98,10 @@ public class XrayCoreServiceImpl implements XrayCoreService { .stream() .forEach( networkPathList -> { + int chainPort = XRAY_PORT_POOL.decrementAndGet(); + generateXrayJsonSinglePath( + chainPort, networkPathList, currentVersion ); @@ -127,12 +130,16 @@ public class XrayCoreServiceImpl implements XrayCoreService { @Override public HashMap manualUpdateProxyNodeListCache() { + // 重新手动获取所有的ProxyNode缓存信息 xrayDBOperator.CacheAllProxyNodeInfo(); + // 清空 XRAY_PORT_POOL + XRAY_PORT_POOL.set(XRAY_PORT_MAX); + return PROXY_NODE_TOPIC_NAME_MAP; } - private void generateXrayJsonSinglePath(List networkPathList, int currentVersion) { + private void generateXrayJsonSinglePath(int chainPort, List networkPathList, int currentVersion) { int pathLength = networkPathList.size(); @@ -153,10 +160,6 @@ public class XrayCoreServiceImpl implements XrayCoreService { .email(tag + "@octopus.io") .build(); - int port = 19999; - - - for (int pos = 0; pos < pathLength; pos++) { ProxyNode proxyNode = networkPathList.get(pos); @@ -165,7 +168,7 @@ public class XrayCoreServiceImpl implements XrayCoreService { XrayConfig xrayConfig = doBuildXrayConfig( tag, clientObject, - port, + chainPort, networkPathList, pos ); @@ -182,7 +185,7 @@ public class XrayCoreServiceImpl implements XrayCoreService { } - private XrayConfig doBuildXrayConfig(String tag, ClientObject clientObject, int port, List networkPathList, int pos) { + private XrayConfig doBuildXrayConfig(String tag, ClientObject clientObject, int chainPort, List networkPathList, int pos) { // 需要从现有的XrayConfig基础上进行生成 // 尝试从 缓存层获取 @@ -201,7 +204,7 @@ public class XrayCoreServiceImpl implements XrayCoreService { buildInbound( xrayConfig, clientObject, - port, + chainPort, tag ); @@ -213,7 +216,7 @@ public class XrayCoreServiceImpl implements XrayCoreService { pos, clientObject, tag, - port + chainPort ); // 设置 路由信息 @@ -262,7 +265,7 @@ public class XrayCoreServiceImpl implements XrayCoreService { ruleObjectList.add(ruleObject); } - private void buildOutbound(XrayConfig xrayConfig, List networkPathList, int pos, ClientObject clientObject, String tag, Integer port) { + private void buildOutbound(XrayConfig xrayConfig, List networkPathList, int pos, ClientObject clientObject, String tag, Integer chainPort) { // 从缓存中获取 List outboundObjectList = xrayConfig.getOutbounds(); @@ -313,7 +316,7 @@ public class XrayCoreServiceImpl implements XrayCoreService { .getPublicIPv4()); // 端口 - serverObject.setPort(port); + serverObject.setPort(chainPort); serverObject.setUsers( List.of(clientObject) @@ -322,7 +325,7 @@ public class XrayCoreServiceImpl implements XrayCoreService { outboundObjectList.add(vmessOutbound); } - private void buildInbound(XrayConfig xrayConfig, ClientObject clientObject, Integer port, String tag) { + private void buildInbound(XrayConfig xrayConfig, ClientObject clientObject, Integer chainPort, String tag) { // 从缓存中获取 List inboundObjectList = xrayConfig.getInbounds(); @@ -345,7 +348,7 @@ public class XrayCoreServiceImpl implements XrayCoreService { inboundObject.setTag(tag); // todo port怎么办 - inboundObject.setPort(port); + inboundObject.setPort(chainPort); // 设置 listen地址 inboundObject.setListen(ListenAddress);