Exemplo n.º 1
0
        public UPnPLease DetectExternalEndpoint(CancellationToken cancellation = default(CancellationToken))
        {
            if (_UPnPLease != null)
            {
                _UPnPLease.Dispose();
                _UPnPLease = null;
            }
            var lease = new UPnPLease(BitcoinPorts, LocalEndpoint.Port, NATRuleName);

            lease.LeasePeriod = NATLeasePeriod;
            if (lease.DetectExternalEndpoint(cancellation))
            {
                _UPnPLease       = lease;
                ExternalEndpoint = _UPnPLease.ExternalEndpoint;
                return(lease);
            }
            else
            {
                using (lease.Trace.Open())
                {
                    NodeServerTrace.Information("No UPNP device found, try to use external web services to deduce external address");
                    try
                    {
                        var ip = GetMyExternalIP(cancellation);
                        if (ip != null)
                        {
                            ExternalEndpoint = new IPEndPoint(ip, ExternalEndpoint.Port);
                        }
                    }
                    catch (Exception ex)
                    {
                        NodeServerTrace.Error("Could not use web service to deduce external address", ex);
                    }
                }
                return(null);
            }
        }
Exemplo n.º 2
0
 public UPnPLease DetectExternalEndpoint(CancellationToken cancellation = default(CancellationToken))
 {
     if(_UPnPLease != null)
     {
         _UPnPLease.Dispose();
         _UPnPLease = null;
     }
     var lease = new UPnPLease(BitcoinPorts, LocalEndpoint.Port, NATRuleName);
     lease.LeasePeriod = NATLeasePeriod;
     if(lease.DetectExternalEndpoint(cancellation))
     {
         _UPnPLease = lease;
         ExternalEndpoint = _UPnPLease.ExternalEndpoint;
         return lease;
     }
     else
     {
         using(lease.Trace.Open())
         {
             NodeServerTrace.Information("No UPNP device found, try to use external web services to deduce external address");
             try
             {
                 var ip = GetMyExternalIP(cancellation);
                 if(ip != null)
                     ExternalEndpoint = new IPEndPoint(ip, ExternalEndpoint.Port);
             }
             catch(Exception ex)
             {
                 NodeServerTrace.Error("Could not use web service to deduce external address", ex);
             }
         }
         return null;
     }
 }