public static async Task <NatTypeTestResult> DiscoveryNatTypeAsync(Socks5Server socks5, CancellationToken ctx = default)
    {
        var stunServer = Global.Settings.STUN_Server;
        var port       = (ushort)Global.Settings.STUN_Server_Port;
        var local      = new IPEndPoint(IPAddress.Any, 0);

        var socks5Option = new Socks5CreateOption
        {
            Address          = await DnsUtils.LookupAsync(socks5.Hostname),
            Port             = socks5.Port,
            UsernamePassword = new UsernamePassword
            {
                UserName = socks5.Username,
                Password = socks5.Password
            }
        };

        var ip = await DnsUtils.LookupAsync(stunServer);

        if (ip == null)
        {
            return(new NatTypeTestResult {
                Result = "Wrong STUN Server!"
            });
        }

        using IUdpProxy proxy = ProxyFactory.CreateProxy(ProxyType.Socks5, new IPEndPoint(IPAddress.Loopback, 0), socks5Option);
        using var client      = new StunClient5389UDP(new IPEndPoint(ip, port), local, proxy);

        await client.ConnectProxyAsync(ctx);

        try
        {
            await client.QueryAsync(ctx);
        }
        finally
        {
            await client.CloseProxyAsync(ctx);
        }

        var res    = client.State;
        var result = GetSimpleResult(res);

        return(new NatTypeTestResult
        {
            Result = result,
            LocalEnd = res.LocalEndPoint?.ToString(),
            PublicEnd = res.PublicEndPoint?.ToString()
        });
    }
Exemple #2
0
        /// <summary>
        ///     测试延迟
        /// </summary>
        /// <returns>延迟</returns>
        public async Task <int> PingAsync()
        {
            try
            {
                var destination = await DnsUtils.LookupAsync(Hostname);

                if (destination == null)
                {
                    return(Delay = -2);
                }

                var list = new Task <int> [3];
                for (var i = 0; i < 3; i++)
                {
                    async Task <int> PingCoreAsync()
                    {
                        try
                        {
                            return(Global.Settings.ServerTCPing
                                ? await Utils.Utils.TCPingAsync(destination, Port)
                                : await Utils.Utils.ICMPingAsync(destination));
                        }
                        catch (Exception)
                        {
                            return(-4);
                        }
                    }

                    list[i] = PingCoreAsync();
                }

                var resTask = await Task.WhenAny(list[0], list[1], list[2]);

                return(Delay = await resTask);
            }
            catch (Exception)
            {
                return(Delay = -4);
            }
        }
Exemple #3
0
 public static async Task <string> AutoResolveHostnameAsync(this Server server, AddressFamily inet = AddressFamily.Unspecified)
 {
     // ! MainController cached
     return((await DnsUtils.LookupAsync(server.Hostname, inet)) !.ToString());
 }
Exemple #4
0
 public static async Task <string> AutoResolveHostnameAsync(this Server server, AddressFamily inet = AddressFamily.Unspecified)
 {
     return(Global.Settings.ResolveServerHostname ? (await DnsUtils.LookupAsync(server.Hostname, inet)) !.ToString() : server.Hostname);
 }