Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Mar 9, 2024
1 parent 9427340 commit 0e7bfa6
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 84 deletions.
8 changes: 1 addition & 7 deletions v2rayN/v2rayN/Handler/ConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,7 @@ public static int LoadConfig(ref Config? config)
};

config.inbound.Add(inItem);

//inItem = new InItem();
//inItem.protocol = "http";
//inItem.localPort = 1081;
//inItem.udpEnabled = true;

//config.inbound.Add(inItem);

}
else
{
Expand Down
4 changes: 2 additions & 2 deletions v2rayN/v2rayN/Handler/CoreConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ private static int GenerateClientCustomConfig(ProfileItem node, string? fileName
fileContent.RemoveAt(indexPort);
}

fileContent.Add($"port: {LazyConfig.Instance.GetLocalPort(Global.InboundHttp)}");
fileContent.Add($"socks-port: {LazyConfig.Instance.GetLocalPort(Global.InboundSocks)}");
fileContent.Add($"port: {LazyConfig.Instance.GetLocalPort(EInboundProtocol.http)}");
fileContent.Add($"socks-port: {LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks)}");
break;
}
File.WriteAllLines(fileName, fileContent);
Expand Down
24 changes: 12 additions & 12 deletions v2rayN/v2rayN/Handler/CoreConfigSingbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private int GenInbounds(SingboxConfig singboxConfig)
};
singboxConfig.inbounds.Add(inbound);

inbound.listen_port = LazyConfig.Instance.GetLocalPort(Global.InboundSocks);
inbound.listen_port = LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks);
inbound.sniff = _config.inbound[0].sniffingEnabled;
inbound.sniff_override_destination = _config.inbound[0].routeOnly ? false : _config.inbound[0].sniffingEnabled;
inbound.domain_strategy = Utile.IsNullOrEmpty(_config.routingBasicItem.domainStrategy4Singbox) ? null : _config.routingBasicItem.domainStrategy4Singbox;
Expand Down Expand Up @@ -405,13 +405,13 @@ private int GenOutboundTransport(ProfileItem node, Outbound4Sbox outbound)

