GetExternalIPAsync() 공개 추상적인 메소드

Gets the external (visible) IP address asynchronous. This is the NAT device IP address
MappingException
public abstract GetExternalIPAsync ( ) : Task
리턴 Task
예제 #1
0
 public async Task<string> GetExternalIP()
 {
     foreach (var natDevice in devices)
     {
         string i = natDevice.GetExternalIPAsync().Result.ToString();
         if (i == "0.0.0.0") continue;
         this.primaryDevice = natDevice;
         break;
     }
     var ip = await primaryDevice.GetExternalIPAsync();
     externalIP = ip.ToString();
     return externalIP;
 }
예제 #2
0
        /// <summary>
        /// Open the NAT port for the current Terraria ip:port
        /// </summary>
        public static void OpenPort()
        {
#if ENABLE_NAT && Full_API
            if (Terraria.Netplay.UseUPNP)
            {
                Terraria.Netplay.portForwardIP = Terraria.Netplay.GetLocalIPAddress();
                Terraria.Netplay.portForwardPort = Terraria.Netplay.ListenPort;

//                Mono.Nat.NatUtility.DeviceFound += NatUtility_DeviceFound;
//                Mono.Nat.NatUtility.StartDiscovery();

                var th = new System.Threading.Thread(async () =>
                    {
                        System.Threading.Thread.CurrentThread.Name = "NAT";
                        try
                        {
                            var discoverer = new NatDiscoverer();
                            _cancel = new CancellationTokenSource(10000);
                            _device = await discoverer.DiscoverDeviceAsync(PortMapper.Upnp, _cancel);

                            if (_device != null)
                            {
                                var ip = await _device.GetExternalIPAsync();

                                var existing = await _device.GetSpecificMappingAsync(Protocol.Tcp, Terraria.Netplay.portForwardPort);
                                if (existing != null && existing.PublicPort == Terraria.Netplay.portForwardPort)
                                {
                                    ProgramLog.Admin.Log("Detected an existing NAT map record for {0} on IP {1}", NatMapName, ip);
                                }
                                else
                                {
                                    await _device.CreatePortMapAsync(new Mapping(Protocol.Tcp, Terraria.Netplay.portForwardPort, Terraria.Netplay.portForwardPort, NatMapName));
                                    ProgramLog.Admin.Log("Created a new NAT map record for {0} on IP {1}", NatMapName, ip);
                                }
                                Terraria.Netplay.portForwardOpen = true;
                            }
                            else ProgramLog.Admin.Log("Failed to find a NAT device");
                        }
                        catch (Exception e)
                        {
                            ProgramLog.Log(e, "Failed to create NAT device mapping");
                        }
                    });
                th.Start();
            }
#endif

            //if (Netplay.mappings != null)
            //{
            //    foreach (IStaticPortMapping staticPortMapping in Netplay.mappings)
            //    {
            //        if (staticPortMapping.InternalPort == Netplay.portForwardPort && staticPortMapping.InternalClient == Netplay.portForwardIP && staticPortMapping.Protocol == "TCP")
            //        {
            //            Netplay.portForwardOpen = true;
            //        }
            //    }
            //    if (!Netplay.portForwardOpen)
            //    {
            //        Netplay.mappings.Add(Netplay.portForwardPort, "TCP", Netplay.portForwardPort, Netplay.portForwardIP, true, "Terraria Server");
            //        Netplay.portForwardOpen = true;
            //    }
            //}
        }