Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit 0f360d9

Browse files
feat: 适配新版v2ray
1 parent 79ffae7 commit 0f360d9

File tree

4 files changed

+95
-5
lines changed

4 files changed

+95
-5
lines changed

proxy/src/main/java/com/jhl/V2rayProxyApplication.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
package com.jhl;
22

33

4+
import lombok.extern.slf4j.Slf4j;
45
import org.springframework.boot.SpringApplication;
56
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import sun.security.action.GetPropertyAction;
68

9+
import java.io.*;
10+
import java.security.AccessController;
711
import java.util.TimeZone;
12+
import java.util.concurrent.TimeUnit;
813

914
@SpringBootApplication
10-
15+
@Slf4j
1116
public class V2rayProxyApplication {
17+
private final static String V2RAY_RESTART_COMMAND = "systemctl restart v2ray";
1218

1319
public static void main(String[] args) {
1420
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
21+
try {
22+
Runtime.getRuntime().exec(V2RAY_RESTART_COMMAND).waitFor(5,TimeUnit.SECONDS);
23+
log.info("执行重启v2ray:{}", V2RAY_RESTART_COMMAND);
24+
} catch (Exception e) {
25+
log.error(" 重启v2ray失败,如果科学不了,一般都是启动先后次序导致,请手动重启v2ray", e);
26+
}
27+
1528
//需要接受args,如果不加载不了自定义配置
16-
SpringApplication.run(V2rayProxyApplication.class,args);
29+
SpringApplication.run(V2rayProxyApplication.class, args);
1730
}
1831

1932

proxy/src/main/java/com/jhl/common/constant/InternalConstant.java

+75-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
import org.springframework.context.annotation.PropertySource;
1010
import org.springframework.stereotype.Component;
1111
import org.springframework.web.client.RestTemplate;
12+
import sun.security.action.GetPropertyAction;
1213

1314
import javax.annotation.PostConstruct;
15+
import java.io.*;
16+
import java.security.AccessController;
17+
import java.util.concurrent.TimeUnit;
1418

1519
@Component
1620
@PropertySource("internal.properties")
@@ -24,10 +28,14 @@ public class InternalConstant {
2428
RestTemplate restTemplate;
2529
@Value("${app.githubURL}")
2630
String githubURL;
31+
private final static String V2RAY_RESTART_COMMAND = "systemctl restart v2ray";
2732
@PostConstruct
2833
public void init() {
29-
3034
new Thread(()->{
35+
36+
// 修复新版v2ray 安全特性更新导致科学不了的问题
37+
fixAid();
38+
3139
JSONObject gitHubJson = restTemplate.getForObject(githubURL, JSONObject.class);
3240
if (gitHubJson ==null) return;
3341
String tagName = gitHubJson.getString("tag_name");
@@ -39,5 +47,71 @@ public void init() {
3947
log.info("===========================================");
4048
}).start();
4149

50+
51+
}
52+
53+
private void fixAid() {
54+
String aeadEnv = "Environment=\"V2RAY_VMESS_AEAD_FORCED=false\"".trim();
55+
File file = new File("/etc/systemd/system/v2ray.service".trim());
56+
if (file.exists() && file.canRead() && file.canWrite()) {
57+
boolean existAeadEnv = isExistAeadEnv(aeadEnv, file);
58+
if (!existAeadEnv) {
59+
writeEev(aeadEnv, file);
60+
log.info("写入v2ray兼容旧版环境变量:{}",aeadEnv);
61+
try {
62+
Runtime.getRuntime().exec("systemctl daemon-reload").waitFor(2, TimeUnit.SECONDS);
63+
Runtime.getRuntime().exec(V2RAY_RESTART_COMMAND).waitFor(2, TimeUnit.SECONDS);
64+
log.info("执行重启v2ray:{}",V2RAY_RESTART_COMMAND);
65+
} catch (Exception e) {
66+
log.error("兼容旧版v2ray重启失败,请手动重启v2ray" ,e);
67+
}
68+
}
69+
}
70+
71+
}
72+
73+
private void writeEev(String AeadEnv, File file) {
74+
75+
String lineSeparator = AccessController.doPrivileged(
76+
new GetPropertyAction("line.separator"));
77+
StringBuilder sb = new StringBuilder();
78+
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
79+
String tempString;
80+
// 判断是否已经存在非强制实用新版AEAD加密的环境变量
81+
while ((tempString = reader.readLine()) != null) {
82+
sb.append(tempString).append(lineSeparator);
83+
if ("[Service]".trim().equalsIgnoreCase(tempString)){
84+
sb.append(AeadEnv).append(lineSeparator);
85+
}
86+
}
87+
} catch (IOException e) {
88+
log.error("读取文件失败", e);
89+
}
90+
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, false))) {
91+
writer.write(sb.toString());
92+
} catch (IOException e) {
93+
log.error("写入环境兼容旧版环境变量失败", e);
94+
}
95+
96+
}
97+
98+
private boolean isExistAeadEnv(String AeadEnv, File file) {
99+
boolean existAeadEnv = false;
100+
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
101+
String tempString;
102+
// 判断是否已经存在非强制实用新版AEAD加密的环境变量
103+
while ((tempString = reader.readLine()) != null) {
104+
if (AeadEnv.equalsIgnoreCase(tempString)) {
105+
existAeadEnv = true;
106+
break;
107+
}
108+
}
109+
} catch (IOException e) {
110+
log.error("读取环境兼容旧版环境变量失败", e);
111+
}
112+
return existAeadEnv;
42113
}
114+
43115
}
116+
117+

vpn-admin/src/main/java/com/jhl/admin/VO/ServerVO.java

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public class ServerVO extends BaseEntityVO implements Serializable {
5252
//ws路径
5353
private String wsPath ="/ws/%s/";
5454

55+
private Integer alterId=64;
56+
5557

5658
}
5759

vpn-admin/src/main/java/com/jhl/admin/model/Server.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import lombok.Builder;
55
import lombok.Data;
66
import lombok.NoArgsConstructor;
7+
import org.apache.commons.lang3.StringUtils;
78

89
import javax.persistence.Column;
910
import javax.persistence.Entity;
@@ -49,15 +50,15 @@ public class Server extends BaseEntity implements Serializable {
4950
/**
5051
* 服务器等级
5152
*/
52-
@Column( columnDefinition="smallint default 0")
53+
@Column( columnDefinition="smallint default 0" )
5354
private Short level;
5455

5556

5657
//ws路径
5758
private String wsPath ="/ws/%s/";
5859
//默认0
60+
@Column(columnDefinition="smallint default 64" )
5961
private Integer alterId=0;
6062

61-
6263
}
6364

0 commit comments

Comments
 (0)