AdvancedBlocking: updated implementation to support using domain names for local endpoint group map feature which will work with requests over DoT, DoH and DoQ protocols.

This commit is contained in:
Shreyas Zare
2024-02-17 17:18:22 +05:30
parent 05ec6a5da4
commit a1ec420f83
2 changed files with 39 additions and 11 deletions

View File

@@ -51,7 +51,7 @@ namespace AdvancedBlocking
bool _enableBlocking;
int _blockListUrlUpdateIntervalHours;
Dictionary<IPEndPoint, string> _localEndPointGroupMap;
Dictionary<EndPoint, string> _localEndPointGroupMap;
Dictionary<NetworkAddress, string> _networkGroupMap;
Dictionary<string, Group> _groups;
@@ -279,16 +279,42 @@ namespace AdvancedBlocking
{
if ((request.Metadata is not null) && (request.Metadata.NameServer is not null))
{
Uri requestLocalUriEP = request.Metadata.NameServer.DoHEndPoint;
if (requestLocalUriEP is not null)
{
foreach (KeyValuePair<EndPoint, string> entry in _localEndPointGroupMap)
{
if (entry.Key is DomainEndPoint ep)
{
if (((ep.Port == 0) || (ep.Port == requestLocalUriEP.Port)) && ep.Address.Equals(requestLocalUriEP.Host, StringComparison.OrdinalIgnoreCase))
return entry.Value;
}
}
}
DomainEndPoint requestLocalDomainEP = request.Metadata.NameServer.DomainEndPoint;
if (requestLocalDomainEP is not null)
{
foreach (KeyValuePair<EndPoint, string> entry in _localEndPointGroupMap)
{
if (entry.Key is DomainEndPoint ep)
{
if (((ep.Port == 0) || (ep.Port == requestLocalDomainEP.Port)) && ep.Address.Equals(requestLocalDomainEP.Address, StringComparison.OrdinalIgnoreCase))
return entry.Value;
}
}
}
IPEndPoint requestLocalEP = request.Metadata.NameServer.IPEndPoint;
if (requestLocalEP is not null)
{
foreach (KeyValuePair<IPEndPoint, string> entry in _localEndPointGroupMap)
foreach (KeyValuePair<EndPoint, string> entry in _localEndPointGroupMap)
{
if ((entry.Key.Port == 0) && entry.Key.Address.Equals(requestLocalEP.Address))
return entry.Value;
if (entry.Key.Equals(requestLocalEP))
return entry.Value;
if (entry.Key is IPEndPoint ep)
{
if (((ep.Port == 0) || (ep.Port == requestLocalEP.Port)) && ep.Address.Equals(requestLocalEP.Address))
return entry.Value;
}
}
}
}
@@ -331,12 +357,12 @@ namespace AdvancedBlocking
if (jsonConfig.TryReadObjectAsMap("localEndPointGroupMap",
delegate (string localEP, JsonElement jsonGroup)
{
if (!IPEndPoint.TryParse(localEP, out IPEndPoint ep))
if (!EndPointExtensions.TryParse(localEP, out EndPoint ep))
throw new InvalidOperationException("Local end point group map contains an invalid end point: " + localEP);
return new Tuple<IPEndPoint, string>(ep, jsonGroup.GetString());
return new Tuple<EndPoint, string>(ep, jsonGroup.GetString());
},
out Dictionary<IPEndPoint, string> localEndPointGroupMap))
out Dictionary<EndPoint, string> localEndPointGroupMap))
{
_localEndPointGroupMap = localEndPointGroupMap;
}

View File

@@ -3,7 +3,9 @@
"blockListUrlUpdateIntervalHours": 24,
"localEndPointGroupMap": {
"127.0.0.1": "bypass",
"192.168.10.2:53": "bypass"
"192.168.10.2:53": "bypass",
"user1.dot.example.com": "kids",
"user2.doh.example.com:443": "bypass"
},
"networkGroupMap": {
"192.168.10.20": "kids",