Skip to content

Commit 5732b84

Browse files
committed
Refactor KillProcess
1 parent c0d2750 commit 5732b84

File tree

4 files changed

+91
-103
lines changed

4 files changed

+91
-103
lines changed

v2rayN/ServiceLib/Common/ProcUtils.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,45 @@ public static void RebootAsAdmin(bool blAdmin = true)
6161
Logging.SaveLog(_tag, ex);
6262
}
6363
}
64+
65+
public static async Task ProcessKill(int pid)
66+
{
67+
try
68+
{
69+
await ProcessKill(Process.GetProcessById(pid), false);
70+
}
71+
catch (Exception ex)
72+
{
73+
Logging.SaveLog(_tag, ex);
74+
}
75+
}
76+
77+
public static async Task ProcessKill(Process? proc, bool review)
78+
{
79+
if (proc is null)
80+
{
81+
return;
82+
}
83+
84+
var fileName = review ? proc?.MainModule?.FileName : null;
85+
var processName = review ? proc?.ProcessName : null;
86+
87+
try { proc?.Kill(true); } catch (Exception ex) { Logging.SaveLog(_tag, ex); }
88+
try { proc?.Kill(); } catch (Exception ex) { Logging.SaveLog(_tag, ex); }
89+
try { proc?.Close(); } catch (Exception ex) { Logging.SaveLog(_tag, ex); }
90+
try { proc?.Dispose(); } catch (Exception ex) { Logging.SaveLog(_tag, ex); }
91+
92+
await Task.Delay(300);
93+
if (review && fileName != null)
94+
{
95+
var proc2 = Process.GetProcessesByName(processName)
96+
.FirstOrDefault(t => t.MainModule?.FileName == fileName);
97+
if (proc2 != null)
98+
{
99+
Logging.SaveLog($"{_tag}, KillProcess not completing the job");
100+
await ProcessKill(proc2, false);
101+
proc2 = null;
102+
}
103+
}
104+
}
64105
}

v2rayN/ServiceLib/Handler/CoreHandler.cs

