示例#1
0
        public bool ClosePort(UpnpProtocolType protocol, int externalPort, TimeSpan timeout)
        {
            if (_services == null)
            {
                throw new UpnpClientException();
            }

            try
            {
                var value = ClosePortFromService(_services, "urn:schemas-upnp-org:service:WANIPConnection:1", _location.Host, _location.Port, protocol, externalPort, timeout);
                if (value)
                {
                    return(value);
                }
            }
            catch (Exception)
            {
            }

            try
            {
                var value = ClosePortFromService(_services, "urn:schemas-upnp-org:service:WANPPPConnection:1", _location.Host, _location.Port, protocol, externalPort, timeout);
                if (value)
                {
                    return(value);
                }
            }
            catch (Exception)
            {
            }

            return(false);
        }
示例#2
0
        public bool ClosePort(UpnpProtocolType protocol, int externalPort, TimeSpan timeout)
        {
            if (_services == null) throw new UpnpClientException();

            try
            {
                var value = ClosePortFromService(_services, "urn:schemas-upnp-org:service:WANIPConnection:1", _location.Host, _location.Port, protocol, externalPort, timeout);
                if (value) return value;
            }
            catch (Exception)
            {

            }

            try
            {
                var value = ClosePortFromService(_services, "urn:schemas-upnp-org:service:WANPPPConnection:1", _location.Host, _location.Port, protocol, externalPort, timeout);
                if (value) return value;
            }
            catch (Exception)
            {

            }

            return false;
        }
示例#3
0
        public bool OpenPort(UpnpProtocolType protocol, int externalPort, int internalPort, string description, TimeSpan timeout)
        {
            if (_services == null)
            {
                throw new UpnpClientException();
            }

#if Mono
            string hostname = Dns.GetHostName();

            foreach (var ipAddress in Dns.GetHostAddresses(hostname))
            {
                if (ipAddress.AddressFamily != AddressFamily.InterNetwork)
                {
                    continue;
                }

                if (OpenPort(protocol, ipAddress.ToString(), externalPort, internalPort, description, timeout))
                {
                    return(true);
                }
            }
#else
            foreach (var nic in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
                     .Where(n => n.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up))
            {
                var machineIp = nic.GetIPProperties().UnicastAddresses
                                .Select(n => n.Address)
                                .Where(n => n.AddressFamily == AddressFamily.InterNetwork)
                                .FirstOrDefault();
                if (machineIp == null)
                {
                    continue;
                }

                var gatewayIp = nic.GetIPProperties().GatewayAddresses
                                .Select(n => n.Address)
                                .Where(n => n.AddressFamily == AddressFamily.InterNetwork)
                                .FirstOrDefault();
                if (gatewayIp == null)
                {
                    continue;
                }

                if (gatewayIp.ToString() == _location.Host && OpenPort(protocol, machineIp.ToString(), externalPort, internalPort, description, timeout))
                {
                    return(true);
                }
            }
#endif

            return(false);
        }
示例#4
0
        public async ValueTask <bool> ClosePort(UpnpProtocolType protocol, int externalPort, CancellationToken token = default)
        {
            if (_contents == null || _location == null)
            {
                throw new UpnpClientException(nameof(UpnpClient) + " is not connected");
            }

            if (await ClosePortFromContents(_contents, "urn:schemas-upnp-org:service:WANIPConnection:1", _location.Host, _location.Port, protocol, externalPort, token))
            {
                return(true);
            }

            if (await ClosePortFromContents(_contents, "urn:schemas-upnp-org:service:WANPPPConnection:1", _location.Host, _location.Port, protocol, externalPort, token))
            {
                return(true);
            }

            return(false);
        }
示例#5
0
        public async ValueTask <bool> OpenPortAsync(UpnpProtocolType protocol, string machineIp, int externalPort, int internalPort, string description, CancellationToken cancellationToken = default)
        {
            if (_contents == null || _location == null)
            {
                throw new UpnpClientException(nameof(UpnpClient) + " is not connected");
            }

            if (await OpenPortFromContentsAsync(_contents, "urn:schemas-upnp-org:service:WANIPConnection:1", _location.Host, _location.Port, protocol, machineIp, externalPort, internalPort, description, cancellationToken))
            {
                return(true);
            }

            if (await OpenPortFromContentsAsync(_contents, "urn:schemas-upnp-org:service:WANPPPConnection:1", _location.Host, _location.Port, protocol, machineIp, externalPort, internalPort, description, cancellationToken))
            {
                return(true);
            }

            return(false);
        }
示例#6
0
        public async ValueTask <bool> OpenPort(UpnpProtocolType protocol, int externalPort, int internalPort, string description, CancellationToken token = default)
        {
            if (_contents == null || _location == null)
            {
                throw new UpnpClientException(nameof(UpnpClient) + " is not connected");
            }

            foreach (var ipAddress in await Dns.GetHostAddressesAsync(Dns.GetHostName()))
            {
                if (ipAddress.AddressFamily != AddressFamily.InterNetwork)
                {
                    continue;
                }

                if (await this.OpenPort(protocol, ipAddress.ToString(), externalPort, internalPort, description, token))
                {
                    return(true);
                }
            }

            return(false);
        }
示例#7
0
        private static bool OpenPortFromService(string services, string serviceType, string gatewayIp, int gatewayPort, UpnpProtocolType protocol, string machineIp, int externalPort, int internalPort, string description, TimeSpan timeout)
        {
            if (services == null || !services.Contains(serviceType)) return false;

            try
            {
                services = services.Substring(services.IndexOf(serviceType));

                string controlUrl = _controlUrlRegex.Match(services).Groups["url"].Value;
                string protocolString = "";

                if (protocol == UpnpProtocolType.Tcp)
                {
                    protocolString = "TCP";
                }
                else if (protocol == UpnpProtocolType.Udp)
                {
                    protocolString = "UDP";
                }

                string soapBody =
                    "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" +
                    " <s:Body>" +
                    "  <u:AddPortMapping xmlns:u=\"" + serviceType + "\">" +
                    "   <NewRemoteHost></NewRemoteHost>" +
                    "   <NewExternalPort>" + externalPort + "</NewExternalPort>" +
                    "   <NewProtocol>" + protocolString + "</NewProtocol>" +
                    "   <NewInternalPort>" + internalPort + "</NewInternalPort>" +
                    "   <NewInternalClient>" + machineIp + "</NewInternalClient>" +
                    "   <NewEnabled>1</NewEnabled>" +
                    "   <NewPortMappingDescription>" + description + "</NewPortMappingDescription>" +
                    "   <NewLeaseDuration>0</NewLeaseDuration>" +
                    "  </u:AddPortMapping>" +
                    " </s:Body>" +
                    "</s:Envelope>";
                byte[] body = Encoding.ASCII.GetBytes(soapBody);
                var uri = new Uri(new Uri("http://" + gatewayIp + ":" + gatewayPort.ToString()), controlUrl);

                System.Net.WebRequest wr = System.Net.WebRequest.Create(uri);

                wr.Method = "POST";
                wr.Headers.Add("SOAPAction", "\"" + serviceType + "#AddPortMapping\"");
                wr.ContentType = "text/xml;charset=\"utf-8\"";
                wr.ContentLength = body.Length;
                wr.Timeout = (int)timeout.TotalMilliseconds;

                using (System.IO.Stream stream = wr.GetRequestStream())
                {
                    stream.Write(body, 0, body.Length);
                }

                using (WebResponse wres = wr.GetResponse())
                {
                    if (((HttpWebResponse)wres).StatusCode == HttpStatusCode.OK)
                    {
                        return true;
                    }
                }
            }
            catch (Exception)
            {

            }

            return false;
        }
示例#8
0
        public bool OpenPort(UpnpProtocolType protocol, int externalPort, int internalPort, string description, TimeSpan timeout)
        {
            if (_services == null) throw new UpnpClientException();

            #if Windows
            foreach (var nic in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
                .Where(n => n.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up))
            {
                var machineIp = nic.GetIPProperties().UnicastAddresses
                    .Select(n => n.Address)
                    .Where(n => n.AddressFamily == AddressFamily.InterNetwork)
                    .FirstOrDefault();
                if (machineIp == null) continue;

                var gatewayIp = nic.GetIPProperties().GatewayAddresses
                    .Select(n => n.Address)
                    .Where(n => n.AddressFamily == AddressFamily.InterNetwork)
                    .FirstOrDefault();
                if (gatewayIp == null) continue;

                if (gatewayIp.ToString() == _location.Host && OpenPort(protocol, machineIp.ToString(), externalPort, internalPort, description, timeout))
                {
                    return true;
                }
            }
            #endif

            #if Unix
            string hostname = Dns.GetHostName();

            foreach (var ipAddress in Dns.GetHostAddresses(hostname))
            {
                if (ipAddress.AddressFamily != AddressFamily.InterNetwork) continue;

                if (OpenPort(protocol, ipAddress.ToString(), externalPort, internalPort, description, timeout))
                {
                    return true;
                }
            }
            #endif

            return false;
        }
