Skip to content

Commit c09c33b

Browse files
committed
Slightly compact domain list
1 parent 6a039ce commit c09c33b

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

code.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ function checkDomainType(host) {
4444
ptr = ptr[segment];
4545
if (ptr === undefined)
4646
break;
47+
if (typeof ptr === "number")
48+
return ptr;
4749
if (ptr["@"] !== undefined)
4850
type = ptr["@"];
4951
}

gfwlist.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
#!/usr/bin/python3
22

3+
import os
34
import base64
45
import json
56
import urllib.parse
67
import requests
78

89

10+
GFWLIST_FILE = "gfwlist.txt"
911
GFWLIST_URL = 'https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt'
1012

1113

1214
def get_gfwlist():
13-
r = requests.get(GFWLIST_URL)
14-
r.raise_for_status()
15-
return base64.b64decode(r.text).decode("utf-8").rstrip("\n")
15+
if os.path.isfile(GFWLIST_FILE):
16+
with open(GFWLIST_FILE, "r") as f:
17+
text = f.read()
18+
else:
19+
r = requests.get(GFWLIST_URL)
20+
r.raise_for_status()
21+
text = r.text
22+
return base64.b64decode(text).decode("utf-8").rstrip("\n")
1623

1724

1825
def update_domains(domains, host, mode=0):
@@ -26,6 +33,19 @@ def update_domains(domains, host, mode=0):
2633
this["@"] = mode
2734

2835

36+
def postproc_domains(domains):
37+
# Turn all {"@": 1} into 1 to save some text
38+
keys = list(domains.keys())
39+
for key in keys:
40+
if key == "@":
41+
continue
42+
obj = domains[key]
43+
if len(obj) == 1 and "@" in obj:
44+
domains[key] = obj["@"]
45+
else:
46+
postproc_domains(obj)
47+
48+
2949
def parse_gfwlist(text):
3050
domains = {}
3151
blackpat = [] # blacklisted patterns
@@ -61,6 +81,7 @@ def parse_gfwlist(text):
6181
blackpat.append(line)
6282
else:
6383
whitepat.append(line)
84+
postproc_domains(domains)
6485
return domains, blackpat, whitepat
6586

6687

0 commit comments

Comments
 (0)