Skip to content

Instantly share code, notes, and snippets.

@mkropat
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkropat/5b3031fbc35f464ebea8 to your computer and use it in GitHub Desktop.
Save mkropat/5b3031fbc35f464ebea8 to your computer and use it in GitHub Desktop.
OpenVPN integration with interfaces(5)
iface tun0 inet manual
pre-up openvpn --mktun --dev tun0
up openvpn --config /etc/openvpn/YOURVPN.ovpn \
--daemon \
--dev tun0 \
--route-noexec \
--script-security 2 \
--route-up /etc/openvpn/on-route-up \
--writepid /var/run/openvpn.tun0
down /etc/openvpn/kill-service -w /var/run/openvpn.tun0
post-down resolvconf -d tun0
post-down openvpn --rmtun --dev tun0
#!/bin/sh
usage() {
echo "Usage: kill-service PID_FILE
Kill a service process given its PID file.
Options:
-w — wait for the process to exit before returning"
}
main() {
local kill_cmd=kill_now
while getopts w opt; do
case "$opt" in
w) kill_cmd=kill_wait ;;
esac
done
shift $(( OPTIND - 1 ))
OPTIND=1
if [ $# -eq 1 ]; then
kill_service "$1"
else
usage
fi
}
kill_service() {
local pid="$(cat "$1")"
if [ -n "$pid" ]; then
"$kill_cmd" "$pid"
rm -f -- "$1"
fi
}
kill_now() {
kill -- "$1"
}
kill_wait() {
kill_now "$1" &&
while ps -p "$1" >/dev/null; do
sleep 1
done
}
main "$@"
#!/bin/sh
usage() {
echo "Usage: on-route-up INTERFACE
Add routes and DNS servers that are pushed from the OpenVPN server. This
script is intended to be run by OpenVPN's \`--route-up\` feature."
}
main() {
if [ "$1" = '-h' ] || [ -z "$dev" ]; then
usage
else
add_routes
configure_dns "$dev"
fi
}
add_routes() {
local n=1
while has_route "$n"; do
add_route "$n"
n=$(( n + 1 ))
done
}
has_route() {
[ -n "$(get_var "route_network_$1")" ]
}
add_route() {
local network="$(get_var "route_network_$n")"
local gateway="$(get_var "route_gateway_$n")"
local netmask="$(get_var "route_netmask_$n")"
ip route add "$network"/"$(mask_to_cidr "$netmask")" via "$gateway"
}
configure_dns() {
get_dns_options | create_resolv_entries | resolvconf -a "$1"
}
get_dns_options() {
local n=1 server
while has_foreign_option "$n"; do
get_var "foreign_option_$n" |
sed -nE 's/^dhcp-option DNS ([0-9.]+)$/\1\n/ p'
n=$(( n + 1 ))
done
}
create_resolv_entries() {
local server
while read -r server; do
echo "nameserver $server"
done
}
has_foreign_option() {
[ -n "$(get_var "foreign_option_$1")" ]
}
get_var() {
eval printf '%s' \"\$"$1"\"
}
mask_to_cidr() {
local IFS=.
set -- $*
echo $(( $(count_bits "$1") + $(count_bits "$2") + $(count_bits "$3") + $(count_bits "$4") ))
}
count_bits() {
case "$1" in
255) echo 8 ;;
254) echo 7 ;;
252) echo 6 ;;
248) echo 5 ;;
240) echo 4 ;;
224) echo 3 ;;
192) echo 2 ;;
128) echo 1 ;;
*) echo 0 ;;
esac
}
main "$@"
The MIT License (MIT)
Copyright (c) 2014 Michael Kropat
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment