/// <summary>
 /// Initializes a new instance of the <see cref="DnsServiceExtractorFirstLabelConvention"/> class.
 /// </summary>
 /// <param name="protocol">protocol to be used (default, TCP).</param>
 /// <param name="serviceWhiteList">Service White list if you not allow all the services.</param>
 /// <param name="domainWhiteList">Domain White list if you not allow all the domains.</param>
 /// <param name="allowSubDomains">allow the sub-domains of a domain (example test.qarnot.com if there is qarnot.com in the domain white.list).</param>
 public DnsServiceExtractorFirstLabelConvention(ProtocolType?protocol, IEnumerable <string> serviceWhiteList = null, IEnumerable <string> domainWhiteList = null, bool allowSubDomains = false)
 {
     Protocol         = protocol ?? DEFAULTPROTOCOL;
     ServiceWhiteList = serviceWhiteList;
     DomainWhiteList  = domainWhiteList;
     AllowSubDomains  = allowSubDomains;
 }
Пример #2
0
 /// <summary>
 /// Forwards the specified port on the specified WAN device, optionally for
 /// the specified protocol only.
 /// </summary>
 /// <param name="wanDevice">The UPnP WAN device to forward the port on.</param>
 /// <param name="targetAddress">The IP address to forward the port to.</param>
 /// <param name="port">The port to forward.</param>
 /// <param name="protocol">If specified, must be either ProcotolType.Tcp or
 /// ProtocolType.Udp to forward the port only for the TCP or the UDP protocol,
 /// respectively. If this is null, the port will be forwarded for both
 /// protocols.</param>
 /// <param name="description">A description of the port-forwarding rule that
 /// may be displayed in the UPnP-device's user interface to help the user
 /// better understand what the port-forwarding rule is for.</param>
 /// <exception cref="ArgumentNullException">The wanDevice parameter or the
 /// targetAddress parameter is null.</exception>
 /// <exception cref="ArgumentOutOfRangeException">The port parameter is not
 /// between 0 and 65535.</exception>
 /// <exception cref="ArgumentException">The protocol parameter has a value
 /// other than ProtocolType.Tcp or ProtocolType.Udp.</exception>
 /// <exception cref="InvalidOperationException">An error occurred while
 /// trying to forward the specified port.</exception>
 static void ForwardPort(UPnPDevice wanDevice, IPAddress targetAddress, int port,
                         ProtocolType?protocol = null, string description = null)
 {
     wanDevice.ThrowIfNull("wanDevice");
     targetAddress.ThrowIfNull("targetAddress");
     port.ThrowIfOutOfRange("port", 0, 65535);
     if (protocol.HasValue && protocol != ProtocolType.Tcp &&
         protocol != ProtocolType.Udp)
     {
         throw new ArgumentException("The argument parameter must be either " +
                                     "ProtocolType.Tcp or ProtocolType.Udp");
     }
     string[] p = protocol.HasValue ? new string[] { protocol.Value.ToString()
                                                     .ToUpperInvariant() } : new string[] { "TCP", "UDP" };
     foreach (string proto in p)
     {
         var result = wanDevice.InvokeAction(stIpConnection, "AddPortMapping",
                                             new object[] { null, port, proto, port, targetAddress.ToString(), 1,
                                                            description, 0 }
                                             );
         if (result.Length > 0)
         {
             throw new InvalidOperationException("The port-forwarding could not be " +
                                                 "performed: " + result[0]);
         }
     }
 }
