This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsoxy
executable file
·156 lines (99 loc) · 2.91 KB
/
soxy
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env bash
set -o errexit -o pipefail
## SCRIPT VARS, DON'T MODIFY
########################################
SCRIPT_PATH="$(dirname $(readlink -f ${0}))"
SOXY_DIR="${HOME}/.soxy"
SOXY_CONF="${SOXY_DIR}/config"
SOXY_LOG="${SOXY_DIR}/soxy.log"
## LOAD TEXT FORMATTER
########################################
function format() {
echo -e "${1}"
}
if [[ -t 1 ]]; then
if [[ -f "${SCRIPT_PATH}/format.sh" ]]; then
source ${SCRIPT_PATH}/format.sh
else
echo "NOTICE: ${SCRIPT_PATH}/format.sh not found... omitting color output"
fi
fi
## CREAT SOXY DIR IF MISSING
########################################
[[ -d "${SOXY_DIR}" ]] || mkdir -p ${SOXY_DIR}
## LOAD/COPY SOXY CONFIG
########################################
if [[ ! -r "${SOXY_CONF}" ]]; then
cp "${SCRIPT_PATH}/config.sample" ${SOXY_CONF}
echo "WARNING: Soxy config not found, default config file created at ${SOXY_CONF}"
exit 1
fi
source ${SOXY_CONF}
## FUNCTIONS
########################################
function getPid {
# Get the PID
local PID="$(ps ax | grep ssh | grep ${LOCAL_PORT} | awk '{ print $1 }')"
# Return the PID
echo ${PID}
}
function startSocks {
# Echo status message to console
printf "Starting Soxy proxy on port %-6s " ${LOCAL_PORT}
if [[ -z "$(getPid)" ]]; then
# Establish SOCKS connection
ssh -qfCD ${LOCAL_PORT} ${REMOTE_USER}@${REMOTE_HOST} -p ${REMOTE_PORT} -N & > /dev/null 2>&1
# Echo status message to console
echo "[ $(format OK ${GREEN} ${BOLD}) ]"
else
echo "[$(format FAIL ${RED} ${BOLD} ${BLACK})]"
echo "Soxy already running, must be stopped first"
fi
}
function stopSocks {
# Echo status message to console
printf "Stopping Soxy proxy on port %-6s " ${LOCAL_PORT}
# Get the PID
local PID="$(getPid)"
if [[ ! -z "${PID}" ]]; then
# Kill the process
kill ${PID} > /dev/null 2>&1
# Echo status message to console
echo "[ $(format OK ${GREEN} ${BOLD}) ]"
else
echo "[$(format FAIL ${RED} ${BOLD} ${BLACK})]"
echo "No running Soxy process found"
fi
}
function getStatus {
# Get the PID
local PID="$(getPid)"
if [[ ! -z "${PID}" ]]; then
echo "Soxy proxy running on port ${LOCAL_PORT} (PID: ${PID})"
else
echo "No Soxy proxy currently running"
if [[ "${AUTO_RECONNECT}" == true ]] || [[ "${AUTO_RECONNECT}" -gt 0 ]]; then
echo "NOTICE: Auto-reconnect enabled, attempting to restart Soxy"
stopSocks && startSocks
fi
fi
}
## SWITCHES
########################################
case ${1} in
'start')
startSocks
;;
'stop')
stopSocks
;;
'restart')
stopSocks && startSocks
;;
'status')
getStatus
;;
*)
echo "Usage: $(basename ${0}) { start | stop | restart | status }"
;;
esac