Lines changed: 26 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,21 @@ public async Task LoadCore(ProfileItem? node)
5555
{
5656
if (node == null)
5757
{
58-
ShowMsg(false, ResUI.CheckServerSettings);
58+
UpdateFunc(false, ResUI.CheckServerSettings);
5959
return;
6060
}
6161

6262
var fileName = Utils.GetConfigPath(Global.CoreConfigFileName);
6363
var result = await CoreConfigHandler.GenerateClientConfig(node, fileName);
6464
if (result.Success != true)
6565
{
66-
ShowMsg(true, result.Msg);
66+
UpdateFunc(true, result.Msg);
6767
return;
6868
}
6969

70-
ShowMsg(true, $"{node.GetSummary()}");
71-
ShowMsg(false, $"{Utils.GetRuntimeInfo()}");
72-
ShowMsg(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
70+
UpdateFunc(true, $"{node.GetSummary()}");
71+
UpdateFunc(false, $"{Utils.GetRuntimeInfo()}");
72+
UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
7373
await CoreStop();
7474
await Task.Delay(100);
7575
await CoreStart(node);
@@ -81,15 +81,23 @@ public async Task<int> LoadCoreConfigSpeedtest(List<ServerTestItem> selecteds)
8181
var coreType = selecteds.Exists(t => t.ConfigType is EConfigType.Hysteria2 or EConfigType.TUIC or EConfigType.WireGuard) ? ECoreType.sing_box : ECoreType.Xray;
8282
var configPath = Utils.GetConfigPath(Global.CoreSpeedtestConfigFileName);
8383
var result = await CoreConfigHandler.GenerateClientSpeedtestConfig(_config, configPath, selecteds, coreType);
84-
ShowMsg(false, result.Msg);
84+
UpdateFunc(false, result.Msg);
8585
if (result.Success != true)
8686
{
8787
return -1;
8888
}
8989

90-
ShowMsg(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
91-
ShowMsg(false, configPath);
92-
return await CoreStartSpeedtest(configPath, coreType);
90+
UpdateFunc(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
91+
UpdateFunc(false, configPath);
92+
93+
var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(coreType);
94+
var proc = await RunProcess(coreInfo, Global.CoreSpeedtestConfigFileName, true, false);
95+
if (proc is null)
96+
{
97+
return -1;
98+
}
99+
100+
return proc.Id;
93101
}
94102

95103
public async Task CoreStop()
@@ -98,13 +106,13 @@ public async Task CoreStop()
98106
{
99107
if (_process != null)
100108
{
101-
await KillProcess(_process, true);
109+
await ProcUtils.ProcessKill(_process, true);
102110
_process = null;
103111
}
104112

105113
if (_processPre != null)
106114
{
107-
await KillProcess(_processPre, true);
115+
await ProcUtils.ProcessKill(_processPre, true);
108116
_processPre = null;
109117
}
110118

@@ -120,41 +128,8 @@ public async Task CoreStop()
120128
}
121129
}
122130

123-
public async Task CoreStopPid(int pid)
124-
{
125-
try
126-
{
127-
await KillProcess(Process.GetProcessById(pid), false);
128-
}
129-
catch (Exception ex)
130-
{
131-
Logging.SaveLog(_tag, ex);
132-
}
133-
}
134-
135131
#region Private
136132

137-
private string CoreFindExe(CoreInfo coreInfo)
138-
{
139-
var fileName = string.Empty;
140-
foreach (var name in coreInfo.CoreExes)
141-
{
142-
var vName = Utils.GetBinPath(Utils.GetExeName(name), coreInfo.CoreType.ToString());
143-
if (File.Exists(vName))
144-
{
145-
fileName = vName;
146-
break;
147-
}
148-
}
149-
if (Utils.IsNullOrEmpty(fileName))
150-
{
151-
var msg = string.Format(ResUI.NotFoundCore, Utils.GetBinPath("", coreInfo.CoreType.ToString()), string.Join(", ", coreInfo.CoreExes.ToArray()), coreInfo.Url);
152-
Logging.SaveLog(msg);
153-
ShowMsg(false, msg);
154-
}
155-
return fileName;
156-
}
157-
158133
private async Task CoreStart(ProfileItem node)
159134
{
160135
var coreType = _config.RunningCoreType = AppHandler.Instance.GetCoreType(node, node.ConfigType);
@@ -194,28 +169,7 @@ private async Task CoreStartPreService(ProfileItem node)
194169
}
195170
}
196171

197-
private async Task<int> CoreStartSpeedtest(string configPath, ECoreType coreType)
198-
{
199-
try
200-
{
201-
var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(coreType);
202-
var proc = await RunProcess(coreInfo, Global.CoreSpeedtestConfigFileName, true, false);
203-
if (proc is null)
204-
{
205-
return -1;
206-
}
207-
208-
return proc.Id;
209-
}
210-
catch (Exception ex)
211-
{
212-
Logging.SaveLog(_tag, ex);
213-
ShowMsg(false, ex.Message);
214-
return -1;
215-
}
216-
}
217-
218-
private void ShowMsg(bool notify, string msg)
172+
private void UpdateFunc(bool notify, string msg)
219173
{
220174
_updateFunc?.Invoke(notify, msg);
221175
}
@@ -233,11 +187,12 @@ private bool IsNeedSudo(ECoreType eCoreType)
233187

234188
#region Process
235189

236-
private async Task<Process?> RunProcess(CoreInfo coreInfo, string configPath, bool displayLog, bool mayNeedSudo)
190+
private async Task<Process?> RunProcess(CoreInfo? coreInfo, string configPath, bool displayLog, bool mayNeedSudo)
237191
{
238-
var fileName = CoreFindExe(coreInfo);
192+
var fileName = CoreInfoHandler.Instance.GetCoreExecFile(coreInfo, out var msg);
239193
if (Utils.IsNullOrEmpty(fileName))
240194
{
195+
UpdateFunc(false, msg);
241196
return null;
242197
}
243198

@@ -272,12 +227,12 @@ private bool IsNeedSudo(ECoreType eCoreType)
272227
proc.OutputDataReceived += (sender, e) =>
273228
{
274229
if (Utils.IsNullOrEmpty(e.Data)) return;
275-
ShowMsg(false, e.Data + Environment.NewLine);
230+
UpdateFunc(false, e.Data + Environment.NewLine);
276231
};
277232
proc.ErrorDataReceived += (sender, e) =>
278233
{
279234
if (Utils.IsNullOrEmpty(e.Data)) return;
280-
ShowMsg(false, e.Data + Environment.NewLine);
235+
UpdateFunc(false, e.Data + Environment.NewLine);
281236

282237
if (!startUpSuccessful)
283238
{
@@ -319,40 +274,11 @@ private bool IsNeedSudo(ECoreType eCoreType)
319274
catch (Exception ex)
320275
{
321276
Logging.SaveLog(_tag, ex);
322-
ShowMsg(true, ex.Message);
277+
UpdateFunc(true, ex.Message);
323278
return null;
324279
}
325280
}
326281

327-
private async Task KillProcess(Process? proc, bool review)
328-
{
329-
if (proc is null)
330-
{
331-
return;
332-
}
333-
334-
var fileName = proc?.MainModule?.FileName;
335-
var processName = proc?.ProcessName;
336-
337-
try { proc?.Kill(true); } catch (Exception ex) { Logging.SaveLog(_tag, ex); }
338-
try { proc?.Kill(); } catch (Exception ex) { Logging.SaveLog(_tag, ex); }
339-
try { proc?.Close(); } catch (Exception ex) { Logging.SaveLog(_tag, ex); }
340-
try { proc?.Dispose(); } catch (Exception ex) { Logging.SaveLog(_tag, ex); }
341-
342-
await Task.Delay(500);
343-
if (review)
344-
{
345-
var proc2 = Process.GetProcessesByName(processName)
346-
.FirstOrDefault(t => t.MainModule?.FileName == fileName);
347-
if (proc2 != null)
348-
{
349-
Logging.SaveLog($"{_tag}, KillProcess not completing the job");
350-
await KillProcess(proc2, false);
351-
proc2 = null;
352-
}
353-
}
354-
}
355-
356282
#endregion Process
357283

358284
#region Linux

v2rayN/ServiceLib/Handler/CoreInfoHandler.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@ public List<CoreInfo> GetCoreInfo()
2929
return _coreInfo ?? [];
3030
}
3131

32+
public string GetCoreExecFile(CoreInfo? coreInfo, out string msg)
33+
{
34+
var fileName = string.Empty;
35+
msg = string.Empty;
36+
foreach (var name in coreInfo?.CoreExes)
37+
{
38+
var vName = Utils.GetBinPath(Utils.GetExeName(name), coreInfo.CoreType.ToString());
39+
if (File.Exists(vName))
40+
{
41+
fileName = vName;
42+
break;
43+
}
44+
}
45+
if (fileName.IsNullOrEmpty())
46+
{
47+
msg = string.Format(ResUI.NotFoundCore, Utils.GetBinPath("", coreInfo.CoreType.ToString()), string.Join(", ", coreInfo.CoreExes.ToArray()), coreInfo.Url);
48+
Logging.SaveLog(msg);
49+
}
50+
return fileName;
51+
}
52+
3253
private void InitCoreInfo()
3354
{
3455
_coreInfo = [];

v2rayN/ServiceLib/Services/SpeedtestService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ private async Task RunRealPingAsync(List<ServerTestItem> selecteds)
216216
{
217217
if (pid > 0)
218218
{
219-
await CoreHandler.Instance.CoreStopPid(pid);
219+
await ProcUtils.ProcessKill(pid);
220220
}
221221
await ProfileExHandler.Instance.SaveTo();
222222
}
@@ -278,7 +278,7 @@ await downloadHandle.DownloadDataAsync(url, webProxy, timeout, (success, msg) =>
278278

279279
if (pid > 0)
280280
{
281-
await CoreHandler.Instance.CoreStopPid(pid);
281+
await ProcUtils.ProcessKill(pid);
282282
}
283283
await ProfileExHandler.Instance.SaveTo();
284284
}
@@ -342,7 +342,7 @@ private async Task RunSpeedTestMulti(List<ServerTestItem> selecteds)
342342

343343
if (pid > 0)
344344
{
345-
await CoreHandler.Instance.CoreStopPid(pid);
345+
await ProcUtils.ProcessKill(pid);
346346
}
347347
await ProfileExHandler.Instance.SaveTo();
348348
}

0 commit comments

Comments
 (0)