This repository was archived by the owner on Jan 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
executable file
·157 lines (127 loc) · 3.74 KB
/
docker-entrypoint.sh
File metadata and controls
executable file
·157 lines (127 loc) · 3.74 KB
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
#!/bin/bash
set -e
### timezone config: UTC
ln -fs "/usr/share/zoneinfo/Etc/UTC" /etc/localtime
dpkg-reconfigure -f noninteractive tzdata || exit
### force creation of common folders
mkdir -p /openvpn/{tmp,sock,pid} /dev/net /config/log /config/etc/tmp
# common tweaks
if [ ! -f /usr/bin/systemctl ] ; then
ln -s /usr/bin/true /usr/bin/systemctl
fi
if [ -f /config/twistd.pid ] ; then
rm /config/twistd.pid
fi
# redirect /config to the install path
rm -rdf /usr/local/openvpn_as || exit
ln -s /config /usr/local/openvpn_as
# create tun device
if [ ! -c /dev/net/tun ]; then
mknod /dev/net/tun c 10 200
fi
# function to move dirs in the container to safe places
function move_dirs()
{
sed -i \
-e 's#~/tmp#/tmp#g' \
-e 's#~/sock#/tmp/sock#g' \
"$1"
}
### Initial config
if [ ! -f /config/configured ]; then
# initial config follows
echo "installing openvpn-as for the first time"
apt-get update && apt-get reinstall -y openvpn-as
# change dirs
move_dirs "/config/etc/as_templ.conf"
move_dirs "/config/etc/as.conf"
# if all gone ok, touch the configured flag
touch /config/configured
else
# old config found, backup
echo "existing data found, backing up before restart"
mkdir -p /config/backup
cd /config/etc/db || exit
DBFILESBAK="*.db"
for f in $DBFILESBAK ; do
echo "backing up $f"
sqlite3 "$f" .dump > /config/backup/"$f"
done
echo "backing up as.conf"
cp /config/etc/as.conf /config/backup/as.conf
cd /config || exit
# Install
apt-get update && apt-get reinstall -y openvpn-as
# change dirs
move_dirs "/config/etc/as_templ.conf"
move_dirs "/config/etc/as.conf"
# restore backups
cd /config/backup || exit
DBFILERES="*.db"
for f in $DBFILERES
do
echo "restoring $f"
rm -f /config/etc/db/"$f"
sqlite3 </config/backup/"$f" /config/etc/db/"$f"
done
rm -f /config/etc/as.conf
echo "restoring as.conf"
cp /config/backup/as.conf /config/etc/as.conf
# remove the backup folder
rm -rf /config/backup
fi
### Startup secuence
cd /config
NOASCONFIG='DELETE\n'
# check if the ADMIN & PASSWD was passed
if [ -z "${ADMIN}" -o -z "${PASSWD}"] ; then
# no credentials, use defaults
ASCONFIG='yes\nyes\n1\nrsa\n4096\nrsa\n4096\n943\n9443\nyes\nyes\nno\n\n\n\n'
else
# credential passed
ASCONFIG='yes\nyes\n1\nrsa\n4096\nrsa\n4096\n943\n9443\nyes\nyes\nno\nno\'${ADMIN}'\n'${PASSWD}'\n\n'
fi
if [ ! -f "/config/etc/as.conf" ]; then
CONFINPUT=$ASCONFIG
else
CONFINPUT=$NOASCONFIG$ASCONFIG
fi
if [ $(find /config/etc/db -type f | wc -l) -eq 0 -o ! -f "/config/etc/as.conf" ] ; then
printf "${CONFINPUT}" | /config/bin/ovpn-init > /config/init.log
fi
if [ -z "$INTERFACE" ]; then
SET_INTERFACE="eth0"
else
SET_INTERFACE=$INTERFACE
fi
/config/scripts/confdba -mk "admin_ui.https.ip_address" -v "$SET_INTERFACE"
/config/scripts/confdba -mk "cs.https.ip_address" -v "$SET_INTERFACE"
/config/scripts/confdba -mk "vpn.daemon.0.listen.ip_address" -v "$SET_INTERFACE"
/config/scripts/confdba -mk "vpn.daemon.0.server.ip_address" -v "$SET_INTERFACE"
# run the openvpn service
/config/scripts/openvpnas -n -l - -p /openvpn/pid/openvpn.pid &
# run CMD parameters
if [ "${1}" != "openvpn" ] ; then
"$@" &
fi
# recognize PIDs
pidlist=$(jobs -p)
# initialize latest result var
latest_exit=0
# define shutdown helper
function shutdown {
trap "" SIGINT
for single in $pidlist; do
if ! kill -0 "$single" 2> /dev/null; then
wait "$single"
latest_exit=$?
fi
done
kill "$pidlist" 2> /dev/null
/bin/bash /config/scripts/openvpn_service_cleanup
}
# run shutdown
trap shutdown SIGINT
wait -n
# return received result
exit $latest_exit