diff --git a/v2rayN/v2rayN/App.xaml.cs b/v2rayN/v2rayN/App.xaml.cs index caa6570c..70147738 100644 --- a/v2rayN/v2rayN/App.xaml.cs +++ b/v2rayN/v2rayN/App.xaml.cs @@ -2,7 +2,6 @@ using System.Windows.Threading; using v2rayN.Handler; using v2rayN.Mode; -using v2rayN.Tool; namespace v2rayN { @@ -45,7 +44,7 @@ namespace v2rayN Logging.Setup(); Init(); Logging.LoggingEnabled(_config.guiItem.enableLog); - Utils.SaveLog($"v2rayN start up | {Utils.GetVersion()} | {Utils.GetExePath()}"); + Logging.SaveLog($"v2rayN start up | {Utils.GetVersion()} | {Utils.GetExePath()}"); Logging.ClearLogs(); Thread.CurrentThread.CurrentUICulture = new(_config.uiItem.currentLanguage); @@ -70,7 +69,7 @@ namespace v2rayN private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) { - Utils.SaveLog("App_DispatcherUnhandledException", e.Exception); + Logging.SaveLog("App_DispatcherUnhandledException", e.Exception); e.Handled = true; } @@ -78,13 +77,13 @@ namespace v2rayN { if (e.ExceptionObject != null) { - Utils.SaveLog("CurrentDomain_UnhandledException", (Exception)e.ExceptionObject!); + Logging.SaveLog("CurrentDomain_UnhandledException", (Exception)e.ExceptionObject!); } } private void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e) { - Utils.SaveLog("TaskScheduler_UnobservedTaskException", e.Exception); + Logging.SaveLog("TaskScheduler_UnobservedTaskException", e.Exception); } } } \ No newline at end of file diff --git a/v2rayN/v2rayN/Base/HttpClientHelper.cs b/v2rayN/v2rayN/Base/HttpClientHelper.cs index 72d9d3c6..8137c873 100644 --- a/v2rayN/v2rayN/Base/HttpClientHelper.cs +++ b/v2rayN/v2rayN/Base/HttpClientHelper.cs @@ -35,7 +35,7 @@ namespace v2rayN.Base public async Task PutAsync(string url, Dictionary headers) { - var jsonContent = Utils.ToJson(headers); + var jsonContent = JsonUtils.ToJson(headers); var content = new StringContent(jsonContent, Encoding.UTF8, MediaTypeNames.Application.Json); var result = await httpClient.PutAsync(url, content); diff --git a/v2rayN/v2rayN/Handler/ConfigHandler.cs b/v2rayN/v2rayN/Handler/ConfigHandler.cs index 70f7993d..687a45e2 100644 --- a/v2rayN/v2rayN/Handler/ConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/ConfigHandler.cs @@ -29,13 +29,13 @@ namespace v2rayN.Handler if (!Utils.IsNullOrEmpty(result)) { //转成Json - config = Utils.FromJson(result); + config = JsonUtils.FromJson(result); } else { if (File.Exists(Utils.GetConfigPath(configRes))) { - Utils.SaveLog("LoadConfig Exception"); + Logging.SaveLog("LoadConfig Exception"); return -1; } } @@ -234,7 +234,7 @@ namespace v2rayN.Handler //save temp file var resPath = Utils.GetConfigPath(configRes); var tempPath = $"{resPath}_temp"; - if (Utils.ToJsonFile(config, tempPath) != 0) + if (JsonUtils.ToJsonFile(config, tempPath) != 0) { return; } @@ -248,7 +248,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog("ToJsonFile", ex); + Logging.SaveLog("ToJsonFile", ex); } } } @@ -261,13 +261,13 @@ namespace v2rayN.Handler return -1; } - var configOld = Utils.FromJson(result); + var configOld = JsonUtils.FromJson(result); if (configOld == null) { return -1; } - var subItem = Utils.FromJson>(Utils.ToJson(configOld.subItem)); + var subItem = JsonUtils.FromJson>(JsonUtils.ToJson(configOld.subItem)); foreach (var it in subItem) { if (Utils.IsNullOrEmpty(it.id)) @@ -277,7 +277,7 @@ namespace v2rayN.Handler SqliteHelper.Instance.Replace(it); } - var profileItems = Utils.FromJson>(Utils.ToJson(configOld.vmess)); + var profileItems = JsonUtils.FromJson>(JsonUtils.ToJson(configOld.vmess)); foreach (var it in profileItems) { if (Utils.IsNullOrEmpty(it.indexId)) @@ -293,13 +293,13 @@ namespace v2rayN.Handler { continue; } - var routing = Utils.FromJson(Utils.ToJson(it)); + var routing = JsonUtils.FromJson(JsonUtils.ToJson(it)); foreach (var it2 in it.rules) { it2.id = Utils.GetGUID(false); } routing.ruleNum = it.rules.Count; - routing.ruleSet = Utils.ToJson(it.rules, false); + routing.ruleSet = JsonUtils.ToJson(it.rules, false); if (Utils.IsNullOrEmpty(routing.id)) { @@ -308,7 +308,7 @@ namespace v2rayN.Handler SqliteHelper.Instance.Replace(routing); } - config = Utils.FromJson(Utils.ToJson(configOld)); + config = JsonUtils.FromJson(JsonUtils.ToJson(configOld)); if (config.coreBasicItem == null) { @@ -424,7 +424,7 @@ namespace v2rayN.Handler continue; } - ProfileItem profileItem = Utils.DeepCopy(item); + ProfileItem profileItem = JsonUtils.DeepCopy(item); profileItem.indexId = string.Empty; profileItem.remarks = $"{item.remarks}-clone"; @@ -595,7 +595,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); return -1; } @@ -1007,7 +1007,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog("Remove Item", ex); + Logging.SaveLog("Remove Item", ex); } return 0; @@ -1163,7 +1163,7 @@ namespace v2rayN.Handler ProfileItem profileItem = new(); //Is v2ray configuration - V2rayConfig? v2rayConfig = Utils.FromJson(clipboardData); + V2rayConfig? v2rayConfig = JsonUtils.FromJson(clipboardData); if (v2rayConfig?.inbounds?.Count > 0 && v2rayConfig.outbounds?.Count > 0) { @@ -1254,10 +1254,10 @@ namespace v2rayN.Handler } //SsSIP008 - var lstSsServer = Utils.FromJson>(clipboardData); + var lstSsServer = JsonUtils.FromJson>(clipboardData); if (lstSsServer?.Count <= 0) { - var ssSIP008 = Utils.FromJson(clipboardData); + var ssSIP008 = JsonUtils.FromJson(clipboardData); if (ssSIP008?.servers?.Count > 0) { lstSsServer = ssSIP008.servers; @@ -1469,7 +1469,7 @@ namespace v2rayN.Handler return -1; } - var lstRules = Utils.FromJson>(clipboardData); + var lstRules = JsonUtils.FromJson>(clipboardData); if (lstRules == null) { return -1; @@ -1480,7 +1480,7 @@ namespace v2rayN.Handler item.id = Utils.GetGUID(false); } routingItem.ruleNum = lstRules.Count; - routingItem.ruleSet = Utils.ToJson(lstRules, false); + routingItem.ruleSet = JsonUtils.ToJson(lstRules, false); if (Utils.IsNullOrEmpty(routingItem.id)) { @@ -1519,7 +1519,7 @@ namespace v2rayN.Handler { return 0; } - var item = Utils.DeepCopy(rules[index]); + var item = JsonUtils.DeepCopy(rules[index]); rules.RemoveAt(index); rules.Insert(0, item); @@ -1531,7 +1531,7 @@ namespace v2rayN.Handler { return 0; } - var item = Utils.DeepCopy(rules[index]); + var item = JsonUtils.DeepCopy(rules[index]); rules.RemoveAt(index); rules.Insert(index - 1, item); @@ -1544,7 +1544,7 @@ namespace v2rayN.Handler { return 0; } - var item = Utils.DeepCopy(rules[index]); + var item = JsonUtils.DeepCopy(rules[index]); rules.RemoveAt(index); rules.Insert(index + 1, item); @@ -1556,7 +1556,7 @@ namespace v2rayN.Handler { return 0; } - var item = Utils.DeepCopy(rules[index]); + var item = JsonUtils.DeepCopy(rules[index]); rules.RemoveAt(index); rules.Add(item); @@ -1565,7 +1565,7 @@ namespace v2rayN.Handler case EMove.Position: { var removeItem = rules[index]; - var item = Utils.DeepCopy(rules[index]); + var item = JsonUtils.DeepCopy(rules[index]); rules.Insert(pos, item); rules.Remove(removeItem); break; diff --git a/v2rayN/v2rayN/Handler/CoreConfigHandler.cs b/v2rayN/v2rayN/Handler/CoreConfigHandler.cs index 2fca828a..525f1cf5 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigHandler.cs @@ -35,11 +35,11 @@ namespace v2rayN.Handler } if (Utils.IsNullOrEmpty(fileName)) { - content = Utils.ToJson(singboxConfig); + content = JsonUtils.ToJson(singboxConfig); } else { - Utils.ToJsonFile(singboxConfig, fileName, false); + JsonUtils.ToJsonFile(singboxConfig, fileName, false); } } else @@ -51,17 +51,17 @@ namespace v2rayN.Handler } if (Utils.IsNullOrEmpty(fileName)) { - content = Utils.ToJson(v2rayConfig); + content = JsonUtils.ToJson(v2rayConfig); } else { - Utils.ToJsonFile(v2rayConfig, fileName, false); + JsonUtils.ToJsonFile(v2rayConfig, fileName, false); } } } catch (Exception ex) { - Utils.SaveLog("GenerateClientConfig", ex); + Logging.SaveLog("GenerateClientConfig", ex); msg = ResUI.FailedGenDefaultConfiguration; return -1; } @@ -143,7 +143,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog("GenerateClientCustomConfig", ex); + Logging.SaveLog("GenerateClientCustomConfig", ex); msg = ResUI.FailedGenDefaultConfiguration; return -1; } @@ -158,7 +158,7 @@ namespace v2rayN.Handler { return -1; } - Utils.ToJsonFile(singboxConfig, fileName, false); + JsonUtils.ToJsonFile(singboxConfig, fileName, false); } else { @@ -166,8 +166,7 @@ namespace v2rayN.Handler { return -1; } - - Utils.ToJsonFile(v2rayConfig, fileName, false); + JsonUtils.ToJsonFile(v2rayConfig, fileName, false); } return 0; } diff --git a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs index a42fe30c..7e1f24ce 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs @@ -36,7 +36,7 @@ namespace v2rayN.Handler return -1; } - singboxConfig = Utils.FromJson(result); + singboxConfig = JsonUtils.FromJson(result); if (singboxConfig == null) { msg = ResUI.FailedGenDefaultConfiguration; @@ -61,7 +61,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog("GenerateClientConfig4Singbox", ex); + Logging.SaveLog("GenerateClientConfig4Singbox", ex); msg = ResUI.FailedGenDefaultConfiguration; return -1; } @@ -101,7 +101,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return 0; } @@ -178,7 +178,7 @@ namespace v2rayN.Handler _config.tunModeItem.stack = Global.TunStacks[0]; } - var tunInbound = Utils.FromJson(Utils.GetEmbedText(Global.TunSingboxInboundFileName)); + var tunInbound = JsonUtils.FromJson(Utils.GetEmbedText(Global.TunSingboxInboundFileName)); tunInbound.mtu = _config.tunModeItem.mtu; tunInbound.strict_route = _config.tunModeItem.strictRoute; tunInbound.stack = _config.tunModeItem.stack; @@ -192,14 +192,14 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return 0; } private Inbound4Sbox? GetInbound(Inbound4Sbox inItem, string tag, int offset, bool bSocks) { - var inbound = Utils.DeepCopy(inItem); + var inbound = JsonUtils.DeepCopy(inItem); inbound.tag = tag; inbound.listen_port = inItem.listen_port + offset; inbound.type = bSocks ? Global.InboundSocks : Global.InboundHttp; @@ -304,7 +304,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return 0; } @@ -329,7 +329,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return 0; } @@ -379,7 +379,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return 0; } @@ -431,7 +431,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return 0; } @@ -459,7 +459,7 @@ namespace v2rayN.Handler if (prevNode is not null && prevNode.configType != EConfigType.Custom) { - var prevOutbound = Utils.FromJson(txtOutbound); + var prevOutbound = JsonUtils.FromJson(txtOutbound); GenOutbound(prevNode, prevOutbound); prevOutbound.tag = $"{Global.ProxyTag}2"; singboxConfig.outbounds.Add(prevOutbound); @@ -472,7 +472,7 @@ namespace v2rayN.Handler if (nextNode is not null && nextNode.configType != EConfigType.Custom) { - var nextOutbound = Utils.FromJson(txtOutbound); + var nextOutbound = JsonUtils.FromJson(txtOutbound); GenOutbound(nextNode, nextOutbound); nextOutbound.tag = Global.ProxyTag; singboxConfig.outbounds.Insert(0, nextOutbound); @@ -483,7 +483,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return 0; @@ -497,7 +497,7 @@ namespace v2rayN.Handler { singboxConfig.route.auto_detect_interface = true; - var tunRules = Utils.FromJson>(Utils.GetEmbedText(Global.TunSingboxRulesFileName)); + var tunRules = JsonUtils.FromJson>(Utils.GetEmbedText(Global.TunSingboxRulesFileName)); singboxConfig.route.rules.AddRange(tunRules); GenRoutingDirectExe(out List lstDnsExe, out List lstDirectExe); @@ -520,7 +520,7 @@ namespace v2rayN.Handler var routing = ConfigHandler.GetDefaultRouting(_config); if (routing != null) { - var rules = Utils.FromJson>(routing.ruleSet); + var rules = JsonUtils.FromJson>(routing.ruleSet); foreach (var item in rules!) { if (item.enabled) @@ -535,7 +535,7 @@ namespace v2rayN.Handler var lockedItem = ConfigHandler.GetLockedRoutingItem(_config); if (lockedItem != null) { - var rules = Utils.FromJson>(lockedItem.ruleSet); + var rules = JsonUtils.FromJson>(lockedItem.ruleSet); foreach (var item in rules!) { GenRoutingUserRule(item, singboxConfig.route.rules); @@ -545,7 +545,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return 0; } @@ -609,8 +609,8 @@ namespace v2rayN.Handler { rule.inbound = item.inboundTag; } - var rule2 = Utils.DeepCopy(rule); - var rule3 = Utils.DeepCopy(rule); + var rule2 = JsonUtils.DeepCopy(rule); + var rule3 = JsonUtils.DeepCopy(rule); var hasDomainIp = false; if (item.domain?.Count > 0) @@ -647,7 +647,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return 0; } @@ -727,7 +727,7 @@ namespace v2rayN.Handler { tunDNS = Utils.GetEmbedText(Global.TunSingboxDNSFileName); } - dns4Sbox = Utils.FromJson(tunDNS); + dns4Sbox = JsonUtils.FromJson(tunDNS); } else { @@ -738,7 +738,7 @@ namespace v2rayN.Handler normalDNS = "{\"servers\":[{\"address\":\"tcp://8.8.8.8\"}]}"; } - dns4Sbox = Utils.FromJson(normalDNS); + dns4Sbox = JsonUtils.FromJson(normalDNS); } if (dns4Sbox is null) { @@ -765,7 +765,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return 0; } @@ -822,7 +822,7 @@ namespace v2rayN.Handler return -1; } - singboxConfig = Utils.FromJson(result); + singboxConfig = JsonUtils.FromJson(result); if (singboxConfig == null) { msg = ResUI.FailedGenDefaultConfiguration; @@ -838,7 +838,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } GenLog(singboxConfig); @@ -920,7 +920,7 @@ namespace v2rayN.Handler continue; } - var outbound = Utils.FromJson(txtOutbound); + var outbound = JsonUtils.FromJson(txtOutbound); GenOutbound(item, outbound); outbound.tag = Global.ProxyTag + inbound.listen_port.ToString(); singboxConfig.outbounds.Add(outbound); @@ -939,7 +939,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); msg = ResUI.FailedGenDefaultConfiguration; return -1; } diff --git a/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs b/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs index 1f1eb390..8206bd54 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs @@ -38,7 +38,7 @@ namespace v2rayN.Handler return -1; } - v2rayConfig = Utils.FromJson(result); + v2rayConfig = JsonUtils.FromJson(result); if (v2rayConfig == null) { msg = ResUI.FailedGenDefaultConfiguration; @@ -63,7 +63,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog("GenerateClientConfig4V2ray", ex); + Logging.SaveLog("GenerateClientConfig4V2ray", ex); msg = ResUI.FailedGenDefaultConfiguration; return -1; } @@ -92,7 +92,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return 0; } @@ -141,7 +141,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return 0; } @@ -154,7 +154,7 @@ namespace v2rayN.Handler return null; } - var inbound = Utils.FromJson(result); + var inbound = JsonUtils.FromJson(result); if (inbound == null) { return null; @@ -187,12 +187,12 @@ namespace v2rayN.Handler { v2rayConfig.routing.domainStrategy = routing.domainStrategy; } - var rules = Utils.FromJson>(routing.ruleSet); + var rules = JsonUtils.FromJson>(routing.ruleSet); foreach (var item in rules) { if (item.enabled) { - var item2 = Utils.FromJson(Utils.ToJson(item)); + var item2 = JsonUtils.FromJson(JsonUtils.ToJson(item)); GenRoutingUserRule(item2, v2rayConfig); } } @@ -203,10 +203,10 @@ namespace v2rayN.Handler var lockedItem = ConfigHandler.GetLockedRoutingItem(_config); if (lockedItem != null) { - var rules = Utils.FromJson>(lockedItem.ruleSet); + var rules = JsonUtils.FromJson>(lockedItem.ruleSet); foreach (var item in rules) { - var item2 = Utils.FromJson(Utils.ToJson(item)); + var item2 = JsonUtils.FromJson(JsonUtils.ToJson(item)); GenRoutingUserRule(item2, v2rayConfig); } } @@ -215,7 +215,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return 0; } @@ -252,7 +252,7 @@ namespace v2rayN.Handler var hasDomainIp = false; if (rules.domain?.Count > 0) { - var it = Utils.DeepCopy(rules); + var it = JsonUtils.DeepCopy(rules); it.ip = null; it.type = "field"; for (int k = it.domain.Count - 1; k >= 0; k--) @@ -268,7 +268,7 @@ namespace v2rayN.Handler } if (rules.ip?.Count > 0) { - var it = Utils.DeepCopy(rules); + var it = JsonUtils.DeepCopy(rules); it.domain = null; it.type = "field"; v2rayConfig.routing.rules.Add(it); @@ -281,7 +281,7 @@ namespace v2rayN.Handler || (rules.inboundTag?.Count > 0) ) { - var it = Utils.DeepCopy(rules); + var it = JsonUtils.DeepCopy(rules); it.type = "field"; v2rayConfig.routing.rules.Add(it); } @@ -289,7 +289,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return 0; } @@ -478,7 +478,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return 0; } @@ -500,7 +500,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return 0; } @@ -688,7 +688,7 @@ namespace v2rayN.Handler pathHttp = string.Join("\",\"", arrPath); } request = request.Replace("$requestPath$", $"\"{pathHttp}\""); - tcpSettings.header.request = Utils.FromJson(request); + tcpSettings.header.request = JsonUtils.FromJson(request); streamSettings.tcpSettings = tcpSettings; } @@ -697,7 +697,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return 0; } @@ -722,7 +722,7 @@ namespace v2rayN.Handler outbound.settings.userLevel = 0; } - var obj = Utils.ParseJson(normalDNS) ?? new JObject(); + var obj = JsonUtils.ParseJson(normalDNS) ?? new JObject(); if (!obj.ContainsKey("servers")) { @@ -769,7 +769,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return 0; } @@ -849,7 +849,7 @@ namespace v2rayN.Handler && prevNode.configType != EConfigType.Hysteria2 && prevNode.configType != EConfigType.Tuic) { - var prevOutbound = Utils.FromJson(txtOutbound); + var prevOutbound = JsonUtils.FromJson(txtOutbound); GenOutbound(prevNode, prevOutbound); prevOutbound.tag = $"{Global.ProxyTag}2"; v2rayConfig.outbounds.Add(prevOutbound); @@ -867,7 +867,7 @@ namespace v2rayN.Handler && nextNode.configType != EConfigType.Hysteria2 && nextNode.configType != EConfigType.Tuic) { - var nextOutbound = Utils.FromJson(txtOutbound); + var nextOutbound = JsonUtils.FromJson(txtOutbound); GenOutbound(nextNode, nextOutbound); nextOutbound.tag = Global.ProxyTag; v2rayConfig.outbounds.Insert(0, nextOutbound); @@ -881,7 +881,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return 0; @@ -912,7 +912,7 @@ namespace v2rayN.Handler return -1; } - v2rayConfig = Utils.FromJson(result); + v2rayConfig = JsonUtils.FromJson(result); if (v2rayConfig == null) { msg = ResUI.FailedGenDefaultConfiguration; @@ -928,7 +928,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } GenLog(v2rayConfig); @@ -1009,7 +1009,7 @@ namespace v2rayN.Handler continue; } - var outbound = Utils.FromJson(txtOutbound); + var outbound = JsonUtils.FromJson(txtOutbound); GenOutbound(item, outbound); outbound.tag = Global.ProxyTag + inbound.port.ToString(); v2rayConfig.outbounds.Add(outbound); @@ -1029,7 +1029,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); msg = ResUI.FailedGenDefaultConfiguration; return -1; } diff --git a/v2rayN/v2rayN/Handler/CoreHandler.cs b/v2rayN/v2rayN/Handler/CoreHandler.cs index 905c245f..f30924e4 100644 --- a/v2rayN/v2rayN/Handler/CoreHandler.cs +++ b/v2rayN/v2rayN/Handler/CoreHandler.cs @@ -59,7 +59,7 @@ namespace v2rayN.Handler { CoreStart(node); ShowMsg(false, "Tun mode restart the core once"); - Utils.SaveLog("Tun mode restart the core once"); + Logging.SaveLog("Tun mode restart the core once"); } } }); @@ -120,7 +120,7 @@ namespace v2rayN.Handler foreach (Process p in existing) { string? path = p.MainModule?.FileName; - if (path == $"{Utils.GetBinPath(vName, it.coreType)}.exe") + if (path == $"{Utils.GetBinPath(vName, it.coreType.ToString())}.exe") { KillProcess(p); } @@ -131,7 +131,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } } @@ -144,7 +144,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } } @@ -154,7 +154,7 @@ namespace v2rayN.Handler foreach (string name in coreInfo.coreExes) { string vName = $"{name}.exe"; - vName = Utils.GetBinPath(vName, coreInfo.coreType); + vName = Utils.GetBinPath(vName, coreInfo.coreType.ToString()); if (File.Exists(vName)) { fileName = vName; @@ -163,8 +163,8 @@ namespace v2rayN.Handler } if (Utils.IsNullOrEmpty(fileName)) { - string msg = string.Format(ResUI.NotFoundCore, Utils.GetBinPath("", coreInfo.coreType), string.Join(", ", coreInfo.coreExes.ToArray()), coreInfo.coreUrl); - Utils.SaveLog(msg); + string msg = string.Format(ResUI.NotFoundCore, Utils.GetBinPath("", coreInfo.coreType.ToString()), string.Join(", ", coreInfo.coreExes.ToArray()), coreInfo.coreUrl); + Logging.SaveLog(msg); ShowMsg(false, msg); } return fileName; @@ -237,7 +237,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); string msg = ex.Message; ShowMsg(false, msg); return -1; @@ -311,7 +311,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); string msg = ex.Message; update(true, msg); return null; @@ -336,7 +336,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } } diff --git a/v2rayN/v2rayN/Handler/DownloadHandle.cs b/v2rayN/v2rayN/Handler/DownloadHandle.cs index 54edadf7..fc3aceb7 100644 --- a/v2rayN/v2rayN/Handler/DownloadHandle.cs +++ b/v2rayN/v2rayN/Handler/DownloadHandle.cs @@ -84,7 +84,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); Error?.Invoke(this, new ErrorEventArgs(ex)); if (ex.InnerException != null) @@ -111,7 +111,7 @@ namespace v2rayN.Handler } else { - Utils.SaveLog("StatusCode error: " + url); + Logging.SaveLog("StatusCode error: " + url); return null; } } @@ -128,7 +128,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); Error?.Invoke(this, new ErrorEventArgs(ex)); if (ex.InnerException != null) { @@ -146,7 +146,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); Error?.Invoke(this, new ErrorEventArgs(ex)); if (ex.InnerException != null) { @@ -166,7 +166,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); Error?.Invoke(this, new ErrorEventArgs(ex)); if (ex.InnerException != null) { @@ -212,7 +212,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); Error?.Invoke(this, new ErrorEventArgs(ex)); if (ex.InnerException != null) { @@ -243,7 +243,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); Error?.Invoke(this, new ErrorEventArgs(ex)); if (ex.InnerException != null) { @@ -270,13 +270,13 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); return -1; } } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); return -1; } } diff --git a/v2rayN/v2rayN/Handler/MainFormHandler.cs b/v2rayN/v2rayN/Handler/MainFormHandler.cs index 24989615..960c6ab9 100644 --- a/v2rayN/v2rayN/Handler/MainFormHandler.cs +++ b/v2rayN/v2rayN/Handler/MainFormHandler.cs @@ -42,7 +42,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); return Properties.Resources.NotifyIcon1; } } @@ -112,7 +112,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); return null; } } @@ -163,7 +163,7 @@ namespace v2rayN.Handler private async Task UpdateTaskRunSubscription(Config config, Action update) { await Task.Delay(60000); - Utils.SaveLog("UpdateTaskRunSubscription"); + Logging.SaveLog("UpdateTaskRunSubscription"); var updateHandle = new UpdateHandle(); while (true) @@ -180,7 +180,7 @@ namespace v2rayN.Handler { update(success, msg); if (success) - Utils.SaveLog("subscription" + msg); + Logging.SaveLog("subscription" + msg); }); item.updateTime = updateTime; ConfigHandler.AddSubItem(config, item); @@ -196,7 +196,7 @@ namespace v2rayN.Handler var autoUpdateGeoTime = DateTime.Now; await Task.Delay(1000 * 120); - Utils.SaveLog("UpdateTaskRunGeo"); + Logging.SaveLog("UpdateTaskRunGeo"); var updateHandle = new UpdateHandle(); while (true) diff --git a/v2rayN/v2rayN/Handler/ProfileExHandler.cs b/v2rayN/v2rayN/Handler/ProfileExHandler.cs index 0b755a06..26d164fc 100644 --- a/v2rayN/v2rayN/Handler/ProfileExHandler.cs +++ b/v2rayN/v2rayN/Handler/ProfileExHandler.cs @@ -82,7 +82,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } } diff --git a/v2rayN/v2rayN/Handler/ShareHandler.cs b/v2rayN/v2rayN/Handler/ShareHandler.cs index f10177c1..79be638b 100644 --- a/v2rayN/v2rayN/Handler/ShareHandler.cs +++ b/v2rayN/v2rayN/Handler/ShareHandler.cs @@ -38,7 +38,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); return ""; } } @@ -66,7 +66,7 @@ namespace v2rayN.Handler fp = item.fingerprint }; - url = Utils.ToJson(vmessQRCode); + url = JsonUtils.ToJson(vmessQRCode); url = Utils.Base64Encode(url); url = $"{Global.ProtocolShares[EConfigType.VMess]}{url}"; @@ -431,7 +431,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); msg = ResUI.Incorrectconfiguration; return null; } @@ -451,7 +451,7 @@ namespace v2rayN.Handler result = Utils.Base64Decode(result); //转成Json - VmessQRCode? vmessQRCode = Utils.FromJson(result); + VmessQRCode? vmessQRCode = JsonUtils.FromJson(result); if (vmessQRCode == null) { msg = ResUI.FailedConversionConfiguration; diff --git a/v2rayN/v2rayN/Handler/SpeedtestHandler.cs b/v2rayN/v2rayN/Handler/SpeedtestHandler.cs index dad40271..9c7bc4ff 100644 --- a/v2rayN/v2rayN/Handler/SpeedtestHandler.cs +++ b/v2rayN/v2rayN/Handler/SpeedtestHandler.cs @@ -107,7 +107,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } } @@ -115,7 +115,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } } @@ -184,7 +184,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } })); } @@ -192,7 +192,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } finally { @@ -371,7 +371,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return responseTime; } @@ -407,7 +407,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); return -1; } return roundtripTime; diff --git a/v2rayN/v2rayN/Handler/StatisticsHandler.cs b/v2rayN/v2rayN/Handler/StatisticsHandler.cs index c32a7d74..032069e7 100644 --- a/v2rayN/v2rayN/Handler/StatisticsHandler.cs +++ b/v2rayN/v2rayN/Handler/StatisticsHandler.cs @@ -44,7 +44,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } } @@ -63,7 +63,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } } diff --git a/v2rayN/v2rayN/Handler/StatisticsSingbox.cs b/v2rayN/v2rayN/Handler/StatisticsSingbox.cs index 14ee1c62..60519d1d 100644 --- a/v2rayN/v2rayN/Handler/StatisticsSingbox.cs +++ b/v2rayN/v2rayN/Handler/StatisticsSingbox.cs @@ -51,7 +51,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } } @@ -113,7 +113,7 @@ namespace v2rayN.Handler up = 0; down = 0; try { - var trafficItem = Utils.FromJson(source); + var trafficItem = JsonUtils.FromJson(source); if (trafficItem != null) { up = trafficItem.up; diff --git a/v2rayN/v2rayN/Handler/StatisticsV2ray.cs b/v2rayN/v2rayN/Handler/StatisticsV2ray.cs index 210e988f..63cdac41 100644 --- a/v2rayN/v2rayN/Handler/StatisticsV2ray.cs +++ b/v2rayN/v2rayN/Handler/StatisticsV2ray.cs @@ -35,7 +35,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } } } diff --git a/v2rayN/v2rayN/Handler/SysProxyHandle.cs b/v2rayN/v2rayN/Handler/SysProxyHandle.cs index ae1b66cf..4bd8fa49 100644 --- a/v2rayN/v2rayN/Handler/SysProxyHandle.cs +++ b/v2rayN/v2rayN/Handler/SysProxyHandle.cs @@ -87,7 +87,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return true; } diff --git a/v2rayN/v2rayN/Handler/UpdateHandle.cs b/v2rayN/v2rayN/Handler/UpdateHandle.cs index 0b677cde..5c9b4e31 100644 --- a/v2rayN/v2rayN/Handler/UpdateHandle.cs +++ b/v2rayN/v2rayN/Handler/UpdateHandle.cs @@ -274,8 +274,8 @@ namespace v2rayN.Handler int ret = ConfigHandler.AddBatchServers(config, result, id, true); if (ret <= 0) { - Utils.SaveLog("FailedImportSubscription"); - Utils.SaveLog(result); + Logging.SaveLog("FailedImportSubscription"); + Logging.SaveLog(result); } _updateFunc(false, ret > 0 @@ -327,13 +327,13 @@ namespace v2rayN.Handler } else { - Utils.SaveLog("StatusCode error: " + url); + Logging.SaveLog("StatusCode error: " + url); return; } } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); _updateFunc(false, ex.Message); } } @@ -350,7 +350,7 @@ namespace v2rayN.Handler foreach (string name in coreInfo.coreExes) { string vName = $"{name}.exe"; - vName = Utils.GetBinPath(vName, coreInfo.coreType); + vName = Utils.GetBinPath(vName, coreInfo.coreType.ToString()); if (File.Exists(vName)) { filePath = vName; @@ -399,7 +399,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); _updateFunc(false, ex.Message); return new SemanticVersion(""); } @@ -409,7 +409,7 @@ namespace v2rayN.Handler { try { - var gitHubReleases = Utils.FromJson>(gitHubReleaseApi); + var gitHubReleases = JsonUtils.FromJson>(gitHubReleaseApi); var gitHubRelease = preRelease ? gitHubReleases!.First() : gitHubReleases!.First(r => r.Prerelease == false); var version = new SemanticVersion(gitHubRelease!.TagName); var body = gitHubRelease!.Body; @@ -524,7 +524,7 @@ namespace v2rayN.Handler } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); _updateFunc(false, ex.Message); } } diff --git a/v2rayN/v2rayN/Tool/FileManager.cs b/v2rayN/v2rayN/Tool/FileManager.cs index fa17fde5..bdfa0fd5 100644 --- a/v2rayN/v2rayN/Tool/FileManager.cs +++ b/v2rayN/v2rayN/Tool/FileManager.cs @@ -15,7 +15,7 @@ namespace v2rayN.Tool } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return false; } @@ -30,7 +30,7 @@ namespace v2rayN.Tool } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } } @@ -49,7 +49,7 @@ namespace v2rayN.Tool } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); throw; } } @@ -75,13 +75,13 @@ namespace v2rayN.Tool } catch (IOException ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } } } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); return false; } return true; diff --git a/v2rayN/v2rayN/Tool/Job.cs b/v2rayN/v2rayN/Tool/Job.cs index 78b1a56e..c25a2221 100644 --- a/v2rayN/v2rayN/Tool/Job.cs +++ b/v2rayN/v2rayN/Tool/Job.cs @@ -52,7 +52,7 @@ namespace v2rayN if (!succ) { - Utils.SaveLog("Failed to call AssignProcessToJobObject! GetLastError=" + Marshal.GetLastWin32Error()); + Logging.SaveLog("Failed to call AssignProcessToJobObject! GetLastError=" + Marshal.GetLastWin32Error()); } return succ; diff --git a/v2rayN/v2rayN/Tool/JsonUtils.cs b/v2rayN/v2rayN/Tool/JsonUtils.cs new file mode 100644 index 00000000..9493411e --- /dev/null +++ b/v2rayN/v2rayN/Tool/JsonUtils.cs @@ -0,0 +1,120 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System.IO; + +namespace v2rayN +{ + internal class JsonUtils + { + /// + /// DeepCopy + /// + /// + /// + /// + public static T DeepCopy(T obj) + { + return FromJson(ToJson(obj, false))!; + } + + /// + /// Deserialize to object + /// + /// + /// + /// + public static T? FromJson(string? strJson) + { + try + { + if (string.IsNullOrEmpty(strJson)) + { + return default; + } + return JsonConvert.DeserializeObject(strJson); + } + catch + { + return default; + } + } + + /// + /// parse + /// + /// + /// + public static JObject? ParseJson(string strJson) + { + try + { + if (string.IsNullOrEmpty(strJson)) + { + return null; + } + + return JObject.Parse(strJson); + } + catch + { + //SaveLog(ex.Message, ex); + return null; + } + } + + /// + /// Serialize Object to Json string + /// + /// + /// + /// + public static string ToJson(object? obj, bool indented = true) + { + string result = string.Empty; + try + { + if (obj == null) + { + return result; + } + result = JsonConvert.SerializeObject(obj, + indented ? Formatting.Indented : Formatting.None, + new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); + } + catch (Exception ex) + { + Logging.SaveLog(ex.Message, ex); + } + return result; + } + + /// + /// Save as json file + /// + /// + /// + /// + /// + public static int ToJsonFile(object? obj, string filePath, bool nullValue = true) + { + int result; + try + { + using StreamWriter file = File.CreateText(filePath); + var serializer = new JsonSerializer() + { + Formatting = Formatting.Indented, + NullValueHandling = nullValue ? NullValueHandling.Include : NullValueHandling.Ignore + }; + serializer.Serialize(file, obj); + result = 0; + } + catch (Exception ex) + { + Logging.SaveLog(ex.Message, ex); + result = -1; + } + return result; + } + } +} \ No newline at end of file diff --git a/v2rayN/v2rayN/Tool/Logging.cs b/v2rayN/v2rayN/Tool/Logging.cs index b390fc99..ba5b97e7 100644 --- a/v2rayN/v2rayN/Tool/Logging.cs +++ b/v2rayN/v2rayN/Tool/Logging.cs @@ -3,7 +3,7 @@ using NLog.Config; using NLog.Targets; using System.IO; -namespace v2rayN.Tool +namespace v2rayN { public class Logging { @@ -51,5 +51,28 @@ namespace v2rayN.Tool catch { } }); } + + public static void SaveLog(string strContent) + { + if (LogManager.IsLoggingEnabled()) + { + var logger = LogManager.GetLogger("Log1"); + logger.Info(strContent); + } + } + + public static void SaveLog(string strTitle, Exception ex) + { + if (LogManager.IsLoggingEnabled()) + { + var logger = LogManager.GetLogger("Log2"); + logger.Debug($"{strTitle},{ex.Message}"); + logger.Debug(ex.StackTrace); + if (ex?.InnerException != null) + { + logger.Error(ex.InnerException); + } + } + } } } \ No newline at end of file diff --git a/v2rayN/v2rayN/Tool/Utils.cs b/v2rayN/v2rayN/Tool/Utils.cs index d14fb433..1377cb3d 100644 --- a/v2rayN/v2rayN/Tool/Utils.cs +++ b/v2rayN/v2rayN/Tool/Utils.cs @@ -1,8 +1,5 @@ using Microsoft.Win32; using Microsoft.Win32.TaskScheduler; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using NLog; using System.Diagnostics; using System.Drawing; using System.IO; @@ -21,8 +18,6 @@ using System.Windows; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; -using v2rayN.Base; -using v2rayN.Mode; using ZXing; using ZXing.Common; using ZXing.QrCode; @@ -53,7 +48,7 @@ namespace v2rayN } catch (Exception ex) { - SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return result; } @@ -71,111 +66,11 @@ namespace v2rayN } catch (Exception ex) { - SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return null; } - /// - /// 反序列化成对象 - /// - /// - /// - /// - public static T? FromJson(string? strJson) - { - try - { - if (string.IsNullOrEmpty(strJson)) - { - return default; - } - return JsonConvert.DeserializeObject(strJson); - } - catch - { - return default; - } - } - - /// - /// 序列化成Json - /// - /// - /// - public static string ToJson(object? obj, bool indented = true) - { - string result = string.Empty; - try - { - if (obj == null) - { - return result; - } - if (indented) - { - result = JsonConvert.SerializeObject(obj, - Formatting.Indented, - new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); - } - else - { - result = JsonConvert.SerializeObject(obj, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); - } - } - catch (Exception ex) - { - SaveLog(ex.Message, ex); - } - return result; - } - - /// - /// 保存成json文件 - /// - /// - /// - /// - public static int ToJsonFile(object? obj, string filePath, bool nullValue = true) - { - int result; - try - { - using StreamWriter file = File.CreateText(filePath); - JsonSerializer serializer; - if (nullValue) - { - serializer = new JsonSerializer() { Formatting = Formatting.Indented }; - } - else - { - serializer = new JsonSerializer() { Formatting = Formatting.Indented, NullValueHandling = NullValueHandling.Ignore }; - } - - serializer.Serialize(file, obj); - result = 0; - } - catch (Exception ex) - { - SaveLog(ex.Message, ex); - result = -1; - } - return result; - } - - public static JObject? ParseJson(string strJson) - { - try - { - return JObject.Parse(strJson); - } - catch (Exception ex) - { - //SaveLog(ex.Message, ex); - return null; - } - } - #endregion 资源Json操作 #region 转换函数 @@ -204,7 +99,7 @@ namespace v2rayN } catch (Exception ex) { - SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); return string.Empty; } } @@ -223,7 +118,7 @@ namespace v2rayN } catch (Exception ex) { - SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); return new List(); } } @@ -244,7 +139,7 @@ namespace v2rayN } catch (Exception ex) { - SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); return new List(); } } @@ -263,7 +158,7 @@ namespace v2rayN } catch (Exception ex) { - SaveLog("Base64Encode", ex); + Logging.SaveLog("Base64Encode", ex); return string.Empty; } } @@ -277,7 +172,7 @@ namespace v2rayN { try { - plainText = plainText.TrimEx() + plainText = plainText.Trim() .Replace(Environment.NewLine, "") .Replace("\n", "") .Replace("\r", "") @@ -295,7 +190,7 @@ namespace v2rayN } catch (Exception ex) { - SaveLog("Base64Decode", ex); + Logging.SaveLog("Base64Decode", ex); return string.Empty; } } @@ -492,7 +387,7 @@ namespace v2rayN } catch (Exception ex) { - SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); return false; } } @@ -598,14 +493,14 @@ namespace v2rayN /// /// /// - public static void SetAutoRun(bool run) + public static void SetAutoRun(string AutoRunRegPath, string AutoRunName, bool run) { try { - var autoRunName = $"{Global.AutoRunName}_{GetMD5(StartupPath())}"; + var autoRunName = $"{AutoRunName}_{GetMD5(StartupPath())}"; //delete first - RegWriteValue(Global.AutoRunRegPath, autoRunName, ""); + RegWriteValue(AutoRunRegPath, autoRunName, ""); if (IsAdministrator()) { AutoStart(autoRunName, "", ""); @@ -620,43 +515,16 @@ namespace v2rayN } else { - RegWriteValue(Global.AutoRunRegPath, autoRunName, exePath); + RegWriteValue(AutoRunRegPath, autoRunName, exePath); } } } catch (Exception ex) { - SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } } - - /// - /// 是否已经设置开机自动启动 - /// - /// - public static bool IsAutoRun() - { - try - { - //clear - if (!RegReadValue(Global.AutoRunRegPath, Global.AutoRunName, "").IsNullOrEmpty()) - { - RegWriteValue(Global.AutoRunRegPath, Global.AutoRunName, ""); - } - - string value = RegReadValue(Global.AutoRunRegPath, Global.AutoRunName, ""); - string exePath = GetExePath(); - if (value == exePath || value == $"\"{exePath}\"") - { - return true; - } - } - catch (Exception ex) - { - SaveLog(ex.Message, ex); - } - return false; - } + /// /// 获取启动了应用程序的可执行文件的路径 @@ -704,7 +572,7 @@ namespace v2rayN } catch (Exception ex) { - SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } finally { @@ -730,7 +598,7 @@ namespace v2rayN } catch (Exception ex) { - SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } finally { @@ -859,7 +727,7 @@ namespace v2rayN } catch (Exception ex) { - SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return inUse; } @@ -891,34 +759,11 @@ namespace v2rayN } catch (Exception ex) { - SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); return string.Empty; } } - /// - /// DeepCopy - /// - /// - /// - /// - public static T DeepCopy(T obj) - { - return FromJson(ToJson(obj, false))!; - - // object retval; - // MemoryStream ms = new(); - //#pragma warning disable SYSLIB0011 // 类型或成员已过时 - // BinaryFormatter bf = new(); - //#pragma warning restore SYSLIB0011 // 类型或成员已过时 - // //序列化成流 - // bf.Serialize(ms, obj); - // ms.Seek(0, SeekOrigin.Begin); - // //反序列化成对象 - // retval = bf.Deserialize(ms); - // return (T)retval; - } - /// /// 获取剪贴板数 /// @@ -937,7 +782,7 @@ namespace v2rayN } catch (Exception ex) { - SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return strData; } @@ -976,7 +821,7 @@ namespace v2rayN } catch (Exception ex) { - SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return string.Empty; } @@ -996,7 +841,7 @@ namespace v2rayN } catch (Exception ex) { - SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); return false; } } @@ -1036,7 +881,7 @@ namespace v2rayN } catch (Exception ex) { - SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } } @@ -1115,7 +960,7 @@ namespace v2rayN } } - public static string GetBinPath(string filename, ECoreType? coreType = null) + public static string GetBinPath(string filename, string? coreType = null) { string _tempPath = Path.Combine(StartupPath(), "bin"); if (!Directory.Exists(_tempPath)) @@ -1176,33 +1021,6 @@ namespace v2rayN #endregion TempPath - #region Log - - public static void SaveLog(string strContent) - { - if (LogManager.IsLoggingEnabled()) - { - var logger = LogManager.GetLogger("Log1"); - logger.Info(strContent); - } - } - - public static void SaveLog(string strTitle, Exception ex) - { - if (LogManager.IsLoggingEnabled()) - { - var logger = LogManager.GetLogger("Log2"); - logger.Debug($"{strTitle},{ex.Message}"); - logger.Debug(ex.StackTrace); - if (ex?.InnerException != null) - { - logger.Error(ex.InnerException); - } - } - } - - #endregion Log - #region scan screen public static string ScanScreen(float dpiX, float dpiY) @@ -1248,7 +1066,7 @@ namespace v2rayN } catch (Exception ex) { - SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } return string.Empty; } diff --git a/v2rayN/v2rayN/ViewModels/AddServer2ViewModel.cs b/v2rayN/v2rayN/ViewModels/AddServer2ViewModel.cs index 137db406..83380bf0 100644 --- a/v2rayN/v2rayN/ViewModels/AddServer2ViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/AddServer2ViewModel.cs @@ -38,7 +38,7 @@ namespace v2rayN.ViewModels } else { - SelectedSource = Utils.DeepCopy(profileItem); + SelectedSource = JsonUtils.DeepCopy(profileItem); } _view = view; @@ -127,7 +127,7 @@ namespace v2rayN.ViewModels _noticeHandler?.Enqueue(ResUI.SuccessfullyImportedCustomServer); if (!Utils.IsNullOrEmpty(item.indexId)) { - SelectedSource = Utils.DeepCopy(item); + SelectedSource = JsonUtils.DeepCopy(item); } IsModified = true; } diff --git a/v2rayN/v2rayN/ViewModels/AddServerViewModel.cs b/v2rayN/v2rayN/ViewModels/AddServerViewModel.cs index a36b0733..6c579567 100644 --- a/v2rayN/v2rayN/ViewModels/AddServerViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/AddServerViewModel.cs @@ -37,7 +37,7 @@ namespace v2rayN.ViewModels } else { - SelectedSource = Utils.DeepCopy(profileItem); + SelectedSource = JsonUtils.DeepCopy(profileItem); } SaveCmd = ReactiveCommand.Create(() => diff --git a/v2rayN/v2rayN/ViewModels/DNSSettingViewModel.cs b/v2rayN/v2rayN/ViewModels/DNSSettingViewModel.cs index 56c5ee45..0192e8fa 100644 --- a/v2rayN/v2rayN/ViewModels/DNSSettingViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/DNSSettingViewModel.cs @@ -63,7 +63,7 @@ namespace v2rayN.ViewModels { if (!Utils.IsNullOrEmpty(normalDNS)) { - var obj = Utils.ParseJson(normalDNS); + var obj = JsonUtils.ParseJson(normalDNS); if (obj != null && obj.ContainsKey("servers") == true) { } @@ -78,7 +78,7 @@ namespace v2rayN.ViewModels } if (!Utils.IsNullOrEmpty(normalDNS2)) { - var obj2 = Utils.FromJson(normalDNS2); + var obj2 = JsonUtils.FromJson(normalDNS2); if (obj2 == null) { UI.Show(ResUI.FillCorrectDNSText); @@ -87,7 +87,7 @@ namespace v2rayN.ViewModels } if (!Utils.IsNullOrEmpty(tunDNS2)) { - var obj2 = Utils.FromJson(tunDNS2); + var obj2 = JsonUtils.FromJson(tunDNS2); if (obj2 == null) { UI.Show(ResUI.FillCorrectDNSText); @@ -102,8 +102,8 @@ namespace v2rayN.ViewModels ConfigHandler.SaveDNSItems(_config, item); var item2 = LazyConfig.Instance.GetDNSItem(ECoreType.sing_box); - item2.normalDNS = Utils.ToJson(Utils.ParseJson(normalDNS2)); - item2.tunDNS = Utils.ToJson(Utils.ParseJson(tunDNS2)); + item2.normalDNS = JsonUtils.ToJson(JsonUtils.ParseJson(normalDNS2)); + item2.tunDNS = JsonUtils.ToJson(JsonUtils.ParseJson(tunDNS2)); ConfigHandler.SaveDNSItems(_config, item2); _noticeHandler?.Enqueue(ResUI.OperationSuccess); diff --git a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs index ef654fe5..cb7b8913 100644 --- a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs @@ -652,13 +652,13 @@ namespace v2rayN.ViewModels if (SelectedProfile?.indexId == item.indexId) { - var temp = Utils.DeepCopy(item); + var temp = JsonUtils.DeepCopy(item); _profileItems.Replace(item, temp); SelectedProfile = temp; } else { - _profileItems.Replace(item, Utils.DeepCopy(item)); + _profileItems.Replace(item, JsonUtils.DeepCopy(item)); } } } @@ -667,7 +667,7 @@ namespace v2rayN.ViewModels } catch (Exception ex) { - Utils.SaveLog(ex.Message, ex); + Logging.SaveLog(ex.Message, ex); } } @@ -700,7 +700,7 @@ namespace v2rayN.ViewModels { item.speedVal = $"{speed} {Global.SpeedUnit}"; } - _profileItems.Replace(item, Utils.DeepCopy(item)); + _profileItems.Replace(item, JsonUtils.DeepCopy(item)); } } @@ -734,7 +734,7 @@ namespace v2rayN.ViewModels { try { - Utils.SaveLog("MyAppExit Begin"); + Logging.SaveLog("MyAppExit Begin"); StorageUI(); ConfigHandler.SaveConfig(_config); @@ -755,7 +755,7 @@ namespace v2rayN.ViewModels _statistics?.Close(); _coreHandler.CoreStop(); - Utils.SaveLog("MyAppExit End"); + Logging.SaveLog("MyAppExit End"); } catch { } finally @@ -835,7 +835,7 @@ namespace v2rayN.ViewModels totalDown = t22 == null ? "" : Utils.HumanFy(t22.totalDown), totalUp = t22 == null ? "" : Utils.HumanFy(t22.totalUp) }).OrderBy(t => t.sort).ToList(); - _lstProfile = Utils.FromJson>(Utils.ToJson(lstModel)); + _lstProfile = JsonUtils.FromJson>(JsonUtils.ToJson(lstModel)); Application.Current.Dispatcher.Invoke((Action)(() => { @@ -941,7 +941,7 @@ namespace v2rayN.ViewModels } else { - lstSelecteds = Utils.FromJson>(Utils.ToJson(orderProfiles)); + lstSelecteds = JsonUtils.FromJson>(JsonUtils.ToJson(orderProfiles)); } return 0; @@ -1456,7 +1456,7 @@ namespace v2rayN.ViewModels CloseV2ray(); string fileName = Utils.GetTempPath(Utils.GetDownloadFileName(msg)); - string toPath = Utils.GetBinPath("", type); + string toPath = Utils.GetBinPath("", type.ToString()); FileManager.ZipExtractToFile(fileName, toPath, _config.guiItem.ignoreGeoUpdateCore ? "geo" : ""); diff --git a/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs b/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs index 8a49279f..1c1a2423 100644 --- a/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs @@ -296,7 +296,7 @@ namespace v2rayN.ViewModels //_config.kcpItem.congestion = Kcpcongestion; //UI - Utils.SetAutoRun(AutoRun); + Utils.SetAutoRun(Global.AutoRunRegPath, Global.AutoRunName, AutoRun); _config.guiItem.autoRun = AutoRun; _config.guiItem.enableStatistics = EnableStatistics; _config.guiItem.keepOlderDedupl = KeepOlderDedupl; diff --git a/v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs b/v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs index c545ef9c..b50005f1 100644 --- a/v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs @@ -60,7 +60,7 @@ namespace v2rayN.ViewModels else { SelectedRouting = routingItem; - _rules = Utils.FromJson>(SelectedRouting.ruleSet); + _rules = JsonUtils.FromJson>(SelectedRouting.ruleSet); } RefreshRulesItems(); @@ -209,7 +209,7 @@ namespace v2rayN.ViewModels } if (lst.Count > 0) { - Utils.SetClipboardData(Utils.ToJson(lst)); + Utils.SetClipboardData(JsonUtils.ToJson(lst)); //UI.Show(ResUI.OperationSuccess")); } } @@ -248,7 +248,7 @@ namespace v2rayN.ViewModels it.id = Utils.GetGUID(false); } item.ruleNum = _rules.Count; - item.ruleSet = Utils.ToJson(_rules, false); + item.ruleSet = JsonUtils.ToJson(_rules, false); if (ConfigHandler.SaveRoutingItem(_config, item) == 0) { @@ -334,7 +334,7 @@ namespace v2rayN.ViewModels { return -1; } - var lstRules = Utils.FromJson>(clipboardData); + var lstRules = JsonUtils.FromJson>(clipboardData); if (lstRules == null) { return -1; diff --git a/v2rayN/v2rayN/ViewModels/RoutingSettingViewModel.cs b/v2rayN/v2rayN/ViewModels/RoutingSettingViewModel.cs index 7b38712d..9de95aed 100644 --- a/v2rayN/v2rayN/ViewModels/RoutingSettingViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/RoutingSettingViewModel.cs @@ -137,7 +137,7 @@ namespace v2rayN.ViewModels _lockedItem = ConfigHandler.GetLockedRoutingItem(_config); if (_lockedItem != null) { - _lockedRules = Utils.FromJson>(_lockedItem.ruleSet); + _lockedRules = JsonUtils.FromJson>(_lockedItem.ruleSet); ProxyDomain = Utils.List2String(_lockedRules[0].domain, true); ProxyIP = Utils.List2String(_lockedRules[0].ip, true); @@ -162,7 +162,7 @@ namespace v2rayN.ViewModels _lockedRules[2].domain = Utils.String2List(Utils.Convert2Comma(BlockDomain.TrimEx())); _lockedRules[2].ip = Utils.String2List(Utils.Convert2Comma(BlockIP.TrimEx())); - _lockedItem.ruleSet = Utils.ToJson(_lockedRules, false); + _lockedItem.ruleSet = JsonUtils.ToJson(_lockedRules, false); ConfigHandler.SaveRoutingItem(_config, _lockedItem); } diff --git a/v2rayN/v2rayN/ViewModels/SubEditViewModel.cs b/v2rayN/v2rayN/ViewModels/SubEditViewModel.cs index d15e9a78..bab90942 100644 --- a/v2rayN/v2rayN/ViewModels/SubEditViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/SubEditViewModel.cs @@ -33,7 +33,7 @@ namespace v2rayN.ViewModels } else { - SelectedSource = Utils.DeepCopy(subItem); + SelectedSource = JsonUtils.DeepCopy(subItem); } SaveCmd = ReactiveCommand.Create(() => diff --git a/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs b/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs index a79879ef..5f511320 100644 --- a/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs @@ -70,7 +70,7 @@ namespace v2rayN.Views private KeyEventItem GetKeyEventItemByEGlobalHotkey(List KELsit, EGlobalHotkey eg) { - return Utils.DeepCopy(KELsit.Find((it) => it.eGlobalHotkey == eg) ?? new() + return JsonUtils.DeepCopy(KELsit.Find((it) => it.eGlobalHotkey == eg) ?? new() { eGlobalHotkey = eg, Control = false, diff --git a/v2rayN/v2rayN/Views/MainWindow.xaml.cs b/v2rayN/v2rayN/Views/MainWindow.xaml.cs index 0d1944f7..e4f30d56 100644 --- a/v2rayN/v2rayN/Views/MainWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/MainWindow.xaml.cs @@ -276,7 +276,7 @@ namespace v2rayN.Views private void Current_SessionEnding(object sender, SessionEndingCancelEventArgs e) { - Utils.SaveLog("Current_SessionEnding"); + Logging.SaveLog("Current_SessionEnding"); StorageUI(); ViewModel?.MyAppExit(true); } diff --git a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs index 45ff0e28..f431e4a0 100644 --- a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs @@ -128,7 +128,7 @@ namespace v2rayN.Views } catch (Exception ex) { - Utils.SaveLog("fill fonts error", ex); + Logging.SaveLog("fill fonts error", ex); } cmbcurrentFontFamily.Items.Add(string.Empty); diff --git a/v2rayN/v2rayN/v2rayN.csproj b/v2rayN/v2rayN/v2rayN.csproj index f6f97e0e..fcb7f0da 100644 --- a/v2rayN/v2rayN/v2rayN.csproj +++ b/v2rayN/v2rayN/v2rayN.csproj @@ -20,7 +20,6 @@ -