output="$(ip route get "${BASH_REMATCH[1]}"||true)"
[[($output=~ mtu\ ([0-9]+)||($output=~ dev\ ([^ ]+)&&$(ip link show dev "${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 < <(ip route show default ||true)||true
[[($output=~ mtu\ ([0-9]+)||($output=~ dev\ ([^ ]+)&&$(ip link show dev "${BASH_REMATCH[1]}")=~ mtu\ ([0-9]+)))&&${BASH_REMATCH[1]} -gt $mtu]]&&mtu="${BASH_REMATCH[1]}"
fi
[[$mtu -gt 0]]||mtu=1500
cndc interface setmtu "$INTERFACE"$(( mtu -80))
}
add_route(){
cndc network route add "$NETID""$INTERFACE""$1"
}
set_config(){
cmd wg setconf "$INTERFACE" <(echo"$WG_CONFIG")
}
cmd_usage(){
cat >&2 <<-_EOF
Usage: $PROGRAM[ up | down ][ CONFIG_FILE | INTERFACE ]
CONFIG_FILE is a configuration file, whose filename is the interface name
followed by \`.conf'. Otherwise, INTERFACE is an interface name, with
configuration found at /data/misc/wireguard/INTERFACE.conf. It is to be readable
by wg(8)'s \`setconf' sub-command, with the exception of the following additions
to the [Interface] section, which are handled by $PROGRAM:
- Address: may be specified one or more times and contains one or more
IP addresses (with an optional CIDR mask) to be setfor the interface.
- MTU: an optional MTU for the interface;if unspecified, auto-calculated.
- DNS: an optional DNS server to use while the device is up.
See wg-quick(8)for more info and examples.
_EOF
}
cmd_up(){
local i
[[ -z $(ip link show dev "$INTERFACE" 2>/dev/null)]]|| die "\`$INTERFACE' already exists"
for i in $(whileread -r _ i;dofor i in $i;do[[$i=~ ^[0-9a-z:.]+/[0-9]+$ ]]&&echo"$i";done;done < <(wg show "$INTERFACE" allowed-ips)| sort -nr -k 2 -t /);do