mirror of
https://github.com/netchx/netch.git
synced 2026-05-11 23:45:06 +08:00
refactor: change port type to ushort
refactor: StatusPortInfoText
This commit is contained in:
@@ -4,7 +4,7 @@ namespace Netch.Controllers
|
||||
{
|
||||
public interface IServerController : IController
|
||||
{
|
||||
public int? Socks5LocalPort { get; set; }
|
||||
public ushort? Socks5LocalPort { get; set; }
|
||||
|
||||
public string LocalAddress { get; set; }
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace Netch.Controllers
|
||||
|
||||
public static class ServerControllerExtension
|
||||
{
|
||||
public static int Socks5LocalPort(this IServerController controller)
|
||||
public static ushort Socks5LocalPort(this IServerController controller)
|
||||
{
|
||||
return controller.Socks5LocalPort ?? Global.Settings.Socks5LocalPort;
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ namespace Netch.Controllers
|
||||
|
||||
private static async Task<bool> StartMode(Server server, Mode mode)
|
||||
{
|
||||
var port = 0;
|
||||
ushort port = 0;
|
||||
switch (mode.Type)
|
||||
{
|
||||
case 0:
|
||||
@@ -193,7 +193,7 @@ namespace Netch.Controllers
|
||||
/// <param name="portName">端口用途名称</param>
|
||||
/// <param name="portType"></param>
|
||||
/// <exception cref="PortInUseException"></exception>
|
||||
private static void PortCheckAndShowMessageBox(int port, string portName, PortType portType = PortType.Both)
|
||||
private static void PortCheckAndShowMessageBox(ushort port, string portName, PortType portType = PortType.Both)
|
||||
{
|
||||
if (PortInUse(port, portType))
|
||||
{
|
||||
|
||||
@@ -183,34 +183,55 @@ namespace Netch.Forms
|
||||
|
||||
public static class StatusPortInfoText
|
||||
{
|
||||
public static int Socks5Port = 0;
|
||||
public static int HttpPort = 0;
|
||||
public static bool ShareLan = false;
|
||||
private static ushort? _socks5Port;
|
||||
private static ushort? _httpPort;
|
||||
private static bool? _shareLan;
|
||||
|
||||
public static bool ShareLan
|
||||
{
|
||||
set => _shareLan = value;
|
||||
}
|
||||
|
||||
public static ushort HttpPort
|
||||
{
|
||||
set => _httpPort = value;
|
||||
}
|
||||
|
||||
public static ushort Socks5Port
|
||||
{
|
||||
set => _socks5Port = value;
|
||||
}
|
||||
|
||||
public static string Value
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Socks5Port == 0 && HttpPort == 0)
|
||||
if (_socks5Port == null && _httpPort == null)
|
||||
return string.Empty;
|
||||
|
||||
var text = new StringBuilder();
|
||||
if (ShareLan)
|
||||
if (_shareLan == true)
|
||||
text.Append(i18N.Translate("Allow other Devices to connect") + " ");
|
||||
|
||||
if (Socks5Port != 0)
|
||||
text.Append($"Socks5 {i18N.Translate("Local Port", ": ")}{Socks5Port}");
|
||||
if (_socks5Port != null)
|
||||
text.Append($"Socks5 {i18N.Translate("Local Port", ": ")}{_socks5Port}");
|
||||
|
||||
if (HttpPort != 0)
|
||||
if (_httpPort != null)
|
||||
{
|
||||
if (Socks5Port != 0)
|
||||
if (_socks5Port != null)
|
||||
text.Append(" | ");
|
||||
text.Append($"HTTP {i18N.Translate("Local Port", ": ")}{HttpPort}");
|
||||
text.Append($"HTTP {i18N.Translate("Local Port", ": ")}{_httpPort}");
|
||||
}
|
||||
|
||||
return $" ({text})";
|
||||
}
|
||||
}
|
||||
|
||||
public static void Reset()
|
||||
{
|
||||
_httpPort = _socks5Port = null;
|
||||
_shareLan = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -132,20 +132,17 @@ namespace Netch.Forms
|
||||
|
||||
#region Port
|
||||
|
||||
int socks5LocalPort;
|
||||
int httpLocalPort;
|
||||
int redirectorTCPPort;
|
||||
ushort socks5LocalPort;
|
||||
ushort httpLocalPort;
|
||||
ushort redirectorTCPPort;
|
||||
try
|
||||
{
|
||||
socks5LocalPort = int.Parse(Socks5PortTextBox.Text);
|
||||
httpLocalPort = int.Parse(HTTPPortTextBox.Text);
|
||||
redirectorTCPPort = int.Parse(RedirectorTextBox.Text);
|
||||
socks5LocalPort = ushort.Parse(Socks5PortTextBox.Text);
|
||||
httpLocalPort = ushort.Parse(HTTPPortTextBox.Text);
|
||||
redirectorTCPPort = ushort.Parse(RedirectorTextBox.Text);
|
||||
|
||||
static void CheckPort(string portName, int port, int originPort, PortType portType = PortType.Both)
|
||||
static void CheckPort(string portName, ushort port, ushort originPort, PortType portType = PortType.Both)
|
||||
{
|
||||
if (port <= 0 || port > 65536)
|
||||
throw new FormatException();
|
||||
|
||||
if (port == originPort)
|
||||
return;
|
||||
|
||||
|
||||
@@ -116,22 +116,22 @@ namespace Netch.Models
|
||||
/// <summary>
|
||||
/// HTTP 本地端口
|
||||
/// </summary>
|
||||
public int HTTPLocalPort = 2802;
|
||||
public ushort HTTPLocalPort = 2802;
|
||||
|
||||
/// <summary>
|
||||
/// Socks5 本地端口
|
||||
/// </summary>
|
||||
public int Socks5LocalPort = 2801;
|
||||
public ushort Socks5LocalPort = 2801;
|
||||
|
||||
/// <summary>
|
||||
/// Redirector TCP 占用端口
|
||||
/// </summary>
|
||||
public int RedirectorTCPPort = 3901;
|
||||
public ushort RedirectorTCPPort = 3901;
|
||||
|
||||
/// <summary>
|
||||
/// UDP Socket 占用端口
|
||||
/// </summary>
|
||||
public int UDPSocketPort = 18291;
|
||||
public ushort UDPSocketPort = 18291;
|
||||
|
||||
/// <summary>
|
||||
/// HTTP 和 Socks5 本地代理地址
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Netch.Servers.Shadowsocks
|
||||
public override string Name { get; protected set; } = "Shadowsocks";
|
||||
public override string MainFile { get; protected set; } = "Shadowsocks.exe";
|
||||
|
||||
public int? Socks5LocalPort { get; set; }
|
||||
public ushort? Socks5LocalPort { get; set; }
|
||||
public string LocalAddress { get; set; }
|
||||
|
||||
private Mode _savedMode;
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Netch.Servers.ShadowsocksR
|
||||
|
||||
public override string Name { get; protected set; } = "ShadowsocksR";
|
||||
|
||||
public int? Socks5LocalPort { get; set; }
|
||||
public ushort? Socks5LocalPort { get; set; }
|
||||
public string LocalAddress { get; set; }
|
||||
|
||||
public bool Start(Server s, Mode mode)
|
||||
|
||||
@@ -33,7 +33,7 @@ namespace Netch.Servers.Socks5
|
||||
StopInstance();
|
||||
}
|
||||
|
||||
public int? Socks5LocalPort { get; set; }
|
||||
public ushort? Socks5LocalPort { get; set; }
|
||||
|
||||
public string LocalAddress { get; set; }
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace Netch.Servers.Trojan
|
||||
|
||||
public override string MainFile { get; protected set; } = "Trojan.exe";
|
||||
public override string Name { get; protected set; } = "Trojan";
|
||||
public int? Socks5LocalPort { get; set; }
|
||||
public ushort? Socks5LocalPort { get; set; }
|
||||
public string LocalAddress { get; set; }
|
||||
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace Netch.Servers.VLESS
|
||||
public override string Name { get; protected set; } = "VLESS";
|
||||
public override string MainFile { get; protected set; } = "v2ray.exe";
|
||||
|
||||
public int? Socks5LocalPort { get; set; }
|
||||
public ushort? Socks5LocalPort { get; set; }
|
||||
|
||||
public string LocalAddress { get; set; }
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace Netch.Servers.VMess
|
||||
|
||||
public override string Name { get; protected set; } = "V2Ray";
|
||||
public override string MainFile { get; protected set; } = "v2ray.exe";
|
||||
public int? Socks5LocalPort { get; set; }
|
||||
public ushort? Socks5LocalPort { get; set; }
|
||||
public string LocalAddress { get; set; }
|
||||
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@ namespace Netch.Utils
|
||||
{
|
||||
public static class PortHelper
|
||||
{
|
||||
private static readonly List<int[]> TCPExcludedRanges = new List<int[]>();
|
||||
private static readonly List<int[]> UDPExcludedRanges = new List<int[]>();
|
||||
private static readonly List<ushort[]> TCPExcludedRanges = new List<ushort[]>();
|
||||
private static readonly List<ushort[]> UDPExcludedRanges = new List<ushort[]>();
|
||||
|
||||
static PortHelper()
|
||||
{
|
||||
@@ -24,7 +24,7 @@ namespace Netch.Utils
|
||||
}
|
||||
}
|
||||
|
||||
private static void GetExcludedPortRange(PortType portType, ref List<int[]> targetList)
|
||||
private static void GetExcludedPortRange(PortType portType, ref List<ushort[]> targetList)
|
||||
{
|
||||
var lines = new List<string>();
|
||||
var process = new Process
|
||||
@@ -63,9 +63,9 @@ namespace Netch.Utils
|
||||
|
||||
var value = line.Trim().Split(' ').Where(s => s != string.Empty);
|
||||
|
||||
var port = 0;
|
||||
ushort port = 0;
|
||||
var _ = (from s1 in value
|
||||
where int.TryParse(s1, out port)
|
||||
where ushort.TryParse(s1, out port)
|
||||
select port).ToArray();
|
||||
|
||||
targetList.Add(_);
|
||||
@@ -80,7 +80,7 @@ namespace Netch.Utils
|
||||
/// <param name="type">端口类型</param>
|
||||
/// <returns>是否是保留端口</returns>
|
||||
/// <exception cref="ArgumentOutOfRangeException"></exception>
|
||||
private static bool IsPortExcluded(int port, PortType type)
|
||||
private static bool IsPortExcluded(ushort port, PortType type)
|
||||
{
|
||||
return type switch
|
||||
{
|
||||
@@ -97,7 +97,7 @@ namespace Netch.Utils
|
||||
/// <param name="port">端口</param>
|
||||
/// <param name="type">检查端口类型</param>
|
||||
/// <returns>是否被占用</returns>
|
||||
public static bool PortInUse(int port, PortType type = PortType.Both)
|
||||
public static bool PortInUse(ushort port, PortType type = PortType.Both)
|
||||
{
|
||||
var netInfo = IPGlobalProperties.GetIPGlobalProperties();
|
||||
var isTcpUsed = type != PortType.UDP &&
|
||||
@@ -111,11 +111,12 @@ namespace Netch.Utils
|
||||
return isPortExcluded && (isTcpUsed || isUdpUsed);
|
||||
}
|
||||
|
||||
public static int GetAvailablePort()
|
||||
public static ushort GetAvailablePort()
|
||||
{
|
||||
for (var i = 0; i < 55535; i++)
|
||||
var random = new Random();
|
||||
for (ushort i = 0; i < 55535; i++)
|
||||
{
|
||||
var p = new Random().Next(10000, 65535);
|
||||
var p = (ushort) random.Next(10000, 65535);
|
||||
if (!PortInUse(p))
|
||||
{
|
||||
return p;
|
||||
@@ -128,7 +129,7 @@ namespace Netch.Utils
|
||||
/// <summary>
|
||||
/// 记录Netch使用的端口
|
||||
/// </summary>
|
||||
public static readonly List<int> UsingPorts = new List<int>();
|
||||
public static readonly List<ushort> UsingPorts = new List<ushort>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user