fn_b/tools/subscription-checker/install_cores.py

21 lines
1.1 KiB
Python

#!/usr/bin/env python3
import gzip,hashlib,json,os,tarfile,tempfile,urllib.request,io
from pathlib import Path
os.umask(0o077)
root=Path('/opt/cfn-checker-cores');root.mkdir(exist_ok=True,mode=0o700)
manifest=json.loads(Path(__file__).with_name('cores.json').read_text())
for name,info in manifest.items():
req=urllib.request.Request(info['url'],headers={'User-Agent':'cfn-subscription-checker'})
with urllib.request.urlopen(req,timeout=90) as r: data=r.read(100*1024*1024)
if hashlib.sha256(data).hexdigest()!=info['sha256']:raise RuntimeError('checksum mismatch: '+name)
if name in ['Country.mmdb','geoip.metadb']:binary=data
elif name=='mihomo':binary=gzip.decompress(data)
else:
with tarfile.open(fileobj=io.BytesIO(data),mode='r:gz') as t:
members=[m for m in t.getmembers() if m.isfile() and Path(m.name).name=='sing-box']
if len(members)!=1:raise RuntimeError('unexpected archive')
binary=t.extractfile(members[0]).read()
f=root/(name+'.new');f.write_bytes(binary);f.chmod(0o700);f.replace(root/name)
print(name,info['version'],'SHA256 verified')
(root/'versions.json').write_text(json.dumps(manifest,indent=2))