/ lib / _ak_network_cjdns
_ak_network_cjdns
  1  #!/usr/bin/env bash
  2  ###
  3  ### arching-kaos-tools
  4  ### Tools to interact and build an Arching Kaos Infochain
  5  ### Copyright (C) 2021 - 2026  kaotisk
  6  ###
  7  ### This program is free software: you can redistribute it and/or modify
  8  ### it under the terms of the GNU General Public License as published by
  9  ### the Free Software Foundation, either version 3 of the License, or
 10  ### (at your option) any later version.
 11  ###
 12  ### This program is distributed in the hope that it will be useful,
 13  ### but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15  ### GNU General Public License for more details.
 16  ###
 17  ### You should have received a copy of the GNU General Public License
 18  ### along with this program.  If not, see <http://www.gnu.org/licenses/>.
 19  ###
 20  source $AK_LIBDIR/_ak_lib_load
 21  _ak_lib_load _ak_log
 22  
 23  cjdnstoolspath="$HOME/cjdns/tools"
 24  
 25  function _ak_network_cjdns_scan_dump(){
 26      #
 27      # Needs CJDNS tools in your PATH
 28      #
 29      # Ref: https://github.com/cjdelisle/cjdns
 30      #
 31      which dumpLinks > /dev/null 2>&1
 32      if [ $? -ne 0 ]
 33      then
 34          echo "You need to install cjdns and cjdns-tools ... falling back to -w"
 35          _ak_network_cjdns_scan_full
 36          exit 0
 37      fi
 38      which publictoip6 > /dev/null 2>&1
 39      if [ $? -ne 0 ]
 40      then
 41          echo "You need to install cjdns and cjdns-tools ... falling back to -w"
 42          _ak_network_cjdns_scan_full
 43          exit 0
 44      fi
 45      TEMPDIR="$(_ak_make_temp_directory)"
 46      AK_ZPEERSFILE="$HOME/.arching-kaos/peersFile"
 47      cd $TEMPDIR
 48      max="$(dumpLinks |wc -l)"
 49      dumpLinks \
 50          | cut -d ' ' -f 2,4 \
 51          | sed 's/ /\n/g' \
 52          | sort \
 53          | uniq \
 54          | grep -v `dumpLinks | grep '0000\.0000\.0000\.0001' \
 55          | cut -d ' ' -f 2` > dumpedLinks
 56      counter=0
 57      count=0
 58      max="$(cat dumpedLinks| wc -l)"
 59      printf '[' > walk.aknet
 60      cat dumpedLinks \
 61          | while read -r ip || [ -n "$ip" ]
 62      do
 63          count="$(( $count + 1 ))"
 64          _ak_log_debug "Scanning [${count}/${max}] $(publictoip6 $ip)..."
 65          node_info="$(curl \
 66              --connect-timeout 3 \
 67              -A 'akd/0.1.0; https://github.com/arching-kaos' \
 68              "http://[$(publictoip6 $ip)]:8610/v0/node_info" 2>/dev/null | jq -c -M)"
 69          if [ $? -eq 0 ] && [ $(echo -n "$node_info" | wc -c) -gt 0 ]
 70          then
 71              if [ "$counter" -ne "0" ]
 72              then
 73                  printf ',' >> walk.aknet
 74              fi
 75              if [ ! -n "$node_info" ]
 76              then
 77                  node_info="null"
 78              fi
 79              printf '{"cjdns":{"public_key":"%s","ip":"%s"},"node_info":%s}' \
 80                  "$ip" "`publictoip6 $ip`" "$node_info" >> walk.aknet
 81              counter="`expr $counter + 1`"
 82          fi
 83      done
 84      printf ']' >> walk.aknet
 85      mv walk.aknet $AK_ZPEERSFILE.hyperboria
 86      rm -rf $TEMPDIR
 87  }
 88  
 89  function _ak_network_cjdns_scan_full(){
 90      # This scan is using HIA resources to scan the whole cjdns network for peers
 91      #
 92      # Ref:
 93      #  - https://github.com/ircerr/hia/
 94      #  - http://hia.cjdns.ca/watchlist/c/walk.pubkey
 95      #  - http://hia.cjdns.ca/watchlist/hia.iplist
 96      #
 97      TEMPDIR="$(_ak_make_temp_directory)"
 98      cd $TEMPDIR
 99      pubkeys_ips="$TEMPDIR/pi"
100      online_ips="$TEMPDIR/oi"
101      filtered_online_pubkeys_ips="$TEMPDIR/fopi"
102      curl -s \
103          --connect-timeout 5 \
104          "http://hia.cjdns.ca/watchlist/c/walk.pubkey" > $pubkeys_ips
105      if [ $? -ne 0 ]
106      then
107          _ak_log_error "Couldn't fetch DB from HIA"
108          exit 1
109      fi
110      curl -s \
111          --connect-timeout 5 \
112          "http://hia.cjdns.ca/watchlist/hia.iplist" > $online_ips
113      if [ $? -ne 0 ]
114      then
115          _ak_log_error "Couldn't fetch iplist from HIA"
116          exit 1
117      fi
118      cat $online_ips | while read line
119      do
120          grep -F "$line" $pubkeys_ips >> $filtered_online_pubkeys_ips
121      done
122      counter=0
123      count=0
124      max="$(cat $filtered_online_pubkeys_ips|wc -l)"
125      printf '[' > walk.aknet
126      cat $filtered_online_pubkeys_ips \
127          | sort \
128          | uniq \
129          | while read -r pkey ip || [ -n "$ip" ]
130      do
131          count="$(( $count + 1 ))"
132          _ak_log_debug "Scanning [${count}/${max}] $ip..."
133          node_info="$(curl \
134              --connect-timeout 3 \
135              -A 'akd/0.1.0; https://github.com/arching-kaos' \
136              "http://[$ip]:8610/v0/node_info" 2>/dev/null | jq -c -M)"
137          if [ $? -eq 0 ] && [ $(echo -n "$node_info" | wc -c) -gt 0 ]
138          then
139              if [ "$counter" -ne "0" ]
140              then
141                  printf ',' >> walk.aknet
142              fi
143              if [ ! -n "$node_info" ]
144              then
145                  node_info="null"
146              fi
147              printf '{"cjdns":{"public_key":"%s","ip":"%s"},"node_info":%s}' \
148                  "$pkey" "$ip" "$node_info" >> walk.aknet
149              counter="`expr $counter + 1`"
150          fi
151      done
152      printf ']' >> walk.aknet
153      mv walk.aknet $AK_ZPEERSFILE.hyperboria
154      rm -rf $TEMPDIR
155  }
156  
157  function _ak_network_cjdns_scan(){
158      if [ ! -z $1 ] && [ -n "$1" ]
159      then
160          case $1 in
161              -w | --whole) _ak_network_cjdns_scan_full; exit;;
162              -d | --dump) _ak_network_cjdns_scan_dump; exit;;
163              *) _ak_network_cjdns_scan_dump; exit;;
164          esac
165      else
166          _ak_network_cjdns_scan_dump
167      fi
168  }
169  
170  function _ak_network_cjdns_show_peers(){
171      if [ -f $AK_ZPEERSFILE.hyperboria ]
172      then
173          cat $AK_ZPEERSFILE.hyperboria | jq
174      else
175          _ak_log_debug "No hyperboria peers found"
176      fi
177  }
178  
179  function _ak_network_cjdns_connect(){
180      if [ ! -z $1 ] && [ -n "$1" ] && [ -f $1 ]
181      then
182          peersfile="$1"
183      else
184          peersfile="$(_ak_make_temp_file)"
185          tmpon="yes"
186          curl -s "https://arching-kaos.net/files/ak_cjdns_bootstrap_peers" > $peersfile
187      fi
188      totalpeers="$(jq '. | length' < $peersfile)"
189      number="0"
190      interface="0"
191      while [ $number -lt $totalpeers ]
192      do
193          address="$(jq -r '.['$number'].address' < $peersfile)"
194          login="$(jq -r '.['$number'].login' < $peersfile)"
195          password="$(jq -r '.['$number'].password' < $peersfile)"
196          publicKey="$(jq -r '.['$number'].publicKey' < $peersfile)"
197          peerName="$(jq -r '.['$number'].peerName' < $peersfile)"
198          if [ $(echo $address | grep '\[') ]
199          then
200              interface="1"
201          else
202              interface="0"
203          fi
204          $cjdnstoolspath/cexec 'UDPInterface_beginConnection("'$publicKey'", "'$address'", "'$peerName'", "'$password'", "'$login'", '$interface')'
205          number="$(( $number + 1 ))"
206      done
207      if [ "$tmpon" == "yes" ]
208      then
209          rm $peersfile
210      fi
211  }
212  
213  function _ak_network_cjdns_connect_bootstrap(){
214      _ak_network_cjdns_connect
215  }
216  
217  _ak_log_debug "_ak_network_cjdns loaded $(caller)"