add Maxmind-GeoLite2 & naming

This commit is contained in:
wits-fe 2023-04-28 23:58:48 +08:00
parent b42f8e2ca5
commit 713f5e397b
2 changed files with 51 additions and 17 deletions

View File

@ -8,14 +8,16 @@ from requests.exceptions import RequestException, HTTPError
import gfwlist import gfwlist
SOURCES_4 = {
SOURCES = {
'ipdeny.com': 'http://www.ipdeny.com/ipblocks/data/aggregated/cn-aggregated.zone', 'ipdeny.com': 'http://www.ipdeny.com/ipblocks/data/aggregated/cn-aggregated.zone',
'17mon': 'https://raw.githubusercontent.com/17mon/china_ip_list/master/china_ip_list.txt', '17mon': 'https://raw.githubusercontent.com/17mon/china_ip_list/master/china_ip_list.txt',
} }
SOURCES2 = { SOURCES_6 = {
'gaoyifan': 'https://gaoyifan.github.io/china-operator-ip/china6.txt', 'gaoyifan': 'https://gaoyifan.github.io/china-operator-ip/china6.txt',
} }
SOURCES_46 = {
'maxmind': 'https://github.com/v2fly/geoip/raw/release/text/cn.txt',
}
OUT_DIR = "dist" OUT_DIR = "dist"
# Stub content to disable GFWList check # Stub content to disable GFWList check
@ -28,6 +30,8 @@ def fetch_and_convert(src):
template = "var CHINA = [\n{}\n];\n" template = "var CHINA = [\n{}\n];\n"
lines = [] lines = []
for iprange in response.text.strip().split("\n"): for iprange in response.text.strip().split("\n"):
if iprange.find(":") != -1:
break
ipnet = ipaddress.IPv4Network(iprange) ipnet = ipaddress.IPv4Network(iprange)
netaddr = int(ipnet.network_address) netaddr = int(ipnet.network_address)
netmask = int(ipnet.netmask) netmask = int(ipnet.netmask)
@ -57,6 +61,8 @@ def fetch_and_convert_ip6(src):
fixlen = len(f" [0xFFFFFFFF, -1, 0xFFFFFFFF],") fixlen = len(f" [0xFFFFFFFF, -1, 0xFFFFFFFF],")
for iprange in text.strip().split("\n"): for iprange in text.strip().split("\n"):
if iprange.find(":") == -1:
continue
ipnet = ipaddress.IPv6Network(iprange) ipnet = ipaddress.IPv6Network(iprange)
prefixlen = ipnet.prefixlen prefixlen = ipnet.prefixlen
fulladdr = str(ipnet.exploded).replace(':', '') fulladdr = str(ipnet.exploded).replace(':', '')
@ -131,34 +137,62 @@ def main():
gfwlist_stub = GFWLIST_STUB gfwlist_stub = GFWLIST_STUB
os.makedirs(OUT_DIR, mode=0o755, exist_ok=True) os.makedirs(OUT_DIR, mode=0o755, exist_ok=True)
for key in SOURCES: for key in SOURCES_4:
print(f"Generating PAC script from source {key}") key_6 = list(SOURCES_6)[0]
print(f"Generating PAC script from source {key}(IPv4) & {key_6}(IPv6)")
try: try:
data = fetch_and_convert(SOURCES[key]) data = fetch_and_convert(SOURCES_4[key])
key2 = list(SOURCES2)[0] data_6 = fetch_and_convert_ip6(SOURCES_6[key_6])
data2 = fetch_and_convert_ip6(SOURCES2[key2])
except RequestException: except RequestException:
continue continue
except HTTPError: except HTTPError:
continue continue
filename = f"pac-{key}-{key2}.txt" filename = f"pac-IPv4_{key}--IPv6_{key_6}.txt"
filename_gfwlist = f"pac-gfwlist-{key}-{key2}.txt" filename_gfwlist = f"pac-gfwlist-IPv4_{key}--IPv6_{key_6}.txt"
with open(os.path.join(OUT_DIR, filename), "w") as f: with open(os.path.join(OUT_DIR, filename), "w") as f:
f.write(code) f.write(code)
f.write(data) f.write(data)
f.write("\n") f.write("\n")
f.write(data2) f.write(data_6)
f.write("\n") f.write("\n")
f.write(gfwlist_stub) f.write(gfwlist_stub)
with open(os.path.join(OUT_DIR, filename_gfwlist), "w") as f: with open(os.path.join(OUT_DIR, filename_gfwlist), "w") as f:
f.write(code) f.write(code)
f.write(data) f.write(data)
f.write("\n") f.write("\n")
f.write(data2) f.write(data_6)
f.write("\n")
f.write(gfwlist_part)
for key in SOURCES_46:
print(f"Generating PAC script from source {key}(IPv4v6)")
try:
data = fetch_and_convert(SOURCES_46[key])
data_6 = fetch_and_convert_ip6(SOURCES_46[key])
except RequestException:
continue
except HTTPError:
continue
filename = f"pac-IPv4v6_{key}.txt"
filename_gfwlist = f"pac-gfwlist-IPv4v6_{key}.txt"
with open(os.path.join(OUT_DIR, filename), "w") as f:
f.write(code)
f.write(data)
f.write("\n")
f.write(data_6)
f.write("\n")
f.write(gfwlist_stub)
with open(os.path.join(OUT_DIR, filename_gfwlist), "w") as f:
f.write(code)
f.write(data)
f.write("\n")
f.write(data_6)
f.write("\n") f.write("\n")
f.write(gfwlist_part) f.write(gfwlist_part)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

