Skip to content

Commit

Permalink
Return api port=socks port +11
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Mar 2, 2024
1 parent 66e40ed commit 4b2b459
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 53 deletions.
5 changes: 2 additions & 3 deletions v2rayN/v2rayN/Common/Utile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -579,11 +579,10 @@ public static bool PortInUse(int port)
return inUse;
}

public static int GetFreePort()
public static int GetFreePort(int defaultPort = 9090)
{
try
{
int defaultPort = 9090;
if (!Utile.PortInUse(defaultPort))
{
return defaultPort;
Expand All @@ -598,7 +597,7 @@ public static int GetFreePort()
catch
{
}
return 69090;
return 59090;
}

#endregion 测速
Expand Down
40 changes: 13 additions & 27 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();
_statePort = Utile.GetFreePort(GetLocalPort(Global.InboundAPITagName));
}

return _statePort.Value;
Expand Down Expand Up @@ -52,32 +52,18 @@ public Config GetConfig()

public int GetLocalPort(string protocol)
{
int localPort = _config.inbound.FirstOrDefault(t => t.protocol == Global.InboundSocks).localPort;
if (protocol == Global.InboundSocks)
{
return localPort;
}
else if (protocol == Global.InboundHttp)
{
return localPort + 1;
}
else if (protocol == Global.InboundSocks2)
{
return localPort + 2;
}
else if (protocol == Global.InboundHttp2)
{
return localPort + 3;
}
else if (protocol == ESysProxyType.Pac.ToString())
{
return localPort + 4;
}
else if (protocol == "speedtest")
{
return localPort + 103;
}
return localPort;
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,
};
}

public void AddProcess(IntPtr processHandle)
Expand Down
24 changes: 1 addition & 23 deletions v2rayN/v2rayN/Handler/StatisticsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,28 +132,6 @@ private void GetServerStatItem(string indexId)
_serverStatItem.todayDown = 0;
_serverStatItem.dateNow = ticks;
}
}

private int GetFreePort()
{
try
{
int defaultPort = 9090;
if (!Utile.PortInUse(defaultPort))
{
return defaultPort;
}

TcpListener l = new(IPAddress.Loopback, 0);
l.Start();
int port = ((IPEndPoint)l.LocalEndpoint).Port;
l.Stop();
return port;
}
catch
{
}
return 69090;
}
}
}
}

0 comments on commit 4b2b459

Please sign in to comment.