refactor: 优化配置代码结构.

This commit is contained in:
2025-01-09 01:33:38 +08:00
parent 99cb0a03a5
commit db43e05770
8 changed files with 216 additions and 17 deletions
@@ -24,7 +24,7 @@ public abstract class BaseOutboundSetting
/// <summary> /// <summary>
/// 出站代理协议类型. /// 出站代理协议类型.
/// </summary> /// </summary>
public required OutboundProtocolType Protocol { get; init; } public OutboundProtocolType Protocol { get; init; }
public BaseStreamSetting StreamSetting { get; init; } = new RawStreamSetting(); public BaseStreamSetting StreamSetting { get; init; } = new RawStreamSetting();
@@ -37,7 +37,7 @@ public abstract class BaseOutboundSetting
/// 生成对应的 OutboundObject JSON 配置. /// 生成对应的 OutboundObject JSON 配置.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public JsonObject ToJsonSetting() public virtual JsonObject ToJsonSetting()
{ {
var root = new JsonObject var root = new JsonObject
{ {
@@ -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();
}
}
@@ -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();
}
}
@@ -17,6 +17,11 @@ public class ShadowSocksOutboundSetting : BaseOutboundSetting
public required string Password { get; init; } public required string Password { get; init; }
public bool EnableUdpOverTcp { get; init; } = false; public bool EnableUdpOverTcp { get; init; } = false;
public uint? UdpOverTcpVersion { get; init; } = null; public uint? UdpOverTcpVersion { get; init; } = null;
public ShadowSocksOutboundSetting()
{
Protocol = OutboundProtocolType.Shadowsocks;
}
public override string GetTag() public override string GetTag()
{ {
@@ -27,13 +32,15 @@ public class ShadowSocksOutboundSetting : BaseOutboundSetting
{ {
var root = new JsonObject(); var root = new JsonObject();
var setting = new JsonObject(); var setting = new JsonObject
setting.Add("address", Address); {
setting.Add("port", Port); { "address", Address },
setting.Add("method", Method); { "port", Port },
setting.Add("password", Password); { "method", Method },
{ "password", Password },
{ "uot", EnableUdpOverTcp }
};
setting.Add("uot", EnableUdpOverTcp);
if (EnableUdpOverTcp) if (EnableUdpOverTcp)
{ {
setting.Add("UoTVersion", UdpOverTcpVersion); setting.Add("UoTVersion", UdpOverTcpVersion);
+112 -6
View File
@@ -1,22 +1,129 @@
namespace Proxd_man.Config; using System.Text.Json.Nodes;
namespace Proxd_man.Config;
public class ProxdConfig public class ProxdConfig
{ {
public InboundConfig Inbound { get; } = new(new HttpInboundSetting()); public InboundConfig Inbound { get; init; } = new(new HttpInboundSetting());
public SubscribeSetting Subscribe { get; } = new(); 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 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 string? BindAddress { get; } = bindAddress;
public ushort Port = port; public ushort Port = port;
public BasicInboundAuthSetting? Authentication { get; } = authentication; 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) public class BasicInboundAuthSetting(string username, string password)
@@ -28,5 +135,4 @@ public class BasicInboundAuthSetting(string username, string password)
public class SubscribeSetting public class SubscribeSetting
{ {
public List<SubscribeSource> Subscribes { get; } = []; public List<SubscribeSource> Subscribes { get; } = [];
} }
+10
View File
@@ -0,0 +1,10 @@
namespace Proxd_man;
public enum DefaultOutboundPolicy
{
Proxy,
Direct,
Block,
}
+11 -1
View File
@@ -8,5 +8,15 @@ public enum OutboundProtocolType
Trojan, Trojan,
Vless, Vless,
VMess, VMess,
WireGuard WireGuard,
/// <summary>
/// 直接发送数据, 不进行处理.
/// </summary>
Freedom,
/// <summary>
/// 丢弃所有数据.
/// </summary>
Blackhole,
} }
@@ -120,7 +120,6 @@ public class ShadowSocksLinkParser : IShareLinkParser
return new ShadowSocksOutboundSetting return new ShadowSocksOutboundSetting
{ {
Protocol = OutboundProtocolType.Shadowsocks,
Name = name ?? $"{host}:{port}", Name = name ?? $"{host}:{port}",
StreamSetting = new RawStreamSetting(), StreamSetting = new RawStreamSetting(),
@@ -177,7 +176,6 @@ public class ShadowSocksLinkParserV2 : IShareLinkParser
return new ShadowSocksOutboundSetting return new ShadowSocksOutboundSetting
{ {
Protocol = OutboundProtocolType.Shadowsocks,
Name = string.IsNullOrEmpty(shareLinkUri.Fragment) ? $"{host}:{port}" : WebUtility.UrlDecode(shareLinkUri.Fragment.Trim()), Name = string.IsNullOrEmpty(shareLinkUri.Fragment) ? $"{host}:{port}" : WebUtility.UrlDecode(shareLinkUri.Fragment.Trim()),
StreamSetting = new RawStreamSetting(), StreamSetting = new RawStreamSetting(),