feat: support socks5 authentication

This commit is contained in:
ChsBuffer
2020-10-12 22:18:56 +08:00
parent 18b905ed0c
commit 202fdfe5a0
9 changed files with 69 additions and 33 deletions

View File

@@ -34,17 +34,7 @@ namespace Netch.Controllers
try
{
if (s.IsSocks5())
{
var server = (Socks5) s;
if (!string.IsNullOrWhiteSpace(server.Username) && !string.IsNullOrWhiteSpace(server.Password)) return false;
pPrivoxyController.Start(s, mode);
}
else
{
pPrivoxyController.Start(s, mode);
}
pPrivoxyController.Start(s, mode);
if (mode.Type == 3) NativeMethods.SetGlobal($"127.0.0.1:{Global.Settings.HTTPLocalPort}", IEProxyExceptions);
}

View File

@@ -3,6 +3,7 @@ using System.IO;
using System.Net;
using System.Threading.Tasks;
using Netch.Models;
using Netch.Servers.Socks5;
using Netch.Utils;
using static Netch.Forms.MainForm;
using static Netch.Utils.PortHelper;
@@ -28,7 +29,7 @@ namespace Netch.Controllers
{
Logging.Info($"启动主控制器: {server.Type} [{mode.Type}]{mode.Remark}");
if (server.IsSocks5() && mode.Type == 4)
if (server is Socks5 && mode.Type == 4)
{
return false;
}
@@ -101,11 +102,6 @@ namespace Netch.Controllers
private static async Task<bool> StartServer(Server server, Mode mode)
{
if (server.IsSocks5())
{
return true;
}
ServerController = ServerHelper.GetUtilByTypeName(server.Type).GetController();
if (ServerController is Guard instanceController)

View File

@@ -3,7 +3,9 @@ using System.IO;
using System.Runtime.InteropServices;
using System.ServiceProcess;
using System.Threading.Tasks;
using Netch.Forms;
using Netch.Models;
using Netch.Servers.Socks5;
using Netch.Utils;
using nfapinet;
@@ -69,7 +71,7 @@ namespace Netch.Controllers
aio_dial((int) NameList.TYPE_ADDNAME, "NTT.exe");
if (s.IsSocks5())
if (s is Socks5 socks5 && !socks5.Auth())
{
var result = DNS.Lookup(s.Hostname);
if (result == null)
@@ -78,8 +80,8 @@ namespace Netch.Controllers
return false;
}
aio_dial((int) NameList.TYPE_TCPHOST, $"{result}:{s.Port}");
aio_dial((int) NameList.TYPE_UDPHOST, $"{result}:{s.Port}");
aio_dial((int) NameList.TYPE_TCPHOST, $"{socks5.AutoResolveHostname()}:{socks5.Port}");
aio_dial((int) NameList.TYPE_UDPHOST, $"{socks5.AutoResolveHostname()}:{socks5.Port}");
}
else
{

View File

@@ -1,5 +1,7 @@
using System.IO;
using System.Text;
using Netch.Models;
using Netch.Servers.Socks5;
namespace Netch.Controllers
{
@@ -16,13 +18,21 @@ namespace Netch.Controllers
public bool Start(Server server, Mode mode)
{
var text = File.ReadAllText("bin\\default.conf")
.Replace("_BIND_PORT_", Global.Settings.HTTPLocalPort.ToString())
.Replace("_DEST_PORT_", (server.IsSocks5() ? server.Port : Global.Settings.Socks5LocalPort).ToString())
.Replace("0.0.0.0", Global.Settings.LocalAddress);
if (server.IsSocks5())
text = text.Replace("/ 127.0.0.1", $"/ {server.Hostname}");
File.WriteAllText("data\\privoxy.conf", text);
var text = new StringBuilder(File.ReadAllText("bin\\default.conf"));
text.Replace("_BIND_PORT_", Global.Settings.LocalAddress);
text.Replace("0.0.0.0", Global.Settings.LocalAddress); /* BIND_HOST */
if (server is Socks5 socks5 && !socks5.Auth())
{
text.Replace("/ 127.0.0.1", $"/ {server.AutoResolveHostname()}"); /* DEST_HOST */
text.Replace("_DEST_PORT_", socks5.Port.ToString());
}
text.Replace("_DEST_PORT_", Global.Settings.Socks5LocalPort.ToString());
File.WriteAllText("data\\privoxy.conf", text.ToString());
return StartInstanceAuto("..\\data\\privoxy.conf");
}

View File

@@ -10,6 +10,7 @@ using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Netch.Models;
using Netch.Servers.Socks5;
using Netch.Utils;
namespace Netch.Controllers
@@ -87,7 +88,7 @@ namespace Netch.Controllers
}
var argument = new StringBuilder();
if (s.IsSocks5())
if (s is Socks5 socks5 && !socks5.Auth())
argument.Append($"-proxyServer {_serverAddresses}:{s.Port} ");
else
argument.Append($"-proxyServer 127.0.0.1:{Global.Settings.Socks5LocalPort} ");

View File

@@ -41,8 +41,6 @@ namespace Netch.Models
/// </summary>
public int Delay = -1;
public bool IsSocks5() => Type == "Socks5";
/// <summary>
/// 获取备注
/// </summary>

View File

@@ -0,0 +1,39 @@
using System.IO;
using Netch.Controllers;
using Netch.Models;
using Netch.Servers.VMess.Utils;
namespace Netch.Servers.Socks5
{
public class S5Controller : Guard, IServerController
{
public override string Name { get; protected set; } = "Socks5";
public override string MainFile { get; protected set; } = "v2ray.exe";
public bool Start(Server s, Mode mode)
{
if (((Socks5) s).Auth())
{
File.WriteAllText("data\\last.json", V2rayConfigUtils.GenerateClientConfig(s, mode));
if (StartInstanceAuto("-config ..\\data\\last.json"))
{
return true;
}
return false;
}
return true;
}
public override void Stop()
{
if (Instance != null)
StopInstance();
}
public int? Socks5LocalPort { get; set; }
public string LocalAddress { get; set; }
}
}

View File

@@ -38,7 +38,7 @@ namespace Netch.Servers.Socks5
public IServerController GetController()
{
return null;
return new S5Controller();
}
public IEnumerable<Server> ParseUri(string text)

View File

@@ -34,9 +34,9 @@ namespace Netch.Servers.VMess.Utils
{
var inbound = new Inbounds
{
port = MainController.ServerController.Socks5LocalPort(),
port = Global.Settings.Socks5LocalPort,
protocol = "socks",
listen = MainController.ServerController.LocalAddress(),
listen = Global.Settings.LocalAddress,
settings = new Inboundsettings
{
udp = true