Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
FromIp.cs
1using System;
2using System.Net;
3using System.Threading.Tasks;
4using System.Xml;
8
10{
15 public class FromIp : WafComparison
16 {
17 private readonly StringAttribute value;
18
23 public FromIp()
24 : base()
25 {
26 }
27
35 public FromIp(XmlElement Xml, WafAction Parent, WebApplicationFirewall Document)
36 : base(Xml, Parent, Document)
37 {
38 this.value = new StringAttribute(Xml, "value");
39 }
40
44 public override string LocalName => nameof(FromIp);
45
53 public override WafAction Create(XmlElement Xml, WafAction Parent, WebApplicationFirewall Document) => new FromIp(Xml, Parent, Document);
54
60 public override async Task<WafResult?> Review(ProcessingState State)
61 {
62 string Endpoint = State.Request.RemoteEndPoint.RemovePortNumber();
63
64 if (!IPAddress.TryParse(Endpoint, out IPAddress Address))
65 return null;
66
67 string Value = await this.value.EvaluateAsync(State.Variables, string.Empty);
68 string Key = Endpoint + "|IP|" + Value;
69
70 if (!State.TryGetCachedObject(Key, out bool IsMatch))
71 {
72 IsMatch = CheckMatch(Address, Value);
73 State.AddToCache(Key, IsMatch, fiveMinutes);
74 }
75
76 return IsMatch ? await this.ReviewChildren(State) : null;
77 }
78
79 private static bool CheckMatch(IPAddress Value, string List)
80 {
81 string[] Items = List.Split(',', StringSplitOptions.RemoveEmptyEntries);
82
83 foreach (string Item in Items)
84 {
85 string s = Item.Trim();
86
87 if (s.Contains('/'))
88 {
89 if (IpCidr.TryParse(s, out IpCidr Cidr) && Cidr.Matches(Value))
90 return true;
91 }
92 else if (IPAddress.TryParse(s, out IPAddress Addr))
93 {
94 if (Addr.Equals(Value))
95 return true;
96 }
97 }
98
99 return false;
100 }
101 }
102}
IP Address Rangee, expressed using CIDR format.
Definition: IpCidr.cs:10
static bool TryParse(string s, out IpCidr Parsed)
Tries to parse an IP Range in CIDR format. (An IP Address alone is considered implicitly having a mas...
Definition: IpCidr.cs:54
Checks if the requests come from a specific IP or IPs using a comma-separated list of CIDR ranges.
Definition: FromIp.cs:16
override async Task< WafResult?> Review(ProcessingState State)
Reviews the processing state, and returns a WAF result, if any.
Definition: FromIp.cs:60
override WafAction Create(XmlElement Xml, WafAction Parent, WebApplicationFirewall Document)
Creates a WAF action from its XML definition.
FromIp()
Checks if the requests come from a specific IP or IPs using a comma-separated list of CIDR ranges.
Definition: FromIp.cs:23
FromIp(XmlElement Xml, WafAction Parent, WebApplicationFirewall Document)
Checks if the requests come from a specific IP or IPs using a comma-separated list of CIDR ranges.
Definition: FromIp.cs:35
override string LocalName
XML Local Name for the XML element defining the action.
Definition: FromIp.cs:44
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.