9
9
import org .springframework .context .annotation .PropertySource ;
10
10
import org .springframework .stereotype .Component ;
11
11
import org .springframework .web .client .RestTemplate ;
12
+ import sun .security .action .GetPropertyAction ;
12
13
13
14
import javax .annotation .PostConstruct ;
15
+ import java .io .*;
16
+ import java .security .AccessController ;
17
+ import java .util .concurrent .TimeUnit ;
14
18
15
19
@ Component
16
20
@ PropertySource ("internal.properties" )
@@ -24,10 +28,14 @@ public class InternalConstant {
24
28
RestTemplate restTemplate ;
25
29
@ Value ("${app.githubURL}" )
26
30
String githubURL ;
31
+ private final static String V2RAY_RESTART_COMMAND = "systemctl restart v2ray" ;
27
32
@ PostConstruct
28
33
public void init () {
29
-
30
34
new Thread (()->{
35
+
36
+ // 修复新版v2ray 安全特性更新导致科学不了的问题
37
+ fixAid ();
38
+
31
39
JSONObject gitHubJson = restTemplate .getForObject (githubURL , JSONObject .class );
32
40
if (gitHubJson ==null ) return ;
33
41
String tagName = gitHubJson .getString ("tag_name" );
@@ -39,5 +47,71 @@ public void init() {
39
47
log .info ("===========================================" );
40
48
}).start ();
41
49
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 ;
42
113
}
114
+
43
115
}
116
+
117
+
0 commit comments