Minecraft-Console-Client/MinecraftClient/Proxy/ProxyHandler.cs

106 lines
3.9 KiB
C#
Raw Normal View History

2022-10-06 14:53:05 +08:00
using System.Net.Sockets;
2022-09-06 19:13:23 +02:00
using Starksoft.Aspen.Proxy;
2022-10-05 15:02:30 +08:00
using Tomlet.Attributes;
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
{
2022-10-05 15:02:30 +08:00
public static Configs Config = new();
[TomlDoNotInlineObject]
public class Configs
{
2022-10-06 18:12:32 +08:00
[TomlInlineComment("$config.Proxy.Enabled_Login$")]
2022-10-05 15:02:30 +08:00
public bool Enabled_Login = false;
2022-10-06 18:12:32 +08:00
[TomlInlineComment("$config.Proxy.Enabled_Ingame$")]
2022-10-05 15:02:30 +08:00
public bool Enabled_Ingame = false;
2022-10-06 18:12:32 +08:00
[TomlInlineComment("$config.Proxy.Server$")]
2022-10-05 15:02:30 +08:00
public ProxyInfoConfig Server = new("0.0.0.0", 8080);
2022-10-06 18:12:32 +08:00
[TomlInlineComment("$config.Proxy.Proxy_Type$")]
2022-10-05 15:02:30 +08:00
public ProxyType Proxy_Type = ProxyType.HTTP;
2022-10-06 18:12:32 +08:00
[TomlInlineComment("$config.Proxy.Username$")]
2022-10-05 15:02:30 +08:00
public string Username = "";
2022-10-06 18:12:32 +08:00
[TomlInlineComment("$config.Proxy.Password$")]
2022-10-05 15:02:30 +08:00
public string Password = "";
public void OnSettingUpdate() { }
public struct ProxyInfoConfig
{
public string Host;
public ushort Port;
public ProxyInfoConfig(string host, ushort port)
{
Host = host;
Port = port;
}
}
public enum ProxyType { HTTP, SOCKS4, SOCKS4a, SOCKS5 };
}
private static readonly ProxyClientFactory factory = new();
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>
/// <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>
public static TcpClient NewTcpClient(string host, int port, bool login = false)
{
try
{
2022-10-05 15:02:30 +08:00
if (login ? Config.Enabled_Login : Config.Enabled_Ingame)
{
ProxyType innerProxytype = ProxyType.Http;
2022-10-05 15:02:30 +08:00
switch (Config.Proxy_Type)
{
2022-10-05 15:02:30 +08:00
case Configs.ProxyType.HTTP: innerProxytype = ProxyType.Http; break;
case Configs.ProxyType.SOCKS4: innerProxytype = ProxyType.Socks4; break;
case Configs.ProxyType.SOCKS4a: innerProxytype = ProxyType.Socks4a; break;
case Configs.ProxyType.SOCKS5: innerProxytype = ProxyType.Socks5; break;
}
2022-10-05 15:02:30 +08:00
if (Config.Username != "" && Config.Password != "")
proxy = factory.CreateProxyClient(innerProxytype, Config.Server.Host, Config.Server.Port, Config.Username, Config.Password);
else
proxy = factory.CreateProxyClient(innerProxytype, Config.Server.Host, Config.Server.Port);
if (!proxy_ok)
{
ConsoleIO.WriteLineFormatted(string.Format(Translations.proxy_connected, Config.Server.Host, Config.Server.Port));
proxy_ok = true;
}
return proxy.CreateConnection(host, port);
}
else return new TcpClient(host, port);
}
catch (ProxyException e)
{
ConsoleIO.WriteLineFormatted("§8" + e.Message);
proxy = null;
throw new SocketException((int)SocketError.HostUnreachable);
}
}
}
}