2022-09-06 19:13:23 +02:00
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
|
using Starksoft.Aspen.Proxy;
|
2014-05-31 01:59:03 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient.Proxy
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Automatically handle proxies according to the app Settings.
|
|
|
|
|
|
/// Note: Underlying proxy handling is taken from Starksoft, LLC's Biko Library.
|
|
|
|
|
|
/// This library is open source and provided under the MIT license. More info at biko.codeplex.com.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
|
|
|
|
public static class ProxyHandler
|
|
|
|
|
|
{
|
|
|
|
|
|
public enum Type { HTTP, SOCKS4, SOCKS4a, SOCKS5 };
|
|
|
|
|
|
|
|
|
|
|
|
private static ProxyClientFactory factory = new ProxyClientFactory();
|
|
|
|
|
|
private static IProxyClient proxy;
|
|
|
|
|
|
private static bool proxy_ok = false;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a regular TcpClient or a proxied TcpClient according to the app Settings.
|
|
|
|
|
|
/// </summary>
|
2015-10-22 20:56:08 +02:00
|
|
|
|
/// <param name="host">Target host</param>
|
|
|
|
|
|
/// <param name="port">Target port</param>
|
|
|
|
|
|
/// <param name="login">True if the purpose is logging in to a Minecraft account</param>
|
2014-05-31 01:59:03 +02:00
|
|
|
|
|
2015-10-22 20:56:08 +02:00
|
|
|
|
public static TcpClient newTcpClient(string host, int port, bool login = false)
|
2014-05-31 01:59:03 +02:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2015-10-22 20:56:08 +02:00
|
|
|
|
if (login ? Settings.ProxyEnabledLogin : Settings.ProxyEnabledIngame)
|
2014-05-31 01:59:03 +02:00
|
|
|
|
{
|
|
|
|
|
|
ProxyType innerProxytype = ProxyType.Http;
|
|
|
|
|
|
|
|
|
|
|
|
switch (Settings.proxyType)
|
|
|
|
|
|
{
|
|
|
|
|
|
case Type.HTTP: innerProxytype = ProxyType.Http; break;
|
|
|
|
|
|
case Type.SOCKS4: innerProxytype = ProxyType.Socks4; break;
|
|
|
|
|
|
case Type.SOCKS4a: innerProxytype = ProxyType.Socks4a; break;
|
|
|
|
|
|
case Type.SOCKS5: innerProxytype = ProxyType.Socks5; break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (Settings.ProxyUsername != "" && Settings.ProxyPassword != "")
|
|
|
|
|
|
{
|
|
|
|
|
|
proxy = factory.CreateProxyClient(innerProxytype, Settings.ProxyHost, Settings.ProxyPort, Settings.ProxyUsername, Settings.ProxyPassword);
|
|
|
|
|
|
}
|
|
|
|
|
|
else proxy = factory.CreateProxyClient(innerProxytype, Settings.ProxyHost, Settings.ProxyPort);
|
|
|
|
|
|
|
|
|
|
|
|
if (!proxy_ok)
|
|
|
|
|
|
{
|
2020-10-17 19:41:31 +08:00
|
|
|
|
ConsoleIO.WriteLineFormatted(Translations.Get("proxy.connected", Settings.ProxyHost, Settings.ProxyPort));
|
2014-05-31 01:59:03 +02:00
|
|
|
|
proxy_ok = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return proxy.CreateConnection(host, port);
|
|
|
|
|
|
}
|
|
|
|
|
|
else return new TcpClient(host, port);
|
|
|
|
|
|
}
|
2015-04-20 17:26:16 +02:00
|
|
|
|
catch (ProxyException e)
|
2014-05-31 01:59:03 +02:00
|
|
|
|
{
|
2015-04-20 17:26:16 +02:00
|
|
|
|
ConsoleIO.WriteLineFormatted("§8" + e.Message);
|
|
|
|
|
|
proxy = null;
|
2019-04-17 05:59:02 +02:00
|
|
|
|
throw new SocketException((int)SocketError.HostUnreachable);
|
2014-05-31 01:59:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|