From 40fde5c892c54ce5de62bbda6e6fe2f1690ace72 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 20 Jun 2017 09:52:29 +1000 Subject: [PATCH] Cleanup uPnP a bit --- MCGalaxy/Server/Extra/UPnP.cs | 89 ++++++++++++++++------------------- 1 file changed, 41 insertions(+), 48 deletions(-) diff --git a/MCGalaxy/Server/Extra/UPnP.cs b/MCGalaxy/Server/Extra/UPnP.cs index 7bcb75105..6108b135d 100644 --- a/MCGalaxy/Server/Extra/UPnP.cs +++ b/MCGalaxy/Server/Extra/UPnP.cs @@ -27,17 +27,18 @@ namespace MCGalaxy.Core { public sealed class UPnP { public static bool CanUseUpnp { get { return Discover(); } } - - private const string req = "M-SEARCH * HTTP/1.1\r\n" + - "HOST: 239.255.255.250:1900\r\n" + - "ST:upnp:rootdevice\r\n" + - "MAN:\"ssdp:discover\"\r\n" + - "MX:3\r\n\r\n"; - - static TimeSpan _timeout = new TimeSpan(0, 0, 0, 3); - public static TimeSpan TimeOut { get { return _timeout; } set { _timeout = value; } } + public static TimeSpan Timeout = TimeSpan.FromSeconds(3); + const string req = + "M-SEARCH * HTTP/1.1\r\n" + + "HOST: 239.255.255.250:1900\r\n" + + "ST:upnp:rootdevice\r\n" + + "MAN:\"ssdp:discover\"\r\n" + + "MX:3\r\n" + + "\r\n"; + static string _serviceUrl; + private static bool Discover() { Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); @@ -75,7 +76,7 @@ namespace MCGalaxy.Core { } } } while (length > 0); - } while (start.Subtract(DateTime.UtcNow) < _timeout); + } while (start.Subtract(DateTime.UtcNow) < Timeout); return false; } catch { @@ -83,7 +84,7 @@ namespace MCGalaxy.Core { } } - private static string GetServiceUrl(string resp) { + static string GetServiceUrl(string resp) { #if !DEBUG try { #endif @@ -98,83 +99,75 @@ namespace MCGalaxy.Core { XmlNode node = desc.SelectSingleNode("//tns:service[tns:serviceType=\"urn:schemas-upnp-org:service:WANIPConnection:1\"]/tns:controlURL/text()", nsMgr); if ( node == null ) return null; - XmlNode eventnode = desc.SelectSingleNode("//tns:service[tns:serviceType=\"urn:schemas-upnp-org:service:WANIPConnection:1\"]/tns:eventSubURL/text()", nsMgr); return CombineUrls(resp, node.Value); #if !DEBUG - } - catch { return null; } + } catch { return null; } #endif } - private static string CombineUrls(string resp, string p) { + static string CombineUrls(string resp, string p) { int n = resp.IndexOf("://"); n = resp.IndexOf('/', n + 3); return resp.Substring(0, n) + p; } public static void ForwardPort(int port, ProtocolType protocol, string description) { - if ( string.IsNullOrEmpty(_serviceUrl) ) + if (String.IsNullOrEmpty(_serviceUrl) ) throw new InvalidOperationException("No UPnP service available or Discover() has not been called"); - XmlDocument xdoc = SOAPRequest(_serviceUrl, "" + - "" + port.ToString() + "" + protocol.ToString().ToUpper() + "" + - "" + port.ToString() + "" + GetLocalIP() + - "1" + description + - "0", "AddPortMapping"); + + XmlDocument xdoc = SOAPRequest(_serviceUrl, + "" + + "" + + "" + port.ToString() + "" + + "" + protocol.ToString().ToUpper() + "" + + "" + port.ToString() + "" + + "" + GetLocalIP() + "" + + "1" + + "" + description + "" + + "0" + + "", "AddPortMapping"); } public static void DeleteForwardingRule(int port, ProtocolType protocol) { - if ( string.IsNullOrEmpty(_serviceUrl) ) + if (String.IsNullOrEmpty(_serviceUrl) ) throw new InvalidOperationException("No UPnP service available or Discover() has not been called"); + XmlDocument xdoc = SOAPRequest(_serviceUrl, "" + - "" + - "" + + """ + "" + port + "" + "" + protocol.ToString().ToUpper() + "" + "", "DeletePortMapping"); - } - [DebuggerStepThrough] - public static IPAddress GetExternalIP() { - if ( string.IsNullOrEmpty(_serviceUrl) ) - throw new InvalidOperationException("No UPnP service available or Discover() has not been called"); - XmlDocument xdoc = SOAPRequest(_serviceUrl, "" + - "", "GetExternalIPAddress"); - XmlNamespaceManager nsMgr = new XmlNamespaceManager(xdoc.NameTable); - nsMgr.AddNamespace("tns", "urn:schemas-upnp-org:device-1-0"); - string IP = xdoc.SelectSingleNode("//NewExternalIPAddress/text()", nsMgr).Value; - return IPAddress.Parse(IP); - } - - [DebuggerStepThrough] public static string GetLocalIP() { - IPHostEntry host; string localIP = "?"; - host = Dns.GetHostEntry(Dns.GetHostName()); - foreach ( IPAddress ip in host.AddressList ) { - if ( ip.AddressFamily == AddressFamily.InterNetwork ) { + IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName()); + + foreach (IPAddress ip in host.AddressList) { + if (ip.AddressFamily == AddressFamily.InterNetwork) { localIP = ip.ToString(); } } return localIP; } + - private static XmlDocument SOAPRequest(string url, string soap, string function) { + static XmlDocument SOAPRequest(string url, string soap, string function) { string req = "" + "" + - "" + - soap + - "" + + "" + soap + "" + ""; WebRequest r = HttpWebRequest.Create(url); - r.Method = "POST"; - byte[] b = Encoding.UTF8.GetBytes(req); + r.Method = "POST"; r.Headers.Add("SOAPACTION", "\"urn:schemas-upnp-org:service:WANIPConnection:1#" + function + "\""); r.ContentType = "text/xml; charset=\"utf-8\""; + + byte[] b = Encoding.UTF8.GetBytes(req); r.ContentLength = b.Length; r.GetRequestStream().Write(b, 0, b.Length); + XmlDocument resp = new XmlDocument(); WebResponse wres = r.GetResponse(); Stream ress = wres.GetResponseStream();