refactor
This commit is contained in:
parent
58e086416b
commit
2d30be9cf9
111
bin/ssh-proxy
111
bin/ssh-proxy
@ -7,15 +7,11 @@ function showHelp() {
|
||||
echo " --help Show help"
|
||||
echo "command;"
|
||||
echo " start profile|port target"
|
||||
echo " stop profile|port target"
|
||||
#echo " stop profile|port target|tmpFile"
|
||||
#echo " stop port"
|
||||
echo " stop port"
|
||||
echo "proxy:"
|
||||
echo " profile Name of the profile (~/.config/ssh-proxy/*)"
|
||||
echo " port Proxy local port"
|
||||
echo " port Proxy local port (1024-65535)"
|
||||
echo " target Proxy target [user@]server[:port]"
|
||||
#echo " tmpFile Proxy instance pid file (/tmp/ssh-proxy/*)"
|
||||
#echo " port 1024-65535"
|
||||
}
|
||||
|
||||
if [ "${1}" == "" ]; then
|
||||
@ -38,68 +34,77 @@ if [ "${2}" == "" ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
LOCAL_PORT=0
|
||||
PROXY=""
|
||||
function startProxy() {
|
||||
LOCAL_PORT=0
|
||||
PROXY=""
|
||||
|
||||
if [ "${3}" == "" ]; then
|
||||
#echo "Use Profile - ${2}"
|
||||
#PROFILE_NAME=${2}
|
||||
PROFILE_FILE=~/.config/ssh-proxy/${2}
|
||||
if [ ! -f "${PROFILE_FILE}" ]; then
|
||||
echo "Error: Profile doesn't exist"
|
||||
if [ "${2}" == "" ]; then
|
||||
#echo "Use Profile - ${1}"
|
||||
#PROFILE_NAME=${1}
|
||||
PROFILE_FILE=~/.config/ssh-proxy/${1}
|
||||
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 - ${1} ${2}"
|
||||
LOCAL_PORT="${1}"
|
||||
PROXY="${2}"
|
||||
fi
|
||||
|
||||
if [[ ! ${LOCAL_PORT} =~ ^[0-9]+$ ]] || [[ ! ${LOCAL_PORT} -lt 65536 ]]; then
|
||||
echo "Port is not valid (1024-65535)"
|
||||
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"
|
||||
PROXY_ADDRESS=""
|
||||
|
||||
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}" == *@* ]]; 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_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
|
||||
|
||||
if [[ "${PROXY_SERVER}" == *:* ]]; then
|
||||
PROXY_SERVER_PORT="${PROXY_SERVER#*:*}"
|
||||
PROXY_SERVER="${PROXY_SERVER%:*}"
|
||||
fi
|
||||
TMP_FILE=/tmp/ssh-proxy-${USER}/${LOCAL_PORT}
|
||||
|
||||
TMP_FILE=/tmp/ssh-proxy-${USER}/${LOCAL_PORT}
|
||||
if [ ! -d /tmp/ssh-proxy-"${USER}" ]; then
|
||||
mkdir /tmp/ssh-proxy-"${USER}"
|
||||
fi
|
||||
|
||||
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
|
||||
|
||||
PROXY_ADDRESS="${PROXY_SERVER}"
|
||||
if [ ! "${PROXY_USER}" == "" ]; then
|
||||
echo ""
|
||||
PROXY_ADDRESS="${PROXY_USER}@${PROXY_SERVER}"
|
||||
fi
|
||||
|
||||
echo "Start proxy ${LOCAL_PORT}"
|
||||
if ! ssh -fND 127.0.0.1:"${LOCAL_PORT}" "${PROXY_USER}"@"${PROXY_SERVER}" -p "${PROXY_SERVER_PORT}"; then
|
||||
if ! ssh -fND 127.0.0.1:"${LOCAL_PORT}" "${PROXY_ADDRESS}" -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"
|
||||
@ -118,6 +123,8 @@ function startProxy() {
|
||||
}
|
||||
|
||||
function stopProxy() {
|
||||
LOCAL_PORT=${1}
|
||||
TMP_FILE=/tmp/ssh-proxy-${USER}/${LOCAL_PORT}
|
||||
if [ ! -f "${TMP_FILE}" ]; then
|
||||
echo "Error: tmpFile doesn't exist"
|
||||
exit 1
|
||||
@ -129,7 +136,7 @@ function stopProxy() {
|
||||
}
|
||||
|
||||
case ${1} in
|
||||
"start") startProxy;;
|
||||
"stop") stopProxy;;
|
||||
"start") startProxy "${2}" "${3}";;
|
||||
"stop") stopProxy "${2}";;
|
||||
*) exit 1;;
|
||||
esac
|
||||
|
@ -8,7 +8,7 @@ _ssh-proxy() {
|
||||
local -r cmdargs="start stop"
|
||||
local -r cnfargs="--help"
|
||||
local profiledir=~/.config/ssh-proxy
|
||||
#local tempdir="/tmp/ssh-proxy-${USER}"
|
||||
local tempdir="/tmp/ssh-proxy-${USER}"
|
||||
|
||||
if [[ ${COMP_WORDS[*]} == *"--help"* ]]; then
|
||||
return
|
||||
@ -30,12 +30,18 @@ _ssh-proxy() {
|
||||
--help)
|
||||
return
|
||||
;;
|
||||
start | stop)
|
||||
start)
|
||||
local -r PROFILELIST=$(\ls ${profiledir})
|
||||
# shellcheck disable=SC2207
|
||||
COMPREPLY=($(compgen -W "${PROFILELIST}" -- "${COMP_WORDS[COMP_CWORD]}"))
|
||||
return
|
||||
;;
|
||||
stop)
|
||||
local -r PROFILELIST=$(\ls ${tempdir})
|
||||
# shellcheck disable=SC2207
|
||||
COMPREPLY=($(compgen -W "${PROFILELIST}" -- "${COMP_WORDS[COMP_CWORD]}"))
|
||||
return
|
||||
;;
|
||||
*)
|
||||
# shellcheck disable=SC2207
|
||||
COMPREPLY=($(compgen -W "${args}" -- "${COMP_WORDS[COMP_CWORD]}"))
|
||||
|
Reference in New Issue
Block a user