You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
1.8 KiB

  1. #!/bin/sh /etc/rc.common
  2. START=90
  3. USE_PROCD=1
  4. PROG=/usr/bin/zerotier-one
  5. CONFIG_PATH=/var/lib/zerotier-one
  6. section_enabled() {
  7. config_get_bool enabled "$1" 'enabled' 0
  8. [ $enabled -gt 0 ]
  9. }
  10. start_instance() {
  11. local cfg="$1"
  12. local port secret config_path
  13. local ARGS=""
  14. if ! section_enabled "$cfg"; then
  15. echo "disabled in config"
  16. return 1
  17. fi
  18. config_get config_path $cfg 'config_path'
  19. config_get_bool port $cfg 'port'
  20. config_get secret $cfg 'secret'
  21. # Remove existing link or folder
  22. rm -rf $CONFIG_PATH
  23. # Create link from CONFIG_PATH to config_path
  24. if [ -n "$config_path" -a "$config_path" != $CONFIG_PATH ]; then
  25. if [ ! -d "$config_path" ]; then
  26. echo "ZeroTier config_path does not exist: $config_path"
  27. return
  28. fi
  29. ln -s $config_path $CONFIG_PATH
  30. fi
  31. mkdir -p $CONFIG_PATH/networks.d
  32. if [ -n "$port" ]; then
  33. ARGS="$ARGS -p$port"
  34. fi
  35. if [ "$secret" = "generate" ]; then
  36. echo "Generate secret - please wait..."
  37. local sf="/tmp/zt.$cfg.secret"
  38. zerotier-idtool generate "$sf" > /dev/null
  39. [ $? -ne 0 ] && return 1
  40. secret="$(cat $sf)"
  41. rm "$sf"
  42. uci set zerotier.$cfg.secret="$secret"
  43. uci commit zerotier
  44. fi
  45. if [ -n "$secret" ]; then
  46. echo "$secret" > $CONFIG_PATH/identity.secret
  47. # make sure there is not previous identity.public
  48. rm -f $CONFIG_PATH/identity.public
  49. fi
  50. add_join() {
  51. # an (empty) config file will cause ZT to join a network
  52. touch $CONFIG_PATH/networks.d/$1.conf
  53. }
  54. config_list_foreach $cfg 'join' add_join
  55. procd_open_instance
  56. procd_set_param command $PROG $ARGS $CONFIG_PATH
  57. procd_set_param stderr 1
  58. procd_close_instance
  59. }
  60. start_service() {
  61. config_load 'zerotier'
  62. config_foreach start_instance 'zerotier'
  63. }
  64. stop_instance() {
  65. local cfg="$1"
  66. # Remove existing link or folder
  67. rm -rf $CONFIG_PATH
  68. }
  69. stop_service() {
  70. config_load 'zerotier'
  71. config_foreach stop_instance 'zerotier'
  72. }