#!/bin/sh
#
# ssh-https-tunnel - tunnel ssh over https connect
# dependencies: socat

set -eu

progname=$(basename "$0")

if [ "$#" -ne 4 ]; then
  cat <<EOF >&2
Tunnel SSH over HTTPS CONNECT

Usage: ${progname} <proxy-host> <proxy-port> <target-host> <target-port>

Example client SSH config:

  Host example-https
    ProxyCommand ${progname} example.com 443 example.com 22
    HostKeyAlias example.com
    ControlMaster no
    ServerAliveInterval 30

Example server Apache config:

  <IfModule mod_proxy.c>
    ProxyRequests On
    AllowCONNECT 22
    ProxyVia on

    <Proxy *>
      Order deny,allow
      Deny from all
    </Proxy>
    <ProxyMatch example.com>
      Order deny,allow
      Allow from all
    </ProxyMatch>
  </IfModule>
EOF
  exit 1
fi

PROXY_HOST="$1"
PROXY_PORT="$2"
TARGET_HOST="$3"
TARGET_PORT="$4"

tmplogfile="$(mktemp "${TMPDIR:-/tmp}/${progname}.XXXXXX")"
trap '{ rm -f "$tmplogfile"; }' EXIT

socat -lf "${tmplogfile}" -d -d tcp4-listen:0,bind=127.0.0.1,accept-timeout=5 "ssl:${PROXY_HOST}:${PROXY_PORT}" &
SOCAT_SSL_PID=$!

while kill -0 "${SOCAT_SSL_PID}" >/dev/null 2>&1 && ! grep -q . "${tmplogfile}"; do
  sleep 0.1
done

if ! SOCAT_SSL_PORT="$(grep -E -m 1 -o '127.0.0.1:[0-9]+$' "${tmplogfile}" | cut -d : -f 2)"; then
  echo Could not detect socat listening port. >&2
  cat "${tmplogfile}" >&2
  exit 1
fi

rm -- "${tmplogfile}"

socat "stdio" "proxy:127.0.0.1:${TARGET_HOST}:${TARGET_PORT},proxyport=${SOCAT_SSL_PORT}"
