Skip to content

Commit

Permalink
Merge pull request dperson#264 from StudioEtrange/add-credentials-com…
Browse files Browse the repository at this point in the history
…mandline

add command line and env var to set vpn username and password
  • Loading branch information
David Personette authored Apr 17, 2020
2 parents 97ae576 + 5814e93 commit 1991808
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,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 @@ -153,6 +154,7 @@ ENVIRONMENT VARIABLES
* `ROUTE` - As above (-r) add a route to allow replies to your private network
* `TZ` - Set a timezone, IE `EST5EDT`
* `VPN` - As above (-v) setup a VPN connection
* `VPN_AUTH` - As above (-a) provide authentication to vpn server
* `VPNPORT` - As above (-p) setup port forwarding (See NOTE below)
* `GROUPID` - Set the GID for the vpn

Expand Down Expand Up @@ -241,6 +243,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
22 changes: 19 additions & 3 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 @@ -262,6 +275,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 @@ -277,9 +291,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:o:p:R:r:v:" opt; do
while getopts ":hc:df:a:m:o: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 @@ -308,6 +324,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 ${OTHER_ARGS:-} \
${MSS:+--fragment $MSS --mssfix}"
exec sg vpn -c "openvpn --cd $dir --config $conf ${AUTH_COMMAND:-} \
${OTHER_ARGS:-} ${MSS:+--fragment $MSS --mssfix}"
fi

0 comments on commit 1991808

Please sign in to comment.