예제 #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);
 }
예제 #2
0
        public static LocalDrpPeerConfiguration  Create(ICryptoLibrary cryptoLibrary, int?numberOfDimensions = null, byte[] ed25519privateKey = null, RegistrationId registrationId = null)
        {
            LocalDrpPeerConfiguration r;

            if (ed25519privateKey != null && registrationId != null)
            {
                r = new LocalDrpPeerConfiguration
                {
                    LocalPeerRegistrationPrivateKey = new RegistrationPrivateKey {
                        ed25519privateKey = ed25519privateKey
                    },
                    LocalPeerRegistrationId = registrationId
                };
            }
            else
            {
                RegistrationId.CreateNew(cryptoLibrary, out var newPrivateKey, out var newRegistrationId);
                r = new LocalDrpPeerConfiguration
                {
                    LocalPeerRegistrationPrivateKey = newPrivateKey,
                    LocalPeerRegistrationId         = newRegistrationId
                };
            }

            if (numberOfDimensions == 2)
            {
                r.MinDesiredNumberOfNeighbors  = 5;
                r.SoftMaxNumberOfNeighbors     = 7;
                r.AbsoluteMaxNumberOfNeighbors = 10;
            }
            return(r);
        }
예제 #3
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;
                    WriteToLog_drpGeneral_detail($"resolved local public IP = {localDrpPeer.PublicIpApiProviderResponse} ({(int)sw.Elapsed.TotalMilliseconds}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);
        }
예제 #4
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");
 }
예제 #5
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");
        }
예제 #6
0
        public static LocalDrpPeerConfiguration  Create(ICryptoLibrary cryptoLibrary, int numberOfDimensions, byte[] ed25519privateKey = null, RegistrationId registrationId = null)
        {
            var privatekey = new RegistrationPrivateKey {
                ed25519privateKey = ed25519privateKey ?? cryptoLibrary.GeneratePrivateKeyEd25519()
            };
            var r = new LocalDrpPeerConfiguration
            {
                LocalPeerRegistrationPrivateKey = privatekey,
                LocalPeerRegistrationId         = new RegistrationId(registrationId?.Ed25519publicKey ?? cryptoLibrary.GetPublicKeyEd25519(privatekey.ed25519privateKey))
            };

            if (numberOfDimensions == 2)
            {
                r.MinDesiredNumberOfNeighbors  = 5;
                r.SoftMaxNumberOfNeighbors     = 7;
                r.AbsoluteMaxNumberOfNeighbors = 10;
            }
            return(r);
        }
예제 #7
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();
            }
        }
예제 #8
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();
            }
        }