# ------------------------------------------------------------
# PSUT IoT Engineering Studio | CR82
# Experiment: ssh tunnel endpoints and trust boundaries
# Purpose: expose algorithm states and verify the experiment invariant.
# ------------------------------------------------------------
# CR82 | SSH tunnel endpoints and trust boundaries
# Execute the named operation; expose states and assert the laboratory invariant.
import json



DEFAULT={'direction': 'local', 'destination_port': 8000}
ALTERNATE={'direction': 'remote'}

def run(overrides=None):
    # Merge only explicitly supplied inputs with the lecture baseline.
    params={**DEFAULT, **(overrides or {})}
    direction=params['direction']
    destination_port=params['destination_port']
    if direction not in ('local','remote') or type(destination_port) is not int or not 1 <= destination_port <= 65535:raise ValueError('invalid forwarding direction')
    listener='client loopback' if direction=='local' else 'server loopback';target='server loopback' if direction=='local' else 'client loopback';steps=[{'from':'client application' if direction=='local' else 'server application','to':listener,'protected_by_SSH':False},{'from':listener,'to':target,'protected_by_SSH':True},{'from':target,'to':f'authorized localhost:{destination_port}','protected_by_SSH':False}];result={'listener':listener,'destination':target,'public_listener':False}
    # Independently check the stated mathematical or protocol invariant.
    assert not result["public_listener"], "verification failed"
    return {"steps":steps,"result":result}

if __name__ == "__main__":
    import argparse
    parser=argparse.ArgumentParser();parser.add_argument("--inputs",default="{}");parser.add_argument("--test",action="store_true");args=parser.parse_args()
    if args.test:
        for case in [{},ALTERNATE]:run(case)
        print("PASS")
    else:print(json.dumps(run(json.loads(args.inputs)),indent=2))
