From d02f0704195811798d4beb9855cadf4b4c831ec0 Mon Sep 17 00:00:00 2001 From: ozfox Date: Mon, 21 Oct 2024 16:31:47 +0200 Subject: [PATCH] This and that --- node/code/dev/border_router/start_network.sh | 123 +++++++++++++++++++ platform/create-certs.sh | 6 + web_backend/build-and-push.sh | 3 + 3 files changed, 132 insertions(+) create mode 100755 node/code/dev/border_router/start_network.sh create mode 100755 platform/create-certs.sh create mode 100755 web_backend/build-and-push.sh diff --git a/node/code/dev/border_router/start_network.sh b/node/code/dev/border_router/start_network.sh new file mode 100755 index 00000000..cf72f07b --- /dev/null +++ b/node/code/dev/border_router/start_network.sh @@ -0,0 +1,123 @@ +#!/usr/bin/env bash + +USB_CDC_ECM_DIR="$(dirname "$(readlink -f "$0")")" + +INTERFACE_CHECK_COUNTER=5 # 5 attempts to find usb interface + +find_interface() { + INTERFACE=$(ls -A /sys/bus/usb/drivers/cdc_ether/*/net/ 2>/dev/null) + INTERFACE_CHECK=$(echo -n "${INTERFACE}" | head -c1 | wc -c) + if [ "${INTERFACE_CHECK}" -eq 0 ] && [ ${INTERFACE_CHECK_COUNTER} != 0 ]; then + # We want to have multiple opportunities to find the USB interface + # as sometimes it can take a few seconds for it to enumerate after + # the device has been flashed. + sleep 1 + ((INTERFACE_CHECK_COUNTER=INTERFACE_CHECK_COUNTER-1)) + find_interface + fi + INTERFACE=${INTERFACE%/} +} + +echo "Waiting for network interface." +find_interface + +if [ "${INTERFACE_CHECK}" -eq 0 ]; then + echo "Unable to find network interface" + exit 1 +else + echo "Found interface: ${INTERFACE}" +fi + +setup_interface() { + sysctl -w net.ipv6.conf."${INTERFACE}".forwarding=1 + sysctl -w net.ipv6.conf."${INTERFACE}".accept_ra=0 + ip link set "${INTERFACE}" up + ip a a fe80::1/64 dev "${INTERFACE}" + ip a a fd00:dead:beef::1/128 dev lo +} + +cleanup_interface() { + ip a d fe80::1/64 dev "${INTERFACE}" + ip a d fd00:dead:beef::1/128 dev lo + ip route del "${PREFIX}" via fe80::2 dev "${INTERFACE}" +} + +cleanup() { + echo "Cleaning up..." + cleanup_interface + if [ -n "${UHCPD_PID}" ]; then + kill "${UHCPD_PID}" + fi + if [ -n "${DHCPD_PIDFILE}" ]; then + kill "$(cat "${DHCPD_PIDFILE}")" + rm "${DHCPD_PIDFILE}" + fi + trap "" INT QUIT TERM EXIT +} + +start_uhcpd() { + ip route add "${PREFIX}" via fe80::2 dev "${INTERFACE}" + ${UHCPD} "${INTERFACE}" "${PREFIX}" > /dev/null & + UHCPD_PID=$! +} + +start_dhcpd() { + ip route add "${PREFIX}" via fe80::2 dev "${INTERFACE}" + DHCPD_PIDFILE=$(mktemp) + ${DHCPD} -d -p "${DHCPD_PIDFILE}" "${INTERFACE}" "${PREFIX}" 2> /dev/null +} + +start_radvd() { + ADDR=$(echo "${PREFIX}" | sed -e 's/::\//::1\//') + ip a a "${ADDR}" dev "${INTERFACE}" + sysctl net.ipv6.conf."${INTERFACE}".accept_ra=2 + sysctl net.ipv6.conf."${INTERFACE}".accept_ra_rt_info_max_plen=64 + ${RADVD} -c "${INTERFACE}" "${PREFIX}" +} + +if [ "$1" = "-d" ] || [ "$1" = "--use-dhcpv6" ]; then + USE_DHCPV6=1 + shift 1 +else + USE_DHCPV6=0 +fi + +if [ "$1" = "-r" ] || [ "$1" = "--use-radvd" ]; then + USE_RADVD=1 + shift 1 +else + USE_RADVD=0 +fi + +PREFIX=$1 +[ -z "${PREFIX}" ] && { + echo "usage: $0 [-d|--use-dhcpv6] [-r|--use-radvd ] []" + exit 1 +} + +if [ -n "$2" ]; then + PORT=$2 +fi + +trap "cleanup" INT QUIT TERM EXIT + +setup_interface + +if [ ${USE_DHCPV6} -eq 1 ]; then + DHCPD="$(readlink -f "${USB_CDC_ECM_DIR}/../dhcpv6-pd_ia/")/dhcpv6-pd_ia.py" + start_dhcpd +elif [ ${USE_RADVD} -eq 1 ]; then + RADVD="$(readlink -f "${USB_CDC_ECM_DIR}/../radvd/")/radvd.sh" + start_radvd +else + UHCPD="$(readlink -f "${USB_CDC_ECM_DIR}/../uhcpd/bin")/uhcpd" + start_uhcpd +fi + +if [ -z "${PORT}" ]; then + echo "Network enabled over CDC-ECM" + echo "Press Return to stop" + read -r +else + "${USB_CDC_ECM_DIR}/../pyterm/pyterm" -p "${PORT}" +fi diff --git a/platform/create-certs.sh b/platform/create-certs.sh new file mode 100755 index 00000000..0a56776a --- /dev/null +++ b/platform/create-certs.sh @@ -0,0 +1,6 @@ +#!/bin/bash +openssl genrsa -out ./data/ssl/certs/server.key 2048 +openssl req -new -out ./data/ssl/certs/server.csr -key ./data/ssl/certs/server.key -config ./data/ssl/openssl.cnf +openssl x509 -req -days 3650 -in ./data/ssl/certs/server.csr -signkey ./data/ssl/certs/server.key -out ./data/ssl/certs/server.crt -extensions v3_req -extfile ./data/ssl/openssl.cnf + +#openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ./data/nginx.key -out ./data/ssl/nginx.crt diff --git a/web_backend/build-and-push.sh b/web_backend/build-and-push.sh new file mode 100755 index 00000000..f933d3e2 --- /dev/null +++ b/web_backend/build-and-push.sh @@ -0,0 +1,3 @@ +#!/bin/bash +echo $TEAMAGOCHI_TOKEN | docker login ghcr.io -u ozfox --password-stdin +./mvnw install -Dquarkus.profile=prod,publish