#!/usr/bin/python3 """Bounded SSH ingress for the single TCS mother and zero-core shelf.""" import json,os,re,sys,urllib.request,urllib.error def endpoint(command): fixed={'zy-rpc health':('GET','/health'),'zy-rpc mother-status':('GET','/v1/mother/status'),'zy-rpc shelf-shared':('GET','/v1/shelf/shared'),'zy-rpc shelf-fifth':('GET','/v1/shelf/fifth'),'zy-rpc shelf-public':('GET','/v1/shelf/public'),'zy-rpc mother-ingest':('POST','/v1/mother/ingest'),'zy-rpc door-challenge':('POST','/v1/door/challenge'),'zy-rpc door-attest':('POST','/v1/door/attest'),'zy-rpc door-model-test':('POST','/v1/door/model-test'),'zy-rpc lamp-ignite':('POST','/v1/lamp/ignite'),'zy-rpc lamp-ask':('POST','/v1/lamp/ask'),'zy-rpc world-status':('GET','/v1/world/status')} if command in fixed:return fixed[command] if re.fullmatch(r'zy-rpc mother-job [a-f0-9]{64}',command):return 'GET','/v1/mother/jobs/'+command.split()[-1] if re.fullmatch(r'zy-rpc world-resolve [A-Za-z0-9_.:∞-]{1,128}',command):return 'GET','/v1/world/resolve?id='+command.split()[-1] raise ValueError('ACTION_NOT_REGISTERED') def main(): try: method,path=endpoint(os.environ.get('SSH_ORIGINAL_COMMAND',''));data=None if method=='POST': data=sys.stdin.buffer.read(160001) if len(data)>160000 or (data and not isinstance(json.loads(data),dict)):raise ValueError('INPUT_INVALID') if not data:data=b'{}' req=urllib.request.Request('http://127.0.0.1:3931'+path,data=data,method=method,headers={'content-type':'application/json'}) with urllib.request.urlopen(req,timeout=110) as response:sys.stdout.buffer.write(response.read(512000)) return 0 except Exception:print(json.dumps({'error':'EXTERNAL_WRITE_OR_INVALID_SOURCE_REJECTED'}));return 77 if __name__=='__main__':raise SystemExit(main())