diff options
-rwxr-xr-x | bin/ak-cjdns | 35 | ||||
-rwxr-xr-x | cjdns-full-installer.sh | 33 | ||||
-rwxr-xr-x | full_test_with_podman.sh | 2 | ||||
-rwxr-xr-x | lib/_ak_cjdns | 170 |
4 files changed, 206 insertions, 34 deletions
diff --git a/bin/ak-cjdns b/bin/ak-cjdns new file mode 100755 index 0000000..e7342ac --- /dev/null +++ b/bin/ak-cjdns @@ -0,0 +1,35 @@ +#!/bin/bash +## +## cjdns helper +## +## Usage: +## +## -h, --help Prints this help message +## +## --check Check for binaries +## +## --install Install cjdns +## +## --connect Connect to ak cjdns nodes +## +fullprogrampath="$(realpath $0)" +PROGRAM=$(basename $0) +descriptionString="cjdns helper" + +# At least these +source $AK_LIBDIR/_ak_log +source $AK_LIBDIR/_ak_script +source $AK_LIBDIR/_ak_cjdns + +# Flags to run +if [ ! -z $1 ] +then + case $1 in + -h | --help) _ak_usage; exit;; + --check) _ak_cjdns_check_availability; exit;; + --install) _ak_cjdns_install; exit;; + --connect) _ak_cjdns_connect_peers; exit;; + * ) _ak_usage;; + esac +else _ak_usage +fi diff --git a/cjdns-full-installer.sh b/cjdns-full-installer.sh deleted file mode 100755 index b9ab3cf..0000000 --- a/cjdns-full-installer.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/sh -curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -source "$HOME/.cargo/env" -git clone https://github.com/cjdelisle/cjdns -cd cjdns -./do -if [ $? -ne 0 ] -then - echo "Failed to compile cjdns" - exit 1 -fi -cd .. - -# Copy binaries to /usr/bin -sudo cp cjdns/cjdroute /usr/bin/ -sudo cp cjdns/target/release/makekeys /usr/bin/ -sudo cp cjdns/target/release/mkpasswd /usr/bin/ -sudo cp cjdns/target/release/privatetopublic /usr/bin/ -sudo cp cjdns/target/release/publictoip6 /usr/bin/ -sudo cp cjdns/target/release/randombytes /usr/bin/ -sudo cp cjdns/target/release/sybilsim /usr/bin/ - -# Copy cjdns tools to $AK_WORKDIR/bin -ln -s `realpath cjdns/tools/dumpLinks` ~/.arching-kaos/bin/dumpLinks -ln -s `realpath cjdns/tools/cexec` ~/.arching-kaos/bin/cjdns-cexec -ln -s `realpath cjdns/tools/peerStats` ~/.arching-kaos/bin/peerStats - -# Systemd setup -sudo cp cjdns/contrib/systemd/cjdns.service /etc/systemd/system/ -sudo cp cjdns/contrib/systemd/cjdns-resume.service /etc/systemd/system -sudo systemctl enable --now cjdns.service - -# TODO Or openrc diff --git a/full_test_with_podman.sh b/full_test_with_podman.sh index 8948ec7..517175e 100755 --- a/full_test_with_podman.sh +++ b/full_test_with_podman.sh @@ -5,7 +5,7 @@ then tag="$(bash ./test_with_podman.sh "$1" | tail -n 2 | head -n 1 | cut -d ' ' -f 3)" name="$(echo -n $tag | cut -d ':' -f 1 | cut -d '/' -f 2)" container="akt-test-$name" - podman run --name $container -d -p 8620:8610 $tag + podman run --name $container -d -p 8620:8610 --cap-add=NET_ADMIN --device /dev/net/tun $tag podman exec -it $container bash podman container stop $container podman container rm $container diff --git a/lib/_ak_cjdns b/lib/_ak_cjdns new file mode 100755 index 0000000..0eda944 --- /dev/null +++ b/lib/_ak_cjdns @@ -0,0 +1,170 @@ +#!/bin/bash + +source $AK_LIBDIR/_ak_log +source $AK_LIBDIR/_ak_network +rust_sh_install_url="https://sh.rustup.rs" +cjdns_src_git_repo_url="https://github.com/cjdelisle/cjdns" +cjdnstoolspath="$HOME/cjdns/tools" +cargo_env="$HOME/.cargo/env" + +_ak_cjdns_check_availability(){ + declare -a cjdns_bins=("cjdroute" "makekeys" "mkpasswd" "privatetopublic" "publictoip6" "randombytes" "sybilsim") + for cbin in "${cjdns_bins[@]}" + do + which $cbin > /dev/null 2>&1 + if [ $? -ne 0 ] + then + _ak_log_error "$cbin not found" + exit 1 + else + _ak_log_info "$cbin found" + fi + done +} + +_ak_cargo_rust_check_install(){ + if [ ! -f $cargo_env ] + then + curl --proto '=https' --tlsv1.2 -sSf $rust_sh_install_url | sh + fi + source $cargo_env +} + +_ak_cjdns_install(){ + _ak_cargo_rust_check_install + git clone $cjdns_src_git_repo_url + cd cjdns + sh do + if [ $? -ne 0 ] + then + _ak_log_error "Failed to compile cjdns" + exit 1 + fi + cd .. + sudo cp cjdns/cjdroute /usr/bin/cjdroute + sudo cp cjdns/target/release/makekeys /usr/bin/makekeys + sudo cp cjdns/target/release/mkpasswd /usr/bin/mkpasswd + sudo cp cjdns/target/release/privatetopublic /usr/bin/privatetopublic + sudo cp cjdns/target/release/publictoip6 /usr/bin/publictoip6 + sudo cp cjdns/target/release/randombytes /usr/bin/randombytes + sudo cp cjdns/target/release/sybilsim /usr/bin/sybilsim + ln -s "$(realpath cjdns/tools/dumpLinks)" $HOME/.arching-kaos/bin/dumpLinks + ln -s "$(realpath cjdns/tools/cexec)" $HOME/.arching-kaos/bin/cjdns-cexec + ln -s "$(realpath cjdns/tools/peerStats)" $HOME/.arching-kaos/bin/peerStats + which systemctl 2> /dev/null 1>&2 + if [ $? -ne 0 ] + then + _ak_log_error "Systemctl not found... TODO" + else + sudo cp "$(realpath cjdns/contrib/systemd/cjdns.service)" /etc/systemd/system/cjdns.service + sudo cp "$(realpath cjdns/contrib/systemd/cjdns-resume.service)" /etc/systemd/system/cjdns-resume.service + sudo systemctl enable --now cjdns.service + fi +} + +_ak_cjdns_read_peers_to_vars_with_jq(){ + totalpeers="$(jq '. | length' < $peersfile)" + number="0" + interface="0" + + while [ $number -lt $totalpeers ] + do + address="$(jq -r '.['$number'].address' < $peersfile)" + login="$(jq -r '.['$number'].login' < $peersfile)" + password="$(jq -r '.['$number'].password' < $peersfile)" + publicKey="$(jq -r '.['$number'].publicKey' < $peersfile)" + peerName="$(jq -r '.['$number'].peerName' < $peersfile)" + if [ $(echo $address | grep '\[') ] + then + interface="1" + else + interface="0" + fi + $cjdnstoolspath/cexec 'UDPInterface_beginConnection("'$publicKey'", "'$address'", "'$peerName'", "'$password'", "'$login'", '$interface')' + number="$(( $number + 1 ))" + done +} + +_ak_cjdns_read_peers_to_vars_natively(){ + number=-1 + cat $peersfile | tr -d $'\n' | sed -e 's/]$/\n/g' | tr -d ' ' | sed -e 's/"//g; s/,/,\n/g; s/}//g; s/,//g' | while read line + do + if [ $(echo "$line" | grep '{') ] + then + number=$(($number + 1)) + if [ $number -ne 0 ] + then + printf '\n' >> peerfile + fi + fi + if [ $(echo "$line" | grep 'address') ] + then + printf '%s ' "$(echo -n $line | cut -d ':' -f 2-)" >> peerfile + fi + if [ $(echo "$line" | grep 'password') ] + then + printf '%s ' "$(echo -n $line | cut -d ':' -f 2-)" >> peerfile + fi + if [ $(echo "$line" | grep 'publicKey') ] + then + printf '%s ' "$(echo -n $line | cut -d ':' -f 2-)" >> peerfile + fi + if [ $(echo "$line" | grep 'login') ] + then + printf '%s ' "$(echo -n $line | cut -d ':' -f 2-)" >> peerfile + fi + if [ $(echo "$line" | grep 'peerName') ] + then + printf '%s ' "$(echo -n $line | cut -d ':' -f 2-)" >> peerfile + fi + done + printf '\n' >> peerfile + cat peerfile | while read address login password publicKey peerName + do + if [ $(echo $address | grep '\[') ] + then + interface="1" + else + interface="0" + fi + $cjdnstoolspath/cexec 'UDPInterface_beginConnection("'$publicKey'", "'$address'", "'$peerName'", "'$password'", "'$login'", '$interface')' + done + rm peerfile +} + + +_ak_cjdns_connect_peers(){ + _ak_network_cjdns_connect + exit $? + # + # Peers file have to look like this: + # + # [ + # { + # "address": "<IPv4|IPv6>:<port>", + # "login": "<login-name>", + # "password": "<password...>", + # "publicKey": "<publickey with .k suffix>", + # "peerName": "<peername>" + # }, + # { ... } <- more peers + # ] + # + # You can have both IPv4 and IPv6 peers on the same file + # + # Assumes there is ~/cjdns/tools/cexec in place, change it below + # + if [ ! -z $1 ] && [ -n "$1" ] && [ -f $1 ] + then + peersfile="$1" + else + echo "Usage: $(basename $0) <json-peer-list-file>" + exit 1 + fi + if command -v jq + then + _ak_cjdns_read_peers_to_vars_with_jq + else + _ak_cjdns_read_peers_to_vars_natively + fi +} |