From 5136f6f529fd6052991bbbd2733b9ca23d79f245 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Tue, 12 Mar 2024 05:30:31 -0500 Subject: [PATCH] improve --- sources/identity/.gitignore | 2 + sources/identity/bootstrap-init.sh | 28 ++++++++- sources/identity/bootstrap.sh | 2 +- sources/identity/go.mod | 2 +- sources/identity/go.sum | 4 +- sources/identity/init.template.sh | 60 +++++++++++++++---- sources/identity/provider.sh | 21 ++++--- sources/identity/provider/pages/link.sh | 28 +++++++-- sources/identity/web/templates/main_templ.go | 4 +- .../web/templates/pages/index_templ.go | 2 +- 10 files changed, 120 insertions(+), 33 deletions(-) diff --git a/sources/identity/.gitignore b/sources/identity/.gitignore index e8a6738a..437fce1b 100644 --- a/sources/identity/.gitignore +++ b/sources/identity/.gitignore @@ -1,5 +1,7 @@ ./init ./.init +.link +./.link ./provider/static/init ./provider/data/ ./provider/stream/ diff --git a/sources/identity/bootstrap-init.sh b/sources/identity/bootstrap-init.sh index a3bb93c3..49409a16 100644 --- a/sources/identity/bootstrap-init.sh +++ b/sources/identity/bootstrap-init.sh @@ -27,8 +27,7 @@ if [ -n "$5" ]; then BOOTSTRAP_SCRIPT="$5" fi if [ -z "$BOOTSTRAP_SCRIPT" ]; then - # BOOTSTRAP_SCRIPT="bootstrap.sh" - BOOTSTRAP_SCRIPT="init.template.sh" + BOOTSTRAP_SCRIPT="bootstrap.sh" fi if [ -n "$6" ]; then BOOTSTRAP_HOST="$6" @@ -36,9 +35,32 @@ fi if [ -z "$BOOTSTRAP_HOST" ]; then BOOTSTRAP_HOST="localhost:3333" fi +if [ -n "$7" ]; then + BOOTSTRAP_PROVIDER_SCRIPT="$7" +fi +if [ -z "$BOOTSTRAP_PROVIDER_SCRIPT" ]; then + BOOTSTRAP_PROVIDER_SCRIPT="provider.sh" +fi +if [ -n "$8" ]; then + TURSO_HOST=$8 +fi +if [ -z "$TURSO_HOST" ]; then + echo "TURSO_HOST not set" + exit 1 +fi +if [ -n "$9" ]; then + TURSO_AUTH_TOKEN=$9 # really shouldn't do this it's viewable in ps, etc. +fi +if [ -z "$TURSO_AUTH_TOKEN" ]; then + echo "TURSO_AUTH_TOKEN not set" + exit 1 +fi apt update apt install -y git git clone "$BOOTSTRAP_REPO" cd "$BOOTSTRAP_DIR" +chmod +x "./$BOOTSTRAP_PROVIDER_SCRIPT" +"TURSO_AUTH_TOKEN=$TURSO_AUTH_TOKEN" "TURSO_HOST=$TURSO_HOST" "PORT=3333" "INIT_URL=$BOOTSTRAP_HOST/init" ./"$BOOTSTRAP_PROVIDER_SCRIPT" & +sleep 15 chmod +x "./$BOOTSTRAP_SCRIPT" -"./$BOOTSTRAP_SCRIPT" "$BOOTSTRAP_HOST" +source "./$BOOTSTRAP_SCRIPT" "$BOOTSTRAP_HOST" diff --git a/sources/identity/bootstrap.sh b/sources/identity/bootstrap.sh index cfd68452..3bed4de2 100644 --- a/sources/identity/bootstrap.sh +++ b/sources/identity/bootstrap.sh @@ -21,4 +21,4 @@ apt update apt install -y curl curl -o "$BOOTSTRAP_SCRIPT" "http://$BOOTSTRAP_HOST/$BOOTSTRAP_URL_PATH/$BOOTSTRAP_SCRIPT" chmod +x "./$BOOTSTRAP_SCRIPT" -"./$BOOTSTRAP_SCRIPT" +source "./$BOOTSTRAP_SCRIPT" diff --git a/sources/identity/go.mod b/sources/identity/go.mod index a068378c..bbbb7444 100644 --- a/sources/identity/go.mod +++ b/sources/identity/go.mod @@ -32,7 +32,7 @@ require ( gopkg.in/go-jose/go-jose.v2 v2.6.3 ) -replace github.com/charmbracelet/charm => github.com/developing-today-forks/charm v0.12.7-0.20240310061729-3b7e199cf626 +replace github.com/charmbracelet/charm => github.com/developing-today-forks/charm v0.12.7-0.20240312075138-411f9ff4f8ac require ( github.com/FZambia/eagle v0.1.0 // indirect diff --git a/sources/identity/go.sum b/sources/identity/go.sum index 065c8252..99bb5d44 100644 --- a/sources/identity/go.sum +++ b/sources/identity/go.sum @@ -100,8 +100,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/developing-today-forks/charm v0.12.7-0.20240310061729-3b7e199cf626 h1:VGhRmEVrZAzupFbnlwBHa+JpwffnJCukj4jtojsQI94= -github.com/developing-today-forks/charm v0.12.7-0.20240310061729-3b7e199cf626/go.mod h1:6xzxrach+U21VLDQYKlWRMq+Basc95gD4AQ8v7LLEhE= +github.com/developing-today-forks/charm v0.12.7-0.20240312075138-411f9ff4f8ac h1:tPq59hN7UOMb0D4LKjS2ZG+OYFUvzKf/A4pxCDarZYQ= +github.com/developing-today-forks/charm v0.12.7-0.20240312075138-411f9ff4f8ac/go.mod h1:6xzxrach+U21VLDQYKlWRMq+Basc95gD4AQ8v7LLEhE= github.com/dgraph-io/badger/v3 v3.2103.5 h1:ylPa6qzbjYRQMU6jokoj4wzcaweHylt//CH0AKt0akg= github.com/dgraph-io/badger/v3 v3.2103.5/go.mod h1:4MPiseMeDQ3FNCYwRbbcBOGJLf5jsE0PPFzRiKjtcdw= github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8= diff --git a/sources/identity/init.template.sh b/sources/identity/init.template.sh index 3d4b781c..e8b89291 100644 --- a/sources/identity/init.template.sh +++ b/sources/identity/init.template.sh @@ -1,4 +1,5 @@ -#!/usr/bin/env sh +#!/usr/bin/env bash +# must be bash because we source a bashrc file if [ -n "$1" ]; then CHARM_URL="$1" fi @@ -14,7 +15,7 @@ export CHARM_URL if [ -n "$2" ]; then CHARM_LINK_URL="$2" fi -if [ -n "{{CHARM_LINK_URL}}" ] && [ "{{CHARM_LINK_URL}}" != "\{\{CHARM_LINK_URL\}\}" ]; then +if [ -z "$CHARM_LINK_URL" ] && [ -n "{{CHARM_LINK_URL}}" ] && [ "{{CHARM_LINK_URL}}" != "\{\{CHARM_LINK_URL\}\}" ]; then CHARM_LINK_URL="{{CHARM_LINK_URL}}" fi if [ -z "$CHARM_LINK_URL" ] || [ "$CHARM_LINK_URL" = "\{\{CHARM_LINK_URL\}\}" ]; then @@ -25,6 +26,7 @@ if [ -z "$CHARM_LINK_URL" ] || [ "$CHARM_LINK_URL" = "\{\{CHARM_LINK_URL\}\}" ]; IP="127.0.0.1" fi fi + PORT=3333 CHARM_LINK_URL="http://$IP:$PORT/link" echo "No charm link provided" echo "Using default link: $CHARM_LINK_URL" @@ -32,9 +34,13 @@ fi export CHARM_LINK_URL /boot/dietpi/dietpi-software uninstall 103 104 # ramlog dropbear /boot/dietpi/dietpi-software install 188 # go (git by dependency) -source /etc/bash.bashrc +if [ -f /etc/bash.bashrc ]; then + source /etc/bash.bashrc +else + echo "/etc/bash.bashrc does not exist, continuing without sourcing it." +fi mkdir -p /etc/apt/keyrings -curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg +curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --batch --yes --dearmor -o /etc/apt/keyrings/nodesource.gpg echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_21.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list apt update apt install -y curl nodejs npm ucspi-tcp unzip @@ -55,15 +61,49 @@ if [ ! -d "code" ]; then else echo "code directory already exists" fi -cd code/source/identity +cd code/src/identity chmod +x *.ps1 ./build-libsql.ps1 -CHARM_LINK=$(curl -sL "$CHARM_LINK_URL") -if [ -z "$CHARM_LINK" ]; then - echo "Failed to obtain charm link" - exit 1 +"CHARM_LINK_URL=$CHARM_LINK_URL" ./provider.sh & +get_http_status() { + local url=$1 + curl -o /dev/null -s -w "%{http_code}\n" "$url" +} + +start_time=$(date +%s) + +while : ; do + current_time=$(date +%s) + elapsed_time=$((current_time - start_time)) + + if [ "$elapsed_time" -ge 60 ]; then + echo "1 minute has elapsed, stopping." + break + fi + + http_status=$(get_http_status "$CHARM_LINK_URL") + echo "Checking URL: $CHARM_LINK_URL - HTTP status: $http_status" + + if [ "$http_status" -eq 405 ]; then + echo "Received 405 code, exiting." + break + fi + + sleep 2 +done +if [ "$elapsed_time" -ge 60 ]; then + echo "Failed to obtain charm link" + exit 1 +fi +response=$(curl -sL "$CHARM_LINK_URL" --data-urlencode "keys=$(./identity charm keys --simple | tr '\n' ',' | sed 's/,$//')") + +if [ -n "$response" ]; then + CHARM_LINK=$response +else + echo "Failed to obtain charm link" + exit 1 fi -./identity charm link "$CHARM_LINK" +./identity charm link -d "$CHARM_LINK" ./identity charm kv sync ./identity charm kv get dt.identity.init > .init chmod +x .init diff --git a/sources/identity/provider.sh b/sources/identity/provider.sh index cd40b2a0..32b5e9cb 100644 --- a/sources/identity/provider.sh +++ b/sources/identity/provider.sh @@ -2,6 +2,9 @@ if [ -n "$1" ]; then CHARM_DIR="$1" fi +random() { + echo $(dd if=/dev/urandom bs=1 count=64 2>/dev/null | xxd -p) +} if [ -z "$CHARM_DIR" ]; then CHARM_DIR=~$USER/code/source/identity/data/charms/$(random) fi @@ -31,17 +34,19 @@ if [ -z "$TURSO_AUTH_TOKEN" ]; then echo "TURSO_AUTH_TOKEN not set" exit 1 fi -random() { - echo $(dd if=/dev/urandom bs=1 count=64 2>/dev/null | xxd -p) -} -IP=$(hostname -I | awk '{print $1}') -if [ "$(expr substr "$IP" 1 4)" = "172." ]; then - IP=$(hostname -I | awk '{print $2}') +if [ -n "$6" ]; then + CHARM_LINK_URL=$6 +fi +if [ -z "$CHARM_LINK_URL" ]; then + IP=$(hostname -I | awk '{print $1}') if [ "$(expr substr "$IP" 1 4)" = "172." ]; then - IP="127.0.0.1" + IP=$(hostname -I | awk '{print $2}') + if [ "$(expr substr "$IP" 1 4)" = "172." ]; then + IP="127.0.0.1" + fi fi + CHARM_LINK_URL="http://$IP:$PORT/link" fi -CHARM_LINK_URL="http://$IP:$PORT/link" cp -f ./init.template.sh ./provider/static/init sed -i "s|{{CHARM_DIR}}|$CHARM_DIR|g" ./provider/static/init sed -i "s|{{CHARM_LINK_URL}}|$CHARM_LINK_URL|g" ./provider/static/init diff --git a/sources/identity/provider/pages/link.sh b/sources/identity/provider/pages/link.sh index 95ec7b24..74cb86fc 100644 --- a/sources/identity/provider/pages/link.sh +++ b/sources/identity/provider/pages/link.sh @@ -5,11 +5,29 @@ random() { echo $(dd if=/dev/urandom bs=1 count=64 2>/dev/null | xxd -p) } for key in "${!FORM_DATA[@]}"; do - if [[ "$key" == "link" ]]; then - echo "CHARM_DIR=$CHARM_DIR/$(random)" - CHARM_DIR=$CHARM_DIR/$(random) ~$USER/code/source/identity/identity charm link ${FORM_DATA[$key]} - if [[ $? -eq 0 ]]; then - respond 200 "${FORM_DATA[$key]}" + if [[ "$key" == "keys" ]]; then + CHARM_DIR=$CHARM_DIR/$(random) + mkdir -p "$CHARM_DIR" + LINK_CODE_PATH=$CHARM_DIR/.link + rm -rf "$LINK_CODE_PATH" + ./identity charm link -d -o "$LINK_CODE_PATH" -keys "${FORM_DATA[$key]}" & + + max_wait=60 # seconds + wait_interval=1 # seconds + elapsed_time=0 + + while [[ ! -f "$LINK_CODE_PATH" && $elapsed_time -lt $max_wait ]]; do + sleep $wait_interval + ((elapsed_time+=wait_interval)) + done + + if [[ -f "$LINK_CODE_PATH" ]]; then + LINK_CODE=$(cat "$LINK_CODE_PATH") + if [[ -z "$LINK_CODE" ]]; then + respond 405 "Failure." + else + respond 200 "$LINK_CODE" + fi else respond 405 "Failure." fi diff --git a/sources/identity/web/templates/main_templ.go b/sources/identity/web/templates/main_templ.go index 199e6a4f..525f2043 100644 --- a/sources/identity/web/templates/main_templ.go +++ b/sources/identity/web/templates/main_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.2.543 +// templ: version: v0.2.598 package templates //lint:file-ignore SA4006 This context is only used if a nested component is present. @@ -32,7 +32,7 @@ func Layout(title string, metaTags, bodyContent templ.Component) templ.Component var templ_7745c5c3_Var2 string templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(title) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web\templates\main.templ`, Line: 13, Col: 17} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates/main.templ`, Line: 13, Col: 17} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) if templ_7745c5c3_Err != nil { diff --git a/sources/identity/web/templates/pages/index_templ.go b/sources/identity/web/templates/pages/index_templ.go index dbc3cf75..3e9b8e36 100644 --- a/sources/identity/web/templates/pages/index_templ.go +++ b/sources/identity/web/templates/pages/index_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.2.543 +// templ: version: v0.2.598 package pages //lint:file-ignore SA4006 This context is only used if a nested component is present.