Refactor Inbound

This commit is contained in:
2dust 2022-04-14 09:56:11 +08:00
parent f110446738
commit 977f0428e2
12 changed files with 145 additions and 72 deletions

View file

@ -1277,26 +1277,12 @@ namespace v2rayN.Forms
}
ConfigHandler.SaveConfig(ref config, false);
DisplayToolStatus();
}
private void DisplayToolStatus()
{
StringBuilder sb = new StringBuilder();
sb.Append($"{Global.InboundSocks} {Global.Loopback}:{config.GetLocalPort(Global.InboundSocks)}");
sb.Append(" | ");
sb.Append($"{Global.InboundHttp} {Global.Loopback}:{config.GetLocalPort(Global.InboundHttp)}");
if (config.sysProxyType == ESysProxyType.ForcedChange)
{
sb.Append(" | ");
sb.Append($"{ResUI.SystemProxy} {Global.Loopback}:{config.GetLocalPort(Global.InboundHttp2)}");
}
mainMsgControl.SetToolSslInfo("inbound", sb.ToString());
mainMsgControl.DisplayToolStatus(config);
notifyMain.Icon = MainFormHandler.Instance.GetNotifyIcon(config, this.Icon);
}
#endregion

View file

@ -9,6 +9,7 @@ using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using v2rayN.Base;
using v2rayN.Mode;
using v2rayN.Resx;
namespace v2rayN.Forms
@ -78,6 +79,32 @@ namespace v2rayN.Forms
});
}
public void DisplayToolStatus(Config config)
{
StringBuilder sb = new StringBuilder();
sb.Append($"{ResUI.LabLocal}:");
sb.Append($"[{Global.InboundSocks}:{config.GetLocalPort(Global.InboundSocks)}]");
sb.Append(" | ");
if (config.sysProxyType == ESysProxyType.ForcedChange)
{
sb.Append($"[{Global.InboundHttp}({ResUI.SystemProxy}):{config.GetLocalPort(Global.InboundHttp)}]");
}
else
{
sb.Append($"[{Global.InboundHttp}:{config.GetLocalPort(Global.InboundHttp)}]");
}
if (config.inbound[0].allowLANConn)
{
sb.Append($" {ResUI.LabLAN}:");
sb.Append($"[{Global.InboundSocks}:{config.GetLocalPort(Global.InboundSocks2)}]");
sb.Append(" | ");
sb.Append($"[{Global.InboundHttp}:{config.GetLocalPort(Global.InboundHttp2)}]");
}
SetToolSslInfo("inbound", sb.ToString());
}
public void SetToolSslInfo(string type, string value)
{
switch (type)

View file

@ -65,9 +65,10 @@ namespace v2rayN
/// </summary>
public const string v2raySampleHttpresponseFileName = "v2rayN.Sample.SampleHttpresponse.txt";
public const string CustomRoutingFileName = "v2rayN.Sample.custom_routing_";
public const string v2raySampleInbound = "v2rayN.Sample.SampleInbound.txt";
/// <summary>
/// 默认加密方式
@ -112,6 +113,7 @@ namespace v2rayN
public const string InboundSocks = "socks";
public const string InboundHttp = "http";
public const string InboundSocks2 = "socks2";
public const string InboundHttp2 = "http2";
public const string Loopback = "127.0.0.1";
public const string InboundAPITagName = "api";

View file

@ -172,7 +172,7 @@ namespace v2rayN.Handler
{
if (webProxy == null)
{
var httpPort = LazyConfig.Instance.GetConfig().GetLocalPort(Global.InboundHttp2);
var httpPort = LazyConfig.Instance.GetConfig().GetLocalPort(Global.InboundHttp);
webProxy = new WebProxy(Global.Loopback, httpPort);
}
@ -233,7 +233,7 @@ namespace v2rayN.Handler
{
return null;
}
var httpPort = LazyConfig.Instance.GetConfig().GetLocalPort(Global.InboundHttp2);
var httpPort = LazyConfig.Instance.GetConfig().GetLocalPort(Global.InboundHttp);
var webProxy = new WebProxy(Global.Loopback, httpPort);
if (RunAvailabilityCheck(webProxy) > 0)
{

View file

@ -59,7 +59,7 @@ namespace v2rayN.Handler
try
{
int port = config.GetLocalPort(Global.InboundHttp2);
int port = config.GetLocalPort(Global.InboundHttp);
if (port <= 0)
{
return false;

View file

@ -146,48 +146,35 @@ namespace v2rayN.Handler
{
try
{
Inbounds inbound = v2rayConfig.inbounds[0];
inbound.tag = Global.InboundSocks;
inbound.port = config.inbound[0].localPort;
inbound.protocol = config.inbound[0].protocol;
if (config.inbound[0].allowLANConn)
{
inbound.listen = "0.0.0.0";
}
else
{
inbound.listen = Global.Loopback;
}
//udp
inbound.settings.udp = config.inbound[0].udpEnabled;
inbound.sniffing.enabled = config.inbound[0].sniffingEnabled;
v2rayConfig.inbounds = new List<Inbounds>();
Inbounds inbound = GetInbound(config.inbound[0], Global.InboundSocks, 0, true);
v2rayConfig.inbounds.Add(inbound);
//http
Inbounds inbound2 = v2rayConfig.inbounds[1];
inbound2.tag = Global.InboundHttp;
inbound2.port = config.GetLocalPort(Global.InboundHttp);
inbound2.protocol = Global.InboundHttp;
inbound2.listen = inbound.listen;
inbound2.settings.allowTransparent = false;
inbound2.sniffing.enabled = inbound.sniffing.enabled;
Inbounds inbound2 = GetInbound(config.inbound[0], Global.InboundHttp, 1, false);
v2rayConfig.inbounds.Add(inbound2);
//auth
if (!Utils.IsNullOrEmpty(config.inbound[0].user) && !Utils.IsNullOrEmpty(config.inbound[0].pass))
if (config.inbound[0].allowLANConn)
{
inbound.settings.auth = "password";
inbound.settings.accounts = new List<AccountsItem> { new AccountsItem() { user = config.inbound[0].user, pass = config.inbound[0].pass } };
inbound2.settings.auth = "password";
inbound2.settings.accounts = new List<AccountsItem> { new AccountsItem() { user = config.inbound[0].user, pass = config.inbound[0].pass } };
}
Inbounds inbound3 = GetInbound(config.inbound[0], Global.InboundSocks2, 2, true);
v2rayConfig.inbounds.Add(inbound3);
//http Loopback
Inbounds inbound3 = v2rayConfig.inbounds[2];
inbound3.tag = Global.InboundHttp2;
inbound3.port = config.GetLocalPort(Global.InboundHttp2);
inbound3.protocol = Global.InboundHttp;
inbound3.listen = Global.Loopback;
inbound3.settings.allowTransparent = false;
inbound3.sniffing.enabled = inbound.sniffing.enabled;
Inbounds inbound4 = GetInbound(config.inbound[0], Global.InboundHttp2, 3, false);
v2rayConfig.inbounds.Add(inbound4);
//auth
if (!Utils.IsNullOrEmpty(config.inbound[0].user) && !Utils.IsNullOrEmpty(config.inbound[0].pass))
{
inbound3.listen = "0.0.0.0";
inbound3.settings.auth = "password";
inbound3.settings.accounts = new List<AccountsItem> { new AccountsItem() { user = config.inbound[0].user, pass = config.inbound[0].pass } };
inbound4.listen = "0.0.0.0";
inbound4.settings.auth = "password";
inbound4.settings.accounts = new List<AccountsItem> { new AccountsItem() { user = config.inbound[0].user, pass = config.inbound[0].pass } };
}
}
}
catch
{
@ -195,6 +182,28 @@ namespace v2rayN.Handler
return 0;
}
private static Inbounds GetInbound(InItem inItem, string tag, int offset, bool bSocks)
{
string result = Utils.GetEmbedText(Global.v2raySampleInbound);
if (Utils.IsNullOrEmpty(result))
{
return null;
}
var inbound = Utils.FromJson<Inbounds>(result);
if (inbound == null)
{
return null;
}
inbound.tag = tag;
inbound.port = inItem.localPort + offset;
inbound.protocol = bSocks ? Global.InboundSocks : Global.InboundHttp;
inbound.settings.udp = inItem.udpEnabled;
inbound.sniffing.enabled = inItem.sniffingEnabled;
return inbound;
}
/// <summary>
/// 路由
/// </summary>
@ -958,7 +967,7 @@ namespace v2rayN.Handler
break;
case ECoreType.clash:
case ECoreType.clash_meta:
fileContent.Add($"port: {LazyConfig.Instance.GetConfig().GetLocalPort(Global.InboundHttp2)}");
fileContent.Add($"port: {LazyConfig.Instance.GetConfig().GetLocalPort(Global.InboundHttp)}");
fileContent.Add($"socks-port: {LazyConfig.Instance.GetConfig().GetLocalPort(Global.InboundSocks)}");
break;
}

View file

@ -208,27 +208,27 @@ namespace v2rayN.Mode
public int GetLocalPort(string protocol)
{
if (protocol == Global.InboundHttp)
int localPort = inbound.FirstOrDefault(t => t.protocol == Global.InboundSocks).localPort;
if (protocol == Global.InboundSocks)
{
return GetLocalPort(Global.InboundSocks) + 1;
return localPort;
}
else if (protocol == Global.InboundHttp)
{
return localPort + 1;
}
else if (protocol == Global.InboundSocks2)
{
return localPort + 2;
}
else if (protocol == Global.InboundHttp2)
{
return GetLocalPort(Global.InboundSocks) + 2;
return localPort + 3;
}
else if (protocol == "speedtest")
{
return GetLocalPort(Global.InboundSocks) + 103;
}
int localPort = 0;
foreach (InItem inItem in inbound)
{
if (inItem.protocol.Equals(protocol))
{
localPort = inItem.localPort;
break;
}
return localPort + 103;
}
return localPort;
}

View file

@ -321,6 +321,24 @@ namespace v2rayN.Resx {
}
}
/// <summary>
/// 查找类似 LAN 的本地化字符串。
/// </summary>
internal static string LabLAN {
get {
return ResourceManager.GetString("LabLAN", resourceCulture);
}
}
/// <summary>
/// 查找类似 Local 的本地化字符串。
/// </summary>
internal static string LabLocal {
get {
return ResourceManager.GetString("LabLocal", resourceCulture);
}
}
/// <summary>
/// 查找类似 Address 的本地化字符串。
/// </summary>

View file

@ -463,4 +463,10 @@
<data name="TooManyServersTip" xml:space="preserve">
<value>Too many servers, please open the main interface</value>
</data>
<data name="LabLAN" xml:space="preserve">
<value>LAN</value>
</data>
<data name="LabLocal" xml:space="preserve">
<value>Local</value>
</data>
</root>

View file

@ -463,4 +463,10 @@
<data name="TooManyServersTip" xml:space="preserve">
<value>服务器太多,请打开主界面操作</value>
</data>
<data name="LabLAN" xml:space="preserve">
<value>局域网</value>
</data>
<data name="LabLocal" xml:space="preserve">
<value>本地</value>
</data>
</root>

View file

@ -0,0 +1,18 @@
{
"tag": "tag1",
"port": 10808,
"protocol": "socks",
"listen": "127.0.0.1",
"settings": {
"auth": "noauth",
"udp": true,
"allowTransparent": false
},
"sniffing": {
"enabled": true,
"destOverride": [
"http",
"tls"
]
}
}

View file

@ -474,6 +474,7 @@
<None Include="Resources\minimize.png" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Sample\SampleInbound.txt" />
<None Include="Resources\share.png" />
<None Include="Resources\promotion.png" />
<None Include="Resources\sub.png" />