29 using System.Collections.Generic;
32 using System.Reflection;
34 using LukeSkywalker.IPNetwork;
37 namespace OpenSim.Framework
41 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
43 public string Name {
get;
private set; }
45 private List<IPNetwork> m_blacklistNetworks;
46 private List<IPEndPoint> m_blacklistEndPoints;
48 private List<IPNetwork> m_blacklistExceptionNetworks;
49 private List<IPEndPoint> m_blacklistExceptionEndPoints;
53 List<IPNetwork> blacklistNetworks, List<IPEndPoint> blacklistEndPoints,
54 List<IPNetwork> blacklistExceptionNetworks, List<IPEndPoint> blacklistExceptionEndPoints)
58 m_blacklistNetworks = blacklistNetworks;
59 m_blacklistEndPoints = blacklistEndPoints;
60 m_blacklistExceptionNetworks = blacklistExceptionNetworks;
61 m_blacklistExceptionEndPoints = blacklistExceptionEndPoints;
73 string configBlacklist
74 =
"0.0.0.0/8|10.0.0.0/8|100.64.0.0/10|127.0.0.0/8|169.254.0.0/16|172.16.0.0/12|192.0.0.0/24|192.0.2.0/24|192.88.99.0/24|192.168.0.0/16|198.18.0.0/15|198.51.100.0/24|203.0.113.0/24|224.0.0.0/4|240.0.0.0/4|255.255.255.255/32";
75 string configBlacklistExceptions =
"";
77 IConfig networkConfig = config.Configs[
"Network"];
79 if (networkConfig != null)
81 configBlacklist = networkConfig.GetString(
"OutboundDisallowForUserScripts", configBlacklist);
82 configBlacklistExceptions
83 = networkConfig.GetString(
"OutboundDisallowForUserScriptsExcept", configBlacklistExceptions);
87 "[OUTBOUND URL FILTER]: OutboundDisallowForUserScripts for {0} is [{1}]", Name, configBlacklist);
89 "[OUTBOUND URL FILTER]: OutboundDisallowForUserScriptsExcept for {0} is [{1}]", Name, configBlacklistExceptions);
91 OutboundUrlFilter.ParseConfigList(
92 configBlacklist, Name, out m_blacklistNetworks, out m_blacklistEndPoints);
93 OutboundUrlFilter.ParseConfigList(
94 configBlacklistExceptions, Name, out m_blacklistExceptionNetworks, out m_blacklistExceptionEndPoints);
97 private static void ParseConfigList(
98 string fullConfigEntry,
string filterName, out List<IPNetwork> networks, out List<IPEndPoint> endPoints)
101 string[] configBlacklistEntries
102 = fullConfigEntry.Split(
new char[] {
'|' }, StringSplitOptions.RemoveEmptyEntries);
104 configBlacklistEntries = configBlacklistEntries.Select(e => e.Trim()).ToArray();
106 networks =
new List<IPNetwork>();
107 endPoints =
new List<IPEndPoint>();
109 foreach (
string configEntry
in configBlacklistEntries)
111 if (configEntry.Contains(
"/"))
115 if (!IPNetwork.TryParse(configEntry, out network))
118 "[OUTBOUND URL FILTER]: Entry [{0}] is invalid network for {1}", configEntry, filterName);
123 networks.Add(network);
129 if (!Uri.TryCreate(
"http://" + configEntry, UriKind.Absolute, out configEntryUri))
132 "[OUTBOUND URL FILTER]: EndPoint entry [{0}] is invalid endpoint for {1}",
133 configEntry, filterName);
138 IPAddress[] addresses = Dns.GetHostAddresses(configEntryUri.Host);
140 foreach (IPAddress addr
in addresses)
142 if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
146 IPEndPoint configEntryEp =
new IPEndPoint(addr, configEntryUri.Port);
147 endPoints.Add(configEntryEp);
165 private static bool IsInNetwork(
166 IPAddress addr,
int port, List<IPNetwork> networks, List<IPEndPoint> endPoints,
string filterName)
168 foreach (IPNetwork ipn
in networks)
173 if (IPNetwork.Contains(ipn, addr))
184 foreach (IPEndPoint ep
in endPoints)
190 if (addr.Equals(ep.Address) && port == ep.Port)
213 bool foundIpv4Address =
false;
215 IPAddress[] addresses = Dns.GetHostAddresses(url.Host);
217 foreach (IPAddress addr
in addresses)
219 if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
223 foundIpv4Address =
true;
226 if (
OutboundUrlFilter.IsInNetwork(addr, url.Port, m_blacklistNetworks, m_blacklistEndPoints, Name))
232 = OutboundUrlFilter.IsInNetwork(
233 addr, url.Port, m_blacklistExceptionNetworks, m_blacklistExceptionEndPoints, Name);
248 if (!foundIpv4Address)
bool CheckAllowed(Uri url)
Checks whether the given url is allowed by the filter.
OutboundUrlFilter(string name, List< IPNetwork > blacklistNetworks, List< IPEndPoint > blacklistEndPoints, List< IPNetwork > blacklistExceptionNetworks, List< IPEndPoint > blacklistExceptionEndPoints)
OutboundUrlFilter(string name, IConfigSource config)
Initializes a new instance of the OpenSim.Framework.OutboundUrlFilter class.