UpnpNatDevice.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // Authors:
  3. // Alan McGovern alan.mcgovern@gmail.com
  4. // Ben Motmans <ben.motmans@gmail.com>
  5. //
  6. // Copyright (C) 2006 Alan McGovern
  7. // Copyright (C) 2007 Ben Motmans
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.IO;
  30. using System.Net;
  31. using System.Xml;
  32. using System.Text;
  33. using System.Diagnostics;
  34. using System.Threading.Tasks;
  35. using MediaBrowser.Common.Net;
  36. using Microsoft.Extensions.Logging;
  37. using MediaBrowser.Model.Dlna;
  38. namespace Mono.Nat.Upnp
  39. {
  40. public sealed class UpnpNatDevice : AbstractNatDevice, IEquatable<UpnpNatDevice>
  41. {
  42. private EndPoint hostEndPoint;
  43. private IPAddress localAddress;
  44. private string serviceDescriptionUrl;
  45. private string controlUrl;
  46. private string serviceType;
  47. private readonly ILogger _logger;
  48. private readonly IHttpClient _httpClient;
  49. public override IPAddress LocalAddress
  50. {
  51. get { return localAddress; }
  52. }
  53. internal UpnpNatDevice(IPAddress localAddress, UpnpDeviceInfo deviceInfo, IPEndPoint hostEndPoint, string serviceType, ILogger logger, IHttpClient httpClient)
  54. {
  55. if (localAddress == null)
  56. {
  57. throw new ArgumentNullException(nameof(localAddress));
  58. }
  59. this.LastSeen = DateTime.Now;
  60. this.localAddress = localAddress;
  61. // Split the string at the "location" section so i can extract the ipaddress and service description url
  62. string locationDetails = deviceInfo.Location.ToString();
  63. this.serviceType = serviceType;
  64. _logger = logger;
  65. _httpClient = httpClient;
  66. // Make sure we have no excess whitespace
  67. locationDetails = locationDetails.Trim();
  68. // FIXME: Is this reliable enough. What if we get a hostname as opposed to a proper http address
  69. // Are we going to get addresses with the "http://" attached?
  70. if (locationDetails.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
  71. {
  72. _logger.LogDebug("Found device at: {0}", locationDetails);
  73. // This bit strings out the "http://" from the string
  74. locationDetails = locationDetails.Substring(7);
  75. this.hostEndPoint = hostEndPoint;
  76. // The service description URL is the remainder of the "locationDetails" string. The bit that was originally after the ip
  77. // and port information
  78. this.serviceDescriptionUrl = locationDetails.Substring(locationDetails.IndexOf('/'));
  79. }
  80. else
  81. {
  82. _logger.LogDebug("Couldn't decode address. Please send following string to the developer: ");
  83. }
  84. }
  85. public async Task GetServicesList()
  86. {
  87. // Create a HTTPWebRequest to download the list of services the device offers
  88. var message = new GetServicesMessage(this.serviceDescriptionUrl, this.hostEndPoint, _logger);
  89. using (var response = await _httpClient.SendAsync(message.Encode(), message.Method).ConfigureAwait(false))
  90. {
  91. OnServicesReceived(response);
  92. }
  93. }
  94. private void OnServicesReceived(HttpResponseInfo response)
  95. {
  96. int abortCount = 0;
  97. int bytesRead = 0;
  98. byte[] buffer = new byte[10240];
  99. var servicesXml = new StringBuilder();
  100. var xmldoc = new XmlDocument();
  101. using (var s = response.Content)
  102. {
  103. if (response.StatusCode != HttpStatusCode.OK)
  104. {
  105. _logger.LogDebug("{0}: Couldn't get services list: {1}", HostEndPoint, response.StatusCode);
  106. return; // FIXME: This the best thing to do??
  107. }
  108. while (true)
  109. {
  110. bytesRead = s.Read(buffer, 0, buffer.Length);
  111. servicesXml.Append(Encoding.UTF8.GetString(buffer, 0, bytesRead));
  112. try
  113. {
  114. xmldoc.LoadXml(servicesXml.ToString());
  115. break;
  116. }
  117. catch (XmlException)
  118. {
  119. // If we can't receive the entire XML within 500ms, then drop the connection
  120. // Unfortunately not all routers supply a valid ContentLength (mine doesn't)
  121. // so this hack is needed to keep testing our recieved data until it gets successfully
  122. // parsed by the xmldoc. Without this, the code will never pick up my router.
  123. if (abortCount++ > 50)
  124. {
  125. return;
  126. }
  127. _logger.LogDebug("{0}: Couldn't parse services list", HostEndPoint);
  128. System.Threading.Thread.Sleep(10);
  129. }
  130. }
  131. var ns = new XmlNamespaceManager(xmldoc.NameTable);
  132. ns.AddNamespace("ns", "urn:schemas-upnp-org:device-1-0");
  133. XmlNodeList nodes = xmldoc.SelectNodes("//*/ns:serviceList", ns);
  134. foreach (XmlNode node in nodes)
  135. {
  136. //Go through each service there
  137. foreach (XmlNode service in node.ChildNodes)
  138. {
  139. //If the service is a WANIPConnection, then we have what we want
  140. string type = service["serviceType"].InnerText;
  141. _logger.LogDebug("{0}: Found service: {1}", HostEndPoint, type);
  142. // TODO: Add support for version 2 of UPnP.
  143. if (string.Equals(type, "urn:schemas-upnp-org:service:WANPPPConnection:1", StringComparison.OrdinalIgnoreCase) ||
  144. string.Equals(type, "urn:schemas-upnp-org:service:WANIPConnection:1", StringComparison.OrdinalIgnoreCase))
  145. {
  146. this.controlUrl = service["controlURL"].InnerText;
  147. _logger.LogDebug("{0}: Found upnp service at: {1}", HostEndPoint, controlUrl);
  148. Uri u;
  149. if (Uri.TryCreate(controlUrl, UriKind.RelativeOrAbsolute, out u))
  150. {
  151. if (u.IsAbsoluteUri)
  152. {
  153. var old = hostEndPoint;
  154. IPAddress parsedHostIpAddress;
  155. if (IPAddress.TryParse(u.Host, out parsedHostIpAddress))
  156. {
  157. this.hostEndPoint = new IPEndPoint(parsedHostIpAddress, u.Port);
  158. //_logger.LogDebug("{0}: Absolute URI detected. Host address is now: {1}", old, HostEndPoint);
  159. this.controlUrl = controlUrl.Substring(u.GetLeftPart(UriPartial.Authority).Length);
  160. //_logger.LogDebug("{0}: New control url: {1}", HostEndPoint, controlUrl);
  161. }
  162. }
  163. }
  164. else
  165. {
  166. _logger.LogDebug("{0}: Assuming control Uri is relative: {1}", HostEndPoint, controlUrl);
  167. }
  168. return;
  169. }
  170. }
  171. }
  172. //If we get here, it means that we didn't get WANIPConnection service, which means no uPnP forwarding
  173. //So we don't invoke the callback, so this device is never added to our lists
  174. }
  175. }
  176. /// <summary>
  177. /// The EndPoint that the device is at
  178. /// </summary>
  179. internal EndPoint HostEndPoint
  180. {
  181. get { return this.hostEndPoint; }
  182. }
  183. /// <summary>
  184. /// The relative url of the xml file that describes the list of services is at
  185. /// </summary>
  186. internal string ServiceDescriptionUrl
  187. {
  188. get { return this.serviceDescriptionUrl; }
  189. }
  190. /// <summary>
  191. /// The relative url that we can use to control the port forwarding
  192. /// </summary>
  193. internal string ControlUrl
  194. {
  195. get { return this.controlUrl; }
  196. }
  197. /// <summary>
  198. /// The service type we're using on the device
  199. /// </summary>
  200. public string ServiceType
  201. {
  202. get { return serviceType; }
  203. }
  204. public override async Task CreatePortMap(Mapping mapping)
  205. {
  206. var message = new CreatePortMappingMessage(mapping, localAddress, this);
  207. using (await _httpClient.SendAsync(message.Encode(), message.Method).ConfigureAwait(false))
  208. {
  209. }
  210. }
  211. public override bool Equals(object obj)
  212. {
  213. var device = obj as UpnpNatDevice;
  214. return (device == null) ? false : this.Equals((device));
  215. }
  216. public bool Equals(UpnpNatDevice other)
  217. {
  218. return (other == null) ? false : (this.hostEndPoint.Equals(other.hostEndPoint)
  219. //&& this.controlUrl == other.controlUrl
  220. && this.serviceDescriptionUrl == other.serviceDescriptionUrl);
  221. }
  222. public override int GetHashCode()
  223. {
  224. return (this.hostEndPoint.GetHashCode() ^ this.controlUrl.GetHashCode() ^ this.serviceDescriptionUrl.GetHashCode());
  225. }
  226. /// <summary>
  227. /// Overridden.
  228. /// </summary>
  229. /// <returns></returns>
  230. public override string ToString()
  231. {
  232. //GetExternalIP is blocking and can throw exceptions, can't use it here.
  233. return String.Format(
  234. "UpnpNatDevice - EndPoint: {0}, External IP: {1}, Control Url: {2}, Service Description Url: {3}, Service Type: {4}, Last Seen: {5}",
  235. this.hostEndPoint, "Manually Check" /*this.GetExternalIP()*/, this.controlUrl, this.serviceDescriptionUrl, this.serviceType, this.LastSeen);
  236. }
  237. }
  238. }