#!/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH # # This is a Shell script for configure and start L2TP/IPSec VPN server with Docker image # # Copyright (C) 2018 Teddysun # # Reference URL: # https://github.com/libreswan/libreswan # https://github.com/xelerance/xl2tpd if [ ! -f "/.dockerenv" ]; then echo "Error: This script must be run in a Docker container." >&2 exit 1 fi if ip link add dummy0 type dummy 2>&1 | grep -q "not permitted"; then echo "Error: This Docker image must be run in privileged mode." >&2 exit 1 fi ip link delete dummy0 >/dev/null 2>&1 rand(){ str=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 1) echo ${str} } is_64bit(){ if [ "$(getconf WORD_BIT)" = "32" ] && [ "$(getconf LONG_BIT)" = "64" ]; then return 0 else return 1 fi } # Environment file name l2tp_env_file="/etc/l2tp.env" # Auto generated if [ -z "${VPN_IPSEC_PSK}" ] && [ -z "${VPN_USER}" ] && [ -z "${VPN_PASSWORD}" ]; then if [ -f "${l2tp_env_file}" ]; then echo "Loading previously generated environment variables for L2TP/IPSec VPN Server..." . "${l2tp_env_file}" else echo "L2TP/IPSec VPN Server environment variables is not set. Use default environment variables..." VPN_IPSEC_PSK="teddysun.com" VPN_USER="vpnuser" VPN_PASSWORD="$(rand)" echo "VPN_IPSEC_PSK=${VPN_IPSEC_PSK}" > ${l2tp_env_file} echo "VPN_USER=${VPN_USER}" >> ${l2tp_env_file} echo "VPN_PASSWORD=${VPN_PASSWORD}" >> ${l2tp_env_file} chmod 600 ${l2tp_env_file} fi fi # Environment variables: # VPN_IPSEC_PSK # VPN_USER # VPN_PASSWORD if [ -z "${VPN_IPSEC_PSK}" ] || [ -z "${VPN_USER}" ] || [ -z "${VPN_PASSWORD}" ]; then echo "Error: Environment variables must be specified. please edit your environment file and retry again." >&2 exit 1 fi if printf '%s' "${VPN_IPSEC_PSK} ${VPN_USER} ${VPN_PASSWORD}" | LC_ALL=C grep -q '[^ -~]\+'; then echo "Error: Environment variables must not contain non-ASCII characters." >&2 exit 1 fi case "${VPN_IPSEC_PSK} ${VPN_USER} ${VPN_PASSWORD}" in *[\\\"\']*) echo "Error: Environment variables must not contain these special characters like: \\ \" '" exit 1 ;; esac # Environment variables: # VPN_PUBLIC_IP PUBLIC_IP=${VPN_PUBLIC_IP:-''} [ -z "${PUBLIC_IP}" ] && PUBLIC_IP=$( wget -qO- -t1 -T2 ipv4.icanhazip.com ) [ -z "${PUBLIC_IP}" ] && PUBLIC_IP=$( wget -qO- -t1 -T2 ipinfo.io/ip ) # Environment variables: # VPN_L2TP_NET # VPN_L2TP_LOCAL # VPN_L2TP_REMOTE # VPN_XAUTH_NET # VPN_XAUTH_REMOTE # VPN_DNS1 # VPN_DNS2 # VPN_SHA2_TRUNCBUG L2TP_NET=${VPN_L2TP_NET:-'192.168.18.0/24'} L2TP_LOCAL=${VPN_L2TP_LOCAL:-'192.168.18.1'} L2TP_REMOTE=${VPN_L2TP_REMOTE:-'192.168.18.10-192.168.18.250'} XAUTH_NET=${VPN_XAUTH_NET:-'192.168.20.0/24'} XAUTH_REMOTE=${VPN_XAUTH_REMOTE:-'192.168.20.10-192.168.20.250'} DNS1=${VPN_DNS1:-'8.8.8.8'} DNS2=${VPN_DNS2:-'8.8.4.4'} case ${VPN_SHA2_TRUNCBUG} in [yY][eE][sS]) SHA2_TRUNCBUG=yes ;; *) SHA2_TRUNCBUG=no ;; esac # Create IPSec config cat > /etc/ipsec.conf < /etc/xl2tpd/xl2tpd.conf < /etc/ppp/options.xl2tpd < /etc/ipsec.secrets </dev/null; then cat > /etc/ppp/chap-secrets </dev/null; then cat > /etc/ipsec.d/passwd <