Skip to content

Commit d37a2fb

Browse files
committed
Improve quick start script
- The VPN quick start script now supports most of the environment variables (such as VPN_DNS_SRV1) that are currently supported by vpnsetup.sh and ikev2.sh. This change enables customization by advanced users when running the quick start script.
1 parent f7c5ecf commit d37a2fb

File tree

1 file changed

+88
-1
lines changed

1 file changed

+88
-1
lines changed

extras/quickstart.sh

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,37 @@
1717
# Attribution required: please include my name in any derivative and let me
1818
# know how you have improved it!
1919

20+
# =====================================================
21+
22+
# Define your own values for these variables
23+
# - IPsec pre-shared key, VPN username and password
24+
# - All values MUST be placed inside 'single quotes'
25+
# - DO NOT use these special characters within values: \ " '
26+
27+
YOUR_IPSEC_PSK=''
28+
YOUR_USERNAME=''
29+
YOUR_PASSWORD=''
30+
31+
# Important notes: https://git.io/vpnnotes
32+
# Setup VPN clients: https://git.io/vpnclients
33+
# IKEv2 guide: https://git.io/ikev2
34+
35+
# =====================================================
36+
2037
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
2138

2239
exiterr() { echo "Error: $1" >&2; exit 1; }
2340

41+
check_ip() {
42+
IP_REGEX='^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$'
43+
printf '%s' "$1" | tr -d '\n' | grep -Eq "$IP_REGEX"
44+
}
45+
46+
check_dns_name() {
47+
FQDN_REGEX='^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$'
48+
printf '%s' "$1" | tr -d '\n' | grep -Eq "$FQDN_REGEX"
49+
}
50+
2451
check_root() {
2552
if [ "$(id -u)" != 0 ]; then
2653
exiterr "Script must be run as root. Try 'sudo sh $0'"
@@ -124,6 +151,53 @@ check_iface() {
124151
fi
125152
}
126153

154+
check_creds() {
155+
[ -n "$YOUR_IPSEC_PSK" ] && VPN_IPSEC_PSK="$YOUR_IPSEC_PSK"
156+
[ -n "$YOUR_USERNAME" ] && VPN_USER="$YOUR_USERNAME"
157+
[ -n "$YOUR_PASSWORD" ] && VPN_PASSWORD="$YOUR_PASSWORD"
158+
159+
if [ -z "$VPN_IPSEC_PSK" ] && [ -z "$VPN_USER" ] && [ -z "$VPN_PASSWORD" ]; then
160+
return 0
161+
fi
162+
163+
if [ -z "$VPN_IPSEC_PSK" ] || [ -z "$VPN_USER" ] || [ -z "$VPN_PASSWORD" ]; then
164+
exiterr "All VPN credentials must be specified. Edit the script and re-enter them."
165+
fi
166+
167+
if printf '%s' "$VPN_IPSEC_PSK $VPN_USER $VPN_PASSWORD" | LC_ALL=C grep -q '[^ -~]\+'; then
168+
exiterr "VPN credentials must not contain non-ASCII characters."
169+
fi
170+
171+
case "$VPN_IPSEC_PSK $VPN_USER $VPN_PASSWORD" in
172+
*[\\\"\']*)
173+
exiterr "VPN credentials must not contain these special characters: \\ \" '"
174+
;;
175+
esac
176+
}
177+
178+
check_dns() {
179+
if { [ -n "$VPN_DNS_SRV1" ] && ! check_ip "$VPN_DNS_SRV1"; } \
180+
|| { [ -n "$VPN_DNS_SRV2" ] && ! check_ip "$VPN_DNS_SRV2"; }; then
181+
exiterr "The DNS server specified is invalid."
182+
fi
183+
}
184+
185+
check_server_dns() {
186+
if [ -n "$VPN_DNS_NAME" ] && ! check_dns_name "$VPN_DNS_NAME"; then
187+
exiterr "Invalid DNS name. 'VPN_DNS_NAME' must be a fully qualified domain name (FQDN)."
188+
fi
189+
}
190+
191+
check_client_name() {
192+
if [ -n "$VPN_CLIENT_NAME" ]; then
193+
name_len="$(printf '%s' "$VPN_CLIENT_NAME" | wc -m)"
194+
if [ "$name_len" -gt "64" ] || printf '%s' "$VPN_CLIENT_NAME" | LC_ALL=C grep -q '[^A-Za-z0-9_-]\+' \
195+
|| case $VPN_CLIENT_NAME in -*) true;; *) false;; esac; then
196+
exiterr "Invalid client name. Use one word only, no special characters except '-' and '_'."
197+
fi
198+
fi
199+
}
200+
127201
check_iptables() {
128202
if [ "$os_type" = "ubuntu" ] || [ "$os_type" = "debian" ] || [ "$os_type" = "raspbian" ]; then
129203
if [ -x /sbin/iptables ] && ! iptables -nL INPUT >/dev/null 2>&1; then
@@ -192,9 +266,18 @@ run_setup() {
192266
if tmpdir=$(mktemp --tmpdir -d vpn.XXXXX 2>/dev/null); then
193267
if ( set -x; wget -t 3 -T 30 -q -O "$tmpdir/vpn.sh" "$setup_url" \
194268
|| curl -fsL "$setup_url" -o "$tmpdir/vpn.sh" 2>/dev/null ); then
195-
if /bin/bash "$tmpdir/vpn.sh"; then
269+
if VPN_IPSEC_PSK="$VPN_IPSEC_PSK" VPN_USER="$VPN_USER" VPN_PASSWORD="$VPN_PASSWORD" \
270+
VPN_PUBLIC_IP="$VPN_PUBLIC_IP" VPN_L2TP_NET="$VPN_L2TP_NET" \
271+
VPN_L2TP_LOCAL="$VPN_L2TP_LOCAL" VPN_L2TP_POOL="$VPN_L2TP_POOL" \
272+
VPN_XAUTH_NET="$VPN_XAUTH_NET" VPN_XAUTH_POOL="$VPN_XAUTH_POOL" \
273+
VPN_DNS_SRV1="$VPN_DNS_SRV1" VPN_DNS_SRV2="$VPN_DNS_SRV2" \
274+
/bin/bash "$tmpdir/vpn.sh"; then
196275
if [ -s /opt/src/ikev2.sh ] && [ ! -f /etc/ipsec.d/ikev2.conf ]; then
197276
sleep 1
277+
VPN_DNS_NAME="$VPN_DNS_NAME" VPN_PUBLIC_IP="$VPN_PUBLIC_IP" \
278+
VPN_CLIENT_NAME="$VPN_CLIENT_NAME" VPN_XAUTH_POOL="$VPN_XAUTH_POOL" \
279+
VPN_DNS_SRV1="$VPN_DNS_SRV1" VPN_DNS_SRV2="$VPN_DNS_SRV2" \
280+
VPN_PROTECT_CONFIG="$VPN_PROTECT_CONFIG" \
198281
/bin/bash /opt/src/ikev2.sh --auto || status=1
199282
fi
200283
else
@@ -217,6 +300,10 @@ quickstart() {
217300
check_lxc
218301
check_os
219302
check_iface
303+
check_creds
304+
check_dns
305+
check_server_dns
306+
check_client_name
220307
check_iptables
221308
install_pkgs
222309
get_setup_url

0 commit comments

Comments
 (0)