-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-entrypoint.sh
207 lines (180 loc) · 6.8 KB
/
docker-entrypoint.sh
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/bash
# exit on any script failure
set -e -o pipefail
trap "echo The script is terminated by SIGINT; exit" SIGINT
trap "echo The script is terminated by SIGTERM; exit" SIGTERM
trap "echo The script is terminated by SIGKILL; exit" SIGKILL
if [ "$ENTRYDEBUG" == "TRUE" ]; then
# print shell input lines as they are read
set -v
fi
# ensure the ppp device exists
[ -c /dev/ppp ] || su-exec root mknod /dev/ppp c 108 0
# make folder for logs (available outside container)
export LOGS_FOLDER=${VPN_2FA_DIR}/logs
rm -rf ${LOGS_FOLDER}
mkdir -p ${LOGS_FOLDER}
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(<"${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}
check_required_variabled() {
if [ -z "$VPN_ADDR" -o -z "$VPN_USER" -o -z "$VPN_PASS" ]; then
echo "`date` [INIT] Variables VPN_ADDR, VPN_USER and VPN_PASS must be set."
exit 1
fi
if [ ! -d "$VPN_2FA_DIR" ]; then
echo "`date` [INIT] The 2FA directory not exist. Please fill variable VPN_2FA_DIR with valid directory."
exit 1
fi
}
docker_env_setup() {
file_env 'VPN_ADDR'
file_env 'VPN_USER'
file_env 'VPN_PASS'
file_env 'VPN_2FA_DIR' '/tmp/2fa/'
file_env 'VPN_2FA_FILE' '/tmp/2fa/2fa.txt'
file_env 'ENABLE_IPTABLES_LEGACY'
file_env 'ENABLE_PORT_FORWARDING'
file_env 'SOCKS_PROXY_PORT' 8443
}
port_forwarding_setup() {
# generate regex search string
r="^" # required start of variable name
r="${r}\(PORT_FORWARD\|REMOTE_ADDR\)[^=]*=" # Required variable name
r="${r}\(\(tcp\|udp\):\)\?" # optional tcp or udp
r="${r}\(\(\d\{1,5\}\):\)\?" # optional LOCAL_PORT
r="${r}[a-zA-Z0-9.-]\+" # required REMOTE_HOST (ip or hostname)
r="${r}:\d\{1,5\}" # required REMOTE_PORT
r="${r}$" # required end of variable contents
# create a space separated list of forwarded ports. Pause immediate script
# termination on non-zero exits to permit use without port forwarding.
set +e
forwards=$(
env \
| grep "${r}" \
| cut -d= -f2-
)
set -e
# remove our old socat entries from ip-up
sed '/^socat/d' -i /etc/ppp/ip-up
# iterate over all REMOTE_ADDR.* environment variables and create ppp ip-up
# scripts
for forward in ${forwards}; do
# replace colons with spaces add them into a bash array
colons=$(echo "${forward}" | grep -o ':' | wc -l)
if [ "${colons}" -eq "3" ]; then
PROTOCOL=$(echo "${forward}" | cut -d: -f1)
LOCAL_PORT=$(echo "${forward}" | cut -d: -f2)
REMOTE_HOST=$(echo "${forward}" | cut -d: -f3)
REMOTE_PORT=$(echo "${forward}" | cut -d: -f4)
elif [ "${colons}" -eq "2" ]; then
PROTOCOL="tcp"
LOCAL_PORT=$(echo "${forward}" | cut -d: -f1)
REMOTE_HOST=$(echo "${forward}" | cut -d: -f2)
REMOTE_PORT=$(echo "${forward}" | cut -d: -f3)
elif [ "${colons}" -eq "1" ]; then
PROTOCOL="tcp"
LOCAL_PORT="1111"
REMOTE_HOST=$(echo "${forward}" | cut -d: -f1)
REMOTE_PORT=$(echo "${forward}" | cut -d: -f2)
else
echo '`date` [INIT] ERROR: unrecognized PORT_FORWARD(*) value: "%s"\n' "${address}" >&2
exit 1
fi
# use ppp's ip-up script to start the socat tunnels. In testing, this works
# well with one exception being hostname resolution doesnt happen within the
# VPN.
# for future attemps at solving this issue: dig/drill resolve properly after
# VPN is established whereas `getent hosts` and whatver ping/ssh use do not.
# it seems potentially related to musl and would be worth testing if this
# docker image should base of debian instead of alpine.
echo "socat ${PROTOCOL}-l:${LOCAL_PORT},fork,reuseaddr ${PROTOCOL}:${REMOTE_HOST}:${REMOTE_PORT} &" \
>> "/etc/ppp/ip-up"
echo "`date` [INIT] INFO: -> socat {LOCAL_PORT}->${REMOTE_HOST}:${REMOTE_PORT}"
done
}
start_proxy() {
PROXY_LOG=${LOGS_FOLDER}/proxy.log
rm -f $PROXY_LOG
echo "`date` [INIT] Starting glider proxy on port ${SOCKS_PROXY_PORT}."
/usr/bin/glider -verbose -listen :${SOCKS_PROXY_PORT} &>$PROXY_LOG & disown
echo "`date` [INIT] -> proxy log: ${PROXY_LOG}"
if [ $? -eq 0 ]; then
echo "`date` [INIT] OK proxy started."
else
echo "`date` [INIT] ERROR while starting proxy! Exiting now."
exit 1
fi
}
run_2fa_listener() {
rm -f "$VPN_2FA_FILE"
touch "$VPN_2FA_FILE"
chmod 777 "$VPN_2FA_FILE"
echo "`date` [INIT] Checking iptables..."
if [ -n "$ENABLE_IPTABLES_LEGACY" ]; then
echo "`date` [INIT] Using 'iptables-legacy' for masquerading."
LEGACY_CMD=$(iptables --version | grep legacy)
if [ ! -z "$VPN_2FA_DIR" ]; then
echo "`date` [INIT] Legacy iptables already enabled."
else
echo "`date` [INIT] Running update-alternatives"
update-alternatives --set iptables /usr/sbin/iptables-legacy
if [ $? -eq 0 ]; then
echo "`date` [INIT] OK update-alternatives"
else
echo "`date` [INIT] ERROR update-alternatives"
fi
fi
else
echo "`date` [INIT] NOT enabling 'iptables-legacy' for masquerading."
fi
echo "`date` [INIT] Setting up masquerading with iptables..."
# setup masquerade, to allow the container to act as a gateway
for iface in $(ip a | grep eth | grep inet | awk '{print $2}'); do
iptables -t nat -A POSTROUTING -s "$iface" -j MASQUERADE
if [ $? -eq 0 ]; then
echo "`date` [INIT] -> OK ipdatbles"
else
echo "`date` [INIT] -> ERROR iptables"
fi
done
echo "`date` [INIT] Iptables setup DONE"
echo "`date` [INIT] Killing any running listeners."
pkill -15 -f -e "inotifywait" || echo "`date` [INIT] No process to kill."
sleep 2
echo "`date` [INIT] Looping listener execution."
while [ true ]; do
echo "`date` [INIT] 2FA Token Listener Start. Waiting for new token."
/usr/bin/inotifywait.sh
echo "`date` [INIT] 2FA Token Listener Terminated!"
sleep 10
done
}
_main() {
check_required_variabled
docker_env_setup
start_proxy
if [ -n "$ENABLE_PORT_FORWARDING" ]; then
echo "`date` [INIT] Setting up port forwarding..."
port_forwarding_setup
else
echo "`date` [INIT] Port forwarding is NOT enabled."
fi
# run listener that will monitoring for 2FA file changes
run_2fa_listener
}
_main "$@"