From 14dc1a27381ad81b208ad84c4d17c311def4be3d Mon Sep 17 00:00:00 2001 From: database64128 Date: Wed, 9 Dec 2020 17:50:06 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A1=20Cleanup=20`Shadowsocks.Interop`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Mark optional fields as nullable - Add `JsonHelper` --- Shadowsocks.Interop/SsRust/Config.cs | 16 ++----- Shadowsocks.Interop/Utils/JsonHelper.cs | 42 +++++++++++++++++ Shadowsocks.Interop/V2Ray/Config.cs | 24 +++------- Shadowsocks.Interop/V2Ray/DnsObject.cs | 7 +++ .../V2Ray/Inbound/AllocateObject.cs | 12 +++-- Shadowsocks.Interop/V2Ray/InboundObject.cs | 16 ++----- Shadowsocks.Interop/V2Ray/OutboundObject.cs | 13 ++---- .../V2Ray/Policy/LevelPolicyObject.cs | 32 ++++++------- Shadowsocks.Interop/V2Ray/PolicyObject.cs | 10 +--- .../Freedom/OutboundConfigurationObject.cs | 6 +-- .../Shadowsocks/InboundConfigurationObject.cs | 9 +--- .../Protocols/Shadowsocks/ServerObject.cs | 12 +---- .../Socks/InboundConfigurationObject.cs | 20 +++----- .../V2Ray/Protocols/Socks/ServerObject.cs | 19 +++++--- .../V2Ray/Protocols/Trojan/ClientObject.cs | 6 +-- .../V2Ray/Protocols/Trojan/FallbackObject.cs | 16 ++++--- .../V2Ray/Protocols/Trojan/ServerObject.cs | 8 +--- .../VMess/InboundConfigurationObject.cs | 6 +-- .../V2Ray/Protocols/VMess/UserObject.cs | 4 +- .../V2Ray/Reverse/BridgeObject.cs | 3 +- .../V2Ray/Routing/RuleObject.cs | 46 +++++++++---------- Shadowsocks.Interop/V2Ray/RoutingObject.cs | 8 +++- .../V2Ray/Transport/CertificateObject.cs | 24 ++++++---- .../V2Ray/Transport/SockoptObject.cs | 10 ++-- .../V2Ray/Transport/StreamSettingsObject.cs | 19 ++++---- .../V2Ray/Transport/TcpObject.cs | 5 ++ .../V2Ray/Transport/TlsObject.cs | 19 ++------ .../V2Ray/Transport/WebSocketObject.cs | 2 + Shadowsocks.Interop/V2Ray/TransportObject.cs | 22 +++------ Shadowsocks.Net/Shadowsocks.Net.csproj | 4 +- .../Shadowsocks.Protobuf.csproj | 2 +- 31 files changed, 212 insertions(+), 230 deletions(-) create mode 100644 Shadowsocks.Interop/Utils/JsonHelper.cs diff --git a/Shadowsocks.Interop/SsRust/Config.cs b/Shadowsocks.Interop/SsRust/Config.cs index b29e854e..d970e5bc 100644 --- a/Shadowsocks.Interop/SsRust/Config.cs +++ b/Shadowsocks.Interop/SsRust/Config.cs @@ -10,7 +10,6 @@ namespace Shadowsocks.Interop.SsRust public int Version { get; set; } /// - [JsonIgnore(Condition = JsonIgnoreCondition.Never)] public List Servers { get; set; } /// @@ -21,14 +20,13 @@ namespace Shadowsocks.Interop.SsRust /// /// Gets or sets the listening port. /// - [JsonIgnore(Condition = JsonIgnoreCondition.Never)] public int LocalPort { get; set; } /// /// Gets or sets the timeout for UDP associations in seconds. /// Defaults to 300 seconds (5 minutes). /// - public int UdpTimeout { get; set; } + public int? UdpTimeout { get; set; } /// /// Gets or sets the maximum number of UDP associations. @@ -39,7 +37,7 @@ namespace Shadowsocks.Interop.SsRust /// /// Gets or sets the server manager address. /// - public string ManagerAddress { get; set; } + public string? ManagerAddress { get; set; } /// /// Gets or sets the server manager port. @@ -49,7 +47,7 @@ namespace Shadowsocks.Interop.SsRust /// /// Gets or sets the DNS server used to resolve hostnames. /// - public string Dns { get; set; } + public string? Dns { get; set; } /// /// Gets or sets the mode. @@ -81,15 +79,7 @@ namespace Shadowsocks.Interop.SsRust Servers = new(); LocalAddress = ""; LocalPort = 1080; - UdpTimeout = 300; - UdpMaxAssociations = 0; - ManagerAddress = ""; - ManagerPort = 0; - Dns = ""; Mode = "tcp_only"; - NoDelay = false; - Nofile = 0; - Ipv6First = false; } /// diff --git a/Shadowsocks.Interop/Utils/JsonHelper.cs b/Shadowsocks.Interop/Utils/JsonHelper.cs new file mode 100644 index 00000000..306d66c9 --- /dev/null +++ b/Shadowsocks.Interop/Utils/JsonHelper.cs @@ -0,0 +1,42 @@ +using Shadowsocks.Models; +using System.Text.Encodings.Web; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Shadowsocks.Interop.Utils +{ + public static class JsonHelper + { + public static readonly JsonSerializerOptions camelCaseJsonSerializerOptions = new JsonSerializerOptions() + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = true, + }; + + public static readonly JsonSerializerOptions snakeCaseJsonSerializerOptions = new JsonSerializerOptions() + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + PropertyNamingPolicy = new JsonSnakeCaseNamingPolicy(), + WriteIndented = true, + }; + + public static readonly JsonSerializerOptions camelCaseJsonDeserializerOptions = new JsonSerializerOptions() + { + AllowTrailingCommas = true, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + ReadCommentHandling = JsonCommentHandling.Skip, + WriteIndented = true, + }; + + public static readonly JsonSerializerOptions snakeCaseJsonDeserializerOptions = new JsonSerializerOptions() + { + AllowTrailingCommas = true, + PropertyNamingPolicy = new JsonSnakeCaseNamingPolicy(), + ReadCommentHandling = JsonCommentHandling.Skip, + WriteIndented = true, + }; + } +} diff --git a/Shadowsocks.Interop/V2Ray/Config.cs b/Shadowsocks.Interop/V2Ray/Config.cs index bc62cf65..0e4ba9be 100644 --- a/Shadowsocks.Interop/V2Ray/Config.cs +++ b/Shadowsocks.Interop/V2Ray/Config.cs @@ -4,30 +4,22 @@ namespace Shadowsocks.Interop.V2Ray { public class Config { - public LogObject Log { get; set; } - public ApiObject Api { get; set; } - public DnsObject Dns { get; set; } + public LogObject? Log { get; set; } + public ApiObject? Api { get; set; } + public DnsObject? Dns { get; set; } public RoutingObject Routing { get; set; } - public PolicyObject Policy { get; set; } + public PolicyObject? Policy { get; set; } public InboundObject Inbounds { get; set; } public OutboundObject Outbounds { get; set; } - public TransportObject Transport { get; set; } - [JsonIgnore(Condition = JsonIgnoreCondition.Never)] + public TransportObject? Transport { get; set; } public StatsObject? Stats { get; set; } - public ReverseObject Reverse { get; set; } + public ReverseObject? Reverse { get; set; } - public Config(bool stats = true) + public Config() { - Log = new(); - Api = new(); - Dns = new(); Routing = new(); - Policy = new(); Inbounds = new(); Outbounds = new(); - Transport = new(); - Stats = stats ? new() : null; - Reverse = new(); } /// @@ -42,9 +34,7 @@ namespace Shadowsocks.Interop.V2Ray Policy = PolicyObject.Default, Inbounds = new(), Outbounds = new(), - Transport = new(), Stats = new(), - Reverse = new(), }; } } diff --git a/Shadowsocks.Interop/V2Ray/DnsObject.cs b/Shadowsocks.Interop/V2Ray/DnsObject.cs index 42327eb7..23381319 100644 --- a/Shadowsocks.Interop/V2Ray/DnsObject.cs +++ b/Shadowsocks.Interop/V2Ray/DnsObject.cs @@ -18,6 +18,13 @@ namespace Shadowsocks.Interop.V2Ray /// public List Servers { get; set; } + /// + /// Gets or sets the client IP used when sending requests to DNS server. + /// + public string? ClientIp { get; set; } + + public string? Tag { get; set; } + public DnsObject() { Hosts = new(); diff --git a/Shadowsocks.Interop/V2Ray/Inbound/AllocateObject.cs b/Shadowsocks.Interop/V2Ray/Inbound/AllocateObject.cs index 855b5d05..46bfad77 100644 --- a/Shadowsocks.Interop/V2Ray/Inbound/AllocateObject.cs +++ b/Shadowsocks.Interop/V2Ray/Inbound/AllocateObject.cs @@ -13,19 +13,23 @@ namespace Shadowsocks.Interop.V2Ray.Inbound /// Gets or sets the random port refreshing interval in minutes. /// Defaults to 5 minutes. /// - public int Refresh { get; set; } + public int? Refresh { get; set; } /// /// Gets or sets the number of random ports. /// Defaults to 3. /// - public int Concurrency { get; set; } + public int? Concurrency { get; set; } public AllocateObject() { Strategy = "always"; - Refresh = 5; - Concurrency = 3; } + + public static AllocateObject Default => new() + { + Refresh = 5, + Concurrency = 3, + }; } } diff --git a/Shadowsocks.Interop/V2Ray/InboundObject.cs b/Shadowsocks.Interop/V2Ray/InboundObject.cs index 16a394b8..94ed120d 100644 --- a/Shadowsocks.Interop/V2Ray/InboundObject.cs +++ b/Shadowsocks.Interop/V2Ray/InboundObject.cs @@ -6,24 +6,18 @@ namespace Shadowsocks.Interop.V2Ray public class InboundObject { public string Tag { get; set; } - public string Listen { get; set; } - public object Port { get; set; } + public string? Listen { get; set; } + public object? Port { get; set; } public string Protocol { get; set; } public object? Settings { get; set; } - public StreamSettingsObject StreamSettings { get; set; } - public SniffingObject Sniffing { get; set; } - public AllocateObject Allocate { get; set; } + public StreamSettingsObject? StreamSettings { get; set; } + public SniffingObject? Sniffing { get; set; } + public AllocateObject? Allocate { get; set; } public InboundObject() { Tag = ""; - Listen = "0.0.0.0"; - Port = ""; Protocol = ""; - Settings = null; - StreamSettings = new(); - Sniffing = new(); - Allocate = new(); } public static InboundObject DefaultLocalSocks => new() diff --git a/Shadowsocks.Interop/V2Ray/OutboundObject.cs b/Shadowsocks.Interop/V2Ray/OutboundObject.cs index c94e3b96..6bc15290 100644 --- a/Shadowsocks.Interop/V2Ray/OutboundObject.cs +++ b/Shadowsocks.Interop/V2Ray/OutboundObject.cs @@ -8,22 +8,17 @@ namespace Shadowsocks.Interop.V2Ray public class OutboundObject { public string Tag { get; set; } - public string SendThrough { get; set; } + public string? SendThrough { get; set; } public string Protocol { get; set; } public object? Settings { get; set; } - public StreamSettingsObject StreamSettings { get; set; } - public ProxySettingsObject ProxySettings { get; set; } - public MuxObject Mux { get; set; } + public StreamSettingsObject? StreamSettings { get; set; } + public ProxySettingsObject? ProxySettings { get; set; } + public MuxObject? Mux { get; set; } public OutboundObject() { Tag = ""; - SendThrough = "0.0.0.0"; Protocol = ""; - Settings = null; - StreamSettings = new(); - ProxySettings = new(); - Mux = new(); } /// diff --git a/Shadowsocks.Interop/V2Ray/Policy/LevelPolicyObject.cs b/Shadowsocks.Interop/V2Ray/Policy/LevelPolicyObject.cs index 6cd9f794..d043e42b 100644 --- a/Shadowsocks.Interop/V2Ray/Policy/LevelPolicyObject.cs +++ b/Shadowsocks.Interop/V2Ray/Policy/LevelPolicyObject.cs @@ -2,23 +2,23 @@ namespace Shadowsocks.Interop.V2Ray.Policy { public class LevelPolicyObject { - public int Handshake { get; set; } - public int ConnIdle { get; set; } - public int UplinkOnly { get; set; } - public int DownlinkOnly { get; set; } - public bool StatsUserUplink { get; set; } - public bool StatsUserDownlink { get; set; } - public int BufferSize { get; set; } + public int? Handshake { get; set; } + public int? ConnIdle { get; set; } + public int? UplinkOnly { get; set; } + public int? DownlinkOnly { get; set; } + public bool? StatsUserUplink { get; set; } + public bool? StatsUserDownlink { get; set; } + public int? BufferSize { get; set; } - public LevelPolicyObject() + public static LevelPolicyObject Default => new() { - Handshake = 4; - ConnIdle = 300; - UplinkOnly = 2; - DownlinkOnly = 5; - StatsUserUplink = false; - StatsUserDownlink = false; - BufferSize = 512; - } + Handshake = 4, + ConnIdle = 300, + UplinkOnly = 2, + DownlinkOnly = 5, + StatsUserUplink = false, + StatsUserDownlink = false, + BufferSize = 512, + }; } } diff --git a/Shadowsocks.Interop/V2Ray/PolicyObject.cs b/Shadowsocks.Interop/V2Ray/PolicyObject.cs index f4a4afe5..684af256 100644 --- a/Shadowsocks.Interop/V2Ray/PolicyObject.cs +++ b/Shadowsocks.Interop/V2Ray/PolicyObject.cs @@ -5,14 +5,8 @@ namespace Shadowsocks.Interop.V2Ray { public class PolicyObject { - public Dictionary Levels { get; set; } - public SystemPolicyObject System { get; set; } - - public PolicyObject() - { - Levels = new(); - System = new(); - } + public Dictionary? Levels { get; set; } + public SystemPolicyObject? System { get; set; } /// /// Gets the default policy object. diff --git a/Shadowsocks.Interop/V2Ray/Protocols/Freedom/OutboundConfigurationObject.cs b/Shadowsocks.Interop/V2Ray/Protocols/Freedom/OutboundConfigurationObject.cs index ae92c680..842cad81 100644 --- a/Shadowsocks.Interop/V2Ray/Protocols/Freedom/OutboundConfigurationObject.cs +++ b/Shadowsocks.Interop/V2Ray/Protocols/Freedom/OutboundConfigurationObject.cs @@ -3,14 +3,12 @@ namespace Shadowsocks.Interop.V2Ray.Protocols.Freedom public class OutboundConfigurationObject { public string DomainStrategy { get; set; } - public string Redirect { get; set; } - public int UserLevel { get; set; } + public string? Redirect { get; set; } + public int? UserLevel { get; set; } public OutboundConfigurationObject() { DomainStrategy = "AsIs"; - Redirect = ""; - UserLevel = 0; } } } diff --git a/Shadowsocks.Interop/V2Ray/Protocols/Shadowsocks/InboundConfigurationObject.cs b/Shadowsocks.Interop/V2Ray/Protocols/Shadowsocks/InboundConfigurationObject.cs index 5e988617..842dc707 100644 --- a/Shadowsocks.Interop/V2Ray/Protocols/Shadowsocks/InboundConfigurationObject.cs +++ b/Shadowsocks.Interop/V2Ray/Protocols/Shadowsocks/InboundConfigurationObject.cs @@ -5,25 +5,20 @@ namespace Shadowsocks.Interop.V2Ray.Protocols.Shadowsocks { public class InboundConfigurationObject { - public string Email { get; set; } + public string? Email { get; set; } - [JsonIgnore(Condition = JsonIgnoreCondition.Never)] public string Method { get; set; } - [JsonIgnore(Condition = JsonIgnoreCondition.Never)] public string Password { get; set; } - public int Level { get; set; } + public int? Level { get; set; } - [JsonIgnore(Condition = JsonIgnoreCondition.Never)] public string Network { get; set; } public InboundConfigurationObject() { - Email = ""; Method = "chacha20-ietf-poly1305"; Password = new Guid().ToString(); - Level = 0; Network = "tcp,udp"; } } diff --git a/Shadowsocks.Interop/V2Ray/Protocols/Shadowsocks/ServerObject.cs b/Shadowsocks.Interop/V2Ray/Protocols/Shadowsocks/ServerObject.cs index 798d57a4..504df42e 100644 --- a/Shadowsocks.Interop/V2Ray/Protocols/Shadowsocks/ServerObject.cs +++ b/Shadowsocks.Interop/V2Ray/Protocols/Shadowsocks/ServerObject.cs @@ -4,40 +4,32 @@ namespace Shadowsocks.Interop.V2Ray.Protocols.Shadowsocks { public class ServerObject { - public string Email { get; set; } + public string? Email { get; set; } - [JsonIgnore(Condition = JsonIgnoreCondition.Never)] public string Address { get; set; } - [JsonIgnore(Condition = JsonIgnoreCondition.Never)] public int Port { get; set; } - [JsonIgnore(Condition = JsonIgnoreCondition.Never)] public string Method { get; set; } - [JsonIgnore(Condition = JsonIgnoreCondition.Never)] public string Password { get; set; } - public int Level { get; set; } + public int? Level { get; set; } public ServerObject() { - Email = ""; Address = ""; Port = 8388; Method = "chacha20-ietf-poly1305"; Password = ""; - Level = 0; } public ServerObject(string address, int port, string method, string password) { - Email = ""; Address = address; Port = port; Method = method; Password = password; - Level = 0; } } } diff --git a/Shadowsocks.Interop/V2Ray/Protocols/Socks/InboundConfigurationObject.cs b/Shadowsocks.Interop/V2Ray/Protocols/Socks/InboundConfigurationObject.cs index 06478b9e..5ac1f5d8 100644 --- a/Shadowsocks.Interop/V2Ray/Protocols/Socks/InboundConfigurationObject.cs +++ b/Shadowsocks.Interop/V2Ray/Protocols/Socks/InboundConfigurationObject.cs @@ -4,24 +4,16 @@ namespace Shadowsocks.Interop.V2Ray.Protocols.Socks { public class InboundConfigurationObject { - public string Auth { get; set; } - public List Accounts { get; set; } - public bool Udp { get; set; } - public string Ip { get; set; } - public int UserLevel { get; set; } - - public InboundConfigurationObject() - { - Auth = "noauth"; - Accounts = new(); - Udp = false; - Ip = "127.0.0.1"; - UserLevel = 0; - } + public string? Auth { get; set; } + public List? Accounts { get; set; } + public bool? Udp { get; set; } + public string? Ip { get; set; } + public int? UserLevel { get; set; } public static InboundConfigurationObject Default => new() { Udp = true, + Ip = "127.0.0.1", }; } } diff --git a/Shadowsocks.Interop/V2Ray/Protocols/Socks/ServerObject.cs b/Shadowsocks.Interop/V2Ray/Protocols/Socks/ServerObject.cs index ec1eea68..f8dae1e5 100644 --- a/Shadowsocks.Interop/V2Ray/Protocols/Socks/ServerObject.cs +++ b/Shadowsocks.Interop/V2Ray/Protocols/Socks/ServerObject.cs @@ -7,27 +7,32 @@ namespace Shadowsocks.Interop.V2Ray.Protocols.Socks { public string Address { get; set; } public int Port { get; set; } - public List Users { get; set; } + public List? Users { get; set; } public ServerObject() { Address = ""; Port = 0; - Users = new(); } - public ServerObject(DnsEndPoint socksEndPoint, string username = "", string password = "") + public ServerObject(DnsEndPoint socksEndPoint, string? username = null, string? password = null) { Address = socksEndPoint.Host; Port = socksEndPoint.Port; Users = new(); var hasCredential = !string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password); if (hasCredential) - Users.Add(new() + { + var user = new UserObject() + { + User = username!, // null check already performed at line 23. + Pass = password!, + }; + Users = new() { - User = username, - Pass = password, - }); + user, + }; + } } } } diff --git a/Shadowsocks.Interop/V2Ray/Protocols/Trojan/ClientObject.cs b/Shadowsocks.Interop/V2Ray/Protocols/Trojan/ClientObject.cs index 898edf54..c5f88921 100644 --- a/Shadowsocks.Interop/V2Ray/Protocols/Trojan/ClientObject.cs +++ b/Shadowsocks.Interop/V2Ray/Protocols/Trojan/ClientObject.cs @@ -3,14 +3,12 @@ namespace Shadowsocks.Interop.V2Ray.Protocols.Trojan public class ClientObject { public string Password { get; set; } - public string Email { get; set; } - public int Level { get; set; } + public string? Email { get; set; } + public int? Level { get; set; } public ClientObject() { Password = ""; - Email = ""; - Level = 0; } } } diff --git a/Shadowsocks.Interop/V2Ray/Protocols/Trojan/FallbackObject.cs b/Shadowsocks.Interop/V2Ray/Protocols/Trojan/FallbackObject.cs index 93fc0ced..fef8e682 100644 --- a/Shadowsocks.Interop/V2Ray/Protocols/Trojan/FallbackObject.cs +++ b/Shadowsocks.Interop/V2Ray/Protocols/Trojan/FallbackObject.cs @@ -2,17 +2,21 @@ namespace Shadowsocks.Interop.V2Ray.Protocols.Trojan { public class FallbackObject { - public string Alpn { get; set; } - public string Path { get; set; } + public string? Alpn { get; set; } + public string? Path { get; set; } public object Dest { get; set; } - public int Xver { get; set; } + public int? Xver { get; set; } public FallbackObject() { - Alpn = ""; - Path = ""; Dest = 0; - Xver = 0; } + + public static FallbackObject Default => new() + { + Alpn = "", + Path = "", + Xver = 0, + }; } } diff --git a/Shadowsocks.Interop/V2Ray/Protocols/Trojan/ServerObject.cs b/Shadowsocks.Interop/V2Ray/Protocols/Trojan/ServerObject.cs index ac75c56d..97c41679 100644 --- a/Shadowsocks.Interop/V2Ray/Protocols/Trojan/ServerObject.cs +++ b/Shadowsocks.Interop/V2Ray/Protocols/Trojan/ServerObject.cs @@ -5,16 +5,14 @@ namespace Shadowsocks.Interop.V2Ray.Protocols.Trojan public string Address { get; set; } public int Port { get; set; } public string Password { get; set; } - public string Email { get; set; } - public int Level { get; set; } + public string? Email { get; set; } + public int? Level { get; set; } public ServerObject() { Address = ""; Port = 0; Password = ""; - Email = ""; - Level = 0; } public ServerObject(string address, int port, string password) @@ -22,8 +20,6 @@ namespace Shadowsocks.Interop.V2Ray.Protocols.Trojan Address = address; Port = port; Password = password; - Email = ""; - Level = 0; } } } diff --git a/Shadowsocks.Interop/V2Ray/Protocols/VMess/InboundConfigurationObject.cs b/Shadowsocks.Interop/V2Ray/Protocols/VMess/InboundConfigurationObject.cs index 3b794d62..4468e719 100644 --- a/Shadowsocks.Interop/V2Ray/Protocols/VMess/InboundConfigurationObject.cs +++ b/Shadowsocks.Interop/V2Ray/Protocols/VMess/InboundConfigurationObject.cs @@ -5,14 +5,12 @@ namespace Shadowsocks.Interop.V2Ray.Protocols.VMess public class InboundConfigurationObject { public List Clients { get; set; } - public UserObject Default { get; set; } - public DetourObject Detour { get; set; } + public UserObject? Default { get; set; } + public DetourObject? Detour { get; set; } public InboundConfigurationObject() { Clients = new(); - Default = new(); - Detour = new(); } } } diff --git a/Shadowsocks.Interop/V2Ray/Protocols/VMess/UserObject.cs b/Shadowsocks.Interop/V2Ray/Protocols/VMess/UserObject.cs index 1e29e086..1162cbbd 100644 --- a/Shadowsocks.Interop/V2Ray/Protocols/VMess/UserObject.cs +++ b/Shadowsocks.Interop/V2Ray/Protocols/VMess/UserObject.cs @@ -8,14 +8,12 @@ namespace Shadowsocks.Interop.V2Ray.Protocols.VMess public class UserObject { public string Id { get; set; } - public string Email { get; set; } + public string? Email { get; set; } public int Level { get; set; } public UserObject(string id = "") { Id = id; - Email = ""; - Level = 0; } public static UserObject Default => new() diff --git a/Shadowsocks.Interop/V2Ray/Reverse/BridgeObject.cs b/Shadowsocks.Interop/V2Ray/Reverse/BridgeObject.cs index f8a33bf6..d9bef236 100644 --- a/Shadowsocks.Interop/V2Ray/Reverse/BridgeObject.cs +++ b/Shadowsocks.Interop/V2Ray/Reverse/BridgeObject.cs @@ -11,12 +11,11 @@ namespace Shadowsocks.Interop.V2Ray.Reverse /// Gets or sets the domain name for the bridge. /// Can be omitted. /// - public string Domain { get; set; } + public string? Domain { get; set; } public BridgeObject() { Tag = ""; - Domain = ""; } } } diff --git a/Shadowsocks.Interop/V2Ray/Routing/RuleObject.cs b/Shadowsocks.Interop/V2Ray/Routing/RuleObject.cs index 20a7d2ec..d2a2e3a2 100644 --- a/Shadowsocks.Interop/V2Ray/Routing/RuleObject.cs +++ b/Shadowsocks.Interop/V2Ray/Routing/RuleObject.cs @@ -7,34 +7,32 @@ namespace Shadowsocks.Interop.V2Ray.Routing { [JsonIgnore(Condition = JsonIgnoreCondition.Never)] public string Type { get; set; } - public List Domain { get; set; } - public List Ip { get; set; } - public object Port { get; set; } - public object SourcePort { get; set; } - public string Network { get; set; } - public List Source { get; set; } - public List User { get; set; } - public List InboundTag { get; set; } - public List Protocol { get; set; } - public string Attrs { get; set; } - public string OutboundTag { get; set; } - public string BalancerTag { get; set; } + public List? Domain { get; set; } + public List? Ip { get; set; } + public object? Port { get; set; } + public object? SourcePort { get; set; } + public string? Network { get; set; } + public List? Source { get; set; } + public List? User { get; set; } + public List? InboundTag { get; set; } + public List? Protocol { get; set; } + public string? Attrs { get; set; } + public string? OutboundTag { get; set; } + public string? BalancerTag { get; set; } public RuleObject() { Type = "field"; - Domain = new(); - Ip = new(); - Port = ""; - SourcePort = ""; - Network = ""; - Source = new(); - User = new(); - InboundTag = new(); - Protocol = new(); - Attrs = ""; - OutboundTag = ""; - BalancerTag = ""; } + + public static RuleObject DefaultOutbound => new() + { + OutboundTag = "", + }; + + public static RuleObject DefaultBalancer => new() + { + BalancerTag = "", + }; } } diff --git a/Shadowsocks.Interop/V2Ray/RoutingObject.cs b/Shadowsocks.Interop/V2Ray/RoutingObject.cs index d27bfb22..97aaaea5 100644 --- a/Shadowsocks.Interop/V2Ray/RoutingObject.cs +++ b/Shadowsocks.Interop/V2Ray/RoutingObject.cs @@ -20,13 +20,17 @@ namespace Shadowsocks.Interop.V2Ray /// /// Gets or sets the list of load balancers. /// - public List Balancers { get; set; } + public List? Balancers { get; set; } public RoutingObject() { DomainStrategy = "AsIs"; Rules = new(); - Balancers = new(); } + + public static RoutingObject DefaultBalancers => new() + { + Balancers = new(), + }; } } diff --git a/Shadowsocks.Interop/V2Ray/Transport/CertificateObject.cs b/Shadowsocks.Interop/V2Ray/Transport/CertificateObject.cs index ae9ee8c3..b17ec086 100644 --- a/Shadowsocks.Interop/V2Ray/Transport/CertificateObject.cs +++ b/Shadowsocks.Interop/V2Ray/Transport/CertificateObject.cs @@ -5,18 +5,26 @@ namespace Shadowsocks.Interop.V2Ray.Transport public class CertificateObject { public string Usage { get; set; } - public string CertificateFile { get; set; } - public string KeyFile { get; set; } - public List Certificate { get; set; } - public List Key { get; set; } + public string? CertificateFile { get; set; } + public string? KeyFile { get; set; } + public List? Certificate { get; set; } + public List? Key { get; set; } public CertificateObject() { Usage = "encipherment"; - CertificateFile = ""; - KeyFile = ""; - Certificate = new(); - Key = new(); } + + public static CertificateObject DefaultFromFile => new() + { + CertificateFile = "", + KeyFile = "", + }; + + public static CertificateObject DefaultEmbedded => new() + { + Certificate = new(), + Key = new(), + }; } } diff --git a/Shadowsocks.Interop/V2Ray/Transport/SockoptObject.cs b/Shadowsocks.Interop/V2Ray/Transport/SockoptObject.cs index dec18a3b..cb97dfe8 100644 --- a/Shadowsocks.Interop/V2Ray/Transport/SockoptObject.cs +++ b/Shadowsocks.Interop/V2Ray/Transport/SockoptObject.cs @@ -4,13 +4,11 @@ namespace Shadowsocks.Interop.V2Ray.Transport { public int Mark { get; set; } public bool TcpFastOpen { get; set; } - public string Tproxy { get; set; } + public string? Tproxy { get; set; } - public SockoptObject() + public static SockoptObject DefaultLinux => new() { - Mark = 0; - TcpFastOpen = false; - Tproxy = "off"; - } + Tproxy = "off", + }; } } diff --git a/Shadowsocks.Interop/V2Ray/Transport/StreamSettingsObject.cs b/Shadowsocks.Interop/V2Ray/Transport/StreamSettingsObject.cs index 2109ae7f..197e4f0e 100644 --- a/Shadowsocks.Interop/V2Ray/Transport/StreamSettingsObject.cs +++ b/Shadowsocks.Interop/V2Ray/Transport/StreamSettingsObject.cs @@ -7,24 +7,23 @@ namespace Shadowsocks.Interop.V2Ray.Transport /// Defaults to "tcp". /// Available values: "tcp" | "kcp" | "ws" | "http" | "domainsocket" | "quic" /// - public string Network { get; set; } + public string? Network { get; set; } /// /// Gets or sets the transport encryption type. /// Defaults to "none" (no encryption). /// Available values: "none" | "tls" /// - public string Security { get; set; } + public string? Security { get; set; } - public TlsObject TlsSettings { get; set; } - public SockoptObject Sockopt { get; set; } + public TlsObject? TlsSettings { get; set; } + public SockoptObject? Sockopt { get; set; } - public StreamSettingsObject() + public static StreamSettingsObject DefaultWsTls => new() { - Network = "tcp"; - Security = "none"; - TlsSettings = new(); - Sockopt = new(); - } + Network = "ws", + Security = "tls", + TlsSettings = new(), + }; } } diff --git a/Shadowsocks.Interop/V2Ray/Transport/TcpObject.cs b/Shadowsocks.Interop/V2Ray/Transport/TcpObject.cs index eada550a..0c8467e8 100644 --- a/Shadowsocks.Interop/V2Ray/Transport/TcpObject.cs +++ b/Shadowsocks.Interop/V2Ray/Transport/TcpObject.cs @@ -19,5 +19,10 @@ namespace Shadowsocks.Interop.V2Ray.Transport AcceptProxyProtocol = false; Header = new HeaderObject(); } + + public static TcpObject DefaultHttp => new() + { + Header = new HttpHeaderObject(), + }; } } diff --git a/Shadowsocks.Interop/V2Ray/Transport/TlsObject.cs b/Shadowsocks.Interop/V2Ray/Transport/TlsObject.cs index f1f60691..8d829b26 100644 --- a/Shadowsocks.Interop/V2Ray/Transport/TlsObject.cs +++ b/Shadowsocks.Interop/V2Ray/Transport/TlsObject.cs @@ -4,23 +4,10 @@ namespace Shadowsocks.Interop.V2Ray.Transport { public class TlsObject { - public string ServerName { get; set; } + public string? ServerName { get; set; } public bool AllowInsecure { get; set; } - public List Alpn { get; set; } - public List Certificates { get; set; } + public List? Alpn { get; set; } + public List? Certificates { get; set; } public bool DisableSystemRoot { get; set; } - - public TlsObject() - { - ServerName = ""; - AllowInsecure = false; - Alpn = new() - { - "h2", - "http/1.1", - }; - Certificates = new(); - DisableSystemRoot = false; - } } } diff --git a/Shadowsocks.Interop/V2Ray/Transport/WebSocketObject.cs b/Shadowsocks.Interop/V2Ray/Transport/WebSocketObject.cs index ff3a303d..3dbbce96 100644 --- a/Shadowsocks.Interop/V2Ray/Transport/WebSocketObject.cs +++ b/Shadowsocks.Interop/V2Ray/Transport/WebSocketObject.cs @@ -11,11 +11,13 @@ namespace Shadowsocks.Interop.V2Ray.Transport /// /// Gets or sets the HTTP query path. + /// Defaults to "/". /// public string Path { get; set; } /// /// Gets or sets HTTP header key-value pairs. + /// Defaults to empty. /// public Dictionary Headers { get; set; } diff --git a/Shadowsocks.Interop/V2Ray/TransportObject.cs b/Shadowsocks.Interop/V2Ray/TransportObject.cs index cf151fa6..78d4ea8b 100644 --- a/Shadowsocks.Interop/V2Ray/TransportObject.cs +++ b/Shadowsocks.Interop/V2Ray/TransportObject.cs @@ -4,21 +4,11 @@ namespace Shadowsocks.Interop.V2Ray { public class TransportObject { - public TcpObject TcpSettings { get; set; } - public KcpObject KcpSettings { get; set; } - public WebSocketObject WsSettings { get; set; } - public HttpObject HttpSettings { get; set; } - public QuicObject QuicSettings { get; set; } - public DomainSocketObject DsSettings { get; set; } - - public TransportObject() - { - TcpSettings = new(); - KcpSettings = new(); - WsSettings = new(); - HttpSettings = new(); - QuicSettings = new(); - DsSettings = new(); - } + public TcpObject? TcpSettings { get; set; } + public KcpObject? KcpSettings { get; set; } + public WebSocketObject? WsSettings { get; set; } + public HttpObject? HttpSettings { get; set; } + public QuicObject? QuicSettings { get; set; } + public DomainSocketObject? DsSettings { get; set; } } } diff --git a/Shadowsocks.Net/Shadowsocks.Net.csproj b/Shadowsocks.Net/Shadowsocks.Net.csproj index 233b8b68..836c9791 100644 --- a/Shadowsocks.Net/Shadowsocks.Net.csproj +++ b/Shadowsocks.Net/Shadowsocks.Net.csproj @@ -5,8 +5,8 @@ - - + + diff --git a/Shadowsocks.Protobuf/Shadowsocks.Protobuf.csproj b/Shadowsocks.Protobuf/Shadowsocks.Protobuf.csproj index 6775442b..af162ea3 100644 --- a/Shadowsocks.Protobuf/Shadowsocks.Protobuf.csproj +++ b/Shadowsocks.Protobuf/Shadowsocks.Protobuf.csproj @@ -5,7 +5,7 @@ - +