Skip to content

Commit 7b05bc9

Browse files
committed
MMDB supports country code filtering
1 parent 2f41abc commit 7b05bc9

3 files changed

Lines changed: 32 additions & 25 deletions

File tree

src/deserialize/mmdb.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
from model import RuleModel
66

77

8-
def deserialize(filepath: Path) -> RuleModel:
8+
def deserialize(filepath: Path, country_code: str | None = None) -> RuleModel:
99
rules = RuleModel()
1010
with maxminddb.open_database(filepath) as reader:
11-
for ip, _ in reader:
12-
ip_cidr_list = rules.ip_cidr if ip.version == 4 else rules.ip_cidr6
13-
ip_cidr_list.add(str(ip))
11+
if country_code is None:
12+
for ip, _ in reader:
13+
ip_cidr_list = rules.ip_cidr if ip.version == 4 else rules.ip_cidr6
14+
ip_cidr_list.add(str(ip))
15+
else:
16+
for ip, info in reader:
17+
if info["country"]["iso_code"] == country_code:
18+
ip_cidr_list = rules.ip_cidr if ip.version == 4 else rules.ip_cidr6
19+
ip_cidr_list.add(str(ip))
1420
return rules

src/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def deserialize_data(
6363
)
6464
return de.deserialize()
6565
elif format == ResourceFormat.MaxMindDB:
66-
return mmdb.deserialize(data)
66+
return mmdb.deserialize(data, country_code=option.geo_ip_country_code)
6767
raise Exception(f"Unknown format: {format}")
6868

6969

src/source.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,13 @@
1818
"https://ruleset.skk.moe/List/ip/reject.conf",
1919
],
2020
target_path="Sukka-ad",
21-
include=[SerializeFormat.Surge, SerializeFormat.Loon],
2221
),
2322
SourceModel(
24-
resources=[
25-
"https://ruleset.skk.moe/List/non_ip/reject-no-drop.conf",
26-
("https://ruleset.skk.moe/List/domainset/reject.conf", "DOMAIN-SET"),
27-
"https://ruleset.skk.moe/List/non_ip/reject.conf",
28-
"https://ruleset.skk.moe/List/ip/reject.conf",
29-
],
30-
target_path="Sukka-ad",
31-
include=SerializeFormat.Clash,
32-
),
33-
SourceModel(
34-
resources=[
35-
"https://ruleset.skk.moe/List/non_ip/reject-no-drop.conf",
36-
"https://ruleset.skk.moe/List/non_ip/reject-drop.conf",
37-
("https://ruleset.skk.moe/List/domainset/reject.conf", "DOMAIN-SET"),
38-
"https://ruleset.skk.moe/List/non_ip/reject.conf",
39-
"https://ruleset.skk.moe/List/ip/reject.conf",
40-
],
41-
target_path="Sukka-ad",
42-
include=[SerializeFormat.Egern, SerializeFormat.Sing_Box],
23+
resources=(
24+
"https://ruleset.skk.moe/List/domainset/reject_extra.conf",
25+
"DOMAIN-SET",
26+
),
27+
target_path="Sukka-ad-extra",
4328
),
4429
SourceModel(
4530
resources="https://raw.githubusercontent.com/Cats-Team/AdRules/main/adrules.list",
@@ -279,6 +264,22 @@
279264
target_path="CN",
280265
option=Option(no_resolve=False, geo_ip_country_code="CN"),
281266
),
267+
SourceModel(
268+
resources=(
269+
"https://github.com/xream/geoip/releases/latest/download/ipinfo.country.mmdb",
270+
"MaxMind DB",
271+
),
272+
target_path="CN-IPinfo",
273+
option=Option(no_resolve=False, geo_ip_country_code="CN"),
274+
),
275+
SourceModel(
276+
resources=(
277+
"https://github.com/xream/geoip/releases/latest/download/ip2location.country.mmdb",
278+
"MaxMind DB",
279+
),
280+
target_path="CN-IP2Location",
281+
option=Option(no_resolve=False, geo_ip_country_code="CN"),
282+
),
282283
SourceModel(
283284
resources="https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Surge/OneDrive/OneDrive.list",
284285
target_path="OneDrive",

0 commit comments

Comments
 (0)