v2rayN/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs

1255 lines
48 KiB
C#
Raw Normal View History

using System.Net;
2023-04-23 12:21:52 +00:00
using System.Net.NetworkInformation;
2024-06-26 08:00:37 +00:00
using System.Text.Json.Nodes;
2023-04-23 12:21:52 +00:00
2024-10-07 02:39:43 +00:00
namespace ServiceLib.Services.CoreConfig
2023-04-23 12:21:52 +00:00
{
2024-10-07 02:39:43 +00:00
public class CoreConfigV2rayService
2023-04-23 12:21:52 +00:00
{
private Config _config;
2024-10-07 02:39:43 +00:00
public CoreConfigV2rayService(Config config)
2023-04-23 12:21:52 +00:00
{
_config = config;
}
2024-07-14 09:16:07 +00:00
#region public gen function
2023-04-23 12:21:52 +00:00
public int GenerateClientConfigContent(ProfileItem node, out V2rayConfig? v2rayConfig, out string msg)
{
v2rayConfig = null;
try
{
if (node == null
|| node.port <= 0)
{
msg = ResUI.CheckServerSettings;
return -1;
}
msg = ResUI.InitialConfiguration;
2024-03-26 06:26:03 +00:00
string result = Utils.GetEmbedText(Global.V2raySampleClient);
if (Utils.IsNullOrEmpty(result))
2023-04-23 12:21:52 +00:00
{
msg = ResUI.FailedGetDefaultConfiguration;
return -1;
}
2024-03-26 06:26:03 +00:00
v2rayConfig = JsonUtils.Deserialize<V2rayConfig>(result);
2023-04-23 12:21:52 +00:00
if (v2rayConfig == null)
{
msg = ResUI.FailedGenDefaultConfiguration;
return -1;
}
2023-12-23 02:19:41 +00:00
GenLog(v2rayConfig);
2023-04-23 12:21:52 +00:00
2023-12-23 02:19:41 +00:00
GenInbounds(v2rayConfig);
2023-04-23 12:21:52 +00:00
2023-12-23 02:19:41 +00:00
GenRouting(v2rayConfig);
2023-04-23 12:21:52 +00:00
2023-12-23 02:19:41 +00:00
GenOutbound(node, v2rayConfig.outbounds[0]);
2023-04-23 12:21:52 +00:00
2023-12-23 12:57:31 +00:00
GenMoreOutbounds(node, v2rayConfig);
2024-06-26 08:00:37 +00:00
GenDns(node, v2rayConfig);
2023-04-23 12:21:52 +00:00
2023-12-23 02:19:41 +00:00
GenStatistic(v2rayConfig);
2023-04-23 12:21:52 +00:00
msg = string.Format(ResUI.SuccessfulConfiguration, "");
}
catch (Exception ex)
{
2024-01-10 02:43:48 +00:00
Logging.SaveLog("GenerateClientConfig4V2ray", ex);
2023-04-23 12:21:52 +00:00
msg = ResUI.FailedGenDefaultConfiguration;
return -1;
}
return 0;
}
2024-07-21 03:26:10 +00:00
public int GenerateClientMultipleLoadConfig(List<ProfileItem> selecteds, out V2rayConfig? v2rayConfig, out string msg)
{
v2rayConfig = null;
try
{
if (_config == null)
{
msg = ResUI.CheckServerSettings;
return -1;
}
msg = ResUI.InitialConfiguration;
string result = Utils.GetEmbedText(Global.V2raySampleClient);
string txtOutbound = Utils.GetEmbedText(Global.V2raySampleOutbound);
if (Utils.IsNullOrEmpty(result) || txtOutbound.IsNullOrEmpty())
{
msg = ResUI.FailedGetDefaultConfiguration;
return -1;
}
v2rayConfig = JsonUtils.Deserialize<V2rayConfig>(result);
if (v2rayConfig == null)
{
msg = ResUI.FailedGenDefaultConfiguration;
return -1;
}
GenLog(v2rayConfig);
GenInbounds(v2rayConfig);
GenRouting(v2rayConfig);
GenDns(null, v2rayConfig);
GenStatistic(v2rayConfig);
v2rayConfig.outbounds.RemoveAt(0);
var tagProxy = new List<string>();
foreach (var it in selecteds)
{
if (it.configType == EConfigType.Custom)
{
continue;
}
2024-09-23 09:17:12 +00:00
if (it.configType is EConfigType.Hysteria2 or EConfigType.TUIC or EConfigType.WireGuard)
2024-07-21 03:26:10 +00:00
{
continue;
}
if (it.port <= 0)
{
continue;
}
2024-10-07 01:51:41 +00:00
var item = AppHandler.Instance.GetProfileItem(it.indexId);
2024-07-21 03:26:10 +00:00
if (item is null)
{
continue;
}
if (it.configType is EConfigType.VMess or EConfigType.VLESS)
{
if (Utils.IsNullOrEmpty(item.id) || !Utils.IsGuidByParse(item.id))
{
continue;
}
}
if (item.configType == EConfigType.Shadowsocks
&& !Global.SsSecuritiesInSingbox.Contains(item.security))
{
continue;
}
if (item.configType == EConfigType.VLESS && !Global.Flows.Contains(item.flow))
{
continue;
}
//outbound
var outbound = JsonUtils.Deserialize<Outbounds4Ray>(txtOutbound);
GenOutbound(item, outbound);
outbound.tag = $"{Global.ProxyTag}-{tagProxy.Count + 1}";
v2rayConfig.outbounds.Add(outbound);
tagProxy.Add(outbound.tag);
}
if (tagProxy.Count <= 0)
{
msg = ResUI.FailedGenDefaultConfiguration;
return -1;
}
//add balancers
var balancer = new BalancersItem4Ray
{
selector = [Global.ProxyTag],
strategy = new() { type = "roundRobin" },
tag = $"{Global.ProxyTag}-round",
};
v2rayConfig.routing.balancers = [balancer];
//add rule
var rules = v2rayConfig.routing.rules.Where(t => t.outboundTag == Global.ProxyTag).ToList();
if (rules?.Count > 0)
{
foreach (var rule in rules)
{
rule.outboundTag = null;
rule.balancerTag = balancer.tag;
}
}
else
{
v2rayConfig.routing.rules.Add(new()
{
network = "tcp,udp",
balancerTag = balancer.tag,
type = "field"
});
}
return 0;
}
catch (Exception ex)
{
Logging.SaveLog(ex.Message, ex);
msg = ResUI.FailedGenDefaultConfiguration;
return -1;
}
}
2024-07-14 09:16:07 +00:00
public int GenerateClientSpeedtestConfig(List<ServerTestItem> selecteds, out V2rayConfig? v2rayConfig, out string msg)
{
v2rayConfig = null;
try
{
if (_config == null)
{
msg = ResUI.CheckServerSettings;
return -1;
}
msg = ResUI.InitialConfiguration;
string result = Utils.GetEmbedText(Global.V2raySampleClient);
string txtOutbound = Utils.GetEmbedText(Global.V2raySampleOutbound);
if (Utils.IsNullOrEmpty(result) || txtOutbound.IsNullOrEmpty())
{
msg = ResUI.FailedGetDefaultConfiguration;
return -1;
}
v2rayConfig = JsonUtils.Deserialize<V2rayConfig>(result);
if (v2rayConfig == null)
{
msg = ResUI.FailedGenDefaultConfiguration;
return -1;
}
List<IPEndPoint> lstIpEndPoints = new();
List<TcpConnectionInformation> lstTcpConns = new();
try
{
lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners());
lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners());
lstTcpConns.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections());
}
catch (Exception ex)
{
Logging.SaveLog(ex.Message, ex);
}
GenLog(v2rayConfig);
v2rayConfig.inbounds.Clear(); // Remove "proxy" service for speedtest, avoiding port conflicts.
v2rayConfig.outbounds.RemoveAt(0);
2024-10-07 01:51:41 +00:00
int httpPort = AppHandler.Instance.GetLocalPort(EInboundProtocol.speedtest);
2024-07-14 09:16:07 +00:00
foreach (var it in selecteds)
{
2024-10-08 01:50:03 +00:00
if (it.ConfigType == EConfigType.Custom)
2024-07-14 09:16:07 +00:00
{
continue;
}
2024-10-08 01:50:03 +00:00
if (it.Port <= 0)
2024-07-14 09:16:07 +00:00
{
continue;
}
2024-10-08 01:50:03 +00:00
var item = AppHandler.Instance.GetProfileItem(it.IndexId);
if (it.ConfigType is EConfigType.VMess or EConfigType.VLESS)
2024-07-14 09:16:07 +00:00
{
2024-09-17 10:41:01 +00:00
if (item is null || Utils.IsNullOrEmpty(item.id) || !Utils.IsGuidByParse(item.id))
2024-07-14 09:16:07 +00:00
{
continue;
}
}
//find unused port
var port = httpPort;
for (int k = httpPort; k < Global.MaxPort; k++)
{
if (lstIpEndPoints?.FindIndex(_it => _it.Port == k) >= 0)
{
continue;
}
if (lstTcpConns?.FindIndex(_it => _it.LocalEndPoint.Port == k) >= 0)
{
continue;
}
//found
port = k;
httpPort = port + 1;
break;
}
//Port In Used
if (lstIpEndPoints?.FindIndex(_it => _it.Port == port) >= 0)
{
continue;
}
2024-10-08 01:50:03 +00:00
it.Port = port;
it.AllowTest = true;
2024-07-14 09:16:07 +00:00
//inbound
Inbounds4Ray inbound = new()
{
listen = Global.Loopback,
port = port,
protocol = EInboundProtocol.http.ToString(),
};
inbound.tag = inbound.protocol + inbound.port.ToString();
v2rayConfig.inbounds.Add(inbound);
//outbound
if (item is null)
{
continue;
}
if (item.configType == EConfigType.Shadowsocks
&& !Global.SsSecuritiesInXray.Contains(item.security))
{
continue;
}
if (item.configType == EConfigType.VLESS
&& !Global.Flows.Contains(item.flow))
{
continue;
}
2024-10-08 01:50:03 +00:00
if (it.ConfigType is EConfigType.VLESS or EConfigType.Trojan
2024-09-17 10:41:01 +00:00
&& item.streamSecurity == Global.StreamSecurityReality
&& item.publicKey.IsNullOrEmpty())
{
continue;
}
2024-07-14 09:16:07 +00:00
var outbound = JsonUtils.Deserialize<Outbounds4Ray>(txtOutbound);
GenOutbound(item, outbound);
outbound.tag = Global.ProxyTag + inbound.port.ToString();
v2rayConfig.outbounds.Add(outbound);
//rule
RulesItem4Ray rule = new()
{
inboundTag = new List<string> { inbound.tag },
outboundTag = outbound.tag,
type = "field"
};
v2rayConfig.routing.rules.Add(rule);
}
//msg = string.Format(ResUI.SuccessfulConfiguration"), node.getSummary());
return 0;
}
catch (Exception ex)
{
Logging.SaveLog(ex.Message, ex);
msg = ResUI.FailedGenDefaultConfiguration;
return -1;
}
}
#endregion public gen function
#region private gen function
2023-12-23 02:19:41 +00:00
private int GenLog(V2rayConfig v2rayConfig)
2023-04-23 12:21:52 +00:00
{
try
{
if (_config.coreBasicItem.logEnabled)
{
var dtNow = DateTime.Now;
v2rayConfig.log.loglevel = _config.coreBasicItem.loglevel;
2024-03-26 06:26:03 +00:00
v2rayConfig.log.access = Utils.GetLogPath($"Vaccess_{dtNow:yyyy-MM-dd}.txt");
v2rayConfig.log.error = Utils.GetLogPath($"Verror_{dtNow:yyyy-MM-dd}.txt");
2023-04-23 12:21:52 +00:00
}
else
{
v2rayConfig.log.loglevel = _config.coreBasicItem.loglevel;
v2rayConfig.log.access = "";
v2rayConfig.log.error = "";
}
}
catch (Exception ex)
{
2024-01-10 02:43:48 +00:00
Logging.SaveLog(ex.Message, ex);
2023-04-23 12:21:52 +00:00
}
return 0;
}
2023-12-23 02:19:41 +00:00
private int GenInbounds(V2rayConfig v2rayConfig)
2023-04-23 12:21:52 +00:00
{
try
{
2024-05-14 05:42:42 +00:00
var listen = "0.0.0.0";
v2rayConfig.inbounds = [];
2023-04-23 12:21:52 +00:00
2024-03-09 02:12:45 +00:00
Inbounds4Ray? inbound = GetInbound(_config.inbound[0], EInboundProtocol.socks, true);
2023-04-23 12:21:52 +00:00
v2rayConfig.inbounds.Add(inbound);
//http
2024-03-09 02:12:45 +00:00
Inbounds4Ray? inbound2 = GetInbound(_config.inbound[0], EInboundProtocol.http, false);
2023-04-23 12:21:52 +00:00
v2rayConfig.inbounds.Add(inbound2);
if (_config.inbound[0].allowLANConn)
{
if (_config.inbound[0].newPort4LAN)
{
2024-03-09 02:12:45 +00:00
var inbound3 = GetInbound(_config.inbound[0], EInboundProtocol.socks2, true);
2024-05-14 05:42:42 +00:00
inbound3.listen = listen;
2023-04-23 12:21:52 +00:00
v2rayConfig.inbounds.Add(inbound3);
2024-03-09 02:12:45 +00:00
var inbound4 = GetInbound(_config.inbound[0], EInboundProtocol.http2, false);
2024-05-14 05:42:42 +00:00
inbound4.listen = listen;
2023-04-23 12:21:52 +00:00
v2rayConfig.inbounds.Add(inbound4);
//auth
2024-09-17 08:52:41 +00:00
if (Utils.IsNotEmpty(_config.inbound[0].user) && Utils.IsNotEmpty(_config.inbound[0].pass))
2023-04-23 12:21:52 +00:00
{
inbound3.settings.auth = "password";
2023-05-07 08:35:46 +00:00
inbound3.settings.accounts = new List<AccountsItem4Ray> { new AccountsItem4Ray() { user = _config.inbound[0].user, pass = _config.inbound[0].pass } };
2023-04-23 12:21:52 +00:00
inbound4.settings.auth = "password";
2023-05-07 08:35:46 +00:00
inbound4.settings.accounts = new List<AccountsItem4Ray> { new AccountsItem4Ray() { user = _config.inbound[0].user, pass = _config.inbound[0].pass } };
2023-04-23 12:21:52 +00:00
}
}
else
{
2024-05-14 05:42:42 +00:00
inbound.listen = listen;
inbound2.listen = listen;
2023-04-23 12:21:52 +00:00
}
}
}
catch (Exception ex)
{
2024-01-10 02:43:48 +00:00
Logging.SaveLog(ex.Message, ex);
2023-04-23 12:21:52 +00:00
}
return 0;
}
2024-05-14 05:42:42 +00:00
private Inbounds4Ray GetInbound(InItem inItem, EInboundProtocol protocol, bool bSocks)
2023-04-23 12:21:52 +00:00
{
2024-03-26 06:26:03 +00:00
string result = Utils.GetEmbedText(Global.V2raySampleInbound);
if (Utils.IsNullOrEmpty(result))
2023-04-23 12:21:52 +00:00
{
2024-05-14 05:42:42 +00:00
return new();
2023-04-23 12:21:52 +00:00
}
2024-03-26 06:26:03 +00:00
var inbound = JsonUtils.Deserialize<Inbounds4Ray>(result);
2023-04-23 12:21:52 +00:00
if (inbound == null)
{
2024-05-14 05:42:42 +00:00
return new();
2023-04-23 12:21:52 +00:00
}
2024-03-09 02:12:45 +00:00
inbound.tag = protocol.ToString();
inbound.port = inItem.localPort + (int)protocol;
inbound.protocol = bSocks ? EInboundProtocol.socks.ToString() : EInboundProtocol.http.ToString();
2023-04-23 12:21:52 +00:00
inbound.settings.udp = inItem.udpEnabled;
inbound.sniffing.enabled = inItem.sniffingEnabled;
2024-05-14 07:31:19 +00:00
inbound.sniffing.destOverride = inItem.destOverride;
2023-04-23 12:21:52 +00:00
inbound.sniffing.routeOnly = inItem.routeOnly;
return inbound;
}
2024-10-20 03:51:05 +00:00
private async Task<int> GenRouting(V2rayConfig v2rayConfig)
2023-04-23 12:21:52 +00:00
{
try
{
if (v2rayConfig.routing?.rules != null)
{
v2rayConfig.routing.domainStrategy = _config.routingBasicItem.domainStrategy;
2024-03-26 06:26:03 +00:00
v2rayConfig.routing.domainMatcher = Utils.IsNullOrEmpty(_config.routingBasicItem.domainMatcher) ? null : _config.routingBasicItem.domainMatcher;
2023-04-23 12:21:52 +00:00
if (_config.routingBasicItem.enableRoutingAdvanced)
{
2024-10-20 03:51:05 +00:00
var routing = await ConfigHandler.GetDefaultRouting(_config);
2023-04-23 12:21:52 +00:00
if (routing != null)
{
2024-09-17 08:52:41 +00:00
if (Utils.IsNotEmpty(routing.domainStrategy))
2023-04-23 12:21:52 +00:00
{
v2rayConfig.routing.domainStrategy = routing.domainStrategy;
}
2024-03-26 06:26:03 +00:00
var rules = JsonUtils.Deserialize<List<RulesItem>>(routing.ruleSet);
2023-04-23 12:21:52 +00:00
foreach (var item in rules)
{
if (item.enabled)
{
2024-03-26 06:26:03 +00:00
var item2 = JsonUtils.Deserialize<RulesItem4Ray>(JsonUtils.Serialize(item));
2023-12-23 02:19:41 +00:00
GenRoutingUserRule(item2, v2rayConfig);
2023-04-23 12:21:52 +00:00
}
}
}
}
else
{
2023-12-22 08:03:25 +00:00
var lockedItem = ConfigHandler.GetLockedRoutingItem(_config);
2023-04-23 12:21:52 +00:00
if (lockedItem != null)
{
2024-03-26 06:26:03 +00:00
var rules = JsonUtils.Deserialize<List<RulesItem>>(lockedItem.ruleSet);
2023-04-23 12:21:52 +00:00
foreach (var item in rules)
{
2024-03-26 06:26:03 +00:00
var item2 = JsonUtils.Deserialize<RulesItem4Ray>(JsonUtils.Serialize(item));
2023-12-23 02:19:41 +00:00
GenRoutingUserRule(item2, v2rayConfig);
2023-04-23 12:21:52 +00:00
}
}
}
}
}
catch (Exception ex)
{
2024-01-10 02:43:48 +00:00
Logging.SaveLog(ex.Message, ex);
2023-04-23 12:21:52 +00:00
}
return 0;
}
private int GenRoutingUserRule(RulesItem4Ray? rule, V2rayConfig v2rayConfig)
2023-04-23 12:21:52 +00:00
{
try
{
if (rule == null)
2023-04-23 12:21:52 +00:00
{
return 0;
}
if (Utils.IsNullOrEmpty(rule.port))
2023-04-23 12:21:52 +00:00
{
rule.port = null;
2023-04-23 12:21:52 +00:00
}
if (Utils.IsNullOrEmpty(rule.network))
2023-04-23 12:21:52 +00:00
{
rule.network = null;
2023-04-23 12:21:52 +00:00
}
if (rule.domain?.Count == 0)
2023-04-23 12:21:52 +00:00
{
rule.domain = null;
2023-04-23 12:21:52 +00:00
}
if (rule.ip?.Count == 0)
2023-04-23 12:21:52 +00:00
{
rule.ip = null;
2023-04-23 12:21:52 +00:00
}
if (rule.protocol?.Count == 0)
2023-04-23 12:21:52 +00:00
{
rule.protocol = null;
}
if (rule.inboundTag?.Count == 0)
{
rule.inboundTag = null;
2023-04-23 12:21:52 +00:00
}
var hasDomainIp = false;
if (rule.domain?.Count > 0)
2023-04-23 12:21:52 +00:00
{
var it = JsonUtils.DeepCopy(rule);
2023-04-23 12:21:52 +00:00
it.ip = null;
it.type = "field";
for (int k = it.domain.Count - 1; k >= 0; k--)
{
if (it.domain[k].StartsWith("#"))
{
it.domain.RemoveAt(k);
}
it.domain[k] = it.domain[k].Replace(Global.RoutingRuleComma, ",");
}
v2rayConfig.routing.rules.Add(it);
hasDomainIp = true;
}
if (rule.ip?.Count > 0)
2023-04-23 12:21:52 +00:00
{
var it = JsonUtils.DeepCopy(rule);
2023-04-23 12:21:52 +00:00
it.domain = null;
it.type = "field";
v2rayConfig.routing.rules.Add(it);
hasDomainIp = true;
}
if (!hasDomainIp)
{
2024-09-17 08:52:41 +00:00
if (Utils.IsNotEmpty(rule.port)
|| rule.protocol?.Count > 0
|| rule.inboundTag?.Count > 0
2023-04-23 12:21:52 +00:00
)
{
var it = JsonUtils.DeepCopy(rule);
2023-04-23 12:21:52 +00:00
it.type = "field";
v2rayConfig.routing.rules.Add(it);
}
}
}
catch (Exception ex)
{
2024-01-10 02:43:48 +00:00
Logging.SaveLog(ex.Message, ex);
2023-04-23 12:21:52 +00:00
}
return 0;
}
2023-12-23 02:19:41 +00:00
private int GenOutbound(ProfileItem node, Outbounds4Ray outbound)
2023-04-23 12:21:52 +00:00
{
try
{
switch (node.configType)
2023-04-23 12:21:52 +00:00
{
case EConfigType.VMess:
{
VnextItem4Ray vnextItem;
if (outbound.settings.vnext.Count <= 0)
{
vnextItem = new VnextItem4Ray();
outbound.settings.vnext.Add(vnextItem);
}
else
{
vnextItem = outbound.settings.vnext[0];
}
vnextItem.address = node.address;
vnextItem.port = node.port;
2023-04-23 12:21:52 +00:00
UsersItem4Ray usersItem;
if (vnextItem.users.Count <= 0)
{
usersItem = new UsersItem4Ray();
vnextItem.users.Add(usersItem);
}
else
{
usersItem = vnextItem.users[0];
}
//远程服务器用户ID
usersItem.id = node.id;
usersItem.alterId = node.alterId;
usersItem.email = Global.UserEMail;
if (Global.VmessSecurities.Contains(node.security))
{
usersItem.security = node.security;
}
else
{
usersItem.security = Global.DefaultSecurity;
}
2023-04-23 12:21:52 +00:00
GenOutboundMux(node, outbound, _config.coreBasicItem.muxEnabled);
2023-04-23 12:21:52 +00:00
outbound.settings.servers = null;
break;
}
case EConfigType.Shadowsocks:
{
ServersItem4Ray serversItem;
if (outbound.settings.servers.Count <= 0)
{
serversItem = new ServersItem4Ray();
outbound.settings.servers.Add(serversItem);
}
else
{
serversItem = outbound.settings.servers[0];
}
serversItem.address = node.address;
serversItem.port = node.port;
serversItem.password = node.id;
2024-10-07 01:51:41 +00:00
serversItem.method = AppHandler.Instance.GetShadowsocksSecurities(node).Contains(node.security) ? node.security : "none";
2023-04-23 12:21:52 +00:00
serversItem.ota = false;
serversItem.level = 1;
2023-04-23 12:21:52 +00:00
GenOutboundMux(node, outbound, false);
2023-04-23 12:21:52 +00:00
outbound.settings.vnext = null;
break;
}
2024-09-23 09:17:12 +00:00
case EConfigType.SOCKS:
case EConfigType.HTTP:
2023-04-23 12:21:52 +00:00
{
ServersItem4Ray serversItem;
if (outbound.settings.servers.Count <= 0)
{
serversItem = new ServersItem4Ray();
outbound.settings.servers.Add(serversItem);
}
else
{
serversItem = outbound.settings.servers[0];
}
serversItem.address = node.address;
serversItem.port = node.port;
serversItem.method = null;
serversItem.password = null;
2023-04-23 12:21:52 +00:00
2024-09-17 08:52:41 +00:00
if (Utils.IsNotEmpty(node.security)
&& Utils.IsNotEmpty(node.id))
{
SocksUsersItem4Ray socksUsersItem = new()
{
user = node.security,
pass = node.id,
level = 1
};
2023-04-23 12:21:52 +00:00
serversItem.users = new List<SocksUsersItem4Ray>() { socksUsersItem };
}
2023-04-23 12:21:52 +00:00
GenOutboundMux(node, outbound, false);
2023-04-23 12:21:52 +00:00
outbound.settings.vnext = null;
break;
}
case EConfigType.VLESS:
{
VnextItem4Ray vnextItem;
if (outbound.settings.vnext?.Count <= 0)
{
vnextItem = new VnextItem4Ray();
outbound.settings.vnext.Add(vnextItem);
}
else
{
vnextItem = outbound.settings.vnext[0];
}
vnextItem.address = node.address;
vnextItem.port = node.port;
2023-04-23 12:21:52 +00:00
UsersItem4Ray usersItem;
if (vnextItem.users.Count <= 0)
{
usersItem = new UsersItem4Ray();
vnextItem.users.Add(usersItem);
}
else
{
usersItem = vnextItem.users[0];
}
usersItem.id = node.id;
usersItem.email = Global.UserEMail;
usersItem.encryption = node.security;
2023-04-23 12:21:52 +00:00
GenOutboundMux(node, outbound, _config.coreBasicItem.muxEnabled);
2023-04-23 12:21:52 +00:00
if (node.streamSecurity == Global.StreamSecurityReality
|| node.streamSecurity == Global.StreamSecurity)
{
2024-09-17 08:52:41 +00:00
if (Utils.IsNotEmpty(node.flow))
{
usersItem.flow = node.flow;
2023-04-23 12:21:52 +00:00
GenOutboundMux(node, outbound, false);
}
}
if (node.streamSecurity == Global.StreamSecurityReality && Utils.IsNullOrEmpty(node.flow))
{
GenOutboundMux(node, outbound, _config.coreBasicItem.muxEnabled);
}
outbound.settings.servers = null;
break;
}
case EConfigType.Trojan:
{
ServersItem4Ray serversItem;
if (outbound.settings.servers.Count <= 0)
{
serversItem = new ServersItem4Ray();
outbound.settings.servers.Add(serversItem);
}
else
{
serversItem = outbound.settings.servers[0];
}
serversItem.address = node.address;
serversItem.port = node.port;
serversItem.password = node.id;
2023-04-23 12:21:52 +00:00
serversItem.ota = false;
serversItem.level = 1;
2023-04-23 12:21:52 +00:00
GenOutboundMux(node, outbound, false);
2023-04-23 12:21:52 +00:00
outbound.settings.vnext = null;
break;
}
2023-04-23 12:21:52 +00:00
}
outbound.protocol = Global.ProtocolTypes[node.configType];
2023-12-23 02:19:41 +00:00
GenBoundStreamSettings(node, outbound.streamSettings);
2023-04-23 12:21:52 +00:00
}
catch (Exception ex)
{
2024-01-10 02:43:48 +00:00
Logging.SaveLog(ex.Message, ex);
2023-04-23 12:21:52 +00:00
}
return 0;
}
2023-12-23 02:19:41 +00:00
private int GenOutboundMux(ProfileItem node, Outbounds4Ray outbound, bool enabled)
{
try
{
2023-05-07 08:35:46 +00:00
if (enabled)
{
outbound.mux.enabled = true;
outbound.mux.concurrency = _config.mux4RayItem.concurrency;
outbound.mux.xudpConcurrency = _config.mux4RayItem.xudpConcurrency;
outbound.mux.xudpProxyUDP443 = _config.mux4RayItem.xudpProxyUDP443;
}
else
{
outbound.mux.enabled = false;
outbound.mux.concurrency = -1;
}
}
catch (Exception ex)
{
2024-01-10 02:43:48 +00:00
Logging.SaveLog(ex.Message, ex);
}
return 0;
}
2023-12-23 02:19:41 +00:00
private int GenBoundStreamSettings(ProfileItem node, StreamSettings4Ray streamSettings)
2023-04-23 12:21:52 +00:00
{
try
{
streamSettings.network = node.GetNetwork();
string host = node.requestHost.TrimEx();
string sni = node.sni;
string useragent = "";
if (!_config.coreBasicItem.defUserAgent.IsNullOrEmpty())
{
try
{
2024-02-19 09:43:36 +00:00
useragent = Global.UserAgentTexts[_config.coreBasicItem.defUserAgent];
2023-04-23 12:21:52 +00:00
}
catch (KeyNotFoundException)
{
useragent = _config.coreBasicItem.defUserAgent;
}
}
//if tls
if (node.streamSecurity == Global.StreamSecurity)
{
streamSettings.security = node.streamSecurity;
2023-05-07 08:35:46 +00:00
TlsSettings4Ray tlsSettings = new()
2023-04-23 12:21:52 +00:00
{
2024-03-26 06:26:03 +00:00
allowInsecure = Utils.ToBool(node.allowInsecure.IsNullOrEmpty() ? _config.coreBasicItem.defAllowInsecure.ToString().ToLower() : node.allowInsecure),
2023-04-23 12:21:52 +00:00
alpn = node.GetAlpn(),
fingerprint = node.fingerprint.IsNullOrEmpty() ? _config.coreBasicItem.defFingerprint : node.fingerprint
};
2024-09-17 08:52:41 +00:00
if (Utils.IsNotEmpty(sni))
2023-04-23 12:21:52 +00:00
{
tlsSettings.serverName = sni;
}
2024-09-17 08:52:41 +00:00
else if (Utils.IsNotEmpty(host))
2023-04-23 12:21:52 +00:00
{
2024-10-14 02:42:05 +00:00
tlsSettings.serverName = Utils.String2List(host)?.First();
2023-04-23 12:21:52 +00:00
}
streamSettings.tlsSettings = tlsSettings;
}
//if Reality
if (node.streamSecurity == Global.StreamSecurityReality)
{
streamSettings.security = node.streamSecurity;
2023-05-07 08:35:46 +00:00
TlsSettings4Ray realitySettings = new()
2023-04-23 12:21:52 +00:00
{
fingerprint = node.fingerprint.IsNullOrEmpty() ? _config.coreBasicItem.defFingerprint : node.fingerprint,
serverName = sni,
publicKey = node.publicKey,
shortId = node.shortId,
spiderX = node.spiderX,
show = false,
2023-04-23 12:21:52 +00:00
};
streamSettings.realitySettings = realitySettings;
}
//streamSettings
switch (node.GetNetwork())
{
2024-03-09 01:27:55 +00:00
case nameof(ETransport.kcp):
2023-05-07 08:35:46 +00:00
KcpSettings4Ray kcpSettings = new()
2023-04-23 12:21:52 +00:00
{
mtu = _config.kcpItem.mtu,
tti = _config.kcpItem.tti
};
kcpSettings.uplinkCapacity = _config.kcpItem.uplinkCapacity;
kcpSettings.downlinkCapacity = _config.kcpItem.downlinkCapacity;
kcpSettings.congestion = _config.kcpItem.congestion;
kcpSettings.readBufferSize = _config.kcpItem.readBufferSize;
kcpSettings.writeBufferSize = _config.kcpItem.writeBufferSize;
2023-05-07 08:35:46 +00:00
kcpSettings.header = new Header4Ray
2023-04-23 12:21:52 +00:00
{
type = node.headerType
};
2024-09-17 08:52:41 +00:00
if (Utils.IsNotEmpty(node.path))
2023-04-23 12:21:52 +00:00
{
kcpSettings.seed = node.path;
}
streamSettings.kcpSettings = kcpSettings;
break;
//ws
2024-03-09 01:27:55 +00:00
case nameof(ETransport.ws):
2023-05-07 08:35:46 +00:00
WsSettings4Ray wsSettings = new();
wsSettings.headers = new Headers4Ray();
2023-04-23 12:21:52 +00:00
string path = node.path;
2024-09-17 08:52:41 +00:00
if (Utils.IsNotEmpty(host))
2023-04-23 12:21:52 +00:00
{
wsSettings.headers.Host = host;
}
2024-09-17 08:52:41 +00:00
if (Utils.IsNotEmpty(path))
2023-04-23 12:21:52 +00:00
{
wsSettings.path = path;
}
2024-09-17 08:52:41 +00:00
if (Utils.IsNotEmpty(useragent))
2023-04-23 12:21:52 +00:00
{
wsSettings.headers.UserAgent = useragent;
}
streamSettings.wsSettings = wsSettings;
2024-03-12 01:05:59 +00:00
break;
//httpupgrade
case nameof(ETransport.httpupgrade):
HttpupgradeSettings4Ray httpupgradeSettings = new();
2024-09-17 08:52:41 +00:00
if (Utils.IsNotEmpty(node.path))
2024-03-12 01:05:59 +00:00
{
httpupgradeSettings.path = node.path;
}
2024-09-17 08:52:41 +00:00
if (Utils.IsNotEmpty(host))
2024-03-12 01:05:59 +00:00
{
httpupgradeSettings.host = host;
}
streamSettings.httpupgradeSettings = httpupgradeSettings;
break;
//splithttp
case nameof(ETransport.splithttp):
SplithttpSettings4Ray splithttpSettings = new()
{
maxUploadSize = 1000000,
maxConcurrentUploads = 10
};
2024-09-17 08:52:41 +00:00
if (Utils.IsNotEmpty(node.path))
{
splithttpSettings.path = node.path;
}
2024-09-17 08:52:41 +00:00
if (Utils.IsNotEmpty(host))
{
splithttpSettings.host = host;
}
streamSettings.splithttpSettings = splithttpSettings;
2023-04-23 12:21:52 +00:00
break;
//h2
2024-03-09 01:27:55 +00:00
case nameof(ETransport.h2):
2023-05-07 08:35:46 +00:00
HttpSettings4Ray httpSettings = new();
2023-04-23 12:21:52 +00:00
2024-09-17 08:52:41 +00:00
if (Utils.IsNotEmpty(host))
2023-04-23 12:21:52 +00:00
{
2024-03-26 06:26:03 +00:00
httpSettings.host = Utils.String2List(host);
2023-04-23 12:21:52 +00:00
}
httpSettings.path = node.path;
streamSettings.httpSettings = httpSettings;
break;
//quic
2024-03-09 01:27:55 +00:00
case nameof(ETransport.quic):
2023-05-07 08:35:46 +00:00
QuicSettings4Ray quicsettings = new()
2023-04-23 12:21:52 +00:00
{
security = host,
key = node.path,
2023-05-07 08:35:46 +00:00
header = new Header4Ray
2023-04-23 12:21:52 +00:00
{
type = node.headerType
}
};
streamSettings.quicSettings = quicsettings;
if (node.streamSecurity == Global.StreamSecurity)
{
2024-09-17 08:52:41 +00:00
if (Utils.IsNotEmpty(sni))
2023-04-23 12:21:52 +00:00
{
streamSettings.tlsSettings.serverName = sni;
}
else
{
streamSettings.tlsSettings.serverName = node.address;
}
}
break;
2024-03-09 01:27:55 +00:00
case nameof(ETransport.grpc):
2023-05-07 08:35:46 +00:00
GrpcSettings4Ray grpcSettings = new()
2023-04-23 12:21:52 +00:00
{
2024-03-26 06:26:03 +00:00
authority = Utils.IsNullOrEmpty(host) ? null : host,
2023-04-23 12:21:52 +00:00
serviceName = node.path,
2024-06-04 01:48:04 +00:00
multiMode = node.headerType == Global.GrpcMultiMode,
2023-04-23 12:21:52 +00:00
idle_timeout = _config.grpcItem.idle_timeout,
health_check_timeout = _config.grpcItem.health_check_timeout,
permit_without_stream = _config.grpcItem.permit_without_stream,
initial_windows_size = _config.grpcItem.initial_windows_size,
};
streamSettings.grpcSettings = grpcSettings;
break;
default:
//tcp
if (node.headerType == Global.TcpHeaderHttp)
{
2023-05-07 08:35:46 +00:00
TcpSettings4Ray tcpSettings = new()
2023-04-23 12:21:52 +00:00
{
2023-05-07 08:35:46 +00:00
header = new Header4Ray
2023-04-23 12:21:52 +00:00
{
type = node.headerType
}
};
//request Host
2024-03-26 06:26:03 +00:00
string request = Utils.GetEmbedText(Global.V2raySampleHttpRequestFileName);
2023-04-23 12:21:52 +00:00
string[] arrHost = host.Split(',');
string host2 = string.Join("\",\"", arrHost);
request = request.Replace("$requestHost$", $"\"{host2}\"");
//request = request.Replace("$requestHost$", string.Format("\"{0}\"", config.requestHost()));
request = request.Replace("$requestUserAgent$", $"\"{useragent}\"");
//Path
string pathHttp = @"/";
2024-09-17 08:52:41 +00:00
if (Utils.IsNotEmpty(node.path))
2023-04-23 12:21:52 +00:00
{
string[] arrPath = node.path.Split(',');
pathHttp = string.Join("\",\"", arrPath);
}
request = request.Replace("$requestPath$", $"\"{pathHttp}\"");
2024-03-26 06:26:03 +00:00
tcpSettings.header.request = JsonUtils.Deserialize<object>(request);
2023-04-23 12:21:52 +00:00
streamSettings.tcpSettings = tcpSettings;
}
break;
}
}
catch (Exception ex)
{
2024-01-10 02:43:48 +00:00
Logging.SaveLog(ex.Message, ex);
2023-04-23 12:21:52 +00:00
}
return 0;
}
2024-07-21 03:26:10 +00:00
private int GenDns(ProfileItem? node, V2rayConfig v2rayConfig)
2023-04-23 12:21:52 +00:00
{
try
{
2024-10-07 01:51:41 +00:00
var item = AppHandler.Instance.GetDNSItem(ECoreType.Xray);
var normalDNS = item?.normalDNS;
var domainStrategy4Freedom = item?.domainStrategy4Freedom;
2024-03-26 06:26:03 +00:00
if (Utils.IsNullOrEmpty(normalDNS))
2023-04-23 12:21:52 +00:00
{
normalDNS = Utils.GetEmbedText(Global.DNSV2rayNormalFileName);
2023-04-23 12:21:52 +00:00
}
//Outbound Freedom domainStrategy
2024-09-17 08:52:41 +00:00
if (Utils.IsNotEmpty(domainStrategy4Freedom))
2023-04-23 12:21:52 +00:00
{
var outbound = v2rayConfig.outbounds[1];
outbound.settings.domainStrategy = domainStrategy4Freedom;
2023-04-23 12:21:52 +00:00
outbound.settings.userLevel = 0;
}
2024-03-26 06:26:03 +00:00
var obj = JsonUtils.ParseJson(normalDNS);
if (obj is null)
2023-04-23 12:21:52 +00:00
{
2024-01-13 08:52:42 +00:00
List<string> servers = [];
string[] arrDNS = normalDNS.Split(',');
2023-04-23 12:21:52 +00:00
foreach (string str in arrDNS)
{
servers.Add(str);
}
2024-03-26 06:26:03 +00:00
obj = JsonUtils.ParseJson("{}");
obj["servers"] = JsonUtils.SerializeToNode(servers);
}
2023-12-01 09:03:49 +00:00
// 追加至 dns 设置
if (item.useSystemHosts)
{
2024-03-26 06:26:03 +00:00
var systemHosts = Utils.GetSystemHosts();
if (systemHosts.Count > 0)
2023-04-23 12:21:52 +00:00
{
var normalHost = obj["hosts"];
if (normalHost != null)
{
foreach (var host in systemHosts)
{
if (normalHost[host.Key] != null)
continue;
normalHost[host.Key] = host.Value;
}
}
}
2023-04-23 12:21:52 +00:00
}
2023-12-01 09:03:49 +00:00
GenDnsDomains(node, obj, item);
2024-06-26 08:00:37 +00:00
v2rayConfig.dns = obj;
2023-04-23 12:21:52 +00:00
}
catch (Exception ex)
{
2024-01-10 02:43:48 +00:00
Logging.SaveLog(ex.Message, ex);
2023-04-23 12:21:52 +00:00
}
return 0;
}
private int GenDnsDomains(ProfileItem? node, JsonNode dns, DNSItem? dNSItem)
2024-06-26 08:00:37 +00:00
{
2024-07-21 03:26:10 +00:00
if (node == null)
{ return 0; }
2024-06-26 08:00:37 +00:00
var servers = dns["servers"];
if (servers != null)
{
if (Utils.IsDomain(node.address))
{
var dnsServer = new DnsServer4Ray()
{
address = Utils.IsNullOrEmpty(dNSItem?.domainDNSAddress) ? Global.DomainDNSAddress.FirstOrDefault() : dNSItem?.domainDNSAddress,
2024-06-26 08:00:37 +00:00
domains = [node.address]
};
servers.AsArray().Add(JsonUtils.SerializeToNode(dnsServer));
2024-06-26 08:00:37 +00:00
}
}
return 0;
}
2023-12-23 02:19:41 +00:00
private int GenStatistic(V2rayConfig v2rayConfig)
2023-04-23 12:21:52 +00:00
{
if (_config.guiItem.enableStatistics)
{
2024-03-09 06:30:39 +00:00
string tag = EInboundProtocol.api.ToString();
2023-05-07 08:35:46 +00:00
API4Ray apiObj = new();
Policy4Ray policyObj = new();
SystemPolicy4Ray policySystemSetting = new();
2023-04-23 12:21:52 +00:00
string[] services = { "StatsService" };
2023-05-07 08:35:46 +00:00
v2rayConfig.stats = new Stats4Ray();
2023-04-23 12:21:52 +00:00
apiObj.tag = tag;
apiObj.services = services.ToList();
v2rayConfig.api = apiObj;
policySystemSetting.statsOutboundDownlink = true;
policySystemSetting.statsOutboundUplink = true;
policyObj.system = policySystemSetting;
v2rayConfig.policy = policyObj;
if (!v2rayConfig.inbounds.Exists(item => item.tag == tag))
{
2023-05-07 08:35:46 +00:00
Inbounds4Ray apiInbound = new();
Inboundsettings4Ray apiInboundSettings = new();
2023-04-23 12:21:52 +00:00
apiInbound.tag = tag;
apiInbound.listen = Global.Loopback;
2024-10-07 01:51:41 +00:00
apiInbound.port = AppHandler.Instance.StatePort;
2024-02-19 09:43:36 +00:00
apiInbound.protocol = Global.InboundAPIProtocol;
2023-04-23 12:21:52 +00:00
apiInboundSettings.address = Global.Loopback;
apiInbound.settings = apiInboundSettings;
v2rayConfig.inbounds.Add(apiInbound);
}
if (!v2rayConfig.routing.rules.Exists(item => item.outboundTag == tag))
{
2023-05-07 08:35:46 +00:00
RulesItem4Ray apiRoutingRule = new()
2023-04-23 12:21:52 +00:00
{
inboundTag = new List<string> { tag },
outboundTag = tag,
type = "field"
};
2023-05-07 08:35:46 +00:00
2023-04-23 12:21:52 +00:00
v2rayConfig.routing.rules.Add(apiRoutingRule);
}
}
return 0;
}
2023-12-23 12:57:31 +00:00
private int GenMoreOutbounds(ProfileItem node, V2rayConfig v2rayConfig)
{
//fragment proxy
if (_config.coreBasicItem.enableFragment
2024-09-17 08:52:41 +00:00
&& Utils.IsNotEmpty(v2rayConfig.outbounds[0].streamSettings?.security))
{
var fragmentOutbound = new Outbounds4Ray
{
protocol = "freedom",
tag = $"{Global.ProxyTag}3",
settings = new()
{
fragment = new()
{
packets = "tlshello",
length = "100-200",
interval = "10-20"
}
}
};
v2rayConfig.outbounds.Add(fragmentOutbound);
v2rayConfig.outbounds[0].streamSettings.sockopt = new()
{
dialerProxy = fragmentOutbound.tag
};
return 0;
}
2023-12-23 12:57:31 +00:00
if (node.subid.IsNullOrEmpty())
{
return 0;
}
try
{
2024-10-07 01:51:41 +00:00
var subItem = AppHandler.Instance.GetSubItem(node.subid);
2023-12-23 12:57:31 +00:00
if (subItem is null)
{
return 0;
}
//current proxy
var outbound = v2rayConfig.outbounds[0];
2024-03-26 06:26:03 +00:00
var txtOutbound = Utils.GetEmbedText(Global.V2raySampleOutbound);
2023-12-23 12:57:31 +00:00
//Previous proxy
2024-10-07 01:51:41 +00:00
var prevNode = AppHandler.Instance.GetProfileItemViaRemarks(subItem.prevProfile);
2023-12-25 08:42:23 +00:00
if (prevNode is not null
&& prevNode.configType != EConfigType.Custom
&& prevNode.configType != EConfigType.Hysteria2
2024-09-23 09:17:12 +00:00
&& prevNode.configType != EConfigType.TUIC
&& prevNode.configType != EConfigType.WireGuard)
2023-12-23 12:57:31 +00:00
{
2024-03-26 06:26:03 +00:00
var prevOutbound = JsonUtils.Deserialize<Outbounds4Ray>(txtOutbound);
2023-12-23 12:57:31 +00:00
GenOutbound(prevNode, prevOutbound);
prevOutbound.tag = $"{Global.ProxyTag}2";
v2rayConfig.outbounds.Add(prevOutbound);
outbound.streamSettings.sockopt = new()
{
dialerProxy = prevOutbound.tag
};
}
//Next proxy
2024-10-07 01:51:41 +00:00
var nextNode = AppHandler.Instance.GetProfileItemViaRemarks(subItem.nextProfile);
2023-12-25 08:42:23 +00:00
if (nextNode is not null
&& nextNode.configType != EConfigType.Custom
&& nextNode.configType != EConfigType.Hysteria2
2024-09-23 09:17:12 +00:00
&& nextNode.configType != EConfigType.TUIC
&& nextNode.configType != EConfigType.WireGuard)
2023-12-23 12:57:31 +00:00
{
2024-03-26 06:26:03 +00:00
var nextOutbound = JsonUtils.Deserialize<Outbounds4Ray>(txtOutbound);
2023-12-23 12:57:31 +00:00
GenOutbound(nextNode, nextOutbound);
nextOutbound.tag = Global.ProxyTag;
v2rayConfig.outbounds.Insert(0, nextOutbound);
outbound.tag = $"{Global.ProxyTag}1";
nextOutbound.streamSettings.sockopt = new()
{
dialerProxy = outbound.tag
};
}
}
catch (Exception ex)
{
2024-01-10 02:43:48 +00:00
Logging.SaveLog(ex.Message, ex);
2023-12-23 12:57:31 +00:00
}
return 0;
}
#endregion private gen function
2023-04-23 12:21:52 +00:00
}
}