Add ip output.
This commit is contained in:
parent
e870639b5f
commit
8ab77a16a6
2 changed files with 40 additions and 6 deletions
|
@ -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" ]
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue