mirror of
https://github.com/2dust/v2rayN.git
synced 2025-05-13 19:58:06 +00:00
Optimize and improve code
This commit is contained in:
parent
41c406b84d
commit
6985328653
3 changed files with 114 additions and 128 deletions
|
@ -9,7 +9,6 @@ public sealed class AppHandler
|
|||
private int? _statePort;
|
||||
private int? _statePort2;
|
||||
private Job? _processJob;
|
||||
private bool? _isAdministrator;
|
||||
public static AppHandler Instance => _instance.Value;
|
||||
public Config Config => _config;
|
||||
|
||||
|
|
|
@ -10,7 +10,6 @@ public class CoreAdminHandler
|
|||
public static CoreAdminHandler Instance => _instance.Value;
|
||||
private Config _config;
|
||||
private Action<bool, string>? _updateFunc;
|
||||
private const string _tag = "CoreAdminHandler";
|
||||
private int _linuxSudoPid = -1;
|
||||
|
||||
public async Task Init(Config config, Action<bool, string> updateFunc)
|
||||
|
@ -29,8 +28,6 @@ public class CoreAdminHandler
|
|||
}
|
||||
|
||||
public async Task<Process?> RunProcessAsLinuxSudo(string fileName, CoreInfo coreInfo, string configPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var cmdLine = $"{fileName.AppendQuotes()} {string.Format(coreInfo.Arguments, Utils.GetBinConfigPath(configPath).AppendQuotes())}";
|
||||
var shFilePath = await CreateLinuxShellFile(cmdLine, "run_as_sudo.sh");
|
||||
|
@ -87,13 +84,6 @@ public class CoreAdminHandler
|
|||
|
||||
return proc;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logging.SaveLog(_tag, ex);
|
||||
UpdateFunc(false, ex.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task KillProcessAsLinuxSudo()
|
||||
{
|
||||
|
@ -102,23 +92,15 @@ public class CoreAdminHandler
|
|||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var cmdLine = $"pkill -P {_linuxSudoPid} ; kill {_linuxSudoPid}";
|
||||
var shFilePath = await CreateLinuxShellFile(cmdLine, "kill_as_sudo.sh");
|
||||
|
||||
var result = await Cli.Wrap(shFilePath)
|
||||
await Cli.Wrap(shFilePath)
|
||||
.WithStandardInputPipe(PipeSource.FromString(AppHandler.Instance.LinuxSudoPwd))
|
||||
.ExecuteAsync();
|
||||
|
||||
_linuxSudoPid = -1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logging.SaveLog(_tag, ex);
|
||||
UpdateFunc(false, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<string> CreateLinuxShellFile(string cmdLine, string fileName)
|
||||
{
|
||||
|
|
|
@ -238,6 +238,8 @@ public class CoreHandler
|
|||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (mayNeedSudo
|
||||
&& _config.TunModeItem.EnableTun
|
||||
&& coreInfo.CoreType == ECoreType.sing_box
|
||||
|
@ -248,7 +250,17 @@ public class CoreHandler
|
|||
return await CoreAdminHandler.Instance.RunProcessAsLinuxSudo(fileName, coreInfo, configPath);
|
||||
}
|
||||
|
||||
try
|
||||
return await RunProcessNormal(fileName, coreInfo, configPath, displayLog);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logging.SaveLog(_tag, ex);
|
||||
UpdateFunc(mayNeedSudo, ex.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<Process?> RunProcessNormal(string fileName, CoreInfo? coreInfo, string configPath, bool displayLog)
|
||||
{
|
||||
Process proc = new()
|
||||
{
|
||||
|
@ -291,7 +303,7 @@ public class CoreHandler
|
|||
proc.BeginErrorReadLine();
|
||||
}
|
||||
|
||||
await Task.Delay(500);
|
||||
await Task.Delay(100);
|
||||
AppHandler.Instance.AddProcess(proc.Handle);
|
||||
if (proc is null or { HasExited: true })
|
||||
{
|
||||
|
@ -299,13 +311,6 @@ public class CoreHandler
|
|||
}
|
||||
return proc;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logging.SaveLog(_tag, ex);
|
||||
UpdateFunc(mayNeedSudo, ex.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Process
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue