#!/usr/bin/env python3 """Only temporary FUC-XB QA units; never edit existing node services/configuration.""" import argparse, hashlib, json, os, pathlib, socket, subprocess, time ROOT=pathlib.Path('/var/www/wwwroot/fuc-xb-acceptance') PRIVATE=ROOT/'private' EXPECTED={'/usr/local/XrayR/XrayR':'31c3beef7afb1a167dc631d16cec881ea6d3049cb0718dd8157e51da14b5296d','/etc/XrayR/config.yml':'a123e676fbc3a52fe3912db953499e2501a67b21aeb6445c3b458ec14570c2f2','/usr/local/XrayR/config.yml':'57c0dd387ec7fd36cf730c669babdbdb65312882e2fa9db6791d55f174fb41c2'} def run(a): p=subprocess.run(a,capture_output=True,text=True,timeout=35) if p.returncode:raise RuntimeError('QA host command failed') return p.stdout.strip() def original_ok(): return all(hashlib.sha256(pathlib.Path(p).read_bytes()).hexdigest()==h for p,h in EXPECTED.items()) and run(['systemctl','show','XrayR','-p','MainPID','--value'])=='107692' def free(port): s=socket.socket() try:s.bind(('127.0.0.1',port));return True except OSError:return False finally:s.close() def main(): os.umask(0o077);p=argparse.ArgumentParser();p.add_argument('action',choices=['start','status','stop']);args=p.parse_args();PRIVATE.mkdir(parents=True,exist_ok=True,mode=0o700) assert original_ok(),'Original XrayR baseline changed' if args.action=='start': assert free(25443) and free(28766),'Port collision' c=json.loads((PRIVATE/'node-runtime.json').read_text()) assert c['expected_exit']=='193.111.31.52' and c['port']==25443 and c['sni']=='jp02.gembox-dns.com' cfg={'Log':{'Level':'error','ErrorPath':str(PRIVATE/'node-error.log')},'Nodes':[{'PanelType':'NewV2board','ApiConfig':{'ApiHost':'http://127.0.0.1:28766','ApiKey':c['server_token'],'NodeID':c['node_id'],'NodeType':'V2ray','Timeout':15,'EnableVless':False,'EnableXTLS':False,'SpeedLimit':10,'DeviceLimit':1},'ControllerConfig':{'ListenIP':'0.0.0.0','SendIP':'0.0.0.0','UpdatePeriodic':10,'EnableProxyProtocol':False,'CertConfig':{'CertMode':'file','CertDomain':'jp02.gembox-dns.com','CertFile':'/etc/letsencrypt/live/jp02.gembox-dns.com/fullchain.pem','KeyFile':'/etc/letsencrypt/live/jp02.gembox-dns.com/privkey.pem'}}}]} (PRIVATE/'xrayr.json').write_text(json.dumps(cfg)) run(['systemd-run','--quiet','--collect','--unit=fuc-xb-qa-api','--property=RuntimeMaxSec=1800','--property=MemoryMax=96M','--property=CPUQuota=20%','/usr/bin/python3',str(pathlib.Path(__file__).with_name('node-bridge.py')),'--config',str(PRIVATE/'node-runtime.json')]) for _ in range(25): if not free(28766):break time.sleep(0.2) assert not free(28766),'QA API bridge did not start' run(['systemd-run','--quiet','--collect','--unit=fuc-xb-qa-node','--property=RuntimeMaxSec=1800','--property=MemoryMax=256M','--property=CPUQuota=25%','--working-directory='+str(PRIVATE),'/usr/local/XrayR/XrayR','-c',str(PRIVATE/'xrayr.json')]) print(json.dumps({'started_temporary_qa_units':True,'runtime_limit_seconds':1800,'original_unchanged':original_ok()})) elif args.action=='status': metrics=json.loads((PRIVATE/'bridge-metrics.json').read_text()) if (PRIVATE/'bridge-metrics.json').exists() else {} print(json.dumps({'node_active':run(['systemctl','show','fuc-xb-qa-node','-p','ActiveState','--value']),'api_active':run(['systemctl','show','fuc-xb-qa-api','-p','ActiveState','--value']),'port_listening':not free(25443),'metrics':metrics,'original_unchanged':original_ok()})) else: for u in ['fuc-xb-qa-node','fuc-xb-qa-api']: subprocess.run(['systemctl','stop',u],capture_output=True,timeout=30) assert free(25443) and free(28766),'Temporary port remains occupied' for name in ['node-runtime.json','xrayr.json']:(PRIVATE/name).unlink(missing_ok=True) print(json.dumps({'temporary_units_stopped':True,'temporary_ports_closed':True,'copied_gate_cookie_and_api_key_removed':True,'original_unchanged':original_ok()})) if __name__=='__main__':main()