peers.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env bash
  2. set -ex
  3. # Never again restart cjdroute for new peerings!
  4. #
  5. # Eases peering by updating both cjdroute.conf and the running cjdroute. Also
  6. # produces a JSON config fragment to give to your new peer.
  7. #
  8. # Usage:
  9. # cd cjdns/
  10. # contrib/bash/peers.sh jane jane@example.net <jane's ipv6>
  11. #
  12. # Requires:
  13. # - $HOME/.cjdnsadmin
  14. # - writable cjdroute.conf
  15. #
  16. # TODO:
  17. # - unwrap the handout object, so that it can actually be copy-pasted
  18. #
  19. HANDOUT=/opt/handout.json
  20. CJDROUTECONF=/etc/cjdroute.conf
  21. CJDNS_DIR=/opt/cjdns
  22. if [ ! -f $HANDOUT ]; then
  23. echo "Please copy $CJDNS_DIR/contrib/bash/handout.json to $HANDOUT and edit it according to your preference."
  24. exit 1
  25. fi
  26. cd $CJDNS_DIR
  27. git checkout `cat REVISION`
  28. user=$1
  29. contact=$2
  30. ipv6=$3
  31. password=`./makekeys | head -c32`
  32. publicKey=`cat $CJDROUTECONF | cjdroute --cleanconf | jq -r '.publicKey'`
  33. localIpv6=`./publictoip6 $publicKey`
  34. # add password to running cjdroute process, using admin api
  35. response=`./contrib/python/cexec -p "AuthorizedPasswords_add(\"$password\", \"$user\", 1, \"$ipv6\")"`
  36. echo $response | grep 'none' > /dev/null
  37. if [ $? -ne 0 ]; then echo "admin error: $response" ; exit 1; fi
  38. echo "Added [$user] to the currently running cjdroute"
  39. # write password to config file
  40. cp $CJDROUTECONF $CJDROUTECONF.backup
  41. cat $CJDROUTECONF.backup | cjdroute --cleanconf | jq ".authorizedPasswords |= . + [{user: \"$user\", contact: \"$contact\", ipv6: \"$ipv6\", password: \"$password\"}]" > $CJDROUTECONF
  42. echo "Added [$user] to $CJDROUTECONF"
  43. # print config snippet to hand out to the peer
  44. echo "Config snippet to hand out:"
  45. cat $HANDOUT | jq ".[].ipv6 = \"$localIpv6\"" | jq ".[].publicKey = \"$publicKey\"" | jq ".[].password = \"$password\""