1
0
mirror of https://github.com/proxysu/ProxySU.git synced 2024-12-01 09:36:06 +03:00
ProxySU/ProxySuper.Core/Services/XrayConfigBuilder.cs

292 lines
12 KiB
C#
Raw Normal View History

2021-05-13 04:58:45 +03:00
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
2021-05-14 14:07:19 +03:00
using ProxySuper.Core.Models.Projects;
2021-05-13 04:58:45 +03:00
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace ProxySuper.Core.Services
{
public class XrayConfigBuilder
{
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 static int VLESS_TCP_Port = 1110;
public static int VLESS_WS_Port = 1111;
public static int VLESS_H2_Port = 1112;
public static int VMESS_TCP_Port = 1210;
public static int VMESS_WS_Port = 1211;
public static int VMESS_H2_Port = 1212;
public static int Trojan_TCP_Port = 1310;
public static int Trojan_WS_Port = 1311;
public static int FullbackPort = 8080;
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"],
//api = apiObj["api"], api不能为空
dns = dnsObj["dns"],
routing = routingObj["routing"],
policy = policyObj["policy"],
inbounds = inboundsObj["inbounds"],
outbounds = outboundsObj["outbounds"],
transport = transportObj["transport"],
stats = statsObj["stats"],
reverse = reverseObj["reverse"]
};
}
public static string BuildCaddyConfig(XraySettings parameters, bool useCustomWeb = false)
{
var caddyStr = File.ReadAllText(Path.Combine(CaddyFileDir, "base.caddyfile"));
2021-07-13 06:05:10 +03:00
caddyStr = caddyStr.Replace("##domain##", parameters.IsIPAddress ? "" : parameters.Domain);
2021-05-13 04:58:45 +03:00
caddyStr = caddyStr.Replace("##port##", FullbackPort.ToString());
if (!useCustomWeb && !string.IsNullOrEmpty(parameters.MaskDomain))
{
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 }}");
}
else
{
caddyStr = caddyStr.Replace("##reverse_proxy##", "");
}
return caddyStr;
}
2022-11-23 17:06:09 +03:00
private static void SetClients(dynamic bound, List<string> uuidList, bool withXtls = false, string flow = "")
2021-06-30 13:36:27 +03:00
{
bound.settings.clients.Clear();
uuidList.ForEach(id =>
{
object obj;
if (!withXtls)
{
obj = new { id = id };
}
else
{
2022-11-23 17:06:09 +03:00
obj = new { id = id, flow = flow };
2021-06-30 13:36:27 +03:00
}
bound.settings.clients.Add(JToken.FromObject(obj));
});
}
2021-05-13 04:58:45 +03:00
public static string BuildXrayConfig(XraySettings parameters)
{
2022-02-28 05:07:34 +03:00
var uuidList = new List<string>();
uuidList.Add(parameters.UUID);
uuidList.AddRange(parameters.MulitUUID);
2021-06-30 13:36:27 +03:00
2021-05-13 04:58:45 +03:00
var xrayConfig = LoadXrayConfig();
2021-07-13 06:05:10 +03:00
var baseBound = GetBound("VLESS_TCP_XTLS.json");
baseBound.port = parameters.Port;
baseBound.settings.fallbacks.Add(JToken.FromObject(new
2021-05-13 04:58:45 +03:00
{
2021-07-13 06:05:10 +03:00
dest = FullbackPort
}));
xrayConfig.inbounds.Add(baseBound);
2022-11-23 17:06:09 +03:00
SetClients(baseBound, uuidList, withXtls: true, flow: parameters.Flow);
2021-07-13 06:05:10 +03:00
#region Fullbacks
2021-08-20 06:52:45 +03:00
if (parameters.Types.Contains(RayType.VLESS_WS))
2021-07-13 06:05:10 +03:00
{
var wsInbound = GetBound("VLESS_WS.json");
wsInbound.port = VLESS_WS_Port;
SetClients(wsInbound, uuidList);
wsInbound.streamSettings.wsSettings.path = parameters.VLESS_WS_Path;
2021-05-13 04:58:45 +03:00
baseBound.settings.fallbacks.Add(JToken.FromObject(new
{
2021-07-13 06:05:10 +03:00
dest = VLESS_WS_Port,
path = parameters.VLESS_WS_Path,
xver = 1,
2021-05-13 04:58:45 +03:00
}));
2021-07-13 06:05:10 +03:00
xrayConfig.inbounds.Add(JToken.FromObject(wsInbound));
}
2021-07-12 12:58:51 +03:00
2021-08-20 06:52:45 +03:00
if (parameters.Types.Contains(RayType.VMESS_TCP))
2021-07-13 06:05:10 +03:00
{
var mtcpBound = GetBound("VMESS_TCP.json");
mtcpBound.port = VMESS_TCP_Port;
SetClients(mtcpBound, uuidList);
mtcpBound.streamSettings.tcpSettings.header.request.path = parameters.VMESS_TCP_Path;
baseBound.settings.fallbacks.Add(JToken.FromObject(new
2021-07-12 12:58:51 +03:00
{
2021-07-13 06:05:10 +03:00
dest = VMESS_TCP_Port,
path = parameters.VMESS_TCP_Path,
xver = 1,
}));
xrayConfig.inbounds.Add(JToken.FromObject(mtcpBound));
}
2021-07-12 12:58:51 +03:00
2021-08-20 06:52:45 +03:00
if (parameters.Types.Contains(RayType.VMESS_WS))
2021-07-13 06:05:10 +03:00
{
var mwsBound = GetBound("VMESS_WS.json");
mwsBound.port = VMESS_WS_Port;
SetClients(mwsBound, uuidList);
mwsBound.streamSettings.wsSettings.path = parameters.VMESS_WS_Path;
baseBound.settings.fallbacks.Add(JToken.FromObject(new
2021-07-12 12:58:51 +03:00
{
2021-07-13 06:05:10 +03:00
dest = VMESS_WS_Port,
path = parameters.VMESS_WS_Path,
xver = 1,
}));
xrayConfig.inbounds.Add(JToken.FromObject(mwsBound));
}
2021-07-12 12:58:51 +03:00
2021-08-20 06:52:45 +03:00
if (parameters.Types.Contains(RayType.Trojan_TCP))
2021-07-13 06:05:10 +03:00
{
var trojanTcpBound = GetBound("Trojan_TCP.json");
trojanTcpBound.port = Trojan_TCP_Port;
trojanTcpBound.settings.clients[0].password = parameters.TrojanPassword;
trojanTcpBound.settings.fallbacks[0].dest = FullbackPort;
baseBound.settings.fallbacks[0] = JToken.FromObject(new
2021-07-12 12:58:51 +03:00
{
2021-07-13 06:05:10 +03:00
dest = Trojan_TCP_Port,
xver = 1,
});
xrayConfig.inbounds.Add(JToken.FromObject(trojanTcpBound));
2021-05-13 04:58:45 +03:00
}
2021-07-13 06:05:10 +03:00
#endregion
2021-05-13 04:58:45 +03:00
2022-11-24 16:07:34 +03:00
#region VLESS GRPC
2021-08-20 06:52:45 +03:00
if (parameters.Types.Contains(RayType.VLESS_gRPC))
2021-05-13 04:58:45 +03:00
{
var gRPCInBound = GetBound("VLESS_gRPC.json");
2021-06-30 06:56:13 +03:00
gRPCInBound.port = parameters.VLESS_gRPC_Port;
2021-06-30 13:36:27 +03:00
SetClients(gRPCInBound, uuidList);
2021-05-13 04:58:45 +03:00
gRPCInBound.streamSettings.grpcSettings.serviceName = parameters.VLESS_gRPC_ServiceName;
2021-06-29 13:43:15 +03:00
gRPCInBound.streamSettings.tlsSettings.serverName = parameters.Domain;
2021-05-13 04:58:45 +03:00
xrayConfig.inbounds.Add(JToken.FromObject(gRPCInBound));
}
2022-11-24 16:07:34 +03:00
#endregion
2021-05-13 04:58:45 +03:00
2022-11-24 16:07:34 +03:00
#region VLESS KCP
2021-08-20 06:52:45 +03:00
if (parameters.Types.Contains(RayType.VLESS_KCP))
2021-05-13 04:58:45 +03:00
{
var kcpBound = GetBound("VLESS_KCP.json");
kcpBound.port = parameters.VLESS_KCP_Port;
2021-06-30 13:36:27 +03:00
SetClients(kcpBound, uuidList);
2021-05-13 04:58:45 +03:00
kcpBound.streamSettings.kcpSettings.header.type = parameters.VLESS_KCP_Type;
kcpBound.streamSettings.kcpSettings.seed = parameters.VLESS_KCP_Seed;
xrayConfig.inbounds.Add(JToken.FromObject(kcpBound));
}
2022-11-24 16:07:34 +03:00
#endregion
#region VLESS QUIC
if (parameters.Types.Contains(RayType.VLESS_QUIC))
{
var quicBound = GetBound("VLESS_QUIC.json");
quicBound.port = parameters.VLESS_QUIC_Port;
SetClients(quicBound, uuidList);
quicBound.streamSettings.quicSettings.security = parameters.VLESS_QUIC_Security;
quicBound.streamSettings.quicSettings.key = parameters.VLESS_QUIC_Key;
quicBound.streamSettings.quicSettings.header.type = parameters.VLESS_QUIC_Type;
xrayConfig.inbounds.Add(JToken.FromObject(quicBound));
}
#endregion
2021-05-13 04:58:45 +03:00
2022-11-24 16:07:34 +03:00
#region VMESS KCP
2021-08-20 06:52:45 +03:00
if (parameters.Types.Contains(RayType.VMESS_KCP))
2021-05-13 04:58:45 +03:00
{
var kcpBound = GetBound("VMESS_KCP.json");
kcpBound.port = parameters.VMESS_KCP_Port;
2021-06-30 13:36:27 +03:00
SetClients(kcpBound, uuidList);
2021-05-13 04:58:45 +03:00
kcpBound.streamSettings.kcpSettings.header.type = parameters.VMESS_KCP_Type;
kcpBound.streamSettings.kcpSettings.seed = parameters.VMESS_KCP_Seed;
xrayConfig.inbounds.Add(JToken.FromObject(kcpBound));
}
2022-11-24 16:07:34 +03:00
#endregion
#region VMESS QUIC
if (parameters.Types.Contains(RayType.VMESS_QUIC))
{
var quicBound = GetBound("VMESS_QUIC.json");
quicBound.port = parameters.VMESS_QUIC_Port;
SetClients(quicBound, uuidList);
quicBound.streamSettings.quicSettings.security = parameters.VMESS_QUIC_Security;
quicBound.streamSettings.quicSettings.key = parameters.VMESS_QUIC_Key;
quicBound.streamSettings.quicSettings.header.type = parameters.VMESS_QUIC_Type;
xrayConfig.inbounds.Add(JToken.FromObject(quicBound));
}
#endregion
2021-05-13 04:58:45 +03:00
2022-11-24 16:07:34 +03:00
#region Shadowsocks
2021-08-20 06:52:45 +03:00
if (parameters.Types.Contains(RayType.ShadowsocksAEAD))
2021-05-13 04:58:45 +03:00
{
var ssBound = GetBound("Shadowsocks-AEAD.json");
2021-05-15 11:45:36 +03:00
ssBound.port = parameters.ShadowSocksPort;
2021-08-20 13:01:16 +03:00
ssBound.settings.password = parameters.ShadowSocksPassword;
ssBound.settings.method = parameters.ShadowSocksMethod;
2021-05-13 04:58:45 +03:00
xrayConfig.inbounds.Add(JToken.FromObject(ssBound));
}
2022-11-24 16:07:34 +03:00
#endregion
2021-05-13 04:58:45 +03:00
return JsonConvert.SerializeObject(
xrayConfig,
Formatting.Indented,
new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
});
}
private static dynamic GetBound(string name)
{
return LoadJsonObj(Path.Combine(ServerInboundsDir, name));
}
private static dynamic LoadJsonObj(string path)
{
if (File.Exists(path))
{
var jsonStr = File.ReadAllText(path, Encoding.UTF8);
return JToken.FromObject(JsonConvert.DeserializeObject(jsonStr));
}
return null;
}
}
}