1
0
mirror of https://github.com/proxysu/ProxySU.git synced 2024-11-29 00:26:09 +03:00
ProxySU/ProxySU_Core/Models/Developers/ConfigBuilder.cs

214 lines
8.9 KiB
C#
Raw Normal View History

2021-02-28 09:19:26 +03:00
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
2021-03-04 11:25:36 +03:00
using ProxySU_Core.Models;
2021-02-28 09:19:26 +03:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
2021-03-17 06:58:56 +03:00
namespace ProxySU_Core.Models.Developers
2021-02-28 09:19:26 +03:00
{
public class ConfigBuilder
{
public dynamic xrayConfig { get; set; }
public string CaddyConfig { get; set; }
private const string ServerLogDir = @"Templates\xray\server\00_log";
private const string ServerApiDir = @"Templates\xray\server\01_api";
private const string ServerDnsDir = @"Templates\xray\server\02_dns";
private const string ServerRoutingDir = @"Templates\xray\server\03_routing";
private const string ServerPolicyDir = @"Templates\xray\server\04_policy";
private const string ServerInboundsDir = @"Templates\xray\server\05_inbounds";
private const string ServerOutboundsDir = @"Templates\xray\server\06_outbounds";
private const string ServerTransportDir = @"Templates\xray\server\07_transport";
private const string ServerStatsDir = @"Templates\xray\server\08_stats";
private const string ServerReverseDir = @"Templates\xray\server\09_reverse";
private const string CaddyFileDir = @"Templates\xray\caddy";
public const int VLESS_TCP_Port = 1110;
public const int VLESS_WS_Port = 1111;
2021-03-17 14:36:35 +03:00
public const int VLESS_H2_Port = 1112;
public const int VLESS_mKCP_Port = 1113;
2021-02-28 09:19:26 +03:00
public const int VMESS_TCP_Port = 2110;
public const int VMESS_WS_Port = 2111;
2021-03-17 14:36:35 +03:00
public const int VMESS_H2_Port = 2112;
public const int VMESS_mKCP_Port = 2113;
2021-02-28 09:19:26 +03:00
public const int Trojan_TCP_Port = 3110;
2021-03-17 14:36:35 +03:00
public const int Trojan_WS_Port = 3111;
2021-02-28 09:19:26 +03:00
2021-03-27 06:47:02 +03:00
public const int ShadowSocksPort = 4110;
2021-02-28 09:19:26 +03:00
public static dynamic LoadXrayConfig()
{
dynamic logObj = LoadJsonObj(Path.Combine(ServerLogDir, "00_log.json"));
dynamic apiObj = LoadJsonObj(Path.Combine(ServerApiDir, "01_api.json"));
dynamic dnsObj = LoadJsonObj(Path.Combine(ServerDnsDir, "02_dns.json"));
dynamic routingObj = LoadJsonObj(Path.Combine(ServerRoutingDir, "03_routing.json"));
dynamic policyObj = LoadJsonObj(Path.Combine(ServerPolicyDir, "04_policy.json"));
dynamic inboundsObj = LoadJsonObj(Path.Combine(ServerInboundsDir, "05_inbounds.json"));
dynamic outboundsObj = LoadJsonObj(Path.Combine(ServerOutboundsDir, "06_outbounds.json"));
dynamic transportObj = LoadJsonObj(Path.Combine(ServerTransportDir, "07_transport.json"));
dynamic statsObj = LoadJsonObj(Path.Combine(ServerStatsDir, "08_stats.json"));
dynamic reverseObj = LoadJsonObj(Path.Combine(ServerReverseDir, "09_reverse.json"));
return new
{
log = logObj["log"],
2021-03-08 18:15:18 +03:00
//api = apiObj["api"], api不能为空
2021-02-28 09:19:26 +03:00
dns = dnsObj["dns"],
routing = routingObj["routing"],
policy = policyObj["policy"],
inbounds = inboundsObj["inbounds"],
outbounds = outboundsObj["outbounds"],
transport = transportObj["transport"],
stats = statsObj["stats"],
reverse = reverseObj["reverse"]
};
}
2021-03-11 07:05:50 +03:00
public static string BuildCaddyConfig(XraySettings parameters, bool useCustomWeb = false)
2021-02-28 09:19:26 +03:00
{
var caddyStr = File.ReadAllText(Path.Combine(CaddyFileDir, "base.caddyfile"));
2021-03-08 18:15:18 +03:00
caddyStr = caddyStr.Replace("##domain##", parameters.Domain);
2021-03-11 07:05:50 +03:00
if (!useCustomWeb && !string.IsNullOrEmpty(parameters.MaskDomain))
2021-02-28 09:19:26 +03:00
{
2021-03-12 10:59:20 +03:00
var prefix = "http://";
if (parameters.MaskDomain.StartsWith("https://"))
{
prefix = "https://";
}
var domain = parameters.MaskDomain
.TrimStart("http://".ToCharArray())
.TrimStart("https://".ToCharArray());
caddyStr = caddyStr.Replace("##reverse_proxy##", $"reverse_proxy {prefix}{domain} {{ \n header_up Host {domain} \n }}");
2021-02-28 09:19:26 +03:00
}
else
{
2021-03-08 18:15:18 +03:00
caddyStr = caddyStr.Replace("##reverse_proxy##", "");
2021-02-28 09:19:26 +03:00
}
return caddyStr;
}
2021-03-04 11:25:36 +03:00
public static string BuildXrayConfig(XraySettings parameters)
2021-02-28 09:19:26 +03:00
{
var xrayConfig = LoadXrayConfig();
2021-03-27 06:47:02 +03:00
var baseBound = GetBound("VLESS_TCP_XTLS.json");
2021-03-05 05:34:39 +03:00
baseBound.port = parameters.Port;
baseBound.settings.fallbacks.Add(JToken.FromObject(new
2021-02-28 09:19:26 +03:00
{
2021-03-17 14:36:35 +03:00
dest = 80
2021-03-05 05:34:39 +03:00
}));
xrayConfig.inbounds.Add(baseBound);
2021-03-17 06:58:56 +03:00
baseBound.settings.clients[0].id = parameters.UUID;
2021-03-04 11:25:36 +03:00
2021-03-26 13:13:32 +03:00
if (parameters.Types.Contains(XrayType.VLESS_WS))
2021-03-04 11:25:36 +03:00
{
2021-03-27 06:47:02 +03:00
var wsInbound = GetBound("VLESS_WS.json");
2021-03-05 05:34:39 +03:00
wsInbound.port = VLESS_WS_Port;
wsInbound.settings.clients[0].id = parameters.UUID;
wsInbound.streamSettings.wsSettings.path = parameters.VLESS_WS_Path;
baseBound.settings.fallbacks.Add(JToken.FromObject(new
2021-03-04 11:25:36 +03:00
{
dest = VLESS_WS_Port,
2021-03-18 10:54:45 +03:00
path = parameters.VLESS_WS_Path,
xver = 1,
2021-03-05 05:34:39 +03:00
}));
xrayConfig.inbounds.Add(JToken.FromObject(wsInbound));
2021-03-04 11:25:36 +03:00
}
2021-03-26 13:13:32 +03:00
if (parameters.Types.Contains(XrayType.VMESS_TCP))
2021-03-04 11:25:36 +03:00
{
2021-03-27 06:47:02 +03:00
var mtcpBound = GetBound("VMESS_TCP.json");
2021-03-05 05:34:39 +03:00
mtcpBound.port = VMESS_TCP_Port;
mtcpBound.settings.clients[0].id = parameters.UUID;
mtcpBound.streamSettings.tcpSettings.header.request.path = parameters.VMESS_TCP_Path;
baseBound.settings.fallbacks.Add(JToken.FromObject(new
2021-03-04 11:25:36 +03:00
{
dest = VMESS_TCP_Port,
path = parameters.VMESS_TCP_Path,
xver = 1,
2021-03-05 05:34:39 +03:00
}));
xrayConfig.inbounds.Add(JToken.FromObject(mtcpBound));
2021-03-04 11:25:36 +03:00
}
2021-03-26 13:13:32 +03:00
if (parameters.Types.Contains(XrayType.VMESS_WS))
2021-03-04 11:25:36 +03:00
{
2021-03-27 06:47:02 +03:00
var mwsBound = GetBound("VMESS_WS.json");
2021-03-05 05:34:39 +03:00
mwsBound.port = VMESS_WS_Port;
mwsBound.settings.clients[0].id = parameters.UUID;
mwsBound.streamSettings.wsSettings.path = parameters.VMESS_WS_Path;
baseBound.settings.fallbacks.Add(JToken.FromObject(new
2021-03-04 11:25:36 +03:00
{
dest = VMESS_WS_Port,
path = parameters.VMESS_WS_Path,
xver = 1,
2021-03-05 05:34:39 +03:00
}));
xrayConfig.inbounds.Add(JToken.FromObject(mwsBound));
2021-03-04 11:25:36 +03:00
}
2021-03-26 13:13:32 +03:00
if (parameters.Types.Contains(XrayType.Trojan_TCP))
2021-02-28 09:19:26 +03:00
{
2021-03-27 06:47:02 +03:00
var trojanTcpBound = GetBound("Trojan_TCP.json");
2021-03-05 05:34:39 +03:00
trojanTcpBound.port = Trojan_TCP_Port;
trojanTcpBound.settings.clients[0].password = parameters.TrojanPassword;
baseBound.settings.fallbacks[0] = JToken.FromObject(new
2021-03-04 11:25:36 +03:00
{
dest = Trojan_TCP_Port,
xver = 1,
2021-03-05 05:34:39 +03:00
});
xrayConfig.inbounds.Add(JToken.FromObject(trojanTcpBound));
2021-02-28 09:19:26 +03:00
}
2021-03-27 06:47:02 +03:00
if (parameters.Types.Contains(XrayType.VMESS_KCP))
{
var kcpBound = GetBound("VMESS_KCP.json");
kcpBound.port = VMESS_mKCP_Port;
kcpBound.settings.clients[0].id = parameters.UUID;
kcpBound.streamSettings.kcpSettings.header.type = parameters.VMESS_KCP_Type;
kcpBound.streamSettings.kcpSettings.seed = parameters.VMESS_KCP_Seed;
xrayConfig.inbounds.Add(JToken.FromObject(kcpBound));
}
if (parameters.Types.Contains(XrayType.ShadowsocksAEAD))
{
var ssBound = GetBound("Shadowsocks-AEAD.json");
ssBound.port = ShadowSocksPort;
ssBound.settings.clients[0].password = parameters.ShadowsocksPassword;
ssBound.settings.clients[0].method = parameters.ShadowsocksMethod;
xrayConfig.inbounds.Add(JToken.FromObject(ssBound));
}
2021-03-17 14:36:35 +03:00
2021-03-05 05:34:39 +03:00
return JsonConvert.SerializeObject(
xrayConfig,
Formatting.Indented,
new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
});
2021-02-28 09:19:26 +03:00
}
2021-03-27 06:47:02 +03:00
private static dynamic GetBound(string name)
{
return LoadJsonObj(Path.Combine(ServerInboundsDir, name));
}
2021-02-28 09:19:26 +03:00
private static dynamic LoadJsonObj(string path)
{
if (File.Exists(path))
{
var jsonStr = File.ReadAllText(path, Encoding.UTF8);
2021-03-05 05:34:39 +03:00
return JToken.FromObject(JsonConvert.DeserializeObject(jsonStr));
2021-02-28 09:19:26 +03:00
}
return null;
}
}
}