diff --git a/.gitignore b/.gitignore index 902a101..d2d4218 100644 --- a/.gitignore +++ b/.gitignore @@ -171,3 +171,4 @@ poetry.toml # End of https://www.toptal.com/developers/gitignore/api/python /.idea +/custom_proxy_hosts.json \ No newline at end of file diff --git a/main.py b/main.py index 28e9c74..3cf7000 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,7 @@ import base64 +import json import logging +import os.path import re import subprocess import sys @@ -19,6 +21,8 @@ GFWLIST_URL_LIST = [ "http://repo.or.cz/gfwlist.git/blob_plain/HEAD:/gfwlist.txt" ] +PWD = os.path.dirname(os.path.realpath(__file__)) + def get_gfwlist_text() -> str: for url in GFWLIST_URL_LIST: @@ -175,17 +179,36 @@ def get_gfwlist_hosts() -> list[str]: ) ) ) - gfwlist_hosts = hosts_deduplicate(gfwlist_hosts) - logging.info('found {num} gfwlist host'.format(num=len(gfwlist_hosts))) - return sorted(gfwlist_hosts) + return gfwlist_hosts + + +def get_custom_proxy_hosts() -> list[str]: + cph_path = os.path.join(PWD, 'custom_proxy_hosts.json') + if not os.path.exists(cph_path): + return [] + else: + with open(cph_path, 'r') as f: + return json.load(f) + + +def get_proxy_hosts() -> list[str]: + proxy_hosts = [ + *get_gfwlist_hosts(), + *get_custom_proxy_hosts() + ] + + proxy_hosts = hosts_deduplicate(proxy_hosts) + logging.info('found {num} proxy host'.format(num=len(proxy_hosts))) + return sorted(proxy_hosts) def get_dnsmasq_text() -> str: rule_list = list( map( - lambda host: "server=/{host}/{dns_ip}#{dns_port}".format(host=host, dns_ip=PROXY_DNS_IP, - dns_port=PROXY_DNS_PORT), - get_gfwlist_hosts() + lambda host: "server=/{host}/{dns_ip}#{dns_port}".format( + host=host, dns_ip=PROXY_DNS_IP, dns_port=PROXY_DNS_PORT + ), + get_proxy_hosts() ) ) return '\n'.join(rule_list) @@ -200,5 +223,4 @@ def main(): if __name__ == '__main__': logging.basicConfig(stream=sys.stderr, level=logging.INFO, format="%(levelname)s:%(message)s") - main() diff --git a/update.sh b/update.sh new file mode 100644 index 0000000..5855257 --- /dev/null +++ b/update.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +cd /srv/app/gfw/ + +yq "" pac/config/custom.yaml > gfwlist-to-dnsmasq-rule/custom_proxy_hosts.json + +rsync --verbose gfwlist-to-dnsmasq-rule/{main.py,custom_proxy_hosts.json} root@192.168.10.1:/root/gfwlist-to-dnsmasq-rule +ssh root@192.168.10.1 "/usr/bin/python /root/gfwlist-to-dnsmasq-rule/main.py" \ No newline at end of file