Beispiel #1
0
 public LocalDrpPeer(DrpPeerEngine engine, LocalDrpPeerConfiguration configuration, IDrpRegisteredPeerApp drpPeerApp)
 {
     Engine         = engine;
     _configuration = configuration;
     _drpPeerApp    = drpPeerApp;
     engine.Configuration.VisionChannel?.RegisterVisibleModule(engine.Configuration.VisionChannelSourceId, this.ToString(), this);
 }
Beispiel #2
0
        async Task <LocalDrpPeer> CreateLocalPeerAsync(LocalDrpPeerConfiguration registrationConfiguration, IDrpRegisteredPeerApp drpPeerApp)
        {
            if (registrationConfiguration.LocalPeerRegistrationId == null)
            {
                throw new ArgumentNullException();
            }
            var localDrpPeer = new LocalDrpPeer(this, registrationConfiguration, drpPeerApp);

            if (Configuration.ForcedPublicIpApiProviderResponse == null)
            {
                var nowUTC = DateTimeNowUtc;
                if (_latestPublicIpAddressResponseTimeUTC == null || nowUTC - _latestPublicIpAddressResponseTimeUTC.Value > TimeSpan.FromSeconds(60))
                {
                    WriteToLog_drpGeneral_detail($"resolving local public IP...");
                    var sw            = Stopwatch.StartNew();
                    var localPublicIp = await SendPublicIpAddressApiRequestAsync("http://ip.seeip.org/");

                    if (localPublicIp == null)
                    {
                        localPublicIp = await SendPublicIpAddressApiRequestAsync("http://api.ipify.org/");
                    }
                    if (localPublicIp == null)
                    {
                        localPublicIp = await SendPublicIpAddressApiRequestAsync("http://bot.whatismyipaddress.com");
                    }
                    if (localPublicIp == null)
                    {
                        localPublicIp = _latestPublicIpAddressResponse.GetAddressBytes();
                    }
                    if (localPublicIp == null)
                    {
                        throw new Exception("Failed to resolve public IP address. Please check your internet connection");
                    }

                    localDrpPeer.PublicIpApiProviderResponse = new IPAddress(localPublicIp);
                    _latestPublicIpAddressResponse           = localDrpPeer.PublicIpApiProviderResponse;
                    _latestPublicIpAddressResponseTimeUTC    = nowUTC;
                    var elapsedMs = (int)sw.Elapsed.TotalMilliseconds;
                    if (elapsedMs > 20000)
                    {
                        WriteToLog_drpGeneral_mediumPain($"resolved local public IP = {localDrpPeer.PublicIpApiProviderResponse} ({elapsedMs}ms) - too long");
                    }
                    else if (elapsedMs > 10000)
                    {
                        WriteToLog_drpGeneral_lightPain($"resolved local public IP = {localDrpPeer.PublicIpApiProviderResponse} ({elapsedMs}ms) - too long");
                    }
                    else
                    {
                        WriteToLog_drpGeneral_detail($"resolved local public IP = {localDrpPeer.PublicIpApiProviderResponse} ({elapsedMs}ms)");
                    }
                    await EngineThreadQueue.EnqueueAsync("resolved local public IP 3518");

                    WriteToLog_drpGeneral_detail($"@engine thread");
                }
                else
                {
                    WriteToLog_drpGeneral_detail($"using cached local public IP address {_latestPublicIpAddressResponse}");
                    localDrpPeer.PublicIpApiProviderResponse = _latestPublicIpAddressResponse;
                }
            }
            else
            {
                localDrpPeer.PublicIpApiProviderResponse = Configuration.ForcedPublicIpApiProviderResponse;
            }


            LocalPeers.Add(registrationConfiguration.LocalPeerRegistrationId, localDrpPeer);
            return(localDrpPeer);
        }
Beispiel #3
0
 public void BeginCreateLocalPeer(LocalDrpPeerConfiguration registrationConfiguration, IDrpRegisteredPeerApp drpPeerApp, Action <LocalDrpPeer> cb = null)
 {
     EngineThreadQueue.Enqueue(async() =>
     {
         var r = await CreateLocalPeerAsync(registrationConfiguration, drpPeerApp);
         if (cb != null)
         {
             cb(r);
         }
     }, "CreateLocalPeerAsync24807");
 }
