This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
forked from dperson/openvpn-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openvpn.sh
executable file
·184 lines (167 loc) · 5.68 KB
/
openvpn.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
#!/usr/bin/env bash
#===============================================================================
# FILE: openvpn.sh
#
# USAGE: ./openvpn.sh
#
# DESCRIPTION: Entrypoint for openvpn docker container
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: David Personette ([email protected]),
# ORGANIZATION:
# CREATED: 09/28/2014 12:11
# REVISION: 1.0
#===============================================================================
set -o nounset # Treat unset variables as an error
### dns: setup openvpn client DNS
# Arguments:
# none)
# Return: conf file that uses VPN provider's DNS resolvers
dns() { local conf="/vpn/vpn.conf"
sed -i '/resolv-*conf/d; /script-security/d' $conf
echo "# This updates the resolvconf with dns settings" >>$conf
echo "script-security 2" >>$conf
echo "up /etc/openvpn/update-resolv-conf" >>$conf
echo "down /etc/openvpn/update-resolv-conf" >>$conf
}
### firewall: firewall all output not DNS/VPN that's not over the VPN
# Arguments:
# none)
# Return: configured firewall
firewall() {
local docker_network=$(ip -o addr show dev eth0 |
awk '$3 == "inet" {print $4}')
iptables -F OUTPUT
iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A OUTPUT -o tap0 -j ACCEPT
iptables -A OUTPUT -o tun0 -j ACCEPT
iptables -A OUTPUT -d ${docker_network} -j ACCEPT
iptables -A OUTPUT -p udp -m udp --dport 53 -j ACCEPT
iptables -A OUTPUT -p tcp -m owner --gid-owner vpn -j ACCEPT
iptables -A OUTPUT -p udp -m owner --gid-owner vpn -j ACCEPT
iptables -A OUTPUT -j DROP
}
### return_route: add a route back to your network, so that return traffic works
# Arguments:
# network) a CIDR specified network range
# Return: configured return route
return_route() { local gw network="$1"
gw=$(ip route | awk '/default/ {print $3}')
ip route add to $network via $gw dev eth0
}
### timezone: Set the timezone for the container
# Arguments:
# timezone) for example EST5EDT
# Return: the correct zoneinfo file will be symlinked into place
timezone() { local timezone="${1:-EST5EDT}"
[[ -e /usr/share/zoneinfo/$timezone ]] || {
echo "ERROR: invalid timezone specified: $timezone" >&2
return
}
if [[ -w /etc/timezone && $(cat /etc/timezone) != $timezone ]]; then
echo "$timezone" >/etc/timezone
ln -sf /usr/share/zoneinfo/$timezone /etc/localtime
dpkg-reconfigure -f noninteractive tzdata >/dev/null 2>&1
fi
}
### vpn: setup openvpn client
# Arguments:
# server) VPN GW server
# user) user name on VPN
# pass) password on VPN
# Return: configured .ovpn file
vpn() { local server="$1" user="$2" pass="$3" \
conf="/vpn/vpn.conf" auth="/vpn/vpn.auth"
cat >$conf <<-EOF
client
dev tun
proto udp
remote $server 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ca /vpn/vpn-ca.crt
tls-client
remote-cert-tls server
auth-user-pass
comp-lzo
verb 1
reneg-sec 0
redirect-gateway def1
auth-user-pass $auth
EOF
echo "$user" >$auth
echo "$pass" >>$auth
chmod 0600 $auth
}
### usage: Help
# Arguments:
# none)
# Return: Help text
usage() { local RC=${1:-0}
echo "Usage: ${0##*/} [-opt] [command]
Options (fields in '[]' are optional, '<>' are required):
-h This help
-d Use the VPN provider's DNS resolvers
-f Firewall rules so that only the VPN and DNS are allowed to
send internet traffic (IE if VPN is down it's offline)
-r \"<network>\" CIDR network (IE 192.168.1.0/24)
required arg: \"<network>\"
<network> add a route to (allows replies once the VPN is up)
-t \"\" Configure timezone
possible arg: \"[timezone]\" - zoneinfo timezone for container
-v '<server;user;password>' Configure OpenVPN
required arg: \"<server>;<user>;<password>\"
<server> to connect to
<user> to authenticate as
<password> to authenticate with
The 'command' (if provided and valid) will be run instead of openvpn
" >&2
exit $RC
}
while getopts ":hdfr:t:v:" opt; do
case "$opt" in
h) usage ;;
d) DNS=true ;;
f) firewall; touch /vpn/.firewall ;;
r) return_route "$OPTARG" ;;
t) timezone "$OPTARG" ;;
v) eval vpn $(sed 's/^\|$/"/g; s/;/" "/g' <<< $OPTARG) ;;
"?") echo "Unknown option: -$OPTARG"; usage 1 ;;
":") echo "No argument value for option: -$OPTARG"; usage 2 ;;
esac
done
shift $(( OPTIND - 1 ))
[[ "${FIREWALL:-""}" || -e /vpn/.firewall ]] && firewall
[[ "${ROUTE:-""}" ]] && return_route "$ROUTE"
[[ "${TZ:-""}" ]] && timezone "$TZ"
[[ "${VPN:-""}" ]] && eval vpn $(sed 's/^\|$/"/g; s/;/" "/g' <<< $VPN)
[[ "${DNS:-""}" ]] && dns
if [[ $# -ge 1 && -x $(which $1 2>&-) ]]; then
exec "$@"
elif [[ $# -ge 1 ]]; then
echo "ERROR: command not found: $1"
exit 13
elif ps -ef | egrep -v 'grep|openvpn.sh' | grep -q openvpn; then
echo "Service already running, please restart container to apply changes"
else
[[ -e /vpn/vpn.conf ]] || { echo "ERROR: VPN not configured!"; sleep 120; }
[[ -e /vpn/vpn-ca.crt ]] || { echo "ERROR: VPN cert missing!"; sleep 120; }
[[ -x /sbin/resolvconf ]] || { cat >/sbin/resolvconf <<-EOF
#!/bin/sh
conf=/etc/resolv.conf
[ -e ${conf}.orig ] || cp -p ${conf} ${conf}.orig
if [ ${1:-""} == "-a" ]; then
cat >${conf}
elif [ ${1:-""} == "-d" ]; then
cat ${conf}.orig >${conf}
fi
EOF
chmod +x /sbin/resolvconf; }
exec sg vpn -c "openvpn --config /vpn/vpn.conf"
fi