diff --git a/v2rayN/v2rayN/Common/JsonUtils.cs b/v2rayN/v2rayN/Common/JsonUtils.cs index b627d426..2ee9ceb2 100644 --- a/v2rayN/v2rayN/Common/JsonUtils.cs +++ b/v2rayN/v2rayN/Common/JsonUtils.cs @@ -1,6 +1,7 @@ -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using System.IO; +using System.IO; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; namespace v2rayN { @@ -31,7 +32,7 @@ namespace v2rayN { return default; } - return JsonConvert.DeserializeObject(strJson); + return JsonSerializer.Deserialize(strJson); } catch { @@ -44,7 +45,7 @@ namespace v2rayN /// /// /// - public static JObject? ParseJson(string strJson) + public static JsonNode? ParseJson(string strJson) { try { @@ -52,8 +53,7 @@ namespace v2rayN { return null; } - - return JObject.Parse(strJson); + return JsonNode.Parse(strJson); } catch { @@ -77,9 +77,12 @@ namespace v2rayN { return result; } - result = JsonConvert.SerializeObject(obj, - indented ? Formatting.Indented : Formatting.None, - new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); + var options = new JsonSerializerOptions + { + WriteIndented = indented ? true : false, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull + }; + result = JsonSerializer.Serialize(obj, options); } catch (Exception ex) { @@ -88,6 +91,13 @@ namespace v2rayN return result; } + /// + /// SerializeToNode + /// + /// + /// + public static JsonNode? SerializeToNode(object? obj) => JsonSerializer.SerializeToNode(obj); + /// /// Save as json file /// @@ -100,13 +110,15 @@ namespace v2rayN int result; try { - using StreamWriter file = File.CreateText(filePath); - var serializer = new JsonSerializer() + using FileStream file = File.Create(filePath); + + var options = new JsonSerializerOptions { - Formatting = Formatting.Indented, - NullValueHandling = nullValue ? NullValueHandling.Include : NullValueHandling.Ignore + WriteIndented = true, + DefaultIgnoreCondition = nullValue ? JsonIgnoreCondition.Never : JsonIgnoreCondition.WhenWritingNull }; - serializer.Serialize(file, obj); + + JsonSerializer.Serialize(file, obj, options); result = 0; } catch (Exception ex) diff --git a/v2rayN/v2rayN/Common/Utils.cs b/v2rayN/v2rayN/Common/Utils.cs index 4b4e5b46..79b91be6 100644 --- a/v2rayN/v2rayN/Common/Utils.cs +++ b/v2rayN/v2rayN/Common/Utils.cs @@ -927,6 +927,37 @@ namespace v2rayN return value is int i && i > 0; } + /// + /// 获取系统hosts + /// + /// + public static Dictionary GetSystemHosts() + { + var systemHosts = new Dictionary(); + var hostfile = @"C:\Windows\System32\drivers\etc\hosts"; + try + { + if (File.Exists(hostfile)) + { + var hosts = File.ReadAllText(hostfile).Replace("\r", ""); + var hostsList = hosts.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); + + foreach (var host in hostsList) + { + if (host.StartsWith("#")) continue; + var hostItem = host.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); + if (hostItem.Length < 2) continue; + systemHosts.Add(hostItem[1], hostItem[0]); + } + } + } + catch (Exception ex) + { + Logging.SaveLog(ex.Message, ex); + } + return systemHosts; + } + #endregion 杂项 #region TempPath diff --git a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs index 195b1757..0f75131f 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs @@ -675,7 +675,7 @@ namespace v2rayN.Handler private void ParseV2Domain(string domain, Rule4Sbox rule) { - if (domain.StartsWith("ext:") || domain.StartsWith("ext-domain:")) + if (domain.StartsWith("#") || domain.StartsWith("ext:") || domain.StartsWith("ext-domain:")) { return; } diff --git a/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs b/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs index 440932de..1538d8ce 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs @@ -1,6 +1,4 @@ -using Newtonsoft.Json.Linq; -using System.IO; -using System.Net; +using System.Net; using System.Net.NetworkInformation; using v2rayN.Mode; using v2rayN.Resx; @@ -721,9 +719,8 @@ namespace v2rayN.Handler outbound.settings.userLevel = 0; } - var obj = JsonUtils.ParseJson(normalDNS) ?? []; - - if (!obj.ContainsKey("servers")) + var obj = JsonUtils.ParseJson(normalDNS); + if (obj is null) { List servers = []; string[] arrDNS = normalDNS.Split(','); @@ -731,36 +728,26 @@ namespace v2rayN.Handler { servers.Add(str); } - obj["servers"] = JArray.FromObject(servers); + obj = JsonUtils.ParseJson("{}"); + obj["servers"] = JsonUtils.SerializeToNode(servers); } + // 追加至 dns 设置 if (item.useSystemHosts) { - var hostfile = @"C:\Windows\System32\drivers\etc\hosts"; - - if (File.Exists(hostfile)) + var systemHosts = Utils.GetSystemHosts(); + if (systemHosts.Count > 0) { - var hosts = File.ReadAllText(hostfile).Replace("\r", ""); - var hostsList = hosts.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); - - // 获取系统hosts - var systemHosts = new Dictionary(); - foreach (var host in hostsList) - { - if (host.StartsWith("#")) continue; - var hostItem = host.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); - if (hostItem.Length < 2) continue; - systemHosts.Add(hostItem[1], hostItem[0]); - } - - // 追加至 dns 设置 - var normalHost = obj["hosts"] ?? new JObject(); - foreach (var host in systemHosts) + var normalHost = obj["hosts"]; + if (normalHost != null) { - if (normalHost[host.Key] != null) continue; - normalHost[host.Key] = host.Value; + foreach (var host in systemHosts) + { + if (normalHost[host.Key] != null) + continue; + normalHost[host.Key] = host.Value; + } } - obj["hosts"] = normalHost; } } diff --git a/v2rayN/v2rayN/Mode/GitHubRelease.cs b/v2rayN/v2rayN/Mode/GitHubRelease.cs index e509b10f..ddcb2582 100644 --- a/v2rayN/v2rayN/Mode/GitHubRelease.cs +++ b/v2rayN/v2rayN/Mode/GitHubRelease.cs @@ -1,68 +1,68 @@ -using Newtonsoft.Json; +using System.Text.Json.Serialization; namespace v2rayN.Mode { public class GitHubReleaseAsset { - [JsonProperty("url")] public string Url { get; set; } + [JsonPropertyName("url")] public string Url { get; set; } - [JsonProperty("id")] public int Id { get; set; } + [JsonPropertyName("id")] public int Id { get; set; } - [JsonProperty("node_id")] public string NodeId { get; set; } + [JsonPropertyName("node_id")] public string NodeId { get; set; } - [JsonProperty("name")] public string Name { get; set; } + [JsonPropertyName("name")] public string Name { get; set; } - [JsonProperty("label")] public object Label { get; set; } + [JsonPropertyName("label")] public object Label { get; set; } - [JsonProperty("content_type")] public string ContentType { get; set; } + [JsonPropertyName("content_type")] public string ContentType { get; set; } - [JsonProperty("state")] public string State { get; set; } + [JsonPropertyName("state")] public string State { get; set; } - [JsonProperty("size")] public int Size { get; set; } + [JsonPropertyName("size")] public int Size { get; set; } - [JsonProperty("download_count")] public int DownloadCount { get; set; } + [JsonPropertyName("download_count")] public int DownloadCount { get; set; } - [JsonProperty("created_at")] public DateTime CreatedAt { get; set; } + [JsonPropertyName("created_at")] public DateTime CreatedAt { get; set; } - [JsonProperty("updated_at")] public DateTime UpdatedAt { get; set; } + [JsonPropertyName("updated_at")] public DateTime UpdatedAt { get; set; } - [JsonProperty("browser_download_url")] public string BrowserDownloadUrl { get; set; } + [JsonPropertyName("browser_download_url")] public string BrowserDownloadUrl { get; set; } } public class GitHubRelease { - [JsonProperty("url")] public string Url { get; set; } + [JsonPropertyName("url")] public string Url { get; set; } - [JsonProperty("assets_url")] public string AssetsUrl { get; set; } + [JsonPropertyName("assets_url")] public string AssetsUrl { get; set; } - [JsonProperty("upload_url")] public string UploadUrl { get; set; } + [JsonPropertyName("upload_url")] public string UploadUrl { get; set; } - [JsonProperty("html_url")] public string HtmlUrl { get; set; } + [JsonPropertyName("html_url")] public string HtmlUrl { get; set; } - [JsonProperty("id")] public int Id { get; set; } + [JsonPropertyName("id")] public int Id { get; set; } - [JsonProperty("node_id")] public string NodeId { get; set; } + [JsonPropertyName("node_id")] public string NodeId { get; set; } - [JsonProperty("tag_name")] public string TagName { get; set; } + [JsonPropertyName("tag_name")] public string TagName { get; set; } - [JsonProperty("target_commitish")] public string TargetCommitish { get; set; } + [JsonPropertyName("target_commitish")] public string TargetCommitish { get; set; } - [JsonProperty("name")] public string Name { get; set; } + [JsonPropertyName("name")] public string Name { get; set; } - [JsonProperty("draft")] public bool Draft { get; set; } + [JsonPropertyName("draft")] public bool Draft { get; set; } - [JsonProperty("prerelease")] public bool Prerelease { get; set; } + [JsonPropertyName("prerelease")] public bool Prerelease { get; set; } - [JsonProperty("created_at")] public DateTime CreatedAt { get; set; } + [JsonPropertyName("created_at")] public DateTime CreatedAt { get; set; } - [JsonProperty("published_at")] public DateTime PublishedAt { get; set; } + [JsonPropertyName("published_at")] public DateTime PublishedAt { get; set; } - [JsonProperty("assets")] public List Assets { get; set; } + [JsonPropertyName("assets")] public List Assets { get; set; } - [JsonProperty("tarball_url")] public string TarballUrl { get; set; } + [JsonPropertyName("tarball_url")] public string TarballUrl { get; set; } - [JsonProperty("zipball_url")] public string ZipballUrl { get; set; } + [JsonPropertyName("zipball_url")] public string ZipballUrl { get; set; } - [JsonProperty("body")] public string Body { get; set; } + [JsonPropertyName("body")] public string Body { get; set; } } } \ No newline at end of file diff --git a/v2rayN/v2rayN/Mode/V2rayConfig.cs b/v2rayN/v2rayN/Mode/V2rayConfig.cs index d19040ad..957af112 100644 --- a/v2rayN/v2rayN/Mode/V2rayConfig.cs +++ b/v2rayN/v2rayN/Mode/V2rayConfig.cs @@ -1,4 +1,4 @@ -using Newtonsoft.Json; +using System.Text.Json.Serialization; namespace v2rayN.Mode { @@ -580,7 +580,7 @@ namespace v2rayN.Mode /// /// 用户代理 /// - [JsonProperty("User-Agent")] + [JsonPropertyName("User-Agent")] public string UserAgent { get; set; } } diff --git a/v2rayN/v2rayN/ViewModels/DNSSettingViewModel.cs b/v2rayN/v2rayN/ViewModels/DNSSettingViewModel.cs index a5cb132e..8eb7df1b 100644 --- a/v2rayN/v2rayN/ViewModels/DNSSettingViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/DNSSettingViewModel.cs @@ -64,7 +64,7 @@ namespace v2rayN.ViewModels if (!Utils.IsNullOrEmpty(normalDNS)) { var obj = JsonUtils.ParseJson(normalDNS); - if (obj != null && obj.ContainsKey("servers") == true) + if (obj != null && obj["servers"] != null) { } else