Exemple #1
0
        public async Task Run(IOutputInput outputInput)
        {
            var address = Common.GetLocalAddress(outputInput);

            if (address == null)
            {
                return;
            }

            var port = Common.GetPort(outputInput, 9989);

            if (port == 0)
            {
                return;
            }

            listener = new TcpListener(address, port);
            listener.Start();
            outputInput.WriteLine($"Listener started at {address}:{port}");
            while (true)
            {
                var client = await listener.AcceptTcpClientAsync();

                outputInput.WriteLine("Client connected from: " + ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString());
            }
        }
        public async Task Run(IOutputInput outputInput)
        {
            var items = Enumerable.Range(1, 5)
                        .ToList();

            outputInput.WriteLine("5 tasks for 5 items without result which should finish in circa 2 seconds:");
            await AsyncHelper.ForEach(items, async (item, cancellationToken) =>
            {
                await Task.Delay(2000);
                outputInput.WriteLine($"Result: {item * 2}");
            });

            outputInput.PutLine();
            outputInput.WriteLine("5 tasks for 5 items without result which should finish in circa 2 seconds:");
            var result = await AsyncHelper.ForEachWithResult(items, async (item, cancellationToken) =>
            {
                await Task.Delay(2000);
                return(item * 2);
            });

            foreach (var pair in result)
            {
                outputInput.WriteLine($"Item: {pair.Key}, result: {pair.Value}");
            }
        }
Exemple #3
0
 private void PortScanner_OnScanProgress(ProcessingQueueItem <PortInput, List <PortResult> > item)
 {
     outputInput.WriteLine("Ports for " + item.Input.Address.ToString());
     foreach (var result in item.Output)
     {
         outputInput.WriteLine($"\t{result.Port} - {(result.IsOpen ? "Opened" : "Closed")}");
     }
 }
Exemple #4
0
        public async Task Run(IOutputInput outputInput)
        {
            this.outputInput = outputInput;
            var address = Common.GetLocalAddress(outputInput);

            if (address == null)
            {
                return;
            }

            var portsString = outputInput.Read("Insert ports (comma separated):", "80, 135, 9989");
            var ports       = new List <int>();

            if (!string.IsNullOrEmpty(portsString))
            {
                foreach (var port in portsString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList())
                {
                    if (int.TryParse(port, out int p) && !ports.Contains(p))
                    {
                        ports.Add(p);
                    }
                }
            }

            if (ports.Count == 0)
            {
                outputInput.WriteLine("No ports provided");
                return;
            }

            ports.Sort();
            outputInput.WriteLine("Current IP address: " + address.ToString());
            outputInput.WriteLine("Ports to scan: " + string.Join(", ", ports.Select(s => s.ToString())));
            outputInput.WriteLine("TCP Scan started!");
            var scanner = new PortScanner()
            {
                StopOnFirstException = false,
                TasksCount           = 0
            };

            scanner.OnScanProgress += PortScanner_OnScanProgress;
            outputInput.StartTime();
            var result = await scanner.PortScan(new PortScanInput
            {
                Addresses = new List <IPAddress> {
                    address
                },
                Ports   = ports,
                Retries = 1,
                Timeout = 1000
            }, new TCPPortClient());

            outputInput.StopTime();
            var processed = result.GetResults();

            outputInput.WriteLine($"Scan completed. Scanned addresses: {processed.Count}, total scanned ports: {processed.Sum(s => s.Count)}");
        }
 private void PingScanner_OnScanProgress(ProcessingQueueItem <PingInput, PingResult> item)
 {
     if (item.Output?.Reply?.Status == IPStatus.Success)
     {
         outputInput.WriteLine("Device found on: " + item.Input.Address);
     }
 }
Exemple #6
0
 private void ServiceScanner_OnScanProgress(ProcessingQueueItem <ServiceInput, bool> item)
 {
     if (item.Output)
     {
         outputInput.WriteLine("Found service on: " + item.Input.Endpoint.ToString());
     }
 }
Exemple #7
0
 protected void StartClient(IOutputInput outputInput, BindingConfigurationBase bindingConfigurationBase)
 {
     using (var client = new Client <ITestService>(bindingConfigurationBase))
     {
         client.Start();
         var message = outputInput.Read("Write message: ", "TestMessage");
         var result  = client.Proxy.Ping(message);
         outputInput.WriteLine("Message from server: " + result);
     }
 }
