from pathlib import Path
import subprocess,os
import sys
p=Path(sys.argv[1]).resolve()
p.mkdir(parents=True,exist_ok=True)
for name in ['host','client']:
 subprocess.run(['ssh-keygen','-q','-t','ed25519','-N','','-f',str(p/name)],check=True)
(p/'remote-home').mkdir(exist_ok=True)
(p/'sshd_config').write_text(f'''Port 19453
ListenAddress 127.0.0.1
HostKey {p}/host
PidFile {p}/sshd.pid
AuthorizedKeysFile {p}/client.pub
StrictModes no
PasswordAuthentication no
KbdInteractiveAuthentication no
UsePAM no
AllowUsers user
PermitUserEnvironment yes
SetEnv HOME={p}/remote-home
''')
config=Path.home()/'.ssh/config'
p.joinpath('original-ssh-config').write_bytes(config.read_bytes() if config.exists() else b'')
p.joinpath('config-existed').write_text(str(config.exists()))
config.parent.mkdir(exist_ok=True)
with config.open('a') as f:
 f.write(f'''\nHost proof-forward-conflict\n  HostName 127.0.0.1\n  Port 19453\n  User user\n  IdentityFile {p}/client\n  IdentitiesOnly yes\n  StrictHostKeyChecking no\n  UserKnownHostsFile /dev/null\n  LocalForward 127.0.0.1:18080 127.0.0.1:18080\n''')
