load custom proxy hosts

This commit is contained in:
bgme 2023-06-19 17:06:11 +08:00
parent 183e073927
commit cf3fc7c347
3 changed files with 38 additions and 7 deletions

1
.gitignore vendored
View file

@ -171,3 +171,4 @@ poetry.toml
# End of https://www.toptal.com/developers/gitignore/api/python
/.idea
/custom_proxy_hosts.json

36
main.py
View file

@ -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()

8
update.sh Normal file
View file

@ -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"