summaryrefslogtreecommitdiffstats
path: root/lass/5pkgs/sshify/default.nix
blob: aba0ab6bba841782a40d093ac3ad18e6372f8719 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{ pkgs }:
pkgs.writers.writeBashBin "sshify" ''
  set -efu

  TMPDIR=$(mktemp -d)

  SSH_ARGS=()

  while [[ "$#" -gt 0 ]]; do
      case $1 in
          --)
            shift
            break
            ;;
          *)
            SSH_ARGS+=($1)
            ;;
      esac
      shift
  done

  if [[ "$#" -le 0 ]]; then
    echo no command specified
    exit 1
  fi

  RANDOM_HIGH_PORT=$(shuf -i 20000-65000 -n 1)

  cat << EOF >$TMPDIR/proxychains.conf
  [ProxyList]
  socks4  127.0.0.1 $RANDOM_HIGH_PORT
  EOF

  ssh -fNM -S "$TMPDIR/socket" -D "$RANDOM_HIGH_PORT" "''${SSH_ARGS[@]}"
  trap "ssh -S $TMPDIR/socket -O exit bla 2>/dev/null; rm -rf $TMPDIR >&2" EXIT

  ${pkgs.proxychains-ng}/bin/proxychains4 -q -f "$TMPDIR/proxychains.conf" "$@"
''