Fred808 commited on
Commit
0e9c8a1
·
verified ·
1 Parent(s): a2d1a08

Upload 5 files

Browse files
Files changed (5) hide show
  1. .dockerignore +9 -0
  2. Dockerfile +64 -0
  3. Dockerfile.debian +68 -0
  4. docker-compose.yml +20 -0
  5. run.sh +766 -0
.dockerignore ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ .git
2
+ .github
3
+ .gitignore
4
+ docker-compose.yml
5
+ LICENSE.md
6
+ README.md
7
+ README-zh.md
8
+ vpn.env
9
+ vpn.env.example
Dockerfile ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (C) 2021-2025 Lin Song <linsongui@gmail.com>
3
+ #
4
+ # This work is licensed under the Creative Commons Attribution-ShareAlike 3.0
5
+ # Unported License: http://creativecommons.org/licenses/by-sa/3.0/
6
+ #
7
+ # Attribution required: please include my name in any derivative and let me
8
+ # know how you have improved it!
9
+
10
+ FROM alpine:3.22
11
+
12
+ ENV SWAN_VER=5.3
13
+ WORKDIR /opt/src
14
+
15
+ RUN set -x \
16
+ && apk add --no-cache \
17
+ bash bind-tools coreutils openssl uuidgen wget xl2tpd iptables iptables-legacy \
18
+ iproute2 libcap-ng libcurl libevent linux-pam musl nspr nss nss-tools openrc \
19
+ bison flex gcc make libc-dev bsd-compat-headers linux-pam-dev \
20
+ nss-dev libcap-ng-dev libevent-dev curl-dev nspr-dev \
21
+ && cd /sbin \
22
+ && for fn in iptables iptables-save iptables-restore; do ln -fs xtables-legacy-multi "$fn"; done \
23
+ && cd /opt/src \
24
+ && wget -t 3 -T 30 -nv -O libreswan.tar.gz "https://github.com/libreswan/libreswan/archive/v${SWAN_VER}.tar.gz" \
25
+ || wget -t 3 -T 30 -nv -O libreswan.tar.gz "https://download.libreswan.org/libreswan-${SWAN_VER}.tar.gz" \
26
+ && tar xzf libreswan.tar.gz \
27
+ && rm -f libreswan.tar.gz \
28
+ && cd "libreswan-${SWAN_VER}" \
29
+ && printf 'WERROR_CFLAGS=-w -s\nUSE_DNSSEC=false\nUSE_DH2=true\n' > Makefile.inc.local \
30
+ && printf 'FINALNSSDIR=/etc/ipsec.d\nNSSDIR=/etc/ipsec.d\n' >> Makefile.inc.local \
31
+ && make -s base \
32
+ && make -s install-base \
33
+ && cd /opt/src \
34
+ && mkdir -p /run/openrc \
35
+ && touch /run/openrc/softlevel \
36
+ && rm -rf "/opt/src/libreswan-${SWAN_VER}" \
37
+ && apk del --no-cache \
38
+ bison flex gcc make libc-dev bsd-compat-headers linux-pam-dev \
39
+ nss-dev libcap-ng-dev libevent-dev curl-dev nspr-dev
40
+
41
+ RUN wget -t 3 -T 30 -nv -O /opt/src/ikev2.sh https://github.com/hwdsl2/setup-ipsec-vpn/raw/ad61b354c57b3fa9871c04ec177bef2a93dd8eeb/extras/ikev2setup.sh \
42
+ && chmod +x /opt/src/ikev2.sh \
43
+ && ln -s /opt/src/ikev2.sh /usr/bin
44
+
45
+ COPY ./run.sh /opt/src/run.sh
46
+ RUN chmod 755 /opt/src/run.sh
47
+ EXPOSE 500/udp 4500/udp
48
+ CMD ["/opt/src/run.sh"]
49
+
50
+ ARG BUILD_DATE
51
+ ARG VERSION
52
+ ARG VCS_REF
53
+ ENV IMAGE_VER=$BUILD_DATE
54
+
55
+ LABEL maintainer="Lin Song <linsongui@gmail.com>" \
56
+ org.opencontainers.image.created="$BUILD_DATE" \
57
+ org.opencontainers.image.version="$VERSION" \
58
+ org.opencontainers.image.revision="$VCS_REF" \
59
+ org.opencontainers.image.authors="Lin Song <linsongui@gmail.com>" \
60
+ org.opencontainers.image.title="IPsec VPN Server on Docker" \
61
+ org.opencontainers.image.description="Docker image to run an IPsec VPN server, with IPsec/L2TP, Cisco IPsec and IKEv2." \
62
+ org.opencontainers.image.url="https://github.com/hwdsl2/docker-ipsec-vpn-server" \
63
+ org.opencontainers.image.source="https://github.com/hwdsl2/docker-ipsec-vpn-server" \
64
+ org.opencontainers.image.documentation="https://github.com/hwdsl2/docker-ipsec-vpn-server"
Dockerfile.debian ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright (C) 2016-2025 Lin Song <linsongui@gmail.com>
3
+ #
4
+ # This work is licensed under the Creative Commons Attribution-ShareAlike 3.0
5
+ # Unported License: http://creativecommons.org/licenses/by-sa/3.0/
6
+ #
7
+ # Attribution required: please include my name in any derivative and let me
8
+ # know how you have improved it!
9
+
10
+ FROM debian:bookworm-slim
11
+
12
+ ENV SWAN_VER=5.3
13
+ WORKDIR /opt/src
14
+
15
+ RUN apt-get -yqq update \
16
+ && DEBIAN_FRONTEND=noninteractive \
17
+ apt-get -yqq --no-install-recommends install \
18
+ wget dnsutils openssl ca-certificates kmod iproute2 \
19
+ gawk net-tools iptables bsdmainutils libcurl3-nss \
20
+ libnss3-tools libevent-dev uuid-runtime xl2tpd \
21
+ libnss3-dev libnspr4-dev pkg-config libpam0g-dev \
22
+ libcap-ng-dev libcap-ng-utils libselinux1-dev \
23
+ libcurl4-nss-dev flex bison gcc make \
24
+ && wget -t 3 -T 30 -nv -O libreswan.tar.gz "https://github.com/libreswan/libreswan/archive/v${SWAN_VER}.tar.gz" \
25
+ || wget -t 3 -T 30 -nv -O libreswan.tar.gz "https://download.libreswan.org/libreswan-${SWAN_VER}.tar.gz" \
26
+ && tar xzf libreswan.tar.gz \
27
+ && rm -f libreswan.tar.gz \
28
+ && cd "libreswan-${SWAN_VER}" \
29
+ && printf 'WERROR_CFLAGS=-w -s\nUSE_DNSSEC=false\nUSE_SYSTEMD_WATCHDOG=false\n' > Makefile.inc.local \
30
+ && printf 'USE_DH2=true\nUSE_NSS_KDF=false\nFINALNSSDIR=/etc/ipsec.d\nNSSDIR=/etc/ipsec.d\n' >> Makefile.inc.local \
31
+ && make -s base \
32
+ && make -s install-base \
33
+ && cd /opt/src \
34
+ && rm -rf "/opt/src/libreswan-${SWAN_VER}" \
35
+ && apt-get -yqq remove \
36
+ libnss3-dev libnspr4-dev pkg-config libpam0g-dev \
37
+ libcap-ng-dev libcap-ng-utils libselinux1-dev \
38
+ libcurl4-nss-dev flex bison gcc make \
39
+ && apt-get -yqq autoremove \
40
+ && apt-get -y clean \
41
+ && rm -rf /var/lib/apt/lists/* \
42
+ && rm -rf /var/log/* \
43
+ && update-alternatives --set iptables /usr/sbin/iptables-legacy
44
+
45
+ RUN wget -t 3 -T 30 -nv -O /opt/src/ikev2.sh https://github.com/hwdsl2/setup-ipsec-vpn/raw/ad61b354c57b3fa9871c04ec177bef2a93dd8eeb/extras/ikev2setup.sh \
46
+ && chmod +x /opt/src/ikev2.sh \
47
+ && ln -s /opt/src/ikev2.sh /usr/bin
48
+
49
+ COPY ./run.sh /opt/src/run.sh
50
+ RUN chmod 755 /opt/src/run.sh
51
+ EXPOSE 500/udp 4500/udp
52
+ CMD ["/opt/src/run.sh"]
53
+
54
+ ARG BUILD_DATE
55
+ ARG VERSION
56
+ ARG VCS_REF
57
+ ENV IMAGE_VER=$BUILD_DATE
58
+
59
+ LABEL maintainer="Lin Song <linsongui@gmail.com>" \
60
+ org.opencontainers.image.created="$BUILD_DATE" \
61
+ org.opencontainers.image.version="$VERSION" \
62
+ org.opencontainers.image.revision="$VCS_REF" \
63
+ org.opencontainers.image.authors="Lin Song <linsongui@gmail.com>" \
64
+ org.opencontainers.image.title="IPsec VPN Server on Docker" \
65
+ org.opencontainers.image.description="Docker image to run an IPsec VPN server, with IPsec/L2TP, Cisco IPsec and IKEv2." \
66
+ org.opencontainers.image.url="https://github.com/hwdsl2/docker-ipsec-vpn-server" \
67
+ org.opencontainers.image.source="https://github.com/hwdsl2/docker-ipsec-vpn-server" \
68
+ org.opencontainers.image.documentation="https://github.com/hwdsl2/docker-ipsec-vpn-server"
docker-compose.yml ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ version: '3'
2
+
3
+ volumes:
4
+ ikev2-vpn-data:
5
+
6
+ services:
7
+ vpn:
8
+ image: hwdsl2/ipsec-vpn-server
9
+ restart: always
10
+ env_file:
11
+ - ./vpn.env
12
+ ports:
13
+ - "500:500/udp"
14
+ - "4500:4500/udp"
15
+ privileged: true
16
+ hostname: ipsec-vpn-server
17
+ container_name: ipsec-vpn-server
18
+ volumes:
19
+ - ikev2-vpn-data:/etc/ipsec.d
20
+ - /lib/modules:/lib/modules:ro
run.sh ADDED
@@ -0,0 +1,766 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ #
3
+ # Docker script to configure and start an IPsec VPN server
4
+ #
5
+ # DO NOT RUN THIS SCRIPT ON YOUR PC OR MAC! THIS IS ONLY MEANT TO BE RUN
6
+ # IN A CONTAINER!
7
+ #
8
+ # This file is part of IPsec VPN Docker image, available at:
9
+ # https://github.com/hwdsl2/docker-ipsec-vpn-server
10
+ #
11
+ # Copyright (C) 2016-2025 Lin Song <linsongui@gmail.com>
12
+ # Based on the work of Thomas Sarlandie (Copyright 2012)
13
+ #
14
+ # This work is licensed under the Creative Commons Attribution-ShareAlike 3.0
15
+ # Unported License: http://creativecommons.org/licenses/by-sa/3.0/
16
+ #
17
+ # Attribution required: please include my name in any derivative and let me
18
+ # know how you have improved it!
19
+
20
+ export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
21
+
22
+ exiterr() { echo "Error: $1" >&2; exit 1; }
23
+ nospaces() { printf '%s' "$1" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'; }
24
+ onespace() { printf '%s' "$1" | tr -s ' '; }
25
+ noquotes() { printf '%s' "$1" | sed -e 's/^"\(.*\)"$/\1/' -e "s/^'\(.*\)'$/\1/"; }
26
+ noquotes2() { printf '%s' "$1" | sed -e 's/" "/ /g' -e "s/' '/ /g"; }
27
+
28
+ check_ip() {
29
+ 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])$'
30
+ printf '%s' "$1" | tr -d '\n' | grep -Eq "$IP_REGEX"
31
+ }
32
+
33
+ check_cidr() {
34
+ CIDR_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])(/(3[0-2]|[1-2][0-9]|[0-9]))$'
35
+ printf '%s' "$1" | tr -d '\n' | grep -Eq "$CIDR_REGEX"
36
+ }
37
+
38
+ check_dns_name() {
39
+ FQDN_REGEX='^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$'
40
+ printf '%s' "$1" | tr -d '\n' | grep -Eq "$FQDN_REGEX"
41
+ }
42
+
43
+ check_client_name() {
44
+ ! { [ "${#1}" -gt "64" ] || printf '%s' "$1" | LC_ALL=C grep -q '[^A-Za-z0-9_-]\+' \
45
+ || case $1 in -*) true ;; *) false ;; esac; }
46
+ }
47
+
48
+ if [ ! -f "/.dockerenv" ] && [ ! -f "/run/.containerenv" ] \
49
+ && [ -z "$KUBERNETES_SERVICE_HOST" ] \
50
+ && ! head -n 1 /proc/1/sched 2>/dev/null | grep -q '^run\.sh '; then
51
+ exiterr "This script ONLY runs in a container (e.g. Docker, Podman)."
52
+ fi
53
+
54
+ if ip link add dummy0 type dummy 2>&1 | grep -q "not permitted"; then
55
+ cat 1>&2 <<'EOF'
56
+ Error: This Docker image should be run in privileged mode.
57
+ See: https://github.com/hwdsl2/docker-ipsec-vpn-server
58
+
59
+ EOF
60
+ exit 1
61
+ fi
62
+ ip link delete dummy0 >/dev/null 2>&1
63
+
64
+ os_type=debian
65
+ os_arch=$(uname -m | tr -dc 'A-Za-z0-9_-')
66
+ [ -f /etc/os-release ] && os_type=$(. /etc/os-release && printf '%s' "$ID")
67
+
68
+ if [ ! -e /dev/ppp ]; then
69
+ cat <<'EOF'
70
+
71
+ Warning: /dev/ppp is missing, and IPsec/L2TP mode may not work.
72
+ Please use IKEv2 or IPsec/XAuth mode to connect.
73
+ Debian 11/10 users, see https://vpnsetup.net/debian10
74
+ EOF
75
+ fi
76
+
77
+ NET_IFACE=$(route 2>/dev/null | grep -m 1 '^default' | grep -o '[^ ]*$')
78
+ [ -z "$NET_IFACE" ] && NET_IFACE=$(ip -4 route list 0/0 2>/dev/null | grep -m 1 -Po '(?<=dev )(\S+)')
79
+ [ -z "$NET_IFACE" ] && NET_IFACE=eth0
80
+
81
+ mkdir -p /opt/src
82
+ vpn_env="/opt/src/vpn.env"
83
+ vpn_env_dir="/opt/src/env/vpn.env"
84
+ if [ -f "$vpn_env_dir" ]; then
85
+ vpn_env="$vpn_env_dir"
86
+ fi
87
+ vpn_gen_env="/etc/ipsec.d/vpn-gen.env"
88
+ if [ -z "$VPN_IPSEC_PSK" ] && [ -z "$VPN_USER" ] && [ -z "$VPN_PASSWORD" ]; then
89
+ if [ -f "$vpn_env" ]; then
90
+ echo
91
+ echo 'Retrieving VPN credentials...'
92
+ . "$vpn_env"
93
+ elif [ -f "$vpn_gen_env" ]; then
94
+ echo
95
+ echo 'Retrieving previously generated VPN credentials...'
96
+ . "$vpn_gen_env"
97
+ else
98
+ echo
99
+ echo 'VPN credentials not set by user. Generating random PSK and password...'
100
+ VPN_IPSEC_PSK=$(LC_CTYPE=C tr -dc 'A-HJ-NPR-Za-km-z2-9' </dev/urandom 2>/dev/null | head -c 20)
101
+ VPN_USER=vpnuser
102
+ VPN_PASSWORD=$(LC_CTYPE=C tr -dc 'A-HJ-NPR-Za-km-z2-9' </dev/urandom 2>/dev/null | head -c 16)
103
+ printf '%s\n' "VPN_IPSEC_PSK='$VPN_IPSEC_PSK'" > "$vpn_gen_env"
104
+ printf '%s\n' "VPN_USER='$VPN_USER'" >> "$vpn_gen_env"
105
+ printf '%s\n' "VPN_PASSWORD='$VPN_PASSWORD'" >> "$vpn_gen_env"
106
+ chmod 600 "$vpn_gen_env"
107
+ fi
108
+ fi
109
+
110
+ # Remove whitespace and quotes around VPN variables, if any
111
+ VPN_IPSEC_PSK=$(nospaces "$VPN_IPSEC_PSK")
112
+ VPN_IPSEC_PSK=$(noquotes "$VPN_IPSEC_PSK")
113
+ VPN_USER=$(nospaces "$VPN_USER")
114
+ VPN_USER=$(noquotes "$VPN_USER")
115
+ VPN_PASSWORD=$(nospaces "$VPN_PASSWORD")
116
+ VPN_PASSWORD=$(noquotes "$VPN_PASSWORD")
117
+ if [ -n "$VPN_ADDL_USERS" ] && [ -n "$VPN_ADDL_PASSWORDS" ]; then
118
+ VPN_ADDL_USERS=$(nospaces "$VPN_ADDL_USERS")
119
+ VPN_ADDL_USERS=$(noquotes "$VPN_ADDL_USERS")
120
+ VPN_ADDL_USERS=$(onespace "$VPN_ADDL_USERS")
121
+ VPN_ADDL_USERS=$(noquotes2 "$VPN_ADDL_USERS")
122
+ VPN_ADDL_PASSWORDS=$(nospaces "$VPN_ADDL_PASSWORDS")
123
+ VPN_ADDL_PASSWORDS=$(noquotes "$VPN_ADDL_PASSWORDS")
124
+ VPN_ADDL_PASSWORDS=$(onespace "$VPN_ADDL_PASSWORDS")
125
+ VPN_ADDL_PASSWORDS=$(noquotes2 "$VPN_ADDL_PASSWORDS")
126
+ if [ -n "$VPN_ADDL_IP_ADDRS" ]; then
127
+ VPN_ADDL_IP_ADDRS=$(nospaces "$VPN_ADDL_IP_ADDRS")
128
+ VPN_ADDL_IP_ADDRS=$(noquotes "$VPN_ADDL_IP_ADDRS")
129
+ VPN_ADDL_IP_ADDRS=$(onespace "$VPN_ADDL_IP_ADDRS")
130
+ VPN_ADDL_IP_ADDRS=$(noquotes2 "$VPN_ADDL_IP_ADDRS")
131
+ fi
132
+ else
133
+ VPN_ADDL_USERS=""
134
+ VPN_ADDL_PASSWORDS=""
135
+ VPN_ADDL_IP_ADDRS=""
136
+ fi
137
+ if [ -n "$VPN_DNS_SRV1" ]; then
138
+ VPN_DNS_SRV1=$(nospaces "$VPN_DNS_SRV1")
139
+ VPN_DNS_SRV1=$(noquotes "$VPN_DNS_SRV1")
140
+ fi
141
+ if [ -n "$VPN_DNS_SRV2" ]; then
142
+ VPN_DNS_SRV2=$(nospaces "$VPN_DNS_SRV2")
143
+ VPN_DNS_SRV2=$(noquotes "$VPN_DNS_SRV2")
144
+ fi
145
+ if [ -n "$VPN_CLIENT_NAME" ]; then
146
+ VPN_CLIENT_NAME=$(nospaces "$VPN_CLIENT_NAME")
147
+ VPN_CLIENT_NAME=$(noquotes "$VPN_CLIENT_NAME")
148
+ fi
149
+ if [ -n "$VPN_DNS_NAME" ]; then
150
+ VPN_DNS_NAME=$(nospaces "$VPN_DNS_NAME")
151
+ VPN_DNS_NAME=$(noquotes "$VPN_DNS_NAME")
152
+ fi
153
+ if [ -n "$VPN_PUBLIC_IP" ]; then
154
+ VPN_PUBLIC_IP=$(nospaces "$VPN_PUBLIC_IP")
155
+ VPN_PUBLIC_IP=$(noquotes "$VPN_PUBLIC_IP")
156
+ fi
157
+ if [ -n "$VPN_ANDROID_MTU_FIX" ]; then
158
+ VPN_ANDROID_MTU_FIX=$(nospaces "$VPN_ANDROID_MTU_FIX")
159
+ VPN_ANDROID_MTU_FIX=$(noquotes "$VPN_ANDROID_MTU_FIX")
160
+ fi
161
+ if [ -n "$VPN_SHA2_TRUNCBUG" ]; then
162
+ VPN_SHA2_TRUNCBUG=$(nospaces "$VPN_SHA2_TRUNCBUG")
163
+ VPN_SHA2_TRUNCBUG=$(noquotes "$VPN_SHA2_TRUNCBUG")
164
+ fi
165
+ if [ -n "$VPN_PROTECT_CONFIG" ]; then
166
+ VPN_PROTECT_CONFIG=$(nospaces "$VPN_PROTECT_CONFIG")
167
+ VPN_PROTECT_CONFIG=$(noquotes "$VPN_PROTECT_CONFIG")
168
+ fi
169
+ if [ -n "$VPN_SPLIT_IKEV2" ]; then
170
+ VPN_SPLIT_IKEV2=$(nospaces "$VPN_SPLIT_IKEV2")
171
+ VPN_SPLIT_IKEV2=$(noquotes "$VPN_SPLIT_IKEV2")
172
+ fi
173
+ if [ -n "$VPN_DISABLE_IPSEC_L2TP" ]; then
174
+ VPN_DISABLE_IPSEC_L2TP=$(nospaces "$VPN_DISABLE_IPSEC_L2TP")
175
+ VPN_DISABLE_IPSEC_L2TP=$(noquotes "$VPN_DISABLE_IPSEC_L2TP")
176
+ fi
177
+ if [ -n "$VPN_DISABLE_IPSEC_XAUTH" ]; then
178
+ VPN_DISABLE_IPSEC_XAUTH=$(nospaces "$VPN_DISABLE_IPSEC_XAUTH")
179
+ VPN_DISABLE_IPSEC_XAUTH=$(noquotes "$VPN_DISABLE_IPSEC_XAUTH")
180
+ fi
181
+ if [ -n "$VPN_IKEV2_ONLY" ]; then
182
+ VPN_IKEV2_ONLY=$(nospaces "$VPN_IKEV2_ONLY")
183
+ VPN_IKEV2_ONLY=$(noquotes "$VPN_IKEV2_ONLY")
184
+ fi
185
+ if [ -n "$VPN_ENABLE_MODP1024" ]; then
186
+ VPN_ENABLE_MODP1024=$(nospaces "$VPN_ENABLE_MODP1024")
187
+ VPN_ENABLE_MODP1024=$(noquotes "$VPN_ENABLE_MODP1024")
188
+ fi
189
+ if [ -n "$VPN_ENABLE_MODP1536" ]; then
190
+ VPN_ENABLE_MODP1536=$(nospaces "$VPN_ENABLE_MODP1536")
191
+ VPN_ENABLE_MODP1536=$(noquotes "$VPN_ENABLE_MODP1536")
192
+ fi
193
+ if [ -n "$VPN_L2TP_NET" ]; then
194
+ VPN_L2TP_NET=$(nospaces "$VPN_L2TP_NET")
195
+ VPN_L2TP_NET=$(noquotes "$VPN_L2TP_NET")
196
+ fi
197
+ if [ -n "$VPN_L2TP_LOCAL" ]; then
198
+ VPN_L2TP_LOCAL=$(nospaces "$VPN_L2TP_LOCAL")
199
+ VPN_L2TP_LOCAL=$(noquotes "$VPN_L2TP_LOCAL")
200
+ fi
201
+ if [ -n "$VPN_L2TP_POOL" ]; then
202
+ VPN_L2TP_POOL=$(nospaces "$VPN_L2TP_POOL")
203
+ VPN_L2TP_POOL=$(noquotes "$VPN_L2TP_POOL")
204
+ fi
205
+ if [ -n "$VPN_XAUTH_NET" ]; then
206
+ VPN_XAUTH_NET=$(nospaces "$VPN_XAUTH_NET")
207
+ VPN_XAUTH_NET=$(noquotes "$VPN_XAUTH_NET")
208
+ fi
209
+ if [ -n "$VPN_XAUTH_POOL" ]; then
210
+ VPN_XAUTH_POOL=$(nospaces "$VPN_XAUTH_POOL")
211
+ VPN_XAUTH_POOL=$(noquotes "$VPN_XAUTH_POOL")
212
+ fi
213
+
214
+ if [ -z "$VPN_IPSEC_PSK" ] || [ -z "$VPN_USER" ] || [ -z "$VPN_PASSWORD" ]; then
215
+ exiterr "All VPN credentials must be specified. Edit your 'env' file and re-enter them."
216
+ fi
217
+ if printf '%s' "$VPN_IPSEC_PSK $VPN_USER $VPN_PASSWORD $VPN_ADDL_USERS $VPN_ADDL_PASSWORDS" | LC_ALL=C grep -q '[^ -~]\+'; then
218
+ exiterr "VPN credentials must not contain non-ASCII characters."
219
+ fi
220
+ case "$VPN_IPSEC_PSK $VPN_USER $VPN_PASSWORD $VPN_ADDL_USERS $VPN_ADDL_PASSWORDS" in
221
+ *[\\\"\']*)
222
+ exiterr "VPN credentials must not contain these special characters: \\ \" '"
223
+ ;;
224
+ esac
225
+ if printf '%s' "$VPN_USER $VPN_ADDL_USERS" | tr ' ' '\n' | sort | uniq -c | grep -qv '^ *1 '; then
226
+ exiterr "VPN usernames must not contain duplicates."
227
+ fi
228
+
229
+ # Check DNS servers and try to resolve hostnames to IPs
230
+ if [ -n "$VPN_DNS_SRV1" ]; then
231
+ check_ip "$VPN_DNS_SRV1" || VPN_DNS_SRV1=$(dig -t A -4 +short "$VPN_DNS_SRV1")
232
+ if ! check_ip "$VPN_DNS_SRV1"; then
233
+ cat <<'EOF'
234
+
235
+ Warning: Invalid DNS server. Check VPN_DNS_SRV1 in your 'env' file.
236
+ EOF
237
+ VPN_DNS_SRV1=""
238
+ fi
239
+ fi
240
+ if [ -n "$VPN_DNS_SRV2" ]; then
241
+ check_ip "$VPN_DNS_SRV2" || VPN_DNS_SRV2=$(dig -t A -4 +short "$VPN_DNS_SRV2")
242
+ if ! check_ip "$VPN_DNS_SRV2"; then
243
+ cat <<'EOF'
244
+
245
+ Warning: Invalid DNS server. Check VPN_DNS_SRV2 in your 'env' file.
246
+ EOF
247
+ VPN_DNS_SRV2=""
248
+ fi
249
+ fi
250
+ if [ -n "$VPN_CLIENT_NAME" ]; then
251
+ if ! check_client_name "$VPN_CLIENT_NAME"; then
252
+ cat <<'EOF'
253
+
254
+ Warning: Invalid client name. Use one word only, no special characters except '-' and '_'.
255
+ Falling back to default client name 'vpnclient'.
256
+ EOF
257
+ VPN_CLIENT_NAME=""
258
+ fi
259
+ fi
260
+ if [ -n "$VPN_DNS_NAME" ]; then
261
+ if ! check_dns_name "$VPN_DNS_NAME"; then
262
+ cat <<'EOF'
263
+
264
+ Warning: Invalid DNS name. 'VPN_DNS_NAME' must be a fully qualified domain name (FQDN).
265
+ Falling back to using this server's IP address.
266
+ EOF
267
+ VPN_DNS_NAME=""
268
+ fi
269
+ fi
270
+ if [ -n "$VPN_SPLIT_IKEV2" ]; then
271
+ if ! check_cidr "$VPN_SPLIT_IKEV2"; then
272
+ cat <<'EOF'
273
+
274
+ Warning: Invalid split VPN subnet. Check VPN_SPLIT_IKEV2 in your 'env' file.
275
+ EOF
276
+ VPN_SPLIT_IKEV2=""
277
+ fi
278
+ fi
279
+
280
+ echo
281
+ echo 'Trying to auto discover IP of this server...'
282
+ # In case auto IP discovery fails, manually define the public IP
283
+ # of this server in your 'env' file, as variable 'VPN_PUBLIC_IP'.
284
+ public_ip=${VPN_PUBLIC_IP:-''}
285
+ check_ip "$public_ip" || public_ip=$(dig @resolver1.opendns.com -t A -4 myip.opendns.com +short)
286
+ check_ip "$public_ip" || public_ip=$(wget -t 2 -T 10 -qO- http://ipv4.icanhazip.com)
287
+ check_ip "$public_ip" || public_ip=$(wget -t 2 -T 10 -qO- http://ip1.dynupdate.no-ip.com)
288
+ check_ip "$public_ip" || exiterr "Cannot detect this server's public IP. Define it in your 'env' file as 'VPN_PUBLIC_IP'."
289
+
290
+ if [ -n "$VPN_DNS_NAME" ]; then
291
+ server_addr="$VPN_DNS_NAME"
292
+ else
293
+ server_addr="$public_ip"
294
+ fi
295
+
296
+ L2TP_NET=${VPN_L2TP_NET:-'192.168.42.0/24'}
297
+ L2TP_LOCAL=${VPN_L2TP_LOCAL:-'192.168.42.1'}
298
+ L2TP_POOL=${VPN_L2TP_POOL:-'192.168.42.10-192.168.42.250'}
299
+ XAUTH_NET=${VPN_XAUTH_NET:-'192.168.43.0/24'}
300
+ XAUTH_POOL=${VPN_XAUTH_POOL:-'192.168.43.10-192.168.43.250'}
301
+ DNS_SRV1=${VPN_DNS_SRV1:-'8.8.8.8'}
302
+ DNS_SRV2=${VPN_DNS_SRV2:-'8.8.4.4'}
303
+ DNS_SRVS="\"$DNS_SRV1 $DNS_SRV2\""
304
+ [ -n "$VPN_DNS_SRV1" ] && [ -z "$VPN_DNS_SRV2" ] && DNS_SRVS="$DNS_SRV1"
305
+
306
+ if [ -n "$VPN_DNS_SRV1" ] && [ -n "$VPN_DNS_SRV2" ]; then
307
+ echo
308
+ echo "Setting DNS servers to $VPN_DNS_SRV1 and $VPN_DNS_SRV2..."
309
+ elif [ -n "$VPN_DNS_SRV1" ]; then
310
+ echo
311
+ echo "Setting DNS server to $VPN_DNS_SRV1..."
312
+ fi
313
+
314
+ sha2_truncbug=no
315
+ case $VPN_SHA2_TRUNCBUG in
316
+ [yY][eE][sS])
317
+ echo
318
+ echo "Setting sha2-truncbug to yes in ipsec.conf..."
319
+ sha2_truncbug=yes
320
+ ;;
321
+ esac
322
+ disable_ipsec_l2tp=no
323
+ case $VPN_DISABLE_IPSEC_L2TP in
324
+ [yY][eE][sS])
325
+ disable_ipsec_l2tp=yes
326
+ ;;
327
+ esac
328
+ disable_ipsec_xauth=no
329
+ case $VPN_DISABLE_IPSEC_XAUTH in
330
+ [yY][eE][sS])
331
+ disable_ipsec_xauth=yes
332
+ ;;
333
+ esac
334
+ case $VPN_IKEV2_ONLY in
335
+ [yY][eE][sS])
336
+ disable_ipsec_l2tp=yes
337
+ disable_ipsec_xauth=yes
338
+ ;;
339
+ esac
340
+ ike_algs="aes256-sha2;modp2048,aes128-sha2;modp2048,aes256-sha1;modp2048,aes128-sha1;modp2048"
341
+ ike_algs_addl_1=",aes256-sha2;modp1024,aes128-sha1;modp1024"
342
+ ike_algs_addl_2=",aes256-sha2;modp1536,aes128-sha1;modp1536"
343
+ case $VPN_ENABLE_MODP1024 in
344
+ [yY][eE][sS])
345
+ echo
346
+ echo "Enabling modp1024 in ipsec.conf..."
347
+ ike_algs="$ike_algs$ike_algs_addl_1"
348
+ ;;
349
+ esac
350
+ case $VPN_ENABLE_MODP1536 in
351
+ [yY][eE][sS])
352
+ echo
353
+ echo "Enabling modp1536 in ipsec.conf..."
354
+ ike_algs="$ike_algs$ike_algs_addl_2"
355
+ ;;
356
+ esac
357
+
358
+ if [ "$disable_ipsec_l2tp" = yes ] && [ "$disable_ipsec_xauth" = yes ]; then
359
+ cat <<'EOF'
360
+
361
+ Note: Running in IKEv2-only mode via env file option.
362
+ IPsec/L2TP and IPsec/XAuth ("Cisco IPsec") modes are disabled.
363
+ EOF
364
+ if ! grep -q " /etc/ipsec.d " /proc/mounts; then
365
+ cat <<'EOF'
366
+
367
+ Warning: /etc/ipsec.d not mounted. IKEv2 setup requires a Docker volume
368
+ mounted at /etc/ipsec.d.
369
+ EOF
370
+ fi
371
+ elif [ "$disable_ipsec_l2tp" = yes ]; then
372
+ cat <<'EOF'
373
+
374
+ Note: IPsec/L2TP mode is disabled via env file option.
375
+ EOF
376
+ elif [ "$disable_ipsec_xauth" = yes ]; then
377
+ cat <<'EOF'
378
+
379
+ Note: IPsec/XAuth ("Cisco IPsec") mode is disabled via env file option.
380
+ EOF
381
+ fi
382
+
383
+ # Create IPsec config
384
+ cat > /etc/ipsec.conf <<EOF
385
+ version 2.0
386
+
387
+ config setup
388
+ ikev1-policy=accept
389
+ virtual-private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12,%v4:!$L2TP_NET,%v4:!$XAUTH_NET
390
+ uniqueids=no
391
+
392
+ conn shared
393
+ left=%defaultroute
394
+ leftid=$public_ip
395
+ right=%any
396
+ encapsulation=yes
397
+ authby=secret
398
+ pfs=no
399
+ rekey=no
400
+ dpddelay=30
401
+ dpdtimeout=300
402
+ ikev2=never
403
+ ike=$ike_algs
404
+ phase2alg=aes_gcm-null,aes128-sha1,aes256-sha1,aes256-sha2_512,aes128-sha2,aes256-sha2
405
+ ikelifetime=24h
406
+ salifetime=24h
407
+ sha2-truncbug=$sha2_truncbug
408
+
409
+ EOF
410
+
411
+ if [ "$disable_ipsec_l2tp" != yes ]; then
412
+ cat >> /etc/ipsec.conf <<'EOF'
413
+ conn l2tp-psk
414
+ auto=add
415
+ leftprotoport=17/1701
416
+ rightprotoport=17/%any
417
+ type=transport
418
+ also=shared
419
+
420
+ EOF
421
+ fi
422
+ if [ "$disable_ipsec_xauth" != yes ]; then
423
+ cat >> /etc/ipsec.conf <<EOF
424
+ conn xauth-psk
425
+ auto=add
426
+ leftsubnet=0.0.0.0/0
427
+ rightaddresspool=$XAUTH_POOL
428
+ modecfgdns=$DNS_SRVS
429
+ leftxauthserver=yes
430
+ rightxauthclient=yes
431
+ leftmodecfgserver=yes
432
+ rightmodecfgclient=yes
433
+ modecfgpull=yes
434
+ cisco-unity=yes
435
+ also=shared
436
+
437
+ EOF
438
+ fi
439
+
440
+ cat >> /etc/ipsec.conf <<'EOF'
441
+ include /etc/ipsec.d/*.conf
442
+ EOF
443
+
444
+ if uname -r | grep -qi 'coreos'; then
445
+ sed -i '/phase2alg/s/,aes256-sha2_512//' /etc/ipsec.conf
446
+ fi
447
+ if grep -qs ike-frag /etc/ipsec.d/ikev2.conf; then
448
+ sed -i 's/^[[:space:]]\+ike-frag=/ fragmentation=/' /etc/ipsec.d/ikev2.conf
449
+ fi
450
+
451
+ # Specify IPsec PSK
452
+ cat > /etc/ipsec.secrets <<EOF
453
+ %any %any : PSK "$VPN_IPSEC_PSK"
454
+ EOF
455
+
456
+ # Create xl2tpd config
457
+ cat > /etc/xl2tpd/xl2tpd.conf <<EOF
458
+ [global]
459
+ port = 1701
460
+
461
+ [lns default]
462
+ ip range = $L2TP_POOL
463
+ local ip = $L2TP_LOCAL
464
+ require chap = yes
465
+ refuse pap = yes
466
+ require authentication = yes
467
+ name = l2tpd
468
+ pppoptfile = /etc/ppp/options.xl2tpd
469
+ length bit = yes
470
+ EOF
471
+
472
+ # Set xl2tpd options
473
+ cat > /etc/ppp/options.xl2tpd <<EOF
474
+ +mschap-v2
475
+ ipcp-accept-local
476
+ ipcp-accept-remote
477
+ noccp
478
+ auth
479
+ mtu 1280
480
+ mru 1280
481
+ proxyarp
482
+ lcp-echo-failure 4
483
+ lcp-echo-interval 30
484
+ connect-delay 5000
485
+ ms-dns $DNS_SRV1
486
+ EOF
487
+
488
+ if [ -z "$VPN_DNS_SRV1" ] || [ -n "$VPN_DNS_SRV2" ]; then
489
+ cat >> /etc/ppp/options.xl2tpd <<EOF
490
+ ms-dns $DNS_SRV2
491
+ EOF
492
+ fi
493
+
494
+ # Create VPN credentials
495
+ cat > /etc/ppp/chap-secrets <<EOF
496
+ "$VPN_USER" l2tpd "$VPN_PASSWORD" *
497
+ EOF
498
+
499
+ VPN_PASSWORD_ENC=$(openssl passwd -1 "$VPN_PASSWORD")
500
+ cat > /etc/ipsec.d/passwd <<EOF
501
+ $VPN_USER:$VPN_PASSWORD_ENC:xauth-psk
502
+ EOF
503
+
504
+ if [ -n "$VPN_ADDL_USERS" ] && [ -n "$VPN_ADDL_PASSWORDS" ]; then
505
+ count=1
506
+ addl_user=$(printf '%s' "$VPN_ADDL_USERS" | cut -d ' ' -f 1)
507
+ addl_password=$(printf '%s' "$VPN_ADDL_PASSWORDS" | cut -d ' ' -f 1)
508
+ addl_ip=$(printf '%s' "$VPN_ADDL_IP_ADDRS" | cut -d ' ' -f 1)
509
+ while [ -n "$addl_user" ] && [ -n "$addl_password" ]; do
510
+ addl_ip_l2tp="$addl_ip"
511
+ addl_ip_xauth="$addl_ip"
512
+ if [ "$addl_ip" = "*" ] || ! check_ip "$addl_ip"; then
513
+ addl_ip_l2tp=""
514
+ addl_ip_xauth=""
515
+ elif [ "$L2TP_NET" = "192.168.42.0/24" ] && [ "$XAUTH_NET" = "192.168.43.0/24" ]; then
516
+ addl_ip_part=$(printf '%s' "$addl_ip" | cut -f 1-3 -d '.')
517
+ if [ "$addl_ip_part" = "192.168.42" ]; then
518
+ addl_ip_xauth=""
519
+ elif [ "$addl_ip_part" = "192.168.43" ]; then
520
+ addl_ip_l2tp=""
521
+ else
522
+ addl_ip_l2tp=""
523
+ addl_ip_xauth=""
524
+ fi
525
+ fi
526
+ cat >> /etc/ppp/chap-secrets <<EOF
527
+ "$addl_user" l2tpd "$addl_password" ${addl_ip_l2tp:-*}
528
+ EOF
529
+ [ -n "$addl_ip_xauth" ] && addl_ip_xauth=$(printf '%s' ":$addl_ip_xauth")
530
+ addl_password_enc=$(openssl passwd -1 "$addl_password")
531
+ cat >> /etc/ipsec.d/passwd <<EOF
532
+ $addl_user:$addl_password_enc:xauth-psk${addl_ip_xauth}
533
+ EOF
534
+ count=$((count+1))
535
+ addl_user=$(printf '%s' "$VPN_ADDL_USERS" | cut -s -d ' ' -f "$count")
536
+ addl_password=$(printf '%s' "$VPN_ADDL_PASSWORDS" | cut -s -d ' ' -f "$count")
537
+ addl_ip=$(printf '%s' "$VPN_ADDL_IP_ADDRS" | cut -s -d ' ' -f "$count")
538
+ done
539
+ fi
540
+
541
+ # Update sysctl settings
542
+ syt='/sbin/sysctl -e -q -w'
543
+ $syt kernel.msgmnb=65536 2>/dev/null
544
+ $syt kernel.msgmax=65536 2>/dev/null
545
+ $syt net.ipv4.ip_forward=1 2>/dev/null
546
+ $syt net.ipv4.conf.all.accept_redirects=0 2>/dev/null
547
+ $syt net.ipv4.conf.all.send_redirects=0 2>/dev/null
548
+ $syt net.ipv4.conf.all.rp_filter=0 2>/dev/null
549
+ $syt net.ipv4.conf.default.accept_redirects=0 2>/dev/null
550
+ $syt net.ipv4.conf.default.send_redirects=0 2>/dev/null
551
+ $syt net.ipv4.conf.default.rp_filter=0 2>/dev/null
552
+ $syt "net.ipv4.conf.$NET_IFACE.send_redirects=0" 2>/dev/null
553
+ $syt "net.ipv4.conf.$NET_IFACE.rp_filter=0" 2>/dev/null
554
+ $syt net.ipv4.tcp_rmem="4096 87380 16777216" 2>/dev/null
555
+ $syt net.ipv4.tcp_wmem="4096 87380 16777216" 2>/dev/null
556
+ if modprobe -q tcp_bbr 2>/dev/null \
557
+ && printf '%s\n%s' "4.20" "$(uname -r)" | sort -C -V; then
558
+ $syt net.ipv4.tcp_congestion_control=bbr 2>/dev/null
559
+ fi
560
+
561
+ # Create IPTables rules
562
+ ipi='iptables -I INPUT'
563
+ ipf='iptables -I FORWARD'
564
+ ipp='iptables -t nat -I POSTROUTING'
565
+ res='RELATED,ESTABLISHED'
566
+ modprobe -q ip_tables 2>/dev/null
567
+ if ! iptables -t nat -C POSTROUTING -s "$L2TP_NET" -o "$NET_IFACE" -j MASQUERADE 2>/dev/null; then
568
+ $ipi 1 -p udp --dport 1701 -m policy --dir in --pol none -j DROP
569
+ $ipi 2 -m conntrack --ctstate INVALID -j DROP
570
+ $ipi 3 -m conntrack --ctstate "$res" -j ACCEPT
571
+ $ipi 4 -p udp -m multiport --dports 500,4500 -j ACCEPT
572
+ $ipi 5 -p udp --dport 1701 -m policy --dir in --pol ipsec -j ACCEPT
573
+ $ipi 6 -p udp --dport 1701 -j DROP
574
+ $ipf 1 -m conntrack --ctstate INVALID -j DROP
575
+ $ipf 2 -i "$NET_IFACE" -o ppp+ -m conntrack --ctstate "$res" -j ACCEPT
576
+ $ipf 3 -i ppp+ -o "$NET_IFACE" -j ACCEPT
577
+ $ipf 4 -i ppp+ -o ppp+ -j ACCEPT
578
+ $ipf 5 -i "$NET_IFACE" -d "$XAUTH_NET" -m conntrack --ctstate "$res" -j ACCEPT
579
+ $ipf 6 -s "$XAUTH_NET" -o "$NET_IFACE" -j ACCEPT
580
+ $ipf 7 -s "$XAUTH_NET" -o ppp+ -j ACCEPT
581
+ # Client-to-client traffic is allowed by default. To *disallow* such traffic,
582
+ # uncomment below and restart the Docker container.
583
+ # $ipf 2 -i ppp+ -o ppp+ -s "$L2TP_NET" -d "$L2TP_NET" -j DROP
584
+ # $ipf 3 -s "$XAUTH_NET" -d "$XAUTH_NET" -j DROP
585
+ # $ipf 4 -i ppp+ -d "$XAUTH_NET" -j DROP
586
+ # $ipf 5 -s "$XAUTH_NET" -o ppp+ -j DROP
587
+ iptables -A FORWARD -j DROP
588
+ if ! $ipp -s "$XAUTH_NET" -o "$NET_IFACE" -m policy --dir out --pol none -j MASQUERADE; then
589
+ $ipp -s "$XAUTH_NET" -o "$NET_IFACE" ! -d "$XAUTH_NET" -j MASQUERADE
590
+ fi
591
+ $ipp -s "$L2TP_NET" -o "$NET_IFACE" -j MASQUERADE
592
+ fi
593
+
594
+ case $VPN_ANDROID_MTU_FIX in
595
+ [yY][eE][sS])
596
+ echo
597
+ echo "Applying fix for Android MTU/MSS issues..."
598
+ iptables -t mangle -A FORWARD -m policy --pol ipsec --dir in \
599
+ -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss --mss 1361:1536 \
600
+ -j TCPMSS --set-mss 1360
601
+ iptables -t mangle -A FORWARD -m policy --pol ipsec --dir out \
602
+ -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss --mss 1361:1536 \
603
+ -j TCPMSS --set-mss 1360
604
+ echo 1 > /proc/sys/net/ipv4/ip_no_pmtu_disc
605
+ ;;
606
+ esac
607
+
608
+ # Update file attributes
609
+ chmod 600 /etc/ipsec.secrets /etc/ppp/chap-secrets /etc/ipsec.d/passwd
610
+
611
+ echo
612
+ echo "Starting IPsec service..."
613
+ mkdir -p /run/pluto /var/run/pluto
614
+ rm -f /run/pluto/pluto.pid /var/run/pluto/pluto.pid
615
+ if [ "$os_type" = "alpine" ]; then
616
+ sed -i '1c\#!/sbin/openrc-run' /etc/init.d/ipsec
617
+ rc-status >/dev/null 2>&1
618
+ rc-service ipsec zap >/dev/null
619
+ rc-service -D ipsec start >/dev/null 2>&1
620
+ mkdir -p /etc/crontabs
621
+ cron_cmd="rc-service -c -D ipsec zap start"
622
+ if ! grep -qs "$cron_cmd" /etc/crontabs/root; then
623
+ cat >> /etc/crontabs/root <<EOF
624
+ * * * * * $cron_cmd
625
+ * * * * * sleep 15; $cron_cmd
626
+ * * * * * sleep 30; $cron_cmd
627
+ * * * * * sleep 45; $cron_cmd
628
+ EOF
629
+ fi
630
+ /usr/sbin/crond -L /dev/null
631
+ else
632
+ service ipsec start >/dev/null 2>&1
633
+ fi
634
+
635
+ if [ -n "$VPN_DNS_NAME" ]; then
636
+ server_text="Server"
637
+ else
638
+ server_text="Server IP"
639
+ fi
640
+
641
+ if [ "$disable_ipsec_l2tp" != yes ] || [ "$disable_ipsec_xauth" != yes ]; then
642
+ cat <<EOF
643
+
644
+ ================================================
645
+
646
+ IPsec VPN server is now ready for use!
647
+
648
+ Connect to your new VPN with these details:
649
+
650
+ $server_text: $server_addr
651
+ IPsec PSK: $VPN_IPSEC_PSK
652
+ Username: $VPN_USER
653
+ Password: $VPN_PASSWORD
654
+ EOF
655
+ if [ -n "$VPN_ADDL_USERS" ] && [ -n "$VPN_ADDL_PASSWORDS" ]; then
656
+ count=1
657
+ addl_user=$(printf '%s' "$VPN_ADDL_USERS" | cut -d ' ' -f 1)
658
+ addl_password=$(printf '%s' "$VPN_ADDL_PASSWORDS" | cut -d ' ' -f 1)
659
+ cat <<'EOF'
660
+
661
+ Additional VPN users (username | password):
662
+ EOF
663
+ while [ -n "$addl_user" ] && [ -n "$addl_password" ]; do
664
+ cat <<EOF
665
+ $addl_user | $addl_password
666
+ EOF
667
+ count=$((count+1))
668
+ addl_user=$(printf '%s' "$VPN_ADDL_USERS" | cut -s -d ' ' -f "$count")
669
+ addl_password=$(printf '%s' "$VPN_ADDL_PASSWORDS" | cut -s -d ' ' -f "$count")
670
+ done
671
+ fi
672
+ cat <<'EOF'
673
+
674
+ Write these down. You'll need them to connect!
675
+
676
+ VPN client setup: https://vpnsetup.net/clients2
677
+
678
+ ================================================
679
+ EOF
680
+ fi
681
+
682
+ # Set up IKEv2
683
+ status=0
684
+ ikev2_sh="/opt/src/ikev2.sh"
685
+ ikev2_conf="/etc/ipsec.d/ikev2.conf"
686
+ ikev2_log="/etc/ipsec.d/ikev2setup.log"
687
+ if grep -q " /etc/ipsec.d " /proc/mounts && [ -s "$ikev2_sh" ] && [ ! -f "$ikev2_conf" ]; then
688
+ echo
689
+ echo "Setting up IKEv2. This may take a few moments..."
690
+ if [ -n "$VPN_SPLIT_IKEV2" ]; then
691
+ sed -i "s|^ leftsubnet=0\.0\.0\.0/0$| leftsubnet=$VPN_SPLIT_IKEV2|g" "$ikev2_sh"
692
+ fi
693
+ if VPN_DNS_NAME="$VPN_DNS_NAME" VPN_PUBLIC_IP="$public_ip" \
694
+ VPN_CLIENT_NAME="$VPN_CLIENT_NAME" VPN_XAUTH_POOL="$VPN_XAUTH_POOL" \
695
+ VPN_DNS_SRV1="$VPN_DNS_SRV1" VPN_DNS_SRV2="$VPN_DNS_SRV2" \
696
+ VPN_PROTECT_CONFIG="$VPN_PROTECT_CONFIG" \
697
+ /bin/bash "$ikev2_sh" --auto >"$ikev2_log" 2>&1; then
698
+ status=1
699
+ status_text="IKEv2 setup successful."
700
+ else
701
+ status=4
702
+ rm -f "$ikev2_conf"
703
+ echo "IKEv2 setup failed."
704
+ fi
705
+ chmod 600 "$ikev2_log"
706
+ fi
707
+ if [ "$status" = 0 ] && [ -f "$ikev2_conf" ] && [ -s "$ikev2_log" ]; then
708
+ status=2
709
+ status_text="IKEv2 is already set up."
710
+ fi
711
+ if [ "$status" = 1 ] || [ "$status" = 2 ]; then
712
+ cat <<EOF
713
+
714
+ ================================================
715
+
716
+ $status_text Details for IKEv2 mode:
717
+
718
+ EOF
719
+ sed -n '/VPN server address:/,/Next steps:/p' "$ikev2_log"
720
+ cat <<'EOF'
721
+ https://vpnsetup.net/clients2
722
+
723
+ ================================================
724
+
725
+ EOF
726
+ else
727
+ echo
728
+ fi
729
+
730
+ if [ "$status" = 2 ] && [ -n "$VPN_DNS_NAME" ]; then
731
+ server_addr_cur=$(grep -s "leftcert=" /etc/ipsec.d/ikev2.conf | cut -f2 -d= | head -n 1)
732
+ if [ "$VPN_DNS_NAME" != "$server_addr_cur" ]; then
733
+ cat <<'EOF'
734
+ Warning: The VPN_DNS_NAME variable you specified has no effect
735
+ for IKEv2 mode, because IKEv2 is already set up in this
736
+ container. To change the IKEv2 server address, see:
737
+ https://vpnsetup.net/ikev2docker
738
+
739
+ EOF
740
+ fi
741
+ fi
742
+
743
+ # Check for new Libreswan version
744
+ ts_file="/opt/src/swanver"
745
+ if [ ! -f "$ts_file" ] || [ "$(find "$ts_file" -mmin +10080)" ]; then
746
+ touch "$ts_file"
747
+ ipsec_ver=$(ipsec --version 2>/dev/null)
748
+ swan_ver=$(printf '%s' "$ipsec_ver" | sed -e 's/.*Libreswan U\?//' -e 's/\( (\|\/K\).*//')
749
+ base_url="https://github.com/hwdsl2/vpn-extras/releases/download/v1.0.0"
750
+ swan_ver_url="$base_url/upg-docker-$os_type-$os_arch-swanver"
751
+ swan_ver_latest=$(wget -t 2 -T 10 -qO- "$swan_ver_url" | head -n 1)
752
+ if printf '%s' "$swan_ver_latest" | grep -Eq '^([3-9]|[1-9][0-9]{1,2})(\.([0-9]|[1-9][0-9]{1,2})){1,2}$' \
753
+ && [ -n "$swan_ver" ] && [ "$swan_ver" != "$swan_ver_latest" ] \
754
+ && printf '%s\n%s' "$swan_ver" "$swan_ver_latest" | sort -C -V; then
755
+ cat <<EOF
756
+ Note: A newer version of Libreswan ($swan_ver_latest) is available.
757
+ To update this Docker image, see: https://vpnsetup.net/dockerupdate
758
+
759
+ EOF
760
+ fi
761
+ fi
762
+
763
+ # Start xl2tpd
764
+ mkdir -p /var/run/xl2tpd
765
+ rm -f /var/run/xl2tpd.pid
766
+ exec /usr/sbin/xl2tpd -D -c /etc/xl2tpd/xl2tpd.conf