Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
FromIpLocation.cs
1using System;
2using System.Reflection;
3using System.Threading.Tasks;
4using System.Xml;
6using Waher.Events;
11
13{
19 {
20 private static IEndpointLocalizationService[] localizationServices;
21
22 private readonly StringAttribute country;
23 private readonly StringAttribute region;
24 private readonly StringAttribute city;
25
26 static FromIpLocation()
27 {
28 InitServices();
29 Types.OnInvalidated += (sender, e) => InitServices();
30 }
31
32 private static void InitServices()
33 {
35
37 {
38 ConstructorInfo CI = Types.GetDefaultConstructor(T);
39 if (CI is null)
40 continue;
41
42 try
43 {
44 IEndpointLocalizationService Service = (IEndpointLocalizationService)CI.Invoke(Array.Empty<object>());
45 Services.Add(Service);
46 }
47 catch (Exception ex)
48 {
49 Log.Exception(ex, T.FullName);
50 }
51 }
52
53 localizationServices = Services.ToArray();
54 }
55
61 : base()
62 {
63 }
64
73 : base(Xml, Parent, Document)
74 {
75 this.country = new StringAttribute(Xml, "country");
76 this.region = new StringAttribute(Xml, "region");
77 this.city = new StringAttribute(Xml, "city");
78 }
79
83 public override string LocalName => nameof(FromIpLocation);
84
92 public override WafAction Create(XmlElement Xml, WafAction Parent, WebApplicationFirewall Document) => new FromIpLocation(Xml, Parent, Document);
93
99 public override async Task<WafResult?> Review(ProcessingState State)
100 {
101 string Endpoint = State.Request.RemoteEndPoint.RemovePortNumber();
102 string Country = await this.country.EvaluateAsync(State.Variables, string.Empty);
103 string Region = await this.region.EvaluateAsync(State.Variables, string.Empty);
104 string City = await this.city.EvaluateAsync(State.Variables, string.Empty);
105 string Key = Endpoint + "|Loc|" + Country + "|" + Region + "|" + City;
106
107 if (!State.TryGetCachedObject(Key, out bool IsMatch))
108 {
109 IEndpointLocalization Loc = null;
110
111 foreach (IEndpointLocalizationService Service in localizationServices)
112 {
113 Loc = await Service.TryGetLocation(Endpoint);
114 if (!(Loc is null))
115 break;
116 }
117
118 if (Loc is null)
119 IsMatch = false;
120 else
121 {
122 IsMatch =
123 CheckMatch(Loc.CountryCode, Country) &&
124 CheckMatch(Loc.Region, Region) &&
125 CheckMatch(Loc.City, City);
126 }
127
128 State.AddToCache(Key, IsMatch, fiveMinutes);
129 }
130
131 return IsMatch ? await this.ReviewChildren(State) : null;
132 }
133
134 private static bool CheckMatch(string Value, string List)
135 {
136 if (string.IsNullOrEmpty(List))
137 return true;
138
139 string[] Items = List.Split(',', StringSplitOptions.RemoveEmptyEntries);
140
141 foreach (string Item in Items)
142 {
143 if (string.Equals(Value, Item.Trim(), StringComparison.OrdinalIgnoreCase))
144 return true;
145 }
146
147 return false;
148 }
149 }
150}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1657
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static Type[] GetTypesImplementingInterface(string InterfaceFullName)
Gets all types implementing a given interface.
Definition: Types.cs:85
static ConstructorInfo GetDefaultConstructor(Type Type)
Gets the default constructor of a type, if one exists.
Definition: Types.cs:1742
Checks if the requests come from a specific location, using available locale information derived from...
override WafAction Create(XmlElement Xml, WafAction Parent, WebApplicationFirewall Document)
Creates a WAF action from its XML definition.
FromIpLocation()
Checks if the requests come from a specific location, using available locale information derived from...
FromIpLocation(XmlElement Xml, WafAction Parent, WebApplicationFirewall Document)
Checks if the requests come from a specific location, using available locale information derived from...
override string LocalName
XML Local Name for the XML element defining the action.
override async Task< WafResult?> Review(ProcessingState State)
Reviews the processing state, and returns a WAF result, if any.
Abstract base class for Web Application Firewall comparisons.
Definition: WafComparison.cs:9
Contains the current state of a review process.
Variables Variables
Set of variables.
void AddToCache(string Key, object Value)
Adds a value to the cache.
HttpRequest Request
Current HTTP Request
Abstract base class for Web Application Firewall actions.
Definition: WafAction.cs:17
WebApplicationFirewall Document
Web Application Firewall document.
Definition: WafAction.cs:60
static readonly TimeSpan fiveMinutes
Five minutes time span.
Definition: WafAction.cs:131
async Task< WafResult?> ReviewChildren(ProcessingState State)
Reviews the processing state, and returns a WAF result, if any.
Definition: WafActions.cs:72
Web Application Firewall for HttpServer.
Interface for Enndpoint localizations.
Task< IEndpointLocalization > TryGetLocation(string Endpoint)
Tries to get localization information about an endpoint.