switch (node.GetNetwork())
{
case "h2":
transport.type = "http";
case nameof(ETransport.h2):
transport.type = nameof(ETransport.http);
transport.host = Utile.IsNullOrEmpty(node.requestHost) ? null : Utile.String2List(node.requestHost);
transport.path = Utile.IsNullOrEmpty(node.path) ? null : node.path;
break;

case "tcp": //http
case nameof(ETransport.tcp): //http
if (node.headerType == Global.TcpHeaderHttp)
{
if (node.configType == EConfigType.Shadowsocks)
Expand All @@ -421,7 +421,7 @@ private int GenOutboundTransport(ProfileItem node, Outbound4Sbox outbound)
}
else
{
transport.type = "http";
transport.type = nameof(ETransport.http);
transport.host = Utile.IsNullOrEmpty(node.requestHost) ? null : Utile.String2List(node.requestHost);
transport.path = Utile.IsNullOrEmpty(node.path) ? null : node.path;
}
Expand All @@ -433,8 +433,8 @@ private int GenOutboundTransport(ProfileItem node, Outbound4Sbox outbound)

break;

case "ws":
transport.type = "ws";
case nameof(ETransport.ws):
transport.type = nameof(ETransport.ws);
transport.path = Utile.IsNullOrEmpty(node.path) ? null : node.path;
if (!Utile.IsNullOrEmpty(node.requestHost))
{
Expand All @@ -445,12 +445,12 @@ private int GenOutboundTransport(ProfileItem node, Outbound4Sbox outbound)
}
break;

case "quic":
transport.type = "quic";
case nameof(ETransport.quic):
transport.type = nameof(ETransport.quic);
break;

case "grpc":
transport.type = "grpc";
case nameof(ETransport.grpc):
transport.type = nameof(ETransport.grpc);
transport.service_name = node.path;
transport.idle_timeout = _config.grpcItem.idle_timeout.ToString("##s");
transport.ping_timeout = _config.grpcItem.health_check_timeout.ToString("##s");
Expand Down Expand Up @@ -881,7 +881,7 @@ public int GenerateClientSpeedtestConfig(List<ServerTestItem> selecteds, out Sin
singboxConfig.inbounds.Clear(); // Remove "proxy" service for speedtest, avoiding port conflicts.
singboxConfig.outbounds.RemoveAt(0);

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

foreach (var it in selecteds)
{
Expand Down
16 changes: 8 additions & 8 deletions v2rayN/v2rayN/Handler/CoreConfigV2ray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ private int GenInbounds(V2rayConfig v2rayConfig)
{
if (_config.inbound[0].newPort4LAN)
{
Inbounds4Ray inbound3 = GetInbound(_config.inbound[0], Global.InboundSocks2, 2, true);
var inbound3 = GetInbound(_config.inbound[0], Global.InboundSocks2, 2, true);
inbound3.listen = "0.0.0.0";
v2rayConfig.inbounds.Add(inbound3);

Inbounds4Ray inbound4 = GetInbound(_config.inbound[0], Global.InboundHttp2, 3, false);
var inbound4 = GetInbound(_config.inbound[0], Global.InboundHttp2, 3, false);
inbound4.listen = "0.0.0.0";
v2rayConfig.inbounds.Add(inbound4);

Expand Down Expand Up @@ -564,7 +564,7 @@ private int GenBoundStreamSettings(ProfileItem node, StreamSettings4Ray streamSe
//streamSettings
switch (node.GetNetwork())
{
case "kcp":
case nameof(ETransport.kcp):
KcpSettings4Ray kcpSettings = new()
{
mtu = _config.kcpItem.mtu,
Expand All @@ -588,7 +588,7 @@ private int GenBoundStreamSettings(ProfileItem node, StreamSettings4Ray streamSe
streamSettings.kcpSettings = kcpSettings;
break;
//ws
case "ws":
case nameof(ETransport.ws):
WsSettings4Ray wsSettings = new();
wsSettings.headers = new Headers4Ray();
string path = node.path;
Expand All @@ -608,7 +608,7 @@ private int GenBoundStreamSettings(ProfileItem node, StreamSettings4Ray streamSe

break;
//h2
case "h2":
case nameof(ETransport.h2):
HttpSettings4Ray httpSettings = new();

if (!string.IsNullOrWhiteSpace(host))
Expand All @@ -621,7 +621,7 @@ private int GenBoundStreamSettings(ProfileItem node, StreamSettings4Ray streamSe

break;
//quic
case "quic":
case nameof(ETransport.quic):
QuicSettings4Ray quicsettings = new()
{
security = host,
Expand All @@ -645,7 +645,7 @@ private int GenBoundStreamSettings(ProfileItem node, StreamSettings4Ray streamSe
}
break;

case "grpc":
case nameof(ETransport.grpc):
GrpcSettings4Ray grpcSettings = new()
{
serviceName = node.path,
Expand Down Expand Up @@ -923,7 +923,7 @@ public int GenerateClientSpeedtestConfig(List<ServerTestItem> selecteds, out V2r
v2rayConfig.inbounds.Clear(); // Remove "proxy" service for speedtest, avoiding port conflicts.
v2rayConfig.outbounds.RemoveAt(0);

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

foreach (var it in selecteds)
{
Expand Down
3 changes: 2 additions & 1 deletion v2rayN/v2rayN/Handler/DownloadHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Sockets;
using v2rayN.Model;
using v2rayN.Resx;

namespace v2rayN.Handler
Expand Down Expand Up @@ -311,7 +312,7 @@ public async Task<int> GetRealPingTime(string url, IWebProxy? webProxy, int down
{
return null;
}
var httpPort = LazyConfig.Instance.GetLocalPort(Global.InboundHttp);
var httpPort = LazyConfig.Instance.GetLocalPort(EInboundProtocol.http);
if (!SocketCheck(Global.Loopback, httpPort))
{
return null;
Expand Down
18 changes: 4 additions & 14 deletions v2rayN/v2rayN/Handler/LazyConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public int StatePort
{
if (_statePort is null)
{
_statePort = Utile.GetFreePort(GetLocalPort(Global.InboundAPITagName));
_statePort = Utile.GetFreePort(GetLocalPort(EInboundProtocol.api));
}

return _statePort.Value;
Expand Down Expand Up @@ -50,20 +50,10 @@ public Config GetConfig()
return _config;
}

public int GetLocalPort(string protocol)
public int GetLocalPort(EInboundProtocol protocol)
{
var localPort = _config.inbound.FirstOrDefault(t => t.protocol == Global.InboundSocks)?.localPort ?? 10808;
return protocol.ToLower() switch
{
Global.InboundSocks => localPort,
Global.InboundHttp => localPort + 1,
Global.InboundSocks2 => localPort + 2,
Global.InboundHttp2 => localPort + 3,
"pac" => localPort + 4,
Global.InboundAPITagName => localPort + 11,
"speedtest" => localPort + 21,
_ => localPort,
};
var localPort = _config.inbound.FirstOrDefault(t => t.protocol == nameof(EInboundProtocol.socks))?.localPort ?? 10808;
return localPort + (int)protocol;
}

public void AddProcess(IntPtr processHandle)
Expand Down
50 changes: 25 additions & 25 deletions v2rayN/v2rayN/Handler/ShareHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,27 +310,27 @@ private static int GetStdTransport(ProfileItem item, string? securityDef, ref Di
dicQuery.Add("spx", Utile.UrlEncode(item.spiderX));
}

dicQuery.Add("type", !Utile.IsNullOrEmpty(item.network) ? item.network : "tcp");
dicQuery.Add("type", !Utile.IsNullOrEmpty(item.network) ? item.network : nameof(ETransport.tcp));

switch (item.network)
{
case "tcp":
case nameof(ETransport.tcp):
dicQuery.Add("headerType", !Utile.IsNullOrEmpty(item.headerType) ? item.headerType : Global.None);
if (!Utile.IsNullOrEmpty(item.requestHost))
{
dicQuery.Add("host", Utile.UrlEncode(item.requestHost));
}
break;

case "kcp":
case nameof(ETransport.kcp):
dicQuery.Add("headerType", !Utile.IsNullOrEmpty(item.headerType) ? item.headerType : Global.None);
if (!Utile.IsNullOrEmpty(item.path))
{
dicQuery.Add("seed", Utile.UrlEncode(item.path));
}
break;

case "ws":
case nameof(ETransport.ws):
if (!Utile.IsNullOrEmpty(item.requestHost))
{
dicQuery.Add("host", Utile.UrlEncode(item.requestHost));
Expand All @@ -341,9 +341,9 @@ private static int GetStdTransport(ProfileItem item, string? securityDef, ref Di
}
break;

case "http":
case "h2":
dicQuery["type"] = "http";
case nameof(ETransport.http):
case nameof(ETransport.h2):
dicQuery["type"] = nameof(ETransport.http);
if (!Utile.IsNullOrEmpty(item.requestHost))
{
dicQuery.Add("host", Utile.UrlEncode(item.requestHost));
Expand All @@ -354,13 +354,13 @@ private static int GetStdTransport(ProfileItem item, string? securityDef, ref Di
}
break;

case "quic":
case nameof(ETransport.quic):
dicQuery.Add("headerType", !Utile.IsNullOrEmpty(item.headerType) ? item.headerType : Global.None);
dicQuery.Add("quicSecurity", Utile.UrlEncode(item.requestHost));
dicQuery.Add("key", Utile.UrlEncode(item.path));
break;

case "grpc":
case nameof(ETransport.grpc):
if (!Utile.IsNullOrEmpty(item.path))
{
dicQuery.Add("serviceName", Utile.UrlEncode(item.path));
Expand Down Expand Up @@ -599,32 +599,32 @@ private static int GetStdTransport(ProfileItem item, string? securityDef, ref Di
i.network = m.Groups["network"].Value;
switch (i.network)
{
case "tcp":
case nameof(ETransport.tcp):
string t1 = query["type"] ?? Global.None;
i.headerType = t1;
break;

case "kcp":
case nameof(ETransport.kcp):
i.headerType = query["type"] ?? Global.None;
break;

case "ws":
case nameof(ETransport.ws):
string p1 = query["path"] ?? "/";
string h1 = query["host"] ?? "";
i.requestHost = Utile.UrlDecode(h1);
i.path = p1;
break;

case "http":
case "h2":
i.network = "h2";
case nameof(ETransport.http):
case nameof(ETransport.h2):
i.network = nameof(ETransport.h2);
string p2 = query["path"] ?? "/";
string h2 = query["host"] ?? "";
i.requestHost = Utile.UrlDecode(h2);
i.path = p2;
break;

case "quic":
case nameof(ETransport.quic):
string s = query["security"] ?? Global.None;
string k = query["key"] ?? "";
string t3 = query["type"] ?? Global.None;
Expand Down Expand Up @@ -945,39 +945,39 @@ private static int ResolveStdTransport(NameValueCollection query, ref ProfileIte
item.shortId = Utile.UrlDecode(query["sid"] ?? "");
item.spiderX = Utile.UrlDecode(query["spx"] ?? "");

item.network = query["type"] ?? "tcp";
item.network = query["type"] ?? nameof(ETransport.tcp);
switch (item.network)
{
case "tcp":
case nameof(ETransport.tcp):
item.headerType = query["headerType"] ?? Global.None;
item.requestHost = Utile.UrlDecode(query["host"] ?? "");

break;

case "kcp":
case nameof(ETransport.kcp):
item.headerType = query["headerType"] ?? Global.None;
item.path = Utile.UrlDecode(query["seed"] ?? "");
break;

case "ws":
case nameof(ETransport.ws):
item.requestHost = Utile.UrlDecode(query["host"] ?? "");
item.path = Utile.UrlDecode(query["path"] ?? "/");
break;

case "http":
case "h2":
item.network = "h2";
case nameof(ETransport.http):
case nameof(ETransport.h2):
item.network = nameof(ETransport.h2);
item.requestHost = Utile.UrlDecode(query["host"] ?? "");
item.path = Utile.UrlDecode(query["path"] ?? "/");
break;

case "quic":
case nameof(ETransport.quic):
item.headerType = query["headerType"] ?? Global.None;
item.requestHost = query["quicSecurity"] ?? Global.None;
item.path = Utile.UrlDecode(query["key"] ?? "");
break;

case "grpc":
case nameof(ETransport.grpc):
item.path = Utile.UrlDecode(query["serviceName"] ?? "");
item.headerType = Utile.UrlDecode(query["mode"] ?? Global.GrpcGunMode);
break;
Expand Down
6 changes: 3 additions & 3 deletions v2rayN/v2rayN/Handler/SysProxyHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public static bool UpdateSysProxy(Config config, bool forceDisable)

try
{
int port = LazyConfig.Instance.GetLocalPort(Global.InboundHttp);
int portSocks = LazyConfig.Instance.GetLocalPort(Global.InboundSocks);
int portPac = LazyConfig.Instance.GetLocalPort(ESysProxyType.Pac.ToString());
int port = LazyConfig.Instance.GetLocalPort(EInboundProtocol.http);
int portSocks = LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks);
int portPac = LazyConfig.Instance.GetLocalPort(EInboundProtocol.pac);
if (port <= 0)
{
return false;
Expand Down
13 changes: 13 additions & 0 deletions v2rayN/v2rayN/Model/EInboundProtocol.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace v2rayN.Model
{
public enum EInboundProtocol
{
socks = 0,
http,
socks2,
http2,
pac,
api = 11,
speedtest = 21
}
}
Loading

0 comments on commit 0e7bfa6

Please sign in to comment.