Skip to content

Commit

Permalink
Improve the code
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Nov 11, 2024
1 parent 61e9101 commit a1c8bc0
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 43 deletions.
70 changes: 34 additions & 36 deletions v2rayN/ServiceLib/Handler/CoreHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public async Task CoreStopPid(int pid)

private string CoreFindExe(CoreInfo coreInfo)
{
string fileName = string.Empty;
var fileName = string.Empty;
foreach (var name in coreInfo.CoreExes)
{
var vName = Utils.GetBinPath(Utils.GetExeName(name), coreInfo.CoreType.ToString());
Expand All @@ -157,7 +157,7 @@ private string CoreFindExe(CoreInfo coreInfo)
}
if (Utils.IsNullOrEmpty(fileName))
{
string msg = string.Format(ResUI.NotFoundCore, Utils.GetBinPath("", coreInfo.CoreType.ToString()), string.Join(", ", coreInfo.CoreExes.ToArray()), coreInfo.Url);
var msg = string.Format(ResUI.NotFoundCore, Utils.GetBinPath("", coreInfo.CoreType.ToString()), string.Join(", ", coreInfo.CoreExes.ToArray()), coreInfo.Url);
Logging.SaveLog(msg);
ShowMsg(false, msg);
}
Expand All @@ -183,7 +183,7 @@ private async Task CoreStart(ProfileItem node)
var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(coreType);

var displayLog = node.ConfigType != EConfigType.Custom || node.DisplayLog;
var proc = await RunProcess(node, coreInfo, "", displayLog);
var proc = await RunProcess(coreInfo, Global.CoreConfigFileName, displayLog);
if (proc is null)
{
return;
Expand Down Expand Up @@ -220,12 +220,12 @@ private async Task CoreStart(ProfileItem node)
}
if (itemSocks != null)
{
string fileName2 = Utils.GetConfigPath(Global.CorePreConfigFileName);
var fileName2 = Utils.GetConfigPath(Global.CorePreConfigFileName);
var result = await CoreConfigHandler.GenerateClientConfig(itemSocks, fileName2);
if (result.Success)
{
var coreInfo2 = CoreInfoHandler.Instance.GetCoreInfo(preCoreType);
var proc2 = await RunProcess(node, coreInfo2, $" -c {Global.CorePreConfigFileName}", true);
var proc2 = await RunProcess(coreInfo2, Global.CorePreConfigFileName, true);
if (proc2 is not null)
{
_processPre = proc2;
Expand All @@ -243,7 +243,7 @@ private async Task<int> CoreStartSpeedtest(string configPath, ECoreType coreType
try
{
var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(coreType);
var proc = await RunProcess(new(), coreInfo, $" -c {Global.CoreSpeedtestConfigFileName}", true);
var proc = await RunProcess(coreInfo, Global.CoreSpeedtestConfigFileName, true);
if (proc is null)
{
return -1;
Expand All @@ -254,8 +254,7 @@ private async Task<int> CoreStartSpeedtest(string configPath, ECoreType coreType
catch (Exception ex)
{
Logging.SaveLog(ex.Message, ex);
string msg = ex.Message;
ShowMsg(false, msg);
ShowMsg(false, ex.Message);
return -1;
}
}
Expand All @@ -269,15 +268,15 @@ private void ShowMsg(bool notify, string msg)

#region Process

private async Task<Process?> RunProcess(ProfileItem node, CoreInfo coreInfo, string configPath, bool displayLog)
private async Task<Process?> RunProcess(CoreInfo coreInfo, string configPath, bool displayLog)
{
var fileName = CoreFindExe(coreInfo);
if (Utils.IsNullOrEmpty(fileName))
{
return null;
}
try
{
string fileName = CoreFindExe(coreInfo);
if (Utils.IsNullOrEmpty(fileName))
{
return null;
}
Process proc = new()
{
StartInfo = new()
Expand All @@ -299,23 +298,17 @@ private void ShowMsg(bool notify, string msg)
{
proc.OutputDataReceived += (sender, e) =>
{
if (Utils.IsNotEmpty(e.Data))
{
string msg = e.Data + Environment.NewLine;
ShowMsg(false, msg);
}
if (Utils.IsNullOrEmpty(e.Data)) return;
ShowMsg(false, e.Data + Environment.NewLine);
};
proc.ErrorDataReceived += (sender, e) =>
{
if (Utils.IsNotEmpty(e.Data))
{
string msg = e.Data + Environment.NewLine;
ShowMsg(false, msg);
if (Utils.IsNullOrEmpty(e.Data)) return;
ShowMsg(false, e.Data + Environment.NewLine);
if (!startUpSuccessful)
{
startUpErrorMessage.Append(msg);
}
if (!startUpSuccessful)
{
startUpErrorMessage.Append(e.Data + Environment.NewLine);
}
};
}
Expand All @@ -342,8 +335,7 @@ private void ShowMsg(bool notify, string msg)
catch (Exception ex)
{
Logging.SaveLog(ex.Message, ex);
string msg = ex.Message;
ShowMsg(true, msg);
ShowMsg(true, ex.Message);
return null;
}
}
Expand All @@ -354,20 +346,26 @@ private async Task KillProcess(Process? proc)
{
return;
}
var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(1));
try
{
await proc.WaitForExitAsync(timeout.Token);
}
catch (OperationCanceledException)
{
proc.Kill();
proc.WaitForExit(100);
if (!proc.HasExited)
}
if (!proc.HasExited)
{
try
{
await proc.WaitForExitAsync(timeout.Token);
}
catch (Exception)
{
proc.Kill();
proc.WaitForExit(100);
}
}
catch (Exception ex)
{
Logging.SaveLog(ex.Message, ex);
}
}

#endregion Process
Expand Down
4 changes: 2 additions & 2 deletions v2rayN/ServiceLib/Handler/CoreInfoHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private void InitCoreInfo()
{
CoreType = ECoreType.Xray,
CoreExes = new List<string> { "xray", "wxray" },
Arguments = "run {0}",
Arguments = "run -c {0}",
Url = Global.XrayCoreUrl,
ReleaseApiUrl = Global.XrayCoreUrl.Replace(Global.GithubUrl, Global.GithubApiUrl),
DownloadUrlWin64 = Global.XrayCoreUrl + "/download/{0}/Xray-windows-64.zip",
Expand Down Expand Up @@ -132,7 +132,7 @@ private void InitCoreInfo()
{
CoreType = ECoreType.sing_box,
CoreExes = new List<string> { "sing-box-client", "sing-box" },
Arguments = "run {0} --disable-color",
Arguments = "run -c {0} --disable-color",
Url = Global.SingboxCoreUrl,
RedirectInfo = true,
ReleaseApiUrl = Global.SingboxCoreUrl.Replace(Global.GithubUrl, Global.GithubApiUrl),
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,7 @@
<value>显示或隐藏主界面</value>
</data>
<data name="UpdateStandalonePackageTip" xml:space="preserve">
<value>您当前运行的是独立包,请手动下载 SelfContained.7z文件解压覆盖</value>
<value>您当前运行的是独立包,请手动下载 SelfContained.7z文件解压覆盖</value>
</data>
<data name="TbPreSocksPort4Sub" xml:space="preserve">
<value>自定义配置的Socks端口</value>
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@
<value>顯示或隱藏主介面</value>
</data>
<data name="UpdateStandalonePackageTip" xml:space="preserve">
<value>您目前運行的是獨立包,請手動下載 SelfContained.7z檔案解壓縮覆蓋</value>
<value>您目前運行的是獨立包,請手動下載 SelfContained.7z檔案解壓縮覆蓋</value>
</data>
<data name="TbPreSocksPort4Sub" xml:space="preserve">
<value>自訂設定的Socks端口</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private async Task SaveRulesAsync()
return;
}
//NoticeHandler.Instance.Enqueue(ResUI.OperationSuccess);
_updateView?.Invoke(EViewAction.CloseWindow, null);
await _updateView?.Invoke(EViewAction.CloseWindow, null);
}
}
}
4 changes: 2 additions & 2 deletions v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public RoutingRuleDetailsWindow(RulesItem rulesItem)
{
rulesItem.Protocol?.ForEach(it =>
{
clbProtocol.SelectedItems.Add(it);
clbProtocol?.SelectedItems?.Add(it);
});
rulesItem.InboundTag?.ForEach(it =>
{
clbInboundTag.SelectedItems.Add(it);
clbInboundTag?.SelectedItems?.Add(it);
});
}

Expand Down

0 comments on commit a1c8bc0

Please sign in to comment.