Skip to content

Commit 6985328

Browse files
committed
Optimize and improve code
1 parent 41c406b commit 6985328

File tree

3 files changed

+109
-123
lines changed

3 files changed

+109
-123
lines changed

v2rayN/ServiceLib/Handler/AppHandler.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ public sealed class AppHandler
99
private int? _statePort;
1010
private int? _statePort2;
1111
private Job? _processJob;
12-
private bool? _isAdministrator;
1312
public static AppHandler Instance => _instance.Value;
1413
public Config Config => _config;
1514

v2rayN/ServiceLib/Handler/CoreAdminHandler.cs

Lines changed: 51 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public class CoreAdminHandler
1010
public static CoreAdminHandler Instance => _instance.Value;
1111
private Config _config;
1212
private Action<bool, string>? _updateFunc;
13-
private const string _tag = "CoreAdminHandler";
1413
private int _linuxSudoPid = -1;
1514

1615
public async Task Init(Config config, Action<bool, string> updateFunc)
@@ -30,69 +29,60 @@ private void UpdateFunc(bool notify, string msg)
3029

3130
public async Task<Process?> RunProcessAsLinuxSudo(string fileName, CoreInfo coreInfo, string configPath)
3231
{
33-
try
34-
{
35-
var cmdLine = $"{fileName.AppendQuotes()} {string.Format(coreInfo.Arguments, Utils.GetBinConfigPath(configPath).AppendQuotes())}";
36-
var shFilePath = await CreateLinuxShellFile(cmdLine, "run_as_sudo.sh");
32+
var cmdLine = $"{fileName.AppendQuotes()} {string.Format(coreInfo.Arguments, Utils.GetBinConfigPath(configPath).AppendQuotes())}";
33+
var shFilePath = await CreateLinuxShellFile(cmdLine, "run_as_sudo.sh");
3734

38-
Process proc = new()
39-
{
40-
StartInfo = new()
41-
{
42-
FileName = shFilePath,
43-
Arguments = "",
44-
WorkingDirectory = Utils.GetBinConfigPath(),
45-
UseShellExecute = false,
46-
RedirectStandardInput = true,
47-
RedirectStandardOutput = true,
48-
RedirectStandardError = true,
49-
CreateNoWindow = true,
50-
StandardInputEncoding = Encoding.UTF8,
51-
StandardOutputEncoding = Encoding.UTF8,
52-
StandardErrorEncoding = Encoding.UTF8,
53-
}
54-
};
55-
56-
proc.OutputDataReceived += (sender, e) =>
35+
Process proc = new()
36+
{
37+
StartInfo = new()
5738
{
58-
if (e.Data.IsNotEmpty())
59-
{
60-
UpdateFunc(false, e.Data + Environment.NewLine);
61-
}
62-
};
63-
proc.ErrorDataReceived += (sender, e) =>
39+
FileName = shFilePath,
40+
Arguments = "",
41+
WorkingDirectory = Utils.GetBinConfigPath(),
42+
UseShellExecute = false,
43+
RedirectStandardInput = true,
44+
RedirectStandardOutput = true,
45+
RedirectStandardError = true,
46+
CreateNoWindow = true,
47+
StandardInputEncoding = Encoding.UTF8,
48+
StandardOutputEncoding = Encoding.UTF8,
49+
StandardErrorEncoding = Encoding.UTF8,
50+
}
51+
};
52+
53+
proc.OutputDataReceived += (sender, e) =>
54+
{
55+
if (e.Data.IsNotEmpty())
6456
{
65-
if (e.Data.IsNotEmpty())
66-
{
67-
UpdateFunc(false, e.Data + Environment.NewLine);
68-
}
69-
};
70-
71-
proc.Start();
72-
proc.BeginOutputReadLine();
73-
proc.BeginErrorReadLine();
74-
75-
await Task.Delay(10);
76-
await proc.StandardInput.WriteLineAsync();
77-
await Task.Delay(10);
78-
await proc.StandardInput.WriteLineAsync(AppHandler.Instance.LinuxSudoPwd);
79-
80-
await Task.Delay(100);
81-
if (proc is null or { HasExited: true })
57+
UpdateFunc(false, e.Data + Environment.NewLine);
58+
}
59+
};
60+
proc.ErrorDataReceived += (sender, e) =>
61+
{
62+
if (e.Data.IsNotEmpty())
8263
{
83-
throw new Exception(ResUI.FailedToRunCore);
64+
UpdateFunc(false, e.Data + Environment.NewLine);
8465
}
66+
};
8567

86-
_linuxSudoPid = proc.Id;
68+
proc.Start();
69+
proc.BeginOutputReadLine();
70+
proc.BeginErrorReadLine();
8771

88-
return proc;
89-
}
90-
catch (Exception ex)
72+
await Task.Delay(10);
73+
await proc.StandardInput.WriteLineAsync();
74+
await Task.Delay(10);
75+
await proc.StandardInput.WriteLineAsync(AppHandler.Instance.LinuxSudoPwd);
76+
77+
await Task.Delay(100);
78+
if (proc is null or { HasExited: true })
9179
{
92-
Logging.SaveLog(_tag, ex);
93-
UpdateFunc(false, ex.Message);
94-
return null;
80+
throw new Exception(ResUI.FailedToRunCore);
9581
}
82+
83+
_linuxSudoPid = proc.Id;
84+
85+
return proc;
9686
}
9787

9888
public async Task KillProcessAsLinuxSudo()
@@ -102,22 +92,14 @@ public async Task KillProcessAsLinuxSudo()
10292
return;
10393
}
10494

105-
try
106-
{
107-
var cmdLine = $"pkill -P {_linuxSudoPid} ; kill {_linuxSudoPid}";
108-
var shFilePath = await CreateLinuxShellFile(cmdLine, "kill_as_sudo.sh");
95+
var cmdLine = $"pkill -P {_linuxSudoPid} ; kill {_linuxSudoPid}";
96+
var shFilePath = await CreateLinuxShellFile(cmdLine, "kill_as_sudo.sh");
10997

110-
var result = await Cli.Wrap(shFilePath)
111-
.WithStandardInputPipe(PipeSource.FromString(AppHandler.Instance.LinuxSudoPwd))
112-
.ExecuteAsync();
98+
await Cli.Wrap(shFilePath)
99+
.WithStandardInputPipe(PipeSource.FromString(AppHandler.Instance.LinuxSudoPwd))
100+
.ExecuteAsync();
113101

114-
_linuxSudoPid = -1;
115-
}
116-
catch (Exception ex)
117-
{
118-
Logging.SaveLog(_tag, ex);
119-
UpdateFunc(false, ex.Message);
120-
}
102+
_linuxSudoPid = -1;
121103
}
122104

123105
private async Task<string> CreateLinuxShellFile(string cmdLine, string fileName)

v2rayN/ServiceLib/Handler/CoreHandler.cs

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -238,73 +238,78 @@ private void UpdateFunc(bool notify, string msg)
238238
return null;
239239
}
240240

241-
if (mayNeedSudo
242-
&& _config.TunModeItem.EnableTun
243-
&& coreInfo.CoreType == ECoreType.sing_box
244-
&& Utils.IsNonWindows())
241+
try
242+
{
243+
if (mayNeedSudo
244+
&& _config.TunModeItem.EnableTun
245+
&& coreInfo.CoreType == ECoreType.sing_box
246+
&& Utils.IsNonWindows())
247+
{
248+
_linuxSudo = true;
249+
await CoreAdminHandler.Instance.Init(_config, _updateFunc);
250+
return await CoreAdminHandler.Instance.RunProcessAsLinuxSudo(fileName, coreInfo, configPath);
251+
}
252+
253+
return await RunProcessNormal(fileName, coreInfo, configPath, displayLog);
254+
}
255+
catch (Exception ex)
245256
{
246-
_linuxSudo = true;
247-
await CoreAdminHandler.Instance.Init(_config, _updateFunc);
248-
return await CoreAdminHandler.Instance.RunProcessAsLinuxSudo(fileName, coreInfo, configPath);
257+
Logging.SaveLog(_tag, ex);
258+
UpdateFunc(mayNeedSudo, ex.Message);
259+
return null;
249260
}
261+
}
250262

