From 8ab77a16a6cfdbd239a40a4cbc7cca78ddb627fb Mon Sep 17 00:00:00 2001 From: redxef Date: Sat, 7 Aug 2021 20:36:47 +0200 Subject: [PATCH] Add ip output. --- Dockerfile | 4 ++-- start-openvpn.sh | 42 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4a6b668..20ea080 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,6 +4,6 @@ RUN apk add bash openvpn openresolv aria2 ADD https://raw.githubusercontent.com/alfredopalhares/openvpn-update-resolv-conf/master/update-resolv-conf.sh \ /etc/openvpn/update-resolv-conf COPY start-openvpn.sh /usr/local/bin/start-openvpn.sh -RUN chmod +x /etc/openvpn/update-resolv-conf -RUN chmod +x /usr/local/bin/start-openvpn.sh +RUN chmod +x /etc/openvpn/update-resolv-conf \ + && chmod +x /usr/local/bin/start-openvpn.sh ENTRYPOINT [ "start-openvpn.sh" ] diff --git a/start-openvpn.sh b/start-openvpn.sh index 8795120..5e5bc7c 100644 --- a/start-openvpn.sh +++ b/start-openvpn.sh @@ -1,4 +1,10 @@ -#!/usr/bin/env sh +#!/usr/bin/env bash + +set -o pipefail + +change_hostname() { + echo VPNDOCKER > /etc/hostname +} create_net_dev() { mkdir -p /dev/net @@ -10,11 +16,39 @@ connect_vpn() { openvpn --config "$OVPN_CONFIGURATION_FILE" --daemon } -switch_user() { - cd "$OVPN_HOME" - su "$OVPN_USER" +get_ip() { + nsout="$(nslookup myip.opendns.com resolver1.opendns.com)" + if [ "$?" != 0 ]; then + echo "WARN: Failed to run nslookup" >&2 + fi + echo "$nsout" | grep -o 'Address:.*' | grep -Eo '(\d+\.?){4}' | tail -n1 } +print_ip() { + echo "VPN IP: $(get_ip)" +} + +switch_user() { + cd "$OVPN_HOME" || exit 1 + su - "$OVPN_USER" +} + +own_ip="$(get_ip)" +echo "OWN IP: $own_ip" +change_hostname create_net_dev connect_vpn +for _ in {1..50}; do + vpn_ip="$(get_ip)" + if [ "$vpn_ip" = "$own_ip" ]; then + sleep .1 + else + break + fi +done +if [ "$vpn_ip" = "$own_ip" ] || [ "$vpn_ip" = "" ]; then + echo "ERROR: Failed to connect to VPN (vpn_ip=$vpn_ip), aborting" >&2 + exit 1 +fi +echo "VPN IP: $vpn_ip" switch_user