diff --git a/bin/ssh-proxy b/bin/ssh-proxy new file mode 100755 index 0000000..ca3117b --- /dev/null +++ b/bin/ssh-proxy @@ -0,0 +1,143 @@ +#!/bin/bash + +function showHelp() { + echo "ssh-proxy [args] command proxy" + echo "" + echo "args :" + echo " --help Show help" + echo "command;" + echo " start profile|port target" + echo " stop profile|port target" + #echo " stop profile|port target|tmpFile" + echo "proxy:" + echo " profile Name of the profile (~/.config/ssh-proxy/*)" + echo " port Proxy local port" + echo " target Proxy target [user@]server[:port]" + #echo " tmpFile Proxy instance pid file (/tmp/ssh-proxy/*)" +} + +if [ "${1}" == "" ]; then + showHelp + exit 1 +fi +case ${1} in + "--help") + showHelp + exit 0 + ;; + "start" | "stop");; + *) + showHelp + exit 1 + ;; +esac +if [ "${2}" == "" ]; then + showHelp + exit 1 +fi + +LOCAL_PORT=0 +PROXY="" + +if [ "${3}" == "" ]; then + #echo "Use Profile - ${2}" + ##echo "Use Profile or tmpFile - ${2}" + PROFILE_NAME=${2} + PROFILE_FILE=~/.config/ssh-proxy/${2} + if [ ! -f "${PROFILE_FILE}" ]; then + echo "Error: Profile doesn't exist" + exit 1 + fi + LOCAL_PORT=$(grep 'PORT=' "${PROFILE_FILE}" | sed 's/PORT=//') + PROXY=$(grep 'PROXY=' "${PROFILE_FILE}" | sed 's/PROXY=//') +else + # echo "Use args - ${2} ${3}" + LOCAL_PORT="${2}" + PROXY="${3}" +fi + +if [[ ! ${LOCAL_PORT} =~ ^[0-9]+$ ]] || [[ ! ${LOCAL_PORT} -lt 65536 ]]; then + echo "Port is not valid (1024-65535)" + exit 1 +fi + +PROXY_USER="" +#PROXY_PASSWORD="" +PROXY_SERVER="" +PROXY_SERVER_PORT="22" + +if [[ "${PROXY}" == *@* ]]; then + PROXY_USER="${PROXY%@*}" + PROXY_SERVER="${PROXY#*@*}" +else + PROXY_SERVER="${PROXY}" +fi + +#if [[ "${PROXY_USER}" == *:* ]]; then +# PROXY_PASSWORD="${PROXY_USER#*:*}" +# PROXY_USER="${PROXY_USER%:*}" +#fi + +if [[ "${PROXY_SERVER}" == *:* ]]; then + PROXY_SERVER_PORT="${PROXY_SERVER#*:*}" + PROXY_SERVER="${PROXY_SERVER%:*}" +fi + +# Debug Point +#echo "PROXY_USER: ${PROXY_USER}" +##echo "PROXY_PASSWORD: ${PROXY_PASSWORD}" +#echo "PROXY_SERVER: ${PROXY_SERVER}" +#echo "PORT: ${PROXY_SERVER_PORT}" + +TMP_FILE=/tmp/ssh-proxy-$(whoami)/proxy-${LOCAL_PORT}-${PROXY_USER}@${PROXY_SERVER} + +# Debug Point +#echo "${TMP_FILE}" + +if [ ! -d /tmp/ssh-proxy-"${USER}" ]; then + mkdir /tmp/ssh-proxy-"${USER}" +fi + +function startProxy() { + if [ -f "${TMP_FILE}" ]; then + echo "Error: tmpFile exist" + exit 1 + fi + echo "Start proxy ${LOCAL_PORT}" + if ! ssh -fND 127.0.0.1:"${LOCAL_PORT}" "${PROXY_USER}"@"${PROXY_SERVER}" -p "${PROXY_SERVER_PORT}"; then + echo "Error: Proxy start error" + exit 1 + fi + PID=$(lsof -i -P | grep ":${LOCAL_PORT} (LISTEN)" | awk -F ' ' '{print $2}') + if [ "${PID}" == "" ]; then + echo "Error: No PID found" + exit 1 + fi + { + if [ ! "${PROFILE_NAME}" == "" ]; then + echo "PROFILE_NAME=${PROFILE_NAME}" + fi + echo "LOCAL_PORT=${LOCAL_PORT}" + #echo "PROXY_USER=${PROXY_USER}" + echo "PROXY_SERVER=${PROXY_SERVER}" + #echo "PROXY_SERVER_PORT=${PROXY_SERVER_PORT}" + echo "PID=${PID}" + } > "${TMP_FILE}" +} + +function stopProxy() { + if [ ! -f "${TMP_FILE}" ]; then + echo "Error: tmpFile doesn't exist" + exit 1 + fi + echo "Stop proxy ${LOCAL_PORT}" + PID=$(grep 'PID=' "${TMP_FILE}" | sed 's/PID=//') + kill "${PID}" + rm "${TMP_FILE}" +} + +case ${1} in + "start") startProxy;; + "stop") stopProxy;; + *) exit 1;; +esac diff --git a/profile.template b/profile.template new file mode 100644 index 0000000..fedb1b2 --- /dev/null +++ b/profile.template @@ -0,0 +1,2 @@ +PORT=5001 +PROXY=[user@]server[:port] \ No newline at end of file