Пример #3
0
 /// <summary>
 /// Forwards the specified port, optionally for the specified protocol only.
 /// </summary>
 /// <param name="port">The port to forward.</param>
 /// <param name="protocol">If specified, must be either ProcotolType.Tcp or
 /// ProtocolType.Udp to forward the port only for the TCP or the UDP protocol,
 /// respectively. If this is null, the port will be forwarded for both
 /// protocols.</param>
 /// <param name="description">A description of the port-forwarding rule that
 /// may be displayed in the UPnP-device's user interface to help the user
 /// better understand what the port-forwarding rule is for.</param>
 /// <exception cref="ArgumentOutOfRangeException">The port parameter is not
 /// between 0 and 65535.</exception>
 /// <exception cref="ArgumentException">The protocol parameter has a value
 /// other than ProtocolType.Tcp or ProtocolType.Udp.</exception>
 /// <exception cref="InvalidOperationException">Querying for UPnP-enabled
 /// devices failed, or an error occurred while trying to forward the
 /// specified port.</exception>
 public static void ForwardPort(int port,
                                ProtocolType?protocol = null, string description = null)
 {
     port.ThrowIfOutOfRange("port", 0, 65535);
     if (protocol.HasValue && protocol != ProtocolType.Tcp &&
         protocol != ProtocolType.Udp)
     {
         throw new ArgumentException("The argument parameter must be either " +
                                     "ProtocolType.Tcp or ProtocolType.Udp");
     }
     VerifyCache();
     foreach (UPnPDevice wanDevice in cachedDevices)
     {
         // Get local IP addresses of WAN device.
         if (!(wanDevice is IUPnPDeviceDocumentAccess))
         {
             continue;
         }
         var uPnPDoc = wanDevice as IUPnPDeviceDocumentAccess;
         IEnumerable <IPAddress> localIPs = uPnPDoc.GetLocalIpAddresses();
         // Check if any of the WAN device's addresses are in the same subnet
         // as any of the host's addresses.
         foreach (IPAddress local in localIPs)
         {
             var ips = Socks5Bytestreams.GetIpAddresses(local);
             // If so, perform port forwarding.
             foreach (var ip in ips)
             {
                 ForwardPort(wanDevice, ip, port, protocol, description);
             }
         }
     }
 }
 internal DscpConfigurationData(string id, string name, string type, string location, IDictionary <string, string> tags, string etag, IList <int> markings, IList <QosIPRange> sourceIPRanges, IList <QosIPRange> destinationIPRanges, IList <QosPortRange> sourcePortRanges, IList <QosPortRange> destinationPortRanges, ProtocolType?protocol, string qosCollectionId, IReadOnlyList <NetworkInterfaceData> associatedNetworkInterfaces, string resourceGuid, ProvisioningState?provisioningState) : base(id, name, type, location, tags)
 {
     Etag                        = etag;
     Markings                    = markings;
     SourceIPRanges              = sourceIPRanges;
     DestinationIPRanges         = destinationIPRanges;
     SourcePortRanges            = sourcePortRanges;
     DestinationPortRanges       = destinationPortRanges;
     Protocol                    = protocol;
     QosCollectionId             = qosCollectionId;
     AssociatedNetworkInterfaces = associatedNetworkInterfaces;
     ResourceGuid                = resourceGuid;
     ProvisioningState           = provisioningState;
 }
Пример #5
0
 private static Socket ValidateSocket(Connection connection, SocketType?socketType = null, ProtocolType?protocolType = null, AddressFamily?addressFamily = null)
 {
     Assert.True(connection.ConnectionProperties.TryGet(out Socket socket));
     Assert.True(socket.Connected);
     if (addressFamily != null)
     {
         Assert.Equal(addressFamily, socket.AddressFamily);
     }
     if (socketType != null)
     {
         Assert.Equal(socketType, socket.SocketType);
     }
     if (protocolType != null)
     {
         Assert.Equal(protocolType, socket.ProtocolType);
     }
     return(socket);
 }
Пример #6
0
        private static async Task CheckPortTcp(IPAddress ipAddr, int port, int timeout = 2000)
        {
            using (var tcpClient = new TcpClient())
            {
                var connectTask = await tcpClient.ConnectAsync(ipAddr, port, timeout);

                PortStatus portStatus;
                switch (connectTask.Status)
                {
                case TaskStatus.RanToCompletion:
                    portStatus = PortStatus.OPEN;
                    break;

                case TaskStatus.Faulted:
                    portStatus = PortStatus.CLOSED;
                    break;

                default:
                    portStatus = PortStatus.FILTERED;
                    break;
                }

                ProtocolType?protocolType = null;
                if (portStatus == PortStatus.OPEN)
                {
                    var data = new byte[256];
                    using (var stream = tcpClient.GetStream())
                    {
                        stream.ReadTimeout  = timeout;
                        stream.WriteTimeout = timeout;
                        var writeTask = stream.WriteAsync(PacketToSend, 0, PacketToSend.Length);
                        var readTask  = stream.ReadAsync(data, 0, data.Length);

                        try
                        {
                            await writeTask;
                        }
                        catch (Exception e)
                        {
                            //Console.WriteLine(e.Message);
                        }

                        if (writeTask.Status == TaskStatus.RanToCompletion)
                        {
                            try
                            {
                                await readTask;
                            }
                            catch (Exception e)
                            {
                                //Console.WriteLine(e.Message);
                            }

                            if (readTask.Status == TaskStatus.RanToCompletion)
                            {
                                var length = readTask.Result;
                                data         = data.Take(length).ToArray();
                                protocolType = ProtocolCheckingUtils.DetectProtocol(data);
                            }
                        }
                    }
                    Console.WriteLine($"TCP {port} {protocolType}");
                }
            }
        }
