jellyfin/SocketHttpListener/Net/EndPointManager.cs

168 lines
5.6 KiB
C#
Raw Normal View History

2016-11-11 12:55:12 -07:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
2017-03-12 12:27:26 -07:00
using MediaBrowser.Model.IO;
2016-11-11 12:55:12 -07:00
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Net;
using SocketHttpListener.Primitives;
namespace SocketHttpListener.Net
{
sealed class EndPointManager
{
// Dictionary<IPAddress, Dictionary<int, EndPointListener>>
static Dictionary<string, Dictionary<int, EndPointListener>> ip_to_endpoints = new Dictionary<string, Dictionary<int, EndPointListener>>();
private EndPointManager()
{
}
public static void AddListener(ILogger logger, HttpListener listener)
{
List<string> added = new List<string>();
try
{
lock (ip_to_endpoints)
{
foreach (string prefix in listener.Prefixes)
{
AddPrefixInternal(logger, prefix, listener);
added.Add(prefix);
}
}
}
catch
{
foreach (string prefix in added)
{
RemovePrefix(logger, prefix, listener);
}
throw;
}
}
public static void AddPrefix(ILogger logger, string prefix, HttpListener listener)
{
lock (ip_to_endpoints)
{
AddPrefixInternal(logger, prefix, listener);
}
}
static void AddPrefixInternal(ILogger logger, string p, HttpListener listener)
{
ListenerPrefix lp = new ListenerPrefix(p);
if (lp.Path.IndexOf('%') != -1)
throw new HttpListenerException(400, "Invalid path.");
if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1) // TODO: Code?
throw new HttpListenerException(400, "Invalid path.");
// listens on all the interfaces if host name cannot be parsed by IPAddress.
EndPointListener epl = GetEPListener(logger, lp.Host, lp.Port, listener, lp.Secure).Result;
epl.AddPrefix(lp, listener);
}
2017-09-02 19:42:13 -07:00
private static IPAddress GetIpAnyAddress(HttpListener listener)
2016-11-11 12:55:12 -07:00
{
2017-09-02 19:42:13 -07:00
return listener.EnableDualMode ? IPAddress.IPv6Any : IPAddress.Any;
2016-11-11 12:55:12 -07:00
}
static async Task<EndPointListener> GetEPListener(ILogger logger, string host, int port, HttpListener listener, bool secure)
{
var networkManager = listener.NetworkManager;
2017-09-02 19:42:13 -07:00
IPAddress addr;
2016-11-11 12:55:12 -07:00
if (host == "*" || host == "+")
addr = GetIpAnyAddress(listener);
2017-09-02 19:42:13 -07:00
else if (IPAddress.TryParse(host, out addr) == false)
2016-11-11 12:55:12 -07:00
{
try
{
2017-08-24 12:52:19 -07:00
var all = (await networkManager.GetHostAddressesAsync(host).ConfigureAwait(false));
2017-09-02 19:42:13 -07:00
addr = (all.Length == 0 ? null : IPAddress.Parse(all[0].Address)) ??
2016-11-11 12:55:12 -07:00
GetIpAnyAddress(listener);
}
catch
{
addr = GetIpAnyAddress(listener);
}
}
Dictionary<int, EndPointListener> p = null; // Dictionary<int, EndPointListener>
2017-09-02 19:42:13 -07:00
if (!ip_to_endpoints.TryGetValue(addr.ToString(), out p))
2016-11-11 12:55:12 -07:00
{
p = new Dictionary<int, EndPointListener>();
2017-09-02 19:42:13 -07:00
ip_to_endpoints[addr.ToString()] = p;
2016-11-11 12:55:12 -07:00
}
EndPointListener epl = null;
if (p.ContainsKey(port))
{
epl = (EndPointListener)p[port];
}
else
{
2017-09-02 19:42:13 -07:00
epl = new EndPointListener(listener, addr, port, secure, listener.Certificate, logger, listener.CryptoProvider, listener.SocketFactory, listener.MemoryStreamFactory, listener.TextEncoding, listener.FileSystem, listener.EnvironmentInfo);
2016-11-11 12:55:12 -07:00
p[port] = epl;
}
return epl;
}
2017-09-02 19:42:13 -07:00
public static void RemoveEndPoint(EndPointListener epl, IPEndPoint ep)
2016-11-11 12:55:12 -07:00
{
lock (ip_to_endpoints)
{
// Dictionary<int, EndPointListener> p
Dictionary<int, EndPointListener> p;
2017-09-02 19:42:13 -07:00
if (ip_to_endpoints.TryGetValue(ep.Address.ToString(), out p))
2016-11-11 12:55:12 -07:00
{
p.Remove(ep.Port);
if (p.Count == 0)
{
2017-09-02 19:42:13 -07:00
ip_to_endpoints.Remove(ep.Address.ToString());
2016-11-11 12:55:12 -07:00
}
}
epl.Close();
}
}
public static void RemoveListener(ILogger logger, HttpListener listener)
{
lock (ip_to_endpoints)
{
foreach (string prefix in listener.Prefixes)
{
RemovePrefixInternal(logger, prefix, listener);
}
}
}
public static void RemovePrefix(ILogger logger, string prefix, HttpListener listener)
{
lock (ip_to_endpoints)
{
RemovePrefixInternal(logger, prefix, listener);
}
}
static void RemovePrefixInternal(ILogger logger, string prefix, HttpListener listener)
{
ListenerPrefix lp = new ListenerPrefix(prefix);
if (lp.Path.IndexOf('%') != -1)
return;
if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1)
return;
EndPointListener epl = GetEPListener(logger, lp.Host, lp.Port, listener, lp.Secure).Result;
epl.RemovePrefix(lp, listener);
}
}
}