#!/bin/bash
set -eu
ROOT=$(cd "$(dirname "$0")" && pwd)
ssh-keygen -q -t ed25519 -N '' -f "$ROOT/client-key"
ssh-keygen -q -t ed25519 -N '' -f "$ROOT/host-key"
sudo useradd -d "$ROOT/remote" -s /bin/sh t3proof24570
sudo passwd -d t3proof24570 >/dev/null
cp "$ROOT/client-key.pub" "$ROOT/authorized_keys"
chmod 644 "$ROOT/authorized_keys"
sudo chown t3proof24570:t3proof24570 "$ROOT/remote"
cat > "$ROOT/sshd_config" <<EOF
Port 22422
ListenAddress 127.0.0.1
HostKey $ROOT/host-key
PidFile $ROOT/sshd.pid
AuthorizedKeysFile $ROOT/authorized_keys
StrictModes no
PasswordAuthentication no
KbdInteractiveAuthentication no
UsePAM no
AllowUsers t3proof24570
PrintMotd no
LogLevel VERBOSE
EOF
cat > "$ROOT/ssh_config" <<EOF
Host t3-control t3-command t3-tty t3-both
  HostName 127.0.0.1
  Port 22422
  User t3proof24570
  IdentityFile $ROOT/client-key
  IdentitiesOnly yes
  UserKnownHostsFile $ROOT/known_hosts
  StrictHostKeyChecking accept-new
Host t3-command t3-both
  RemoteCommand tmux new -A -s main
Host t3-tty t3-both
  RequestTTY force
EOF
cat > "$ROOT/bin/ssh" <<EOF
#!/bin/sh
exec /usr/bin/ssh -F "$ROOT/ssh_config" "\$@"
EOF
chmod +x "$ROOT/bin/ssh"