Exemple #8
0
        public async Task Run(IOutputInput outputInput)
        {
            this.outputInput = outputInput;

            var address = Common.GetLocalAddress(outputInput);

            if (address == null)
            {
                return;
            }

            var mask = NetworkUtils.GetSubnetMask(address);

            var port = Common.GetPort(outputInput, 9989);

            if (port == 0)
            {
                return;
            }

            var addresses = NetworkUtils.GetAddressesInNetwork(address, mask);

            if (addresses.Count == 0)
            {
                outputInput.WriteLine("No addresses to scan");
                return;
            }

            outputInput.WriteLine("Current IP address: " + address.ToString());
            outputInput.WriteLine("Mask: " + mask.ToString());
            outputInput.WriteLine("Addresses to scan: " + addresses.Count);
            outputInput.WriteLine("Port to scan: " + port);
            outputInput.WriteLine("Scan started!");

            var scanner = new ServiceScanner()
            {
                StopOnFirstException = false,
                TasksCount           = 0
            };

            scanner.OnScanProgress += ServiceScanner_OnScanProgress;
            outputInput.StartTime();
            var result = await scanner.ServiceScan(new ServiceScanInput
            {
                Addresses = addresses,
                Port      = port,
                Retries   = 1,
                Timeout   = 1000
            }, new TCPPortClient());

            outputInput.StopTime();
            var processed = result.GetResults();

            outputInput.WriteLine($"Scan completed. Scanned addresses: {processed.Count}, found services: {result.Count(r => r.Output)}");

            return;
        }
Exemple #9
0
        protected async Task StartServer(IOutputInput outputInput, BindingConfigurationBase bindingConfigurationBase, ServerConfiguration serverConfiguration)
        {
            var service = new TestService(outputInput);
            var server  = Server.CreateSingle <ITestService>(bindingConfigurationBase, service, serverConfiguration);

            server.Start();
            outputInput.WriteLine("Server started at: " + bindingConfigurationBase.ServiceAddress);
            await outputInput.Wait();

            server.Dispose();
        }
        public async Task Run(IOutputInput outputInput)
        {
            var queue   = new Queue(outputInput);
            var numbers = Enumerable.Range(1, 5);
            var items   = numbers.Select(s => new ProcessingQueueItem <int, int>(s)).ToList();

            queue.TasksCount = 1;
            outputInput.WriteLine($"Processing started with {queue.TasksCount} task");
            outputInput.StartTime();
            await queue.Run(items);

            outputInput.WriteLine($"Processed {items.Count(i => i.Processed)} items");
            outputInput.StopTime();

            queue.TasksCount = 4;
            outputInput.WriteLine($"Processing started with {queue.TasksCount} tasks");
            outputInput.StartTime();
            await queue.Run(items);

            outputInput.WriteLine($"Processed {items.Count(i => i.Processed)} items");
            outputInput.StopTime();

            queue.TasksCount = 1;
            var delay = 3000;
            var cts   = new CancellationTokenSource(delay);

            outputInput.WriteLine($"Processing started with {queue.TasksCount} tasks. Will be cancelled in {delay}ms");
            outputInput.StartTime();
            await queue.Run(items, cts.Token);

            outputInput.WriteLine($"Processed {items.Count(i => i.Processed)} items");
            outputInput.StopTime();

            queue.TasksCount           = 1;
            queue.StopOnFirstException = true;
            queue.Throw = true;
            outputInput.WriteLine($"Processing started with {queue.TasksCount} tasks. Will crash soon");
            outputInput.StartTime();
            await queue.Run(items);

            outputInput.WriteLine($"Processed {items.Count(i => i.Processed)} items");
            outputInput.StopTime();
        }
Exemple #11
0
        public static int GetPort(IOutputInput outputInput, int port)
        {
            var portString = outputInput.Read("Insert port:", port.ToString(), s =>
            {
                if (!int.TryParse(s, out int _))
                {
                    return("Invalid port value");
                }
                return(null);
            });

            if (string.IsNullOrEmpty(portString))
            {
                outputInput.WriteLine("Invalid port value");
                return(0);
            }
            return(int.Parse(portString));
        }
Exemple #12
0
        public static IPAddress GetMask(IOutputInput outputInput)
        {
            var inputString = outputInput.Read("Insert mask:", "255.255.255.0", s =>
            {
                if (!IPAddress.TryParse(s, out IPAddress _))
                {
                    return("Invalid mask format");
                }
                return(null);
            });

            if (string.IsNullOrEmpty(inputString))
            {
                outputInput.WriteLine("No mask provided");
                return(null);
            }
            return(IPAddress.Parse(inputString));
        }
