view release on metacpan or search on metacpan
wireguard-tools/src/wg-quick/darwin.bash view on Meta::CPAN
local todelete=( ) destination gateway netif
while read -r destination _ _ _ _ netif _; do
[[ $netif == "$REAL_INTERFACE" ]] && todelete+=( "$destination" )
done < <(netstat -nr -f inet)
for destination in "${todelete[@]}"; do
cmd route -q -n delete -inet "$destination" >/dev/null || true
done
todelete=( )
while read -r destination gateway _ netif; do
[[ $netif == "$REAL_INTERFACE" || ( $netif == lo* && $gateway == "$REAL_INTERFACE" ) ]] && todelete+=( "$destination" )
done < <(netstat -nr -f inet6)
for destination in "${todelete[@]}"; do
cmd route -q -n delete -inet6 "$destination" >/dev/null || true
done
for destination in "${ENDPOINTS[@]}"; do
if [[ $destination == *:* ]]; then
cmd route -q -n delete -inet6 "$destination" >/dev/null || true
else
cmd route -q -n delete -inet "$destination" >/dev/null || true
fi
done
}
del_if() {
[[ -z $REAL_INTERFACE ]] || cmd rm -f "/var/run/wireguard/$REAL_INTERFACE.sock"
cmd rm -f "/var/run/wireguard/$INTERFACE.name"
}
up_if() {
cmd ifconfig "$REAL_INTERFACE" up
}
add_addr() {
if [[ $1 == *:* ]]; then
cmd ifconfig "$REAL_INTERFACE" inet6 "$1" alias
else
cmd ifconfig "$REAL_INTERFACE" inet "$1" "${1%%/*}" alias
fi
}
set_mtu() {
# TODO: use better set_mtu algorithm from freebsd.bash
local mtu=0 current_mtu=-1 destination netif defaultif
if [[ -n $MTU ]]; then
cmd ifconfig "$REAL_INTERFACE" mtu "$MTU"
wireguard-tools/src/wg-quick/darwin.bash view on Meta::CPAN
[[ $destination == default && $gateway != "link#"* ]] || continue
GATEWAY4="$gateway"
break
done < <(netstat -nr -f inet)
GATEWAY6=""
while read -r destination gateway _; do
[[ $destination == default && $gateway != "link#"* ]] || continue
GATEWAY6="$gateway"
break
done < <(netstat -nr -f inet6)
}
collect_endpoints() {
ENDPOINTS=( )
while read -r _ endpoint; do
[[ $endpoint =~ ^\[?([a-z0-9:.]+)\]?:[0-9]+$ ]] || continue
ENDPOINTS+=( "${BASH_REMATCH[1]}" )
done < <(wg show "$REAL_INTERFACE" endpoints)
}
wireguard-tools/src/wg-quick/darwin.bash view on Meta::CPAN
if [[ $remove_all_old -eq 1 ]]; then
for endpoint in "${ENDPOINTS[@]}"; do
[[ " ${old_endpoints[*]} " == *" $endpoint "* ]] || old_endpoints+=( "$endpoint" )
done
fi
for endpoint in "${old_endpoints[@]}"; do
[[ $remove_all_old -eq 0 && " ${ENDPOINTS[*]} " == *" $endpoint "* ]] && continue
if [[ $endpoint == *:* && $AUTO_ROUTE6 -eq 1 ]]; then
cmd route -q -n delete -inet6 "$endpoint" >/dev/null 2>&1 || true
elif [[ $AUTO_ROUTE4 -eq 1 ]]; then
cmd route -q -n delete -inet "$endpoint" >/dev/null 2>&1 || true
fi
done
for endpoint in "${ENDPOINTS[@]}"; do
if [[ $remove_all_old -eq 0 && " ${old_endpoints[*]} " == *" $endpoint "* ]]; then
added+=( "$endpoint" )
continue
fi
if [[ $endpoint == *:* && $AUTO_ROUTE6 -eq 1 ]]; then
if [[ -n $GATEWAY6 ]]; then
cmd route -q -n add -inet6 "$endpoint" -gateway "$GATEWAY6" >/dev/null || true
else
# Prevent routing loop
cmd route -q -n add -inet6 "$endpoint" ::1 -blackhole >/dev/null || true
fi
added+=( "$endpoint" )
elif [[ $AUTO_ROUTE4 -eq 1 ]]; then
if [[ -n $GATEWAY4 ]]; then
cmd route -q -n add -inet "$endpoint" -gateway "$GATEWAY4" >/dev/null || true
else
# Prevent routing loop
cmd route -q -n add -inet "$endpoint" 127.0.0.1 -blackhole >/dev/null || true
fi
added+=( "$endpoint" )
wireguard-tools/src/wg-quick/darwin.bash view on Meta::CPAN
fi
done
kill $mpid) &
[[ -n $LAUNCHED_BY_LAUNCHD ]] || disown
}
add_route() {
[[ $TABLE != off ]] || return 0
local family=inet
[[ $1 == *:* ]] && family=inet6
if [[ $1 == */0 && ( -z $TABLE || $TABLE == auto ) ]]; then
if [[ $1 == *:* ]]; then
AUTO_ROUTE6=1
cmd route -q -n add -inet6 ::/1 -interface "$REAL_INTERFACE" >/dev/null
cmd route -q -n add -inet6 8000::/1 -interface "$REAL_INTERFACE" >/dev/null
else
AUTO_ROUTE4=1
cmd route -q -n add -inet 0.0.0.0/1 -interface "$REAL_INTERFACE" >/dev/null
cmd route -q -n add -inet 128.0.0.0/1 -interface "$REAL_INTERFACE" >/dev/null
fi
else
[[ $TABLE == main || $TABLE == auto || -z $TABLE ]] || die "Darwin only supports TABLE=auto|main|off"
[[ $(route -n get "-$family" "$1" 2>/dev/null) =~ interface:\ $REAL_INTERFACE$'\n' ]] || cmd route -q -n add -$family "$1" -interface "$REAL_INTERFACE" >/dev/null
fi
}
set_config() {
cmd wg setconf "$REAL_INTERFACE" <(echo "$WG_CONFIG")
}
save_config() {
local old_umask new_config current_config address cmd
new_config=$'[Interface]\n'
while read -r address; do
[[ $address =~ inet6?\ ([^ ]+) ]] && new_config+="Address = ${BASH_REMATCH[1]}"$'\n'
done < <(ifconfig "$REAL_INTERFACE")
# TODO: actually determine current DNS for interface
for address in "${DNS[@]}"; do
new_config+="DNS = $address"$'\n'
done
[[ -n $MTU ]] && new_config+="MTU = $MTU"$'\n'
[[ -n $TABLE ]] && new_config+="Table = $TABLE"$'\n'
[[ $SAVE_CONFIG -eq 0 ]] || new_config+=$'SaveConfig = true\n'
for cmd in "${PRE_UP[@]}"; do
new_config+="PreUp = $cmd"$'\n'
wireguard-tools/src/wg-quick/freebsd.bash view on Meta::CPAN
local todelete=( ) destination gateway netif
while read -r destination _ _ _ _ netif _; do
[[ $netif == "$INTERFACE" ]] && todelete+=( "$destination" )
done < <(netstat -nr -f inet)
for destination in "${todelete[@]}"; do
cmd route -q -n delete -inet "$destination" || true
done
todelete=( )
while read -r destination gateway _ netif; do
[[ $netif == "$INTERFACE" || ( $netif == lo* && $gateway == "$INTERFACE" ) ]] && todelete+=( "$destination" )
done < <(netstat -nr -f inet6)
for destination in "${todelete[@]}"; do
cmd route -q -n delete -inet6 "$destination" || true
done
for destination in "${ENDPOINTS[@]}"; do
if [[ $destination == *:* ]]; then
cmd route -q -n delete -inet6 "$destination" || true
else
cmd route -q -n delete -inet "$destination" || true
fi
done
}
del_if() {
[[ $HAVE_SET_DNS -eq 0 ]] || unset_dns
if [[ -S /var/run/wireguard/$INTERFACE.sock ]]; then
cmd rm -f "/var/run/wireguard/$INTERFACE.sock"
wireguard-tools/src/wg-quick/freebsd.bash view on Meta::CPAN
sleep 0.1
done
}
up_if() {
cmd ifconfig "$INTERFACE" up
}
add_addr() {
if [[ $1 == *:* ]]; then
cmd ifconfig "$INTERFACE" inet6 "$1" alias
else
cmd ifconfig "$INTERFACE" inet "$1" alias
fi
}
set_mtu() {
local mtu=0 endpoint output family
if [[ -n $MTU ]]; then
cmd ifconfig "$INTERFACE" mtu "$MTU"
return
fi
while read -r _ endpoint; do
[[ $endpoint =~ ^\[?([a-z0-9:.]+)\]?:[0-9]+$ ]] || continue
family=inet
[[ ${BASH_REMATCH[1]} == *:* ]] && family=inet6
output="$(route -n get "-$family" "${BASH_REMATCH[1]}" || true)"
[[ $output =~ interface:\ ([^ ]+)$'\n' && $(ifconfig "${BASH_REMATCH[1]}") =~ mtu\ ([0-9]+) && ${BASH_REMATCH[1]} -gt $mtu ]] && mtu="${BASH_REMATCH[1]}"
done < <(wg show "$INTERFACE" endpoints)
if [[ $mtu -eq 0 ]]; then
read -r output < <(route -n get default || true) || true
[[ $output =~ interface:\ ([^ ]+)$'\n' && $(ifconfig "${BASH_REMATCH[1]}") =~ mtu\ ([0-9]+) && ${BASH_REMATCH[1]} -gt $mtu ]] && mtu="${BASH_REMATCH[1]}"
fi
[[ $mtu -gt 0 ]] || mtu=1500
cmd ifconfig "$INTERFACE" mtu $(( mtu - 80 ))
}
wireguard-tools/src/wg-quick/freebsd.bash view on Meta::CPAN
[[ $destination == default ]] || continue
GATEWAY4="$gateway"
break
done < <(netstat -nr -f inet)
GATEWAY6=""
while read -r destination gateway _; do
[[ $destination == default ]] || continue
GATEWAY6="$gateway"
break
done < <(netstat -nr -f inet6)
}
collect_endpoints() {
ENDPOINTS=( )
while read -r _ endpoint; do
[[ $endpoint =~ ^\[?([a-z0-9:.]+)\]?:[0-9]+$ ]] || continue
ENDPOINTS+=( "${BASH_REMATCH[1]}" )
done < <(wg show "$INTERFACE" endpoints)
}
wireguard-tools/src/wg-quick/freebsd.bash view on Meta::CPAN
if [[ $remove_all_old -eq 1 ]]; then
for endpoint in "${ENDPOINTS[@]}"; do
[[ " ${old_endpoints[*]} " == *" $endpoint "* ]] || old_endpoints+=( "$endpoint" )
done
fi
for endpoint in "${old_endpoints[@]}"; do
[[ $remove_all_old -eq 0 && " ${ENDPOINTS[*]} " == *" $endpoint "* ]] && continue
if [[ $endpoint == *:* && $AUTO_ROUTE6 -eq 1 ]]; then
cmd route -q -n delete -inet6 "$endpoint" 2>/dev/null || true
elif [[ $AUTO_ROUTE4 -eq 1 ]]; then
cmd route -q -n delete -inet "$endpoint" 2>/dev/null || true
fi
done
for endpoint in "${ENDPOINTS[@]}"; do
if [[ $remove_all_old -eq 0 && " ${old_endpoints[*]} " == *" $endpoint "* ]]; then
added+=( "$endpoint" )
continue
fi
if [[ $endpoint == *:* && $AUTO_ROUTE6 -eq 1 ]]; then
if [[ -n $GATEWAY6 ]]; then
cmd route -q -n add -inet6 "$endpoint" -gateway "$GATEWAY6" || true
else
# Prevent routing loop
cmd route -q -n add -inet6 "$endpoint" ::1 -blackhole || true
fi
added+=( "$endpoint" )
elif [[ $AUTO_ROUTE4 -eq 1 ]]; then
if [[ -n $GATEWAY4 ]]; then
cmd route -q -n add -inet "$endpoint" -gateway "$GATEWAY4" || true
else
# Prevent routing loop
cmd route -q -n add -inet "$endpoint" 127.0.0.1 -blackhole || true
fi
added+=( "$endpoint" )
wireguard-tools/src/wg-quick/freebsd.bash view on Meta::CPAN
unset_dns() {
[[ ${#DNS[@]} -gt 0 ]] || return 0
cmd resolvconf -d "$INTERFACE"
}
add_route() {
[[ $TABLE != off ]] || return 0
local family=inet
[[ $1 == *:* ]] && family=inet6
if [[ -n $TABLE && $TABLE != auto ]]; then
cmd route -q -n add "-$family" -fib "$TABLE" "$1" -interface "$INTERFACE"
elif [[ $1 == */0 ]]; then
if [[ $1 == *:* ]]; then
AUTO_ROUTE6=1
cmd route -q -n add -inet6 ::/1 -interface "$INTERFACE"
cmd route -q -n add -inet6 8000::/1 -interface "$INTERFACE"
else
AUTO_ROUTE4=1
cmd route -q -n add -inet 0.0.0.0/1 -interface "$INTERFACE"
cmd route -q -n add -inet 128.0.0.0/1 -interface "$INTERFACE"
fi
else
[[ $(route -n get "-$family" "$1" 2>/dev/null) =~ interface:\ $INTERFACE$'\n' ]] || cmd route -q -n add "-$family" "$1" -interface "$INTERFACE"
fi
}
wireguard-tools/src/wg-quick/freebsd.bash view on Meta::CPAN
}
save_config() {
local old_umask new_config current_config address cmd
new_config=$'[Interface]\n'
{ read -r _; while read -r _ _ _ address _; do
new_config+="Address = $address"$'\n'
done } < <(netstat -I "$INTERFACE" -n -W -f inet)
{ read -r _; while read -r _ _ _ address _; do
new_config+="Address = $address"$'\n'
done } < <(netstat -I "$INTERFACE" -n -W -f inet6)
while read -r address; do
[[ $address =~ ^nameserver\ ([a-zA-Z0-9_=+:%.-]+)$ ]] && new_config+="DNS = ${BASH_REMATCH[1]}"$'\n'
done < <(resolvconf -l "$INTERFACE" 2>/dev/null)
[[ -n $MTU ]] && new_config+="MTU = $MTU"$'\n'
[[ -n $TABLE ]] && new_config+="Table = $TABLE"$'\n'
[[ $SAVE_CONFIG -eq 0 ]] || new_config+=$'SaveConfig = true\n'
for cmd in "${PRE_UP[@]}"; do
new_config+="PreUp = $cmd"$'\n'
done
for cmd in "${POST_UP[@]}"; do
wireguard-tools/src/wg-quick/linux.bash view on Meta::CPAN
cmd ip $proto rule add not fwmark $table table $table
cmd ip $proto rule add table main suppress_prefixlength 0
cmd ip $proto route add "$1" dev "$INTERFACE" table $table
local marker="-m comment --comment \"wg-quick(8) rule for $INTERFACE\"" restore=$'*raw\n' nftable="wg-quick-$INTERFACE" nftcmd
printf -v nftcmd '%sadd table %s %s\n' "$nftcmd" "$pf" "$nftable"
printf -v nftcmd '%sadd chain %s %s preraw { type filter hook prerouting priority -300; }\n' "$nftcmd" "$pf" "$nftable"
printf -v nftcmd '%sadd chain %s %s premangle { type filter hook prerouting priority -150; }\n' "$nftcmd" "$pf" "$nftable"
printf -v nftcmd '%sadd chain %s %s postmangle { type filter hook postrouting priority -150; }\n' "$nftcmd" "$pf" "$nftable"
while read -r line; do
[[ $line =~ .*inet6?\ ([0-9a-f:.]+)/[0-9]+.* ]] || continue
printf -v restore '%s-I PREROUTING ! -i %s -d %s -m addrtype ! --src-type LOCAL -j DROP %s\n' "$restore" "$INTERFACE" "${BASH_REMATCH[1]}" "$marker"
printf -v nftcmd '%sadd rule %s %s preraw iifname != "%s" %s daddr %s fib saddr type != local drop\n' "$nftcmd" "$pf" "$nftable" "$INTERFACE" "$pf" "${BASH_REMATCH[1]}"
done < <(ip -o $proto addr show dev "$INTERFACE" 2>/dev/null)
printf -v restore '%sCOMMIT\n*mangle\n-I POSTROUTING -m mark --mark %d -p udp -j CONNMARK --save-mark %s\n-I PREROUTING -p udp -j CONNMARK --restore-mark %s\nCOMMIT\n' "$restore" $table "$marker" "$marker"
printf -v nftcmd '%sadd rule %s %s postmangle meta l4proto udp mark %d ct mark set mark \n' "$nftcmd" "$pf" "$nftable" $table
printf -v nftcmd '%sadd rule %s %s premangle meta l4proto udp meta mark set ct mark \n' "$nftcmd" "$pf" "$nftable"
[[ $proto == -4 ]] && cmd sysctl -q net.ipv4.conf.all.src_valid_mark=1
if type -p nft >/dev/null; then
cmd nft -f <(echo -n "$nftcmd")
else
wireguard-tools/src/wg-quick/openbsd.bash view on Meta::CPAN
[[ -n $REAL_INTERFACE ]] || return 0
while read -r destination _ _ _ _ netif _; do
[[ $netif == "$REAL_INTERFACE" ]] && todelete+=( "$destination" )
done < <(netstat -nr -f inet)
for destination in "${todelete[@]}"; do
cmd route -q -n delete -inet "$destination" || true
done
todelete=( )
while read -r destination gateway _ netif; do
[[ $netif == "$REAL_INTERFACE" || ( $netif == lo* && $gateway == "$REAL_INTERFACE" ) ]] && todelete+=( "$destination" )
done < <(netstat -nr -f inet6)
for destination in "${todelete[@]}"; do
cmd route -q -n delete -inet6 "$destination" || true
done
for destination in "${ENDPOINTS[@]}"; do
if [[ $destination == *:* ]]; then
cmd route -q -n delete -inet6 "$destination" || true
else
cmd route -q -n delete -inet "$destination" || true
fi
done
}
del_if() {
unset_dns
[[ -n $REAL_INTERFACE ]] && cmd ifconfig $REAL_INTERFACE destroy
}
up_if() {
cmd ifconfig "$REAL_INTERFACE" up
}
add_addr() {
local family
if [[ $1 == *:* ]]; then
family=inet6
[[ -n $FIRSTADDR6 ]] || FIRSTADDR6="${1%/*}"
else
family=inet
[[ -n $FIRSTADDR4 ]] || FIRSTADDR4="${1%/*}"
fi
cmd ifconfig "$REAL_INTERFACE" $family "$1" alias
}
set_mtu() {
local mtu=0 endpoint output family
if [[ -n $MTU ]]; then
cmd ifconfig "$REAL_INTERFACE" mtu "$MTU"
return
fi
while read -r _ endpoint; do
[[ $endpoint =~ ^\[?([a-z0-9:.]+)\]?:[0-9]+$ ]] || continue
family=inet
[[ ${BASH_REMATCH[1]} == *:* ]] && family=inet6
output="$(route -n get "-$family" "${BASH_REMATCH[1]}" || true)"
[[ $output =~ interface:\ ([^ ]+)$'\n' && $(ifconfig "${BASH_REMATCH[1]}") =~ mtu\ ([0-9]+) && ${BASH_REMATCH[1]} -gt $mtu ]] && mtu="${BASH_REMATCH[1]}"
done < <(wg show "$REAL_INTERFACE" endpoints)
if [[ $mtu -eq 0 ]]; then
read -r output < <(route -n get default || true) || true
[[ $output =~ interface:\ ([^ ]+)$'\n' && $(ifconfig "${BASH_REMATCH[1]}") =~ mtu\ ([0-9]+) && ${BASH_REMATCH[1]} -gt $mtu ]] && mtu="${BASH_REMATCH[1]}"
fi
[[ $mtu -gt 0 ]] || mtu=1500
cmd ifconfig "$REAL_INTERFACE" mtu $(( mtu - 80 ))
}
wireguard-tools/src/wg-quick/openbsd.bash view on Meta::CPAN
[[ $destination == default ]] || continue
GATEWAY4="$gateway"
break
done < <(netstat -nr -f inet)
GATEWAY6=""
while read -r destination gateway _; do
[[ $destination == default ]] || continue
GATEWAY6="$gateway"
break
done < <(netstat -nr -f inet6)
}
collect_endpoints() {
ENDPOINTS=( )
while read -r _ endpoint; do
[[ $endpoint =~ ^\[?([a-z0-9:.]+)\]?:[0-9]+$ ]] || continue
ENDPOINTS+=( "${BASH_REMATCH[1]}" )
done < <(wg show "$REAL_INTERFACE" endpoints)
}
wireguard-tools/src/wg-quick/openbsd.bash view on Meta::CPAN
if [[ $remove_all_old -eq 1 ]]; then
for endpoint in "${ENDPOINTS[@]}"; do
[[ " ${old_endpoints[*]} " == *" $endpoint "* ]] || old_endpoints+=( "$endpoint" )
done
fi
for endpoint in "${old_endpoints[@]}"; do
[[ $remove_all_old -eq 0 && " ${ENDPOINTS[*]} " == *" $endpoint "* ]] && continue
if [[ $endpoint == *:* && $AUTO_ROUTE6 -eq 1 ]]; then
cmd route -q -n delete -inet6 "$endpoint" 2>/dev/null || true
elif [[ $AUTO_ROUTE4 -eq 1 ]]; then
cmd route -q -n delete -inet "$endpoint" 2>/dev/null || true
fi
done
for endpoint in "${ENDPOINTS[@]}"; do
if [[ $remove_all_old -eq 0 && " ${old_endpoints[*]} " == *" $endpoint "* ]]; then
added+=( "$endpoint" )
continue
fi
if [[ $endpoint == *:* && $AUTO_ROUTE6 -eq 1 ]]; then
if [[ -n $GATEWAY6 ]]; then
cmd route -q -n add -inet6 "$endpoint" -gateway "$GATEWAY6" || true
else
# Prevent routing loop
cmd route -q -n add -inet6 "$endpoint" ::1 -blackhole || true
fi
added+=( "$endpoint" )
elif [[ $AUTO_ROUTE4 -eq 1 ]]; then
if [[ -n $GATEWAY4 ]]; then
cmd route -q -n add -inet "$endpoint" -gateway "$GATEWAY4" || true
else
# Prevent routing loop
cmd route -q -n add -inet "$endpoint" 127.0.0.1 -blackhole || true
fi
added+=( "$endpoint" )
wireguard-tools/src/wg-quick/openbsd.bash view on Meta::CPAN
[[ -f "/etc/resolv.conf.wg-quick-backup.$INTERFACE" ]] || return 0
route nameserver ${REAL_INTERFACE}
cmd mv "/etc/resolv.conf.wg-quick-backup.$INTERFACE" /etc/resolv.conf
}
add_route() {
[[ $TABLE != off ]] || return 0
local family ifaceroute
if [[ $1 == *:* ]]; then
family=inet6
[[ -n $FIRSTADDR6 ]] || die "Local IPv6 address must be set to have routes"
ifaceroute="$FIRSTADDR6"
else
family=inet
[[ -n $FIRSTADDR4 ]] || die "Local IPv4 address must be set to have routes"
ifaceroute="$FIRSTADDR4"
fi
if [[ -n $TABLE && $TABLE != auto ]]; then
cmd route -q -n -T "$TABLE" add "-$family" "$1" -iface "$ifaceroute"
elif [[ $1 == */0 ]]; then
if [[ $1 == *:* ]]; then
AUTO_ROUTE6=1
cmd route -q -n add -inet6 ::/1 -iface "$ifaceroute"
cmd route -q -n add -inet6 8000::/1 -iface "$ifaceroute"
else
AUTO_ROUTE4=1
cmd route -q -n add -inet 0.0.0.0/1 -iface "$ifaceroute"
cmd route -q -n add -inet 128.0.0.0/1 -iface "$ifaceroute"
fi
else
[[ $(route -n get "-$family" "$1" 2>/dev/null) =~ interface:\ $REAL_INTERFACE$'\n' ]] || cmd route -q -n add "-$family" "$1" -iface "$ifaceroute"
fi
}