Skip to content

Commit

Permalink
Improve
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Jul 14, 2024
1 parent c79e2e3 commit 4e9dfe5
Show file tree
Hide file tree
Showing 11 changed files with 817 additions and 742 deletions.
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Handler/ConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ public static int AddCustomServer4Multiple(Config config, List<ProfileItem> sele
return -1;
}

var profileItem = LazyConfig.Instance.GetProfileItem(config.indexId) ?? new();
var profileItem = LazyConfig.Instance.GetProfileItem(indexId) ?? new();
profileItem.indexId = indexId;
profileItem.remarks = "Multi-server Config";
profileItem.address = Global.CoreMultipleLoadConfigFileName;
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Handler/CoreConfig/CoreConfigClash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public CoreConfigClash(Config config)
/// <param name="fileName"></param>
/// <param name="msg"></param>
/// <returns></returns>
public int GenerateClientConfig(ProfileItem node, string? fileName, out string msg)
public int GenerateClientCustomConfig(ProfileItem node, string? fileName, out string msg)
{
if (node == null || fileName is null)
{
Expand Down
7 changes: 6 additions & 1 deletion v2rayN/v2rayN/Handler/CoreConfig/CoreConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ public static int GenerateClientConfig(ProfileItem node, string? fileName, out s
if (node.coreType is ECoreType.clash or ECoreType.clash_meta or ECoreType.mihomo)
{
var configGenClash = new CoreConfigClash(config);
return configGenClash.GenerateClientConfig(node, fileName, out msg);
return configGenClash.GenerateClientCustomConfig(node, fileName, out msg);
}
if (node.coreType is ECoreType.sing_box)
{
var configGenSingbox = new CoreConfigSingbox(config);
return configGenSingbox.GenerateClientCustomConfig(node, fileName, out msg);
}
else
{
Expand Down
1,212 changes: 638 additions & 574 deletions v2rayN/v2rayN/Handler/CoreConfig/CoreConfigSingbox.cs

Large diffs are not rendered by default.

296 changes: 148 additions & 148 deletions v2rayN/v2rayN/Handler/CoreConfig/CoreConfigV2ray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public CoreConfigV2ray(Config config)
_config = config;
}

#region public gen function

public int GenerateClientConfigContent(ProfileItem node, out V2rayConfig? v2rayConfig, out string msg)
{
v2rayConfig = null;
Expand Down Expand Up @@ -69,6 +71,152 @@ public int GenerateClientConfigContent(ProfileItem node, out V2rayConfig? v2rayC
return 0;
}

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);

int httpPort = LazyConfig.Instance.GetLocalPort(EInboundProtocol.speedtest);

foreach (var it in selecteds)
{
if (it.configType == EConfigType.Custom)
{
continue;
}
if (it.port <= 0)
{
continue;
}
if (it.configType is EConfigType.VMess or EConfigType.VLESS)
{
var item2 = LazyConfig.Instance.GetProfileItem(it.indexId);
if (item2 is null || Utils.IsNullOrEmpty(item2.id) || !Utils.IsGuidByParse(item2.id))
{
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;
}
it.port = port;
it.allowTest = true;

//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
var item = LazyConfig.Instance.GetProfileItem(it.indexId);
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;
}

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

private int GenLog(V2rayConfig v2rayConfig)
Expand Down Expand Up @@ -973,153 +1121,5 @@ private int GenMoreOutbounds(ProfileItem node, V2rayConfig v2rayConfig)
}

#endregion private gen function

#region Gen speedtest config

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);

int httpPort = LazyConfig.Instance.GetLocalPort(EInboundProtocol.speedtest);

foreach (var it in selecteds)
{
if (it.configType == EConfigType.Custom)
{
continue;
}
if (it.port <= 0)
{
continue;
}
if (it.configType is EConfigType.VMess or EConfigType.VLESS)
{
var item2 = LazyConfig.Instance.GetProfileItem(it.indexId);
if (item2 is null || Utils.IsNullOrEmpty(item2.id) || !Utils.IsGuidByParse(item2.id))
{
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;
}
it.port = port;
it.allowTest = true;

//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
var item = LazyConfig.Instance.GetProfileItem(it.indexId);
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;
}

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 Gen speedtest config
}
}
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Resx/ResUI.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Resx/ResUI.fa-Ir.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@
<value>قانون</value>
</data>
<data name="menuProxiesDelaytest" xml:space="preserve">
<value>All Node Latency Test</value>
<value>Latency Test</value>
</data>
<data name="menuProxiesDelaytestPart" xml:space="preserve">
<value>Part Node Latency Test</value>
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Resx/ResUI.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@
<value>Rule</value>
</data>
<data name="menuProxiesDelaytest" xml:space="preserve">
<value>All Node Latency Test</value>
<value>Latency Test</value>
</data>
<data name="menuProxiesDelaytestPart" xml:space="preserve">
<value>Part Node Latency Test</value>
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@
<value>规则</value>
</data>
<data name="menuProxiesDelaytest" xml:space="preserve">
<value>全部节点延迟测试</value>
<value>延迟测试</value>
</data>
<data name="menuProxiesDelaytestPart" xml:space="preserve">
<value>当前部分节点延迟测试</value>
Expand Down
Loading

0 comments on commit 4e9dfe5

Please sign in to comment.