From db43e05770769ace1af19a4040bbc0a4c1828d28 Mon Sep 17 00:00:00 2001 From: LamGC Date: Thu, 9 Jan 2025 01:33:38 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Config/Outbound/BaseOutboundSetting.cs | 4 +- .../Outbound/BlackholeOutboundSetting.cs | 34 +++++ .../Config/Outbound/FreedomOutboundSetting.cs | 34 +++++ .../Outbound/ShadowSocksOutboundSetting.cs | 19 ++- Proxd-man/Config/ProxdConfigs.cs | 118 +++++++++++++++++- Proxd-man/DefaultOutboundPolicy.cs | 10 ++ Proxd-man/OutboundProtocolType.cs | 12 +- Proxd-man/ShareLink/ShareLinkParserImpls.cs | 2 - 8 files changed, 216 insertions(+), 17 deletions(-) create mode 100644 Proxd-man/Config/Outbound/BlackholeOutboundSetting.cs create mode 100644 Proxd-man/Config/Outbound/FreedomOutboundSetting.cs create mode 100644 Proxd-man/DefaultOutboundPolicy.cs diff --git a/Proxd-man/Config/Outbound/BaseOutboundSetting.cs b/Proxd-man/Config/Outbound/BaseOutboundSetting.cs index c495ea0..05427ec 100644 --- a/Proxd-man/Config/Outbound/BaseOutboundSetting.cs +++ b/Proxd-man/Config/Outbound/BaseOutboundSetting.cs @@ -24,7 +24,7 @@ public abstract class BaseOutboundSetting /// /// 出站代理协议类型. /// - public required OutboundProtocolType Protocol { get; init; } + public OutboundProtocolType Protocol { get; init; } public BaseStreamSetting StreamSetting { get; init; } = new RawStreamSetting(); @@ -37,7 +37,7 @@ public abstract class BaseOutboundSetting /// 生成对应的 OutboundObject JSON 配置. /// /// - public JsonObject ToJsonSetting() + public virtual JsonObject ToJsonSetting() { var root = new JsonObject { diff --git a/Proxd-man/Config/Outbound/BlackholeOutboundSetting.cs b/Proxd-man/Config/Outbound/BlackholeOutboundSetting.cs new file mode 100644 index 0000000..af931ae --- /dev/null +++ b/Proxd-man/Config/Outbound/BlackholeOutboundSetting.cs @@ -0,0 +1,34 @@ +using System.Text.Json.Nodes; + +namespace Proxd_man.Config.Outbound; + +public class BlackholeOutboundSetting : BaseOutboundSetting +{ + + private readonly string _tag; + + public BlackholeOutboundSetting(string tag = "block") + { + Protocol = OutboundProtocolType.Blackhole; + this._tag = tag; + } + + public override JsonObject ToJsonSetting() + { + return new JsonObject + { + { "protocol", "freedom" }, + { "tag", _tag } + }; + } + + public override string GetTag() + { + return _tag; + } + + protected override JsonObject GetProtocolSetting() + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/Proxd-man/Config/Outbound/FreedomOutboundSetting.cs b/Proxd-man/Config/Outbound/FreedomOutboundSetting.cs new file mode 100644 index 0000000..29f68ef --- /dev/null +++ b/Proxd-man/Config/Outbound/FreedomOutboundSetting.cs @@ -0,0 +1,34 @@ +using System.Text.Json.Nodes; + +namespace Proxd_man.Config.Outbound; + +public class FreedomOutboundSetting : BaseOutboundSetting +{ + + private readonly string _tag; + + public FreedomOutboundSetting(string tag = "direct") + { + Protocol = OutboundProtocolType.Freedom; + this._tag = tag; + } + + public override JsonObject ToJsonSetting() + { + return new JsonObject + { + { "protocol", "freedom" }, + { "tag", _tag } + }; + } + + public override string GetTag() + { + return _tag; + } + + protected override JsonObject GetProtocolSetting() + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/Proxd-man/Config/Outbound/ShadowSocksOutboundSetting.cs b/Proxd-man/Config/Outbound/ShadowSocksOutboundSetting.cs index b92d67a..cb33604 100644 --- a/Proxd-man/Config/Outbound/ShadowSocksOutboundSetting.cs +++ b/Proxd-man/Config/Outbound/ShadowSocksOutboundSetting.cs @@ -17,6 +17,11 @@ public class ShadowSocksOutboundSetting : BaseOutboundSetting public required string Password { get; init; } public bool EnableUdpOverTcp { get; init; } = false; public uint? UdpOverTcpVersion { get; init; } = null; + + public ShadowSocksOutboundSetting() + { + Protocol = OutboundProtocolType.Shadowsocks; + } public override string GetTag() { @@ -27,13 +32,15 @@ public class ShadowSocksOutboundSetting : BaseOutboundSetting { var root = new JsonObject(); - var setting = new JsonObject(); - setting.Add("address", Address); - setting.Add("port", Port); - setting.Add("method", Method); - setting.Add("password", Password); + var setting = new JsonObject + { + { "address", Address }, + { "port", Port }, + { "method", Method }, + { "password", Password }, + { "uot", EnableUdpOverTcp } + }; - setting.Add("uot", EnableUdpOverTcp); if (EnableUdpOverTcp) { setting.Add("UoTVersion", UdpOverTcpVersion); diff --git a/Proxd-man/Config/ProxdConfigs.cs b/Proxd-man/Config/ProxdConfigs.cs index 966b50b..28a1a62 100644 --- a/Proxd-man/Config/ProxdConfigs.cs +++ b/Proxd-man/Config/ProxdConfigs.cs @@ -1,22 +1,129 @@ -namespace Proxd_man.Config; +using System.Text.Json.Nodes; + +namespace Proxd_man.Config; public class ProxdConfig { - public InboundConfig Inbound { get; } = new(new HttpInboundSetting()); - public SubscribeSetting Subscribe { get; } = new(); + public InboundConfig Inbound { get; init; } = new(new HttpInboundSetting()); + public DefaultOutboundPolicy DefaultOutboundPolicy { get; init; } = DefaultOutboundPolicy.Proxy; + public SubscribeSetting Subscribe { get; init; } = new(); } -public class InboundConfig(HttpInboundSetting? http = null) +public class InboundConfig(HttpInboundSetting? http = null, SocksInboundSetting? socks = null) { public HttpInboundSetting? Http { get; } = http; + public SocksInboundSetting? Socks { get; } = socks; } -public class HttpInboundSetting(string? bindAddress = null, ushort port = 1080, BasicInboundAuthSetting? authentication = null) +public interface IInboundSetting +{ + JsonObject ToJsonSetting(); +} + +public class HttpInboundSetting( + string? bindAddress = null, + ushort port = 1080, + BasicInboundAuthSetting? authentication = null) : IInboundSetting { public string? BindAddress { get; } = bindAddress; public ushort Port = port; public BasicInboundAuthSetting? Authentication { get; } = authentication; + + public JsonObject ToJsonSetting() + { + JsonObject setting = new() + { + { "protocol", "http" }, + { "listen", $"{BindAddress ?? "0.0.0.0"}" }, + { "port", Port }, + { "tag", "http-in" }, + { + "sniffing", new JsonObject + { + { "enabled", true }, + { "routeOnly", true }, + { "metadataOnly", true }, + { "destOverride", new JsonArray("http", "tls", "quic") } + } + } + }; + + if (Authentication != null) + { + setting.Add("settings", new JsonObject + { + { + "accounts", new JsonArray(new JsonObject + { + { "user", Authentication.Username }, + { "pass", Authentication.Password } + }) + } + }); + } + + return setting; + } +} + +public class SocksInboundSetting( + string? bindAddress = null, + ushort port = 1080, + bool enableUdp = true, + string? bindAddressOnUdp = "127.0.0.1", + BasicInboundAuthSetting? authentication = null) : IInboundSetting +{ + public string? BindAddress { get; } = bindAddress; + public ushort Port = port; + + public bool EnableUdp { get; } = enableUdp; + public string? BindAddressOnUdp { get; } = bindAddressOnUdp; + public BasicInboundAuthSetting? Authentication { get; } = authentication; + + public JsonObject ToJsonSetting() + { + JsonObject setting = new() + { + { "protocol", "http" }, + { "listen", $"{BindAddress ?? "0.0.0.0"}" }, + { "port", Port }, + { "tag", "http-in" }, + { + "sniffing", new JsonObject + { + { "enabled", true }, + { "routeOnly", true }, + { "metadataOnly", true }, + { "destOverride", new JsonArray("http", "tls", "quic") } + } + } + }; + + var settingJson = new JsonObject + { + { "udp", EnableUdp }, + { "ip", BindAddressOnUdp} + }; + + if (Authentication != null) + { + settingJson.Add("auth", "password"); + settingJson.Add("accounts", new JsonArray(new JsonObject + { + { "user", Authentication.Username }, + { "pass", Authentication.Password } + })); + } + else + { + settingJson.Add("auth", "noauth"); + } + + setting.Add("settings", settingJson); + return setting; + } + } public class BasicInboundAuthSetting(string username, string password) @@ -28,5 +135,4 @@ public class BasicInboundAuthSetting(string username, string password) public class SubscribeSetting { public List Subscribes { get; } = []; - } \ No newline at end of file diff --git a/Proxd-man/DefaultOutboundPolicy.cs b/Proxd-man/DefaultOutboundPolicy.cs new file mode 100644 index 0000000..74dbd66 --- /dev/null +++ b/Proxd-man/DefaultOutboundPolicy.cs @@ -0,0 +1,10 @@ +namespace Proxd_man; + +public enum DefaultOutboundPolicy +{ + + Proxy, + Direct, + Block, + +} \ No newline at end of file diff --git a/Proxd-man/OutboundProtocolType.cs b/Proxd-man/OutboundProtocolType.cs index bc0528a..9532d7d 100644 --- a/Proxd-man/OutboundProtocolType.cs +++ b/Proxd-man/OutboundProtocolType.cs @@ -8,5 +8,15 @@ public enum OutboundProtocolType Trojan, Vless, VMess, - WireGuard + WireGuard, + + /// + /// 直接发送数据, 不进行处理. + /// + Freedom, + + /// + /// 丢弃所有数据. + /// + Blackhole, } \ No newline at end of file diff --git a/Proxd-man/ShareLink/ShareLinkParserImpls.cs b/Proxd-man/ShareLink/ShareLinkParserImpls.cs index 1af4218..c149d44 100644 --- a/Proxd-man/ShareLink/ShareLinkParserImpls.cs +++ b/Proxd-man/ShareLink/ShareLinkParserImpls.cs @@ -120,7 +120,6 @@ public class ShadowSocksLinkParser : IShareLinkParser return new ShadowSocksOutboundSetting { - Protocol = OutboundProtocolType.Shadowsocks, Name = name ?? $"{host}:{port}", StreamSetting = new RawStreamSetting(), @@ -177,7 +176,6 @@ public class ShadowSocksLinkParserV2 : IShareLinkParser return new ShadowSocksOutboundSetting { - Protocol = OutboundProtocolType.Shadowsocks, Name = string.IsNullOrEmpty(shareLinkUri.Fragment) ? $"{host}:{port}" : WebUtility.UrlDecode(shareLinkUri.Fragment.Trim()), StreamSetting = new RawStreamSetting(),