Skip to content

Commit

Permalink
add command line and env var to set vpn username and password while u…
Browse files Browse the repository at this point in the history
…sing your own openvpn conf
  • Loading branch information
StudioEtrange committed Apr 16, 2020
1 parent c26f35b commit 0e86745
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ the second container (that's what `--net=container:vpn` does).
-c '<passwd>' Configure an authentication password to open the cert
required arg: '<passwd>'
<passwd> password to access the certificate file
-a '<user;password>' Configure authentication username and password
-d Use the VPN provider's DNS resolvers
-f '[port]' Firewall rules so that only the VPN and DNS are allowed to
send internet traffic (IE if VPN is down it's offline)
Expand Down Expand Up @@ -145,6 +146,7 @@ ENVIRONMENT VARIABLES
* `ROUTE` - As above, add a route to allow replies to your private network
* `TZ` - Set a timezone, IE `EST5EDT`
* `VPN` - As above, setup a VPN connection
* `VPN_AUTH` - As above, provide authentication to vpn server
* `VPNPORT` - As above, setup port forwarding (See NOTE below)
* `GROUPID` - Set the GID for the vpn

Expand Down Expand Up @@ -231,6 +233,14 @@ The vpn.conf should look like this:
persist-key
persist-tun

### Run with openvpn client configuration and provided auth

In case you want to use your client configuration in /vpn named vpn.conf
but adding your vpn user and password by command line

sudo docker run -it --cap-add=NET_ADMIN --device /dev/net/tun --name vpn \
-v /some/path:/vpn -d dperson/openvpn-client -a 'username;password'

# User Feedback

## Issues
Expand Down
20 changes: 18 additions & 2 deletions openvpn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ return_route() { local network="$1" gw="$(ip route |awk '/default/ {print $3}')"
[[ -e $route ]] && grep -q "^$network\$" $route || echo "$network" >>$route
}

### vpn_auth: configure authentication username and password
# Arguments:
# user) user name on VPN
# pass) password on VPN
# Return: configured auth file
vpn_auth() { local user="$1" pass="$2"

echo "$user" >$auth
echo "$pass" >>$auth
chmod 0600 $auth
}

### vpn: setup openvpn client
# Arguments:
# server) VPN GW server
Expand Down Expand Up @@ -217,6 +229,7 @@ Options (fields in '[]' are optional, '<>' are required):
-c '<passwd>' Configure an authentication password to open the cert
required arg: '<passwd>'
<passwd> password to access the certificate file
-a '<user;password>' Configure authentication username and password
-d Use the VPN provider's DNS resolvers
-f '[port]' Firewall rules so that only the VPN and DNS are allowed to
send internet traffic (IE if VPN is down it's offline)
Expand Down Expand Up @@ -258,6 +271,7 @@ route6="$dir/.firewall6"
[[ -f $cert ]] || { [[ $(ls -d $dir/* | egrep '\.ce?rt$' 2>&- | wc -w) -eq 1 \
]] && cert="$(ls -d $dir/* | egrep '\.ce?rt$' 2>&-)"; }

[[ "${VPN_AUTH:-""}" ]] && eval vpn_auth $(sed 's/^/"/; s/$/"/; s/;/" "/g' <<< $VPN_AUTH)
[[ "${CERT_AUTH:-""}" ]] && cert_auth "$CERT_AUTH"
[[ "${DNS:-""}" ]] && dns
[[ "${GROUPID:-""}" =~ ^[0-9]+$ ]] && groupmod -g $GROUPID -o vpn
Expand All @@ -273,9 +287,11 @@ while read i; do
eval vpnportforward $(sed 's/^/"/; s/$/"/; s/;/" "/g' <<< $i)
done < <(env | awk '/^VPNPORT[0-9=_]/ {sub (/^[^=]*=/, "", $0); print}')

while getopts ":hc:df:m:p:R:r:v:" opt; do
while getopts ":hc:df:a:m:p:R:r:v:" opt; do
case "$opt" in
h) usage ;;
a) eval vpn_auth $(sed 's/^/"/; s/$/"/; s/;/" "/g' <<< $OPTARG)
AUTH_COMMAND="--auth-user-pass $auth" ;;
c) cert_auth "$OPTARG" ;;
d) dns ;;
f) firewall "$OPTARG"; touch $route $route6 ;;
Expand Down Expand Up @@ -303,6 +319,6 @@ else
[[ -e $conf ]] || { echo "ERROR: VPN not configured!"; sleep 120; }
[[ -e $cert ]] || grep -Eq '^ *(<ca>|ca +)' $conf ||
{ echo "ERROR: VPN CA cert missing!"; sleep 120; }
exec sg vpn -c "openvpn --cd $dir --config $conf \
exec sg vpn -c "openvpn --cd $dir --config $conf ${AUTH_COMMAND:-} \
${MSS:+--fragment $MSS --mssfix}"
fi

0 comments on commit 0e86745

Please sign in to comment.