#!/bin/sh # 2020-06-13, Nico Schottelius # See https://ungleich.ch/u/products/viirb-ipv6-box/ if [ $# -ne 4 ]; then echo "$0 interface viirb-id your-dot-cdist" echo " interface to add the config ip address to" echo " viirb-id: number in decimal format" echo " your-dot-cdist: path to YOUR ungleich-dot-cdist repo" echo " owner-mail-reference: How to identify the owner" echo "" echo "Example to configure viirb02:" echo "$0 wlan0 2 ~/vcs/ungleich-dot-cdist 'Nico Schottelius, nico.schottelius@ungleich.ch, Ticket 2342'" exit 1 fi set -x set -x dev=$1; shift id=$1; shift dot_cdist=$1; shift owner=$1; shift hex_id=$(printf "%0.2x\n" "$id") viirb_hostname=viirb${hex_id} prefix_base=2a0a:e5c1:3 my_prefix=${prefix_base}${hex_id} my_network=${my_prefix}::/48 my_wireguard_ip=${my_prefix}::42 my_lan_ip=${my_prefix}:cafe::42 my_wifi_ip=${my_prefix}:7ea::42 # openwrt version=19.07.3 filename=openwrt-${version}-ramips-mt76x8-vocore2-squashfs-sysupgrade.bin # IP address for setting it up initially viirb_ip=192.168.61.1 # wireguard private_key=$(wg genkey) private_key=EL76tScnk84v8TGSSD3tPDhUjjYVPrfmNMBE3zbuRXg= public_key=$(echo $private_key | wg pubkey) vpn_endpoint_host=vpn-2a0ae5c1300.ungleich.ch vpn_endpoint_pubkey=ft68G2RID7gZ6PXjFCSCOdJ9yspRg+tUw0YrNK9cTxE= # cdist dot_cdist_files=${dot_cdist}/type/__ungleich_wireguard/files peerfile=${dot_cdist_files}/${vpn_endpoint_host}.peer${hex_id} vpnconfig=${dot_cdist_files}/${vpn_endpoint_host} # Configure VPN server / update cdist echo Updating VPNserver cat < ${peerfile} # ${viirb_hostname} ${owner} [Peer] PublicKey = ${public_key} AllowedIPs = ${my_network} EOF # Generate real config cat ${dot_cdist_files}/${vpn_endpoint_host}.* > ${vpnconfig} cd ${dot_cdist_files} git add ${vpn_endpoint_host} git commit -m "[vpn] Updated config for peer ${viirb_hostname} ${my_network}" git pull git push cdist config -vv -j8 ${vpn_endpoint_host} -c ${dot_cdist} exit 0 # System cat </dev/null || true sudo ip addr add 192.168.61.2/24 dev "$dev" # don't care about other/old known_host entries ssh-keygen -R 192.168.61.1 ping -c2 ${viirb_ip} if [ $? -ne 0 ]; then echo "Cannot reach any VIIRB - exiting" exit 1 fi set -e # Don't re-download if we already have it wget -c http://downloads.openwrt.org/releases/${version}/targets/ramips/mt76x8/${filename} scp ${filename} root@${viirb_ip}:/tmp ssh root@${viirb_ip} "sysupgrade /tmp/*.bin" wait=0 found="" while [ $wait -lt 180 ]; do ping -c1 ${viirb_ip} >/dev/null if [ $? -eq 0 ]; then found=yes # wait for ssh to come up sleep 10 fi done if [ ! "$found" ]; then echo "Did not find updated viirb - debug / restart it" exit 1 fi exit 0