Пример #7
0
        private static async Task CheckPortUdp(IPAddress ipAddr, int port, int timeout = 2000)
        {
            using (var udpClient = new UdpClient())
            {
                var          connectTask = udpClient.Client.ConnectAsync(ipAddr, port);
                PortStatus   portStatus;
                ProtocolType?protocolType = null;
                await Task.WhenAny(Task.Delay(timeout), connectTask);

                if (connectTask.IsCompleted &&
                    connectTask.Exception != null && connectTask.Exception.InnerExceptions
                    .Where(x => x is SocketException)
                    .Any(x => ((SocketException)x).ErrorCode == 10054))      //WSAECONNRESET
                {
                    portStatus = PortStatus.CLOSED;
                }
                else
                {
                    portStatus = PortStatus.OPEN;
                }

                if (portStatus == PortStatus.OPEN)
                {
                    var writeTask = udpClient.SendAsync(PacketToSend, PacketToSend.Length);
                    var readTask  = udpClient.ReceiveAsync();
                    try
                    {
                        await Task.WhenAny(writeTask, Task.Delay(timeout));
                    }
                    catch (Exception e)
                    {
                        //Console.WriteLine(e);
                    }

                    if (writeTask.Status == TaskStatus.RanToCompletion)
                    {
                        try
                        {
                            await Task.WhenAny(readTask, Task.Delay(timeout));
                        }
                        catch (Exception e)
                        {
                            //Console.WriteLine(e);
                        }

                        if (readTask.Status == TaskStatus.RanToCompletion)
                        {
                            protocolType = ProtocolCheckingUtils.DetectProtocol(readTask.Result.Buffer);
                        }
                        else
                        {
                            return;
                        }
                    }
                    else
                    {
                        return;
                    }

                    Console.WriteLine($"UDP {port} {protocolType}");
                }
            }
        }
Пример #8
0
        public void ConfigTests_DependencyInjection(string json, int expectedBufferSize, AddressFamily?expectedAddressFamily = default, ProtocolType?expectedProtocolType = default, SocketType?expectedSocketType = default)
        {
            var dictionary = JsonSerializer.Deserialize <Dictionary <string, JsonElement> >(json);
            IEnumerable <KeyValuePair <string, string> > kvps = from kvp in dictionary
                                                                let key                         = kvp.Key
                                                                                      let value = kvp.Value.ToString()
                                                                                                  select new KeyValuePair <string, string>(key, value);

            var configuration = new ConfigurationBuilder()
                                .AddInMemoryCollection(kvps)
                                .Build();

            var config = configuration.Get <Clients.Concrete.SocketClient.Config>();

            Assert.NotNull(config);
            Assert.Equal(expectedBufferSize, config !.BufferSize);
            if (expectedAddressFamily is not null)
            {
                Assert.Equal(expectedAddressFamily !.Value, config.AddressFamily);
            }
            if (expectedProtocolType is not null)
            {
                Assert.Equal(expectedProtocolType !.Value, config.ProtocolType);
            }
            if (expectedSocketType is not null)
            {
                Assert.Equal(expectedSocketType !.Value, config.SocketType);
            }
        }
Пример #9
0
        public void ConfigTests_JsonDeserialization(string json, int expectedBufferSize, AddressFamily?expectedAddressFamily = default, ProtocolType?expectedProtocolType = default, SocketType?expectedSocketType = default)
        {
            var config = JsonSerializer.Deserialize <Helpers.Networking.Clients.Concrete.SocketClient.Config>(json);

            Assert.Equal(expectedBufferSize, config !.BufferSize);
            if (expectedAddressFamily is not null)
            {
                Assert.Equal(expectedAddressFamily !.Value, config.AddressFamily);
            }
            if (expectedProtocolType is not null)
            {
                Assert.Equal(expectedProtocolType !.Value, config.ProtocolType);
            }
            if (expectedSocketType is not null)
            {
                Assert.Equal(expectedSocketType !.Value, config.SocketType);
            }
        }