示例#9
0
        private static async ValueTask <bool> ClosePortFromContents(string services, string serviceType, string gatewayIp, int gatewayPort, UpnpProtocolType protocol, int externalPort, CancellationToken token = default)
        {
            if (services == null || !services.Contains(serviceType))
            {
                return(false);
            }

            try
            {
                services = services.Substring(services.IndexOf(serviceType));

                string controlUrl     = _controlUrlRegex.Match(services).Groups["url"].Value.Trim();
                string protocolString = "";

                if (protocol == UpnpProtocolType.Tcp)
                {
                    protocolString = "TCP";
                }
                else if (protocol == UpnpProtocolType.Udp)
                {
                    protocolString = "UDP";
                }

                string soapBody =
                    "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" +
                    " <s:Body>" +
                    "  <u:DeletePortMapping xmlns:u=\"" + serviceType + "\">" +
                    "   <NewRemoteHost></NewRemoteHost>" +
                    "   <NewExternalPort>" + externalPort + "</NewExternalPort>" +
                    "   <NewProtocol>" + protocolString + "</NewProtocol>" +
                    "  </u:DeletePortMapping>" +
                    " </s:Body>" +
                    "</s:Envelope>";
                var uri = new Uri(new Uri("http://" + gatewayIp + ":" + gatewayPort.ToString()), controlUrl);

                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Add("SOAPAction", "\"" + serviceType + "#DeletePortMapping\"");

                    using (var response = await client.PostAsync(uri, new StringContent(soapBody, new UTF8Encoding(false), "text/xml"), token))
                    {
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            return(true);
                        }
                    }
                }

                return(false);
            }
            catch (Exception e)
            {
                throw new UpnpClientException("Failed to get external ip address.", e);
            }
        }
示例#10
0
        private static bool ClosePortFromService(string services, string serviceType, string gatewayIp, int gatewayPort, UpnpProtocolType protocol, int externalPort, TimeSpan timeout)
        {
            if (services == null || !services.Contains(serviceType))
            {
                return(false);
            }

            try
            {
                services = services.Substring(services.IndexOf(serviceType));

                string controlUrl     = _controlUrlRegex.Match(services).Groups["url"].Value;
                string protocolString = "";

                if (protocol == UpnpProtocolType.Tcp)
                {
                    protocolString = "TCP";
                }
                else if (protocol == UpnpProtocolType.Udp)
                {
                    protocolString = "UDP";
                }

                string soapBody =
                    "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" +
                    " <s:Body>" +
                    "  <u:DeletePortMapping xmlns:u=\"" + serviceType + "\">" +
                    "   <NewRemoteHost></NewRemoteHost>" +
                    "   <NewExternalPort>" + externalPort + "</NewExternalPort>" +
                    "   <NewProtocol>" + protocolString + "</NewProtocol>" +
                    "  </u:DeletePortMapping>" +
                    " </s:Body>" +
                    "</s:Envelope>";
                byte[] body = System.Text.UTF8Encoding.ASCII.GetBytes(soapBody);

                Uri uri = new Uri(controlUrl, UriKind.RelativeOrAbsolute);

                if (!uri.IsAbsoluteUri)
                {
                    Uri baseUri = new Uri("http://" + gatewayIp + ":" + gatewayPort.ToString());
                    uri = new Uri(baseUri, uri);
                }

                System.Net.WebRequest wr = System.Net.WebRequest.Create(uri);

                wr.Method = "POST";
                wr.Headers.Add("SOAPAction", "\"" + serviceType + "#DeletePortMapping\"");
                wr.ContentType   = "text/xml;charset=\"utf-8\"";
                wr.ContentLength = body.Length;
                wr.Timeout       = (int)timeout.TotalMilliseconds;

                using (System.IO.Stream stream = wr.GetRequestStream())
                {
                    stream.Write(body, 0, body.Length);
                }

                using (WebResponse wres = wr.GetResponse())
                {
                    if (((HttpWebResponse)wres).StatusCode == HttpStatusCode.OK)
                    {
                        return(true);
                    }
                }
            }
            catch (Exception)
            {
            }

            return(false);
        }