Exemple #13
0
        public static IPAddress GetLocalAddress(IOutputInput outputInput)
        {
            IPAddress address        = null;
            var       localAddresses = NetworkUtils.GetLocalIPAddresses();

            if (localAddresses.Count > 1)
            {
                address = outputInput.SelectValue("Select local address:", localAddresses);
            }
            else if (localAddresses.Count == 1)
            {
                address = localAddresses[0];
            }

            if (address == null)
            {
                outputInput.WriteLine("No IP address selected");
                return(null);
            }
            return(address);
        }
        public async Task Run(IOutputInput outputInput)
        {
            this.outputInput = outputInput;
            var address = Common.GetLocalAddress(outputInput);

            if (address == null)
            {
                return;
            }

            var mask = NetworkUtils.GetSubnetMask(address);

            var addresses = NetworkUtils.GetAddressesInNetwork(address, mask);

            if (addresses.Count == 0)
            {
                outputInput.WriteLine("No addresses to scan");
                return;
            }

            outputInput.WriteLine("Current IP address: " + address.ToString());
            outputInput.WriteLine("Mask: " + mask.ToString());
            outputInput.WriteLine("Addresses to scan: " + addresses.Count);
            outputInput.WriteLine("Scan started!");

            var pingScanner = new PingScanner()
            {
                StopOnFirstException = false,
                TasksCount           = 0
            };

            pingScanner.OnScanProgress += PingScanner_OnScanProgress;
            outputInput.StartTime();
            var result = await pingScanner.PingScan(new PingScanInput
            {
                Addresses = addresses,
                Retries   = 1,
                Timeout   = 1000
            });

            outputInput.StopTime();
            var processed = result.GetResults();
            var found     = processed.Where(s => s.Reply?.Status == IPStatus.Success);

            outputInput.WriteLine($"Scan completed. Scanned addresses: {processed.Count}, found {found.Count()} devices");
        }
Exemple #15
0
 public string Ping(string message)
 {
     outputInput.WriteLine("Message received: " + message);
     return($"Reply from server: {message.ToUpper()}");
 }
 public override Task ReportProgress(ProcessingQueueItem <int, int> item)
 {
     outputInput.WriteLine($"Processed item {item.Input} with result {item.Output}");
     return(Task.CompletedTask);
 }
        public Task Run(IOutputInput outputInput)
        {
            outputInput.WriteLine("Connected to local network: " + NetworkUtils.ConnectedToLocalNetwork());
            outputInput.WriteLine("Connected to internet: " + NetworkUtils.ConnectedToInternet());
            outputInput.PutLine();

            outputInput.WriteLine("Addresses:");
            foreach (var address in NetworkUtils.GetLocalIPAddresses())
            {
                outputInput.WriteLine($"\t{address}");
            }
            outputInput.PutLine();

            outputInput.WriteLine("Gateways:");
            foreach (var gateway in NetworkUtils.GetGatewayAddresses())
            {
                outputInput.WriteLine($"\t{gateway}");
            }
            outputInput.PutLine();

            outputInput.WriteLine("Host names:");
            foreach (var address in NetworkUtils.GetLocalIPAddresses())
            {
                outputInput.WriteLine($"\t{NetworkUtils.GetHostName(address)}");
            }
            outputInput.PutLine();

            var address1 = new IPAddress(new byte[] { 192, 168, 0, 1 });
            var address2 = new IPAddress(new byte[] { 192, 168, 1, 127 });
            var mask     = new IPAddress(new byte[] { 255, 255, 255, 0 });

            outputInput.WriteLine($"Broadcast address for {address1}/{mask}: {NetworkUtils.GetBroadcastAddress(address1, mask)}");
            outputInput.WriteLine($"Network address for {address1}/{mask}: {NetworkUtils.GetNetworkAddress(address1, mask)}");
            outputInput.WriteLine($"Is {address1} and {address2} in the same subnet? (mask: {mask}): {NetworkUtils.IsInSameSubnet(address1, address2, mask)}");
            outputInput.WriteLine($"First address in subnet for {address2}/{mask}: {NetworkUtils.FirstAddressInSubnet(address2, mask)}");
            outputInput.WriteLine($"Last address in subnet for {address2}/{mask}: {NetworkUtils.LastAddressInSubnet(address2, mask)}");
            outputInput.PutLine();

            outputInput.WriteLine("Series of addresses:");
            foreach (var address in NetworkUtils.GetContinousAddressesInRange(new IPAddress(new byte[] { 192, 168, 1, 252 }), new IPAddress(new byte[] { 192, 168, 2, 3 })))
            {
                outputInput.WriteLine($"\t{address}");
            }
            outputInput.PutLine();

            outputInput.WriteLine("Active TCP connections ports:");
            foreach (var port in NetworkUtils.GetActiveTcpConnections())
            {
                outputInput.WriteLine($"\t{port}");
            }
            outputInput.WriteLine("Opened TCP ports:");
            foreach (var port in NetworkUtils.GetOpenTcpPorts())
            {
                outputInput.WriteLine($"\t{port}");
            }
            outputInput.WriteLine("Opened UDP ports:");
            foreach (var port in NetworkUtils.GetOpenUdpPorts())
            {
                outputInput.WriteLine($"\t{port}");
            }

            return(Task.CompletedTask);
        }
Exemple #18
0
 public Task Run(IOutputInput outputInput)
 {
     outputInput.WriteLine("OS information:");
     outputInput.WriteLine(SystemInformation.GetCPUInfo().PublicPropertiesToString());
     return(Task.CompletedTask);
 }