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
+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 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<SubscribeSource> Subscribes { get; } = [];
}