forked from vpnhood/VpnHood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetFilter.cs
44 lines (36 loc) · 1.19 KB
/
NetFilter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System.Net;
using PacketDotNet;
using VpnHood.Common.Net;
namespace VpnHood.Server;
public class NetFilter : INetFilter
{
private readonly IpRangeOrderedList _loopbackIpRange = IpNetwork.LoopbackNetworks.ToIpRanges();
private IpRangeOrderedList _blockedIpRanges = new([]);
public NetFilter()
{
BlockedIpRanges = _loopbackIpRange;
}
public IpRangeOrderedList BlockedIpRanges
{
get => _blockedIpRanges;
set => _blockedIpRanges = _loopbackIpRange.Union(value);
}
private bool IsIpAddressBlocked(IPAddress ipAddress)
{
return BlockedIpRanges.IsInRange(ipAddress);
}
// ReSharper disable once ReturnTypeCanBeNotNullable
public virtual IPPacket? ProcessRequest(IPPacket ipPacket)
{
return IsIpAddressBlocked(ipPacket.DestinationAddress) ? null : ipPacket;
}
// ReSharper disable once ReturnTypeCanBeNotNullable
public virtual IPEndPoint? ProcessRequest(ProtocolType protocol, IPEndPoint requestEndPoint)
{
return IsIpAddressBlocked(requestEndPoint.Address) ? null : requestEndPoint;
}
public virtual IPPacket ProcessReply(IPPacket ipPacket)
{
return ipPacket;
}
}