public async Task ProxyTest() { using var proxy = ProxyFactory.CreateProxy(ProxyType.Socks5, IPEndPoint.Parse(@"0.0.0.0:0"), IPEndPoint.Parse(@"127.0.0.1:10000")); using var client = new StunClient5389UDP(@"stun.syncthing.net", 3478, new IPEndPoint(IPAddress.Any, 0), proxy); var result = await client.QueryAsync(); Assert.AreEqual(result.BindingTestResult, BindingTestResult.Success); Assert.IsNotNull(result.LocalEndPoint); Assert.IsNotNull(result.PublicEndPoint); Assert.IsNotNull(result.OtherEndPoint); Assert.AreNotEqual(result.LocalEndPoint.Address, IPAddress.Any); Assert.IsTrue(result.MappingBehavior == MappingBehavior.Direct || result.MappingBehavior == MappingBehavior.EndpointIndependent || result.MappingBehavior == MappingBehavior.AddressDependent || result.MappingBehavior == MappingBehavior.AddressAndPortDependent); Assert.IsTrue(result.FilteringBehavior == FilteringBehavior.EndpointIndependent || result.FilteringBehavior == FilteringBehavior.AddressDependent || result.FilteringBehavior == FilteringBehavior.AddressAndPortDependent); Console.WriteLine(result.BindingTestResult); Console.WriteLine(result.MappingBehavior); Console.WriteLine(result.FilteringBehavior); Console.WriteLine(result.OtherEndPoint); Console.WriteLine(result.LocalEndPoint); Console.WriteLine(result.PublicEndPoint); }
private async Task DiscoveryNatTypeImpl(CancellationToken token) { var server = new StunServer(); if (!server.Parse(_config.StunServer)) { throw new Exception(@"Wrong STUN Server!"); } using var proxy = ProxyFactory.CreateProxy( _config.ProxyType, Result5389.LocalEndPoint, NetUtils.ParseEndpoint(_config.ProxyServer), _config.ProxyUser, _config.ProxyPassword ); using var client = new StunClient5389UDP(server.Hostname, server.Port, Result5389.LocalEndPoint, proxy); Result5389 = client.Status; await client.QueryAsync(); var cache = new StunResult5389(); cache.Clone(client.Status); cache.LocalEndPoint = client.LocalEndPoint; Result5389 = cache; }
/// <summary> /// stun.qq.com 3478 0.0.0.0:0 /// </summary> private static async Task Main(string[] args) { var server = @"stun.syncthing.net"; ushort port = 3478; IPEndPoint?local = null; if (args.Length > 0 && (Uri.CheckHostName(args[0]) == UriHostNameType.Dns || IPAddress.TryParse(args[0], out _))) { server = args[0]; } if (args.Length > 1) { ushort.TryParse(args[1], out port); } if (args.Length > 2) { local = NetUtils.ParseEndpoint(args[2]); } using var client = new StunClient5389UDP(server, port, local); await client.QueryAsync(); var res = client.Status; Console.WriteLine($@"Other address is {res.OtherEndPoint}"); Console.WriteLine($@"Binding test: {res.BindingTestResult}"); Console.WriteLine($@"Local address: {res.LocalEndPoint}"); Console.WriteLine($@"Mapped address: {res.PublicEndPoint}"); Console.WriteLine($@"Nat mapping behavior: {res.MappingBehavior}"); Console.WriteLine($@"Nat filtering behavior: {res.FilteringBehavior}"); }
public async Task BindingTest() { using var client = new StunClient5389UDP(@"stun.syncthing.net", 3478, new IPEndPoint(IPAddress.Any, 0)); var result = await client.BindingTestAsync(); Assert.AreEqual(result.BindingTestResult, BindingTestResult.Success); Assert.IsNotNull(result.LocalEndPoint); Assert.IsNotNull(result.PublicEndPoint); Assert.IsNotNull(result.OtherEndPoint); Assert.AreNotEqual(result.LocalEndPoint.Address, IPAddress.Any); Assert.AreEqual(result.MappingBehavior, MappingBehavior.Unknown); Assert.AreEqual(result.FilteringBehavior, FilteringBehavior.Unknown); }
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() }); }
public async Task FilteringBehaviorTest() { using var client = new StunClient5389UDP(@"stun.syncthing.net", 3478, new IPEndPoint(IPAddress.Any, 0)); await client.FilteringBehaviorTestAsync(); var result = client.Status; Assert.AreEqual(result.BindingTestResult, BindingTestResult.Success); Assert.IsNotNull(result.LocalEndPoint); Assert.IsNotNull(result.PublicEndPoint); Assert.IsNotNull(result.OtherEndPoint); Assert.AreNotEqual(result.LocalEndPoint !.Address, IPAddress.Any); Assert.AreEqual(result.MappingBehavior, MappingBehavior.Unknown); Assert.IsTrue(result.FilteringBehavior is FilteringBehavior.EndpointIndependent or FilteringBehavior.AddressDependent or FilteringBehavior.AddressAndPortDependent ); }
public async Task CombiningTest() { using var client = new StunClient5389UDP(@"stun.syncthing.net", 3478, new IPEndPoint(IPAddress.Any, 0)); var result = await client.QueryAsync(); Assert.AreEqual(result.BindingTestResult, BindingTestResult.Success); Assert.IsNotNull(result.LocalEndPoint); Assert.IsNotNull(result.PublicEndPoint); Assert.IsNotNull(result.OtherEndPoint); Assert.AreNotEqual(result.LocalEndPoint.Address, IPAddress.Any); Assert.IsTrue(result.MappingBehavior == MappingBehavior.Direct || result.MappingBehavior == MappingBehavior.EndpointIndependent || result.MappingBehavior == MappingBehavior.AddressDependent || result.MappingBehavior == MappingBehavior.AddressAndPortDependent ); Assert.IsTrue(result.FilteringBehavior == FilteringBehavior.EndpointIndependent || result.FilteringBehavior == FilteringBehavior.AddressDependent || result.FilteringBehavior == FilteringBehavior.AddressAndPortDependent ); }
/// <summary> /// stun.qq.com 3478 0.0.0.0:0 /// </summary> private static async Task Main(string[] args) { var server = @"stun.syncthing.net"; ushort port = 3478; IPEndPoint?local = null; if (args.Length > 0 && (Uri.CheckHostName(args[0]) == UriHostNameType.Dns || IPAddress.TryParse(args[0], out _))) { server = args[0]; } if (args.Length > 1) { ushort.TryParse(args[1], out port); } if (args.Length > 2) { local = NetUtils.ParseEndpoint(args[2]); } using var client = new StunClient5389UDP(server, port, local); await client.QueryAsync(); var res = client.Status; Console.WriteLine($@"Other address is {res.OtherEndPoint}"); Console.WriteLine($@"Binding test: {res.BindingTestResult}"); Console.WriteLine($@"Local address: {res.LocalEndPoint}"); Console.WriteLine($@"Mapped address: {res.PublicEndPoint}"); Console.WriteLine($@"Nat mapping behavior: {res.MappingBehavior}"); Console.WriteLine($@"Nat filtering behavior: {res.FilteringBehavior}"); var result = "error"; switch (res.FilteringBehavior) { case STUN.Enums.FilteringBehavior.Unknown: case STUN.Enums.FilteringBehavior.UnsupportedServer: case STUN.Enums.FilteringBehavior.Fail: result = res.FilteringBehavior.ToString(); break; case STUN.Enums.FilteringBehavior.EndpointIndependent: switch (res.MappingBehavior) { case STUN.Enums.MappingBehavior.EndpointIndependent: result = "1"; break; case STUN.Enums.MappingBehavior.AddressDependent: case STUN.Enums.MappingBehavior.AddressAndPortDependent: result = "2"; break; } break; case STUN.Enums.FilteringBehavior.AddressDependent: result = "3"; break; case STUN.Enums.FilteringBehavior.AddressAndPortDependent: result = "4"; break; } Console.WriteLine($@"result: {result}"); }
public static async Task <StunResult5389> NatBehaviorDiscovery(string server, ushort port, IPEndPoint local) { // proxy is not supported yet using var client = new StunClient5389UDP(server, port, local); return(await client.QueryAsync()); }