251-
try
263+
private async Task<Process?> RunProcessNormal(string fileName, CoreInfo? coreInfo, string configPath, bool displayLog)
264+
{
265+
Process proc = new()
266+
{
267+
StartInfo = new()
268+
{
269+
FileName = fileName,
270+
Arguments = string.Format(coreInfo.Arguments, coreInfo.AbsolutePath ? Utils.GetBinConfigPath(configPath).AppendQuotes() : configPath),
271+
WorkingDirectory = Utils.GetBinConfigPath(),
272+
UseShellExecute = false,
273+
RedirectStandardOutput = displayLog,
274+
RedirectStandardError = displayLog,
275+
CreateNoWindow = true,
276+
StandardOutputEncoding = displayLog ? Encoding.UTF8 : null,
277+
StandardErrorEncoding = displayLog ? Encoding.UTF8 : null,
278+
}
279+
};
280+
281+
if (displayLog)
252282
{
253-
Process proc = new()
283+
proc.OutputDataReceived += (sender, e) =>
254284
{
255-
StartInfo = new()
285+
if (e.Data.IsNotEmpty())
256286
{
257-
FileName = fileName,
258-
Arguments = string.Format(coreInfo.Arguments, coreInfo.AbsolutePath ? Utils.GetBinConfigPath(configPath).AppendQuotes() : configPath),
259-
WorkingDirectory = Utils.GetBinConfigPath(),
260-
UseShellExecute = false,
261-
RedirectStandardOutput = displayLog,
262-
RedirectStandardError = displayLog,
263-
CreateNoWindow = true,
264-
StandardOutputEncoding = displayLog ? Encoding.UTF8 : null,
265-
StandardErrorEncoding = displayLog ? Encoding.UTF8 : null,
287+
UpdateFunc(false, e.Data + Environment.NewLine);
266288
}
267289
};
268-
269-
if (displayLog)
290+
proc.ErrorDataReceived += (sender, e) =>
270291
{
271-
proc.OutputDataReceived += (sender, e) =>
292+
if (e.Data.IsNotEmpty())
272293
{
273-
if (e.Data.IsNotEmpty())
274-
{
275-
UpdateFunc(false, e.Data + Environment.NewLine);
276-
}
277-
};
278-
proc.ErrorDataReceived += (sender, e) =>
279-
{
280-
if (e.Data.IsNotEmpty())
281-
{
282-
UpdateFunc(false, e.Data + Environment.NewLine);
283-
}
284-
};
285-
}
286-
proc.Start();
287-
288-
if (displayLog)
289-
{
290-
proc.BeginOutputReadLine();
291-
proc.BeginErrorReadLine();
292-
}
294+
UpdateFunc(false, e.Data + Environment.NewLine);
295+
}
296+
};
297+
}
298+
proc.Start();
293299

294-
await Task.Delay(500);
295-
AppHandler.Instance.AddProcess(proc.Handle);
296-
if (proc is null or { HasExited: true })
297-
{
298-
throw new Exception(ResUI.FailedToRunCore);
299-
}
300-
return proc;
300+
if (displayLog)
301+
{
302+
proc.BeginOutputReadLine();
303+
proc.BeginErrorReadLine();
301304
}
302-
catch (Exception ex)
305+
306+
await Task.Delay(100);
307+
AppHandler.Instance.AddProcess(proc.Handle);
308+
if (proc is null or { HasExited: true })
303309
{
304-
Logging.SaveLog(_tag, ex);
305-
UpdateFunc(mayNeedSudo, ex.Message);
306-
return null;
310+
throw new Exception(ResUI.FailedToRunCore);
307311
}
312+
return proc;
308313
}
309314

310315
#endregion Process

0 commit comments

Comments
 (0)