Beispiel #4
0
        /// <summary>
        /// returns control only when LocalDrpPeer is registered and ready for operation ("local user logged in")
        /// </summary>
        public void BeginRegister(LocalDrpPeerConfiguration registrationConfiguration, IDrpRegisteredPeerApp drpPeerApp, Action <LocalDrpPeer> cb = null)
        {
            WriteToLog_reg_requesterSide_detail($">> BeginRegister()", null, null);
            if (registrationConfiguration.LocalPeerRegistrationId == null)
            {
                throw new ArgumentNullException();
            }

            EngineThreadQueue.Enqueue(async() =>
            {
                var r = await BeginRegister2(registrationConfiguration, drpPeerApp);
                cb?.Invoke(r);
            }, "BeginRegister792");
        }
Beispiel #5
0
        async Task <LocalDrpPeer> BeginRegister2(LocalDrpPeerConfiguration registrationConfiguration, IDrpRegisteredPeerApp user)
        {
            WriteToLog_reg_requesterSide_detail($"@BeginRegister2() engine thread", null, null);

            var localDrpPeer = await CreateLocalPeerAsync(registrationConfiguration, user);

            if (registrationConfiguration.EntryPeerEndpoints.Length != 0)
            {
                for (; ;)
                {
                    var epIndex    = _insecureRandom.Next(registrationConfiguration.EntryPeerEndpoints.Length);
                    var epEndpoint = registrationConfiguration.EntryPeerEndpoints[epIndex];

                    try
                    {
                        if (await RegisterAsync(localDrpPeer, epEndpoint, 0, RegisterRequestPacket.MaxNumberOfHopsRemaining, null, true) != null)
                        {
                            break;
                        }

                        //  on error or timeout try next entry peer
                    }
                    catch (Exception exc)
                    {
                        HandleExceptionWhileConnectingToEP(epEndpoint, exc);
                    }
                }

                WriteToLog_reg_requesterSide_detail($"@RegisterAsync() returned {localDrpPeer}", null, null);
                return(localDrpPeer);
            }
            else
            {
                throw new ArgumentException();
            }
        }
Beispiel #6
0
        async Task <LocalDrpPeer> BeginRegister2(LocalDrpPeerConfiguration registrationConfiguration, IDrpRegisteredPeerApp user)
        {
            WriteToLog_reg_requesterSide_detail($"@BeginRegister2() engine thread", null, null);

            var localDrpPeer = await CreateLocalPeerAsync(registrationConfiguration, user);

            if (registrationConfiguration.EntryPeerEndpoints.Length != 0)
            {
                for (; ;)
                {
                    var epIndex    = _insecureRandom.Next(registrationConfiguration.EntryPeerEndpoints.Length);
                    var epEndpoint = registrationConfiguration.EntryPeerEndpoints[epIndex];

                    // if (MiscProcedures.EqualByteArrays(epEndpoint.Address.GetAddressBytes(), localDrpPeer.LocalPublicIpAddressForRegistration.GetAddressBytes()) == true)
                    //  {
                    //      if (logger.WriteToLog_detail2_enabled) logger.WriteToLog_detail($"not connecting to EP {epEndpoint}: same IP address as local public IP");
                    //  }
                    // else
                    //  {
                    try
                    {
                        if (await RegisterAsync(localDrpPeer, epEndpoint, 0, 20, null) != null)
                        {
                            break;
                        }

                        //  on error or timeout try next entry peer
                    }
                    catch (Exception exc)
                    {
                        HandleExceptionWhileConnectingToEP(epEndpoint, exc);
                    }
                    //  }
                }

                WriteToLog_reg_requesterSide_detail($"@RegisterAsync() returned {localDrpPeer}", null, null);
                return(localDrpPeer);
            }
            else
            {
                throw new ArgumentException();
            }
        }