Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

execute script to update mac system dns servers after openvpn connect… #136

Merged
merged 2 commits into from Feb 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
83 changes: 83 additions & 0 deletions bin/update-resolv-conf
@@ -0,0 +1,83 @@
#!/bin/bash

# Mac name-resolution updater based on @cl's script here:
# https://blog.netnerds.net/2011/10/openvpn-update-client-dns-on-mac-os-x-using-from-the-command-line/
# Openvpn envar parsing taken from the script in debian's openvpn package.
# Smushed together and improved by @andrewgdotcom.

# Parses DHCP options from openvpn to update resolv.conf
# To use set as 'up' and 'down' script in your openvpn *.conf:
# up /etc/openvpn/update-resolv-conf
# down /etc/openvpn/update-resolv-conf
# installation:
# copy update-resolv-conf to myst client current directory
# build/client/ (by default)

[ "$script_type" ] || exit 0
[ "$dev" ] || exit 0

NMSRVRS=()
SRCHS=()
adapters=()

# Set bash delimeter to be line break (temporarily)
IFSSAVE=$IFS
IFS=$'\n'
# Get adapter list
for i in `networksetup -listallnetworkservices |grep -v denotes`; do
adapters=(${adapters[@]} "$i")
done
IFS=$IFSSAVE

split_into_parts()
{
part1="$1"
part2="$2"
part3="$3"
}

update_all_dns()
{
for adapter in "${adapters[@]}"
do
echo updating dns for $adapter
# set dns server to the vpn dns server
if [[ "${SRCHS[@]}" ]]; then
networksetup -setsearchdomains "$adapter" "${SRCHS[@]}"
fi
if [[ "${NMSRVRS[@]}" ]]; then
networksetup -setdnsservers "$adapter" "${NMSRVRS[@]}"
fi
done
}

clear_all_dns()
{
for adapter in "${adapters[@]}"
do
echo updating dns for $adapter
networksetup -setdnsservers "$adapter" empty
networksetup -setsearchdomains "$adapter" empty
done
}

case "$script_type" in
up)
for optionvarname in ${!foreign_option_*} ; do
option="${!optionvarname}"
echo "$option"
split_into_parts $option
if [ "$part1" = "dhcp-option" ] ; then
if [ "$part2" = "DNS" ] ; then
NMSRVRS=(${NMSRVRS[@]} $part3)
elif [ "$part2" = "DOMAIN" ] ; then
SRCHS=(${SRCHS[@]} $part3)
fi
fi
done
update_all_dns
;;
down)
clear_all_dns
;;
esac
1 change: 1 addition & 0 deletions openvpn/client.go
Expand Up @@ -18,6 +18,7 @@ func NewClient(config *ClientConfig, directoryRuntime string, middlewares ...Man
// Add the management interface socketAddress to the config
socketAddress := tempFilename(directoryRuntime, "openvpn-management-", ".sock")
config.SetManagementSocket(socketAddress)
config.SetScripts()

return &openVpnClient{
config: config,
Expand Down
2 changes: 2 additions & 0 deletions openvpn/client_config.go
Expand Up @@ -6,6 +6,8 @@ type ClientConfig struct {

func (c *ClientConfig) SetClientMode(serverIP string, serverPort int) {
c.setFlag("client")
c.setParam("script-security", "2")
c.setFlag("auth-nocache")
c.setParam("remote", serverIP)
c.SetPort(serverPort)
c.setFlag("nobind")
Expand Down
6 changes: 6 additions & 0 deletions openvpn/config.go
@@ -1,6 +1,7 @@
package openvpn

import (
"path/filepath"
"strconv"
)

Expand Down Expand Up @@ -39,6 +40,11 @@ func (c *Config) SetManagementSocket(socketAddress string) {
c.setFlag("management-client")
}

func (c *Config) SetScripts() {
c.setParam("up", "update-resolv-conf")
c.setParam("down", "update-resolv-conf")
}

func (c *Config) SetPort(port int) {
c.setParam("port", strconv.Itoa(port))
}
Expand Down
1 change: 0 additions & 1 deletion openvpn/factory.go
Expand Up @@ -52,7 +52,6 @@ func NewClientConfig(

config.setParam("reneg-sec", "60")
config.setParam("resolv-retry", "infinite")
config.setParam("setenv", "opt block-outside-dns")
config.setParam("redirect-gateway", "def1 bypass-dhcp")
config.setParam("dhcp-option", "DNS 208.67.222.222")
config.setParam("dhcp-option", "DNS 208.67.220.220")
Expand Down