Add ip output.

This commit is contained in:
redxef 2021-08-07 20:36:47 +02:00
parent e870639b5f
commit 8ab77a16a6
2 changed files with 40 additions and 6 deletions

View file

@ -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 \ ADD https://raw.githubusercontent.com/alfredopalhares/openvpn-update-resolv-conf/master/update-resolv-conf.sh \
/etc/openvpn/update-resolv-conf /etc/openvpn/update-resolv-conf
COPY start-openvpn.sh /usr/local/bin/start-openvpn.sh COPY start-openvpn.sh /usr/local/bin/start-openvpn.sh
RUN chmod +x /etc/openvpn/update-resolv-conf RUN chmod +x /etc/openvpn/update-resolv-conf \
RUN chmod +x /usr/local/bin/start-openvpn.sh && chmod +x /usr/local/bin/start-openvpn.sh
ENTRYPOINT [ "start-openvpn.sh" ] ENTRYPOINT [ "start-openvpn.sh" ]

View file

@ -1,4 +1,10 @@
#!/usr/bin/env sh #!/usr/bin/env bash
set -o pipefail
change_hostname() {
echo VPNDOCKER > /etc/hostname
}
create_net_dev() { create_net_dev() {
mkdir -p /dev/net mkdir -p /dev/net
@ -10,11 +16,39 @@ connect_vpn() {
openvpn --config "$OVPN_CONFIGURATION_FILE" --daemon openvpn --config "$OVPN_CONFIGURATION_FILE" --daemon
} }
switch_user() { get_ip() {
cd "$OVPN_HOME" nsout="$(nslookup myip.opendns.com resolver1.opendns.com)"
su "$OVPN_USER" 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 create_net_dev
connect_vpn 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 switch_user