-
Notifications
You must be signed in to change notification settings - Fork 3
/
run
executable file
·197 lines (160 loc) · 4.24 KB
/
run
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
#!/bin/sh
#
# Shell script to start the Pritunl Docker image.
#
# Copyright 2016-2019, Frederico Martins
# Author: Frederico Martins <http:https://github.com/fscm>
#
# SPDX-License-Identifier: MIT
#
# This program is free software. You can use it and/or modify it under the
# terms of the MIT License.
#
set -e
BASENAME=$(basename "${0}")
__TS__=$(date +%Y%m%d%H%M%S)
# Variables
ACTION_HELP=0
ACTION_INIT=0
ACTION_START=0
MONGODB_URI=
MONGODB_URI_DB=
MONGODB_URI_PORT=
MONGODB_URI_PROTO=
MONGODB_URI_ADDRESS=
MONGODB_URI_OPTIONS=
MONGODB_URI_CREDENTIALS=
__PRITUNL_DATA__="/data/pritunl"
# Configuration files
PRITUNL_CONF="${__PRITUNL_DATA__}/pritunl.conf"
# Usage
show_usage() {
echo "Usage: ${BASENAME} [options] (help|init|start)"
echo " help:"
echo " <none>"
echo " init:"
echo " -m <URI> The MongoDB URI (e.g.: mongodb:https://mongodb.host:27017/pritunl)"
echo " start:"
echo " <none>"
}
# Options parsing
while getopts ":m:" opt; do
case ${opt} in
m)
MONGODB_URI=${OPTARG}
;;
\?)
echo >&2 " [ERROR] Invalid option: -${OPTARG}"
exit 1
;;
:)
echo >&2 " [ERROR] Option -${OPTARG} requires an argument"
exit 2
;;
esac
done
shift $((OPTIND-1))
for command in "${@}"; do
case "${command}" in
[Hh][Ee][Ll][Pp])
ACTION_HELP=1
;;
[Ii][Nn][Ii][Tt])
ACTION_INIT=1
;;
[Ss][Tt][Aa][Rr][Tt])
ACTION_START=1
;;
*)
echo >&2 " [WARN] Unknown command '${command}'"
;;
esac
done
# Check arguments
if [ $# -eq 0 ]; then
show_usage
exit 3
fi
if [ "$((ACTION_HELP+ACTION_INIT+ACTION_START))" -eq 0 ]; then
show_usage
exit 0
fi
# Check permissions
if [ "$(id -u)" -ne 0 ]; then
echo >&2 " [ERROR] This script requires privileged access to system files"
exit 4
fi
# === HELP ===
if [ "${ACTION_HELP}" -gt 0 ]; then
show_usage
exit 0
fi
# === INIT ===
if [ "${ACTION_INIT}" -gt 0 ]; then
# Check requirements
if [ "x${MONGODB_URI}" = "x" ]; then
echo >&2 " [ERROR] The MongoDB URI (-m) option is mandatory."
exit 5
fi
# Create configuration dir
if ! [ -d "${__PRITUNL_DATA__}" ]; then
mkdir -p -m 0755 ${__PRITUNL_DATA__}
fi
# Backup configuration files or copy if it does not exist yet
if [ -f "${PRITUNL_CONF}" ]; then
cp "${PRITUNL_CONF}" "${PRITUNL_CONF}.${__TS__}.bck"
else
cp "/etc/pritunl.conf.orig" "${PRITUNL_CONF}"
fi
# set Pritunl MongoDB URI
eval $(echo "$MONGODB_URI" | sed -n -r -e "s,^((mongodb):https://)?(([0-9a-zA-Z]+:[0-9a-zA-Z]+)@)?([0-9a-zA-Z\.\-]+)(:([0-9]+))?/([0-9a-zA-Z\.\-]+)(\?[=\&0-9a-zA-Z]+)?$,MONGODB_URI_PROTO='\2' MONGODB_URI_CREDENTIALS='\3' MONGODB_URI_ADDRESS='\5' MONGODB_URI_PORT='\7' MONGODB_URI_DB='\8' MONGODB_URI_OPTIONS='\9',p")
if [ "x${MONGODB_URI_ADDRESS}" = "x" ]; then
echo >&2 " [ERROR] Invalid MongoDB URI."
exit 6
else
MONGODB_URI="${MONGODB_URI_PROTO:-mongodb}:https://${MONGODB_URI_CREDENTIALS}${MONGODB_URI_ADDRESS}:${MONGODB_URI_PORT:-27017}/${MONGODB_URI_DB}${MONGODB_URI_OPTIONS}"
fi
/bin/pritunl set-mongodb "${MONGODB_URI}"
# Get setup-key
echo "=================================="
echo "SetupKey:"
/bin/pritunl setup-key 2>/dev/null
echo "=================================="
# Get default credentials
echo "=================================="
/bin/pritunl default-password
echo "=================================="
# Clean up unneeded backups
if [ -f "${PRITUNL_CONF}.${__TS__}.bck" ]; then
diff -q "${PRITUNL_CONF}" "${PRITUNL_CONF}.${__TS__}.bck" 2>&1 > /dev/null && rm -f "${PRITUNL_CONF}.${__TS__}.bck" || true
fi
# All done
echo " [INFO] Configuration(s) successfully updated"
fi
# === START ===
if [ "${ACTION_START}" -gt 0 ]; then
# Create temporary dir (if needed)
if ! [ -d /tmp ]; then
mkdir -p -m 1777 /tmp
fi
# Create run dir (if needed)
if ! [ -d /var/run ]; then
mkdir -p -m 0755 /var/run
fi
# Create required device(s)
if ! [ -d /dev/net ]; then
mkdir -p -m 755 /dev/net
fi
if ! [ -c /dev/net/tun ]; then
mknod /dev/net/tun c 10 200
fi
# Start the Pritunl server
if [ -f "${PRITUNL_CONF}" ]; then
/bin/pritunl start --conf ${PRITUNL_CONF}
else
echo >&2 " [ERROR] Pritunl configuration not found."
exit 7
fi
fi
# All done
exit 0