10
code.js
View File

@ -19,14 +19,14 @@ function binarySearch(list, num, lower, upper) {
return x; return x;
} }
function convertToInt6(high, low) { function convertToUInt6(high, low) {
var num1 = parseInt(high, 16) & 0xFFFF; var num1 = parseInt(high, 16) & 0xFFFF;
var num2 = parseInt(low, 16) & 0xFFFF; var num2 = parseInt(low, 16) & 0xFFFF;
return (((num1 << 16) | num2) >>> 0); return (((num1 << 16) | num2) >>> 0);
} }
function isInNet6(parts, list, list2) { function isInNet6(parts, list, list2) {
var num = convertToInt6(parts[0], parts[1]); var num = convertToUInt6(parts[0], parts[1]);
var x = binarySearch(list, num, 0, list.length); var x = binarySearch(list, num, 0, list.length);
if (list[x][1] == -1) if (list[x][1] == -1)
@ -36,7 +36,7 @@ function isInNet6(parts, list, list2) {
if (num !== list[x][0]) if (num !== list[x][0])
return false; return false;
var num2 = convertToInt6(parts[2], parts[3]); var num2 = convertToUInt6(parts[2], parts[3]);
var x2 = binarySearch(list2, num2, list[x][1], list[x][2] + 1); var x2 = binarySearch(list2, num2, list[x][1], list[x][2] + 1);
return (((num2 & list2[x2][1]) ^ list2[x2][0]) === 0); return (((num2 & list2[x2][1]) ^ list2[x2][0]) === 0);
@ -91,14 +91,14 @@ function isLanOrChina6(host) {
return isInNet6(parts, CHINA6_F, CHINA6_S) || isInNet6(parts, LAN6_F, LAN6_S); return isInNet6(parts, CHINA6_F, CHINA6_S) || isInNet6(parts, LAN6_F, LAN6_S);
} }
function convertToInt(host) { function convertToUInt(host) {
var bytes = host.split("."); var bytes = host.split(".");
var result = ((bytes[0] & 0xFF) << 24) | ((bytes[1] & 0xFF) << 16) | ((bytes[2] & 0xFF) << 8) | (bytes[3] & 0xFF); var result = ((bytes[0] & 0xFF) << 24) | ((bytes[1] & 0xFF) << 16) | ((bytes[2] & 0xFF) << 8) | (bytes[3] & 0xFF);
return (result >>> 0); return (result >>> 0);
} }
function belongsToSubnet(host, list) { function belongsToSubnet(host, list) {
var ip = convertToInt(host); var ip = convertToUInt(host);
if (list.length === 0 || ip < list[0][0]) if (list.length === 0 || ip < list[0][0])
return false; return false;