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.

103 lines
1.9 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 -ne 0 ]
  9. }
  10. start_instance() {
  11. local cfg="$1"
  12. local port secret config_path 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 port $cfg 'port'
  20. config_get secret $cfg 'secret'
  21. path=${CONFIG_PATH}_$cfg
  22. # Remove existing link or folder
  23. rm -rf $path
  24. # Create link from CONFIG_PATH to config_path
  25. if [ -n "$config_path" -a "$config_path" != "$path" ]; then
  26. if [ ! -d "$config_path" ]; then
  27. echo "ZeroTier config_path does not exist: $config_path" 1>&2
  28. return
  29. fi
  30. ln -s $config_path $path
  31. fi
  32. mkdir -p $path/networks.d
  33. # link latest default config path to latest config path
  34. rm -f $CONFIG_PATH
  35. ln -s $path $CONFIG_PATH
  36. if [ -n "$port" ]; then
  37. args="$args -p${port}"
  38. fi
  39. if [ -z "$secret" ]; then
  40. echo "Generate secret - please wait..."
  41. local sf="/tmp/zt.$cfg.secret"
  42. zerotier-idtool generate "$sf" > /dev/null
  43. [ $? -ne 0 ] && return 1
  44. secret="$(cat $sf)"
  45. rm "$sf"
  46. uci set zerotier.$cfg.secret="$secret"
  47. uci commit zerotier
  48. fi
  49. if [ -n "$secret" ]; then
  50. echo "$secret" > $path/identity.secret
  51. # make sure there is not previous identity.public
  52. rm -f $path/identity.public
  53. fi
  54. add_join() {
  55. # an (empty) config file will cause ZT to join a network
  56. touch $path/networks.d/$1.conf
  57. }
  58. config_list_foreach $cfg 'join' add_join
  59. procd_open_instance
  60. procd_set_param command $PROG $args $path
  61. procd_set_param stderr 1
  62. procd_close_instance
  63. }
  64. start_service() {
  65. config_load 'zerotier'
  66. config_foreach start_instance 'zerotier'
  67. }
  68. stop_instance() {
  69. local cfg="$1"
  70. # Remove existing link or folder
  71. rm -rf ${CONFIG_PATH}_${cfg}
  72. }
  73. stop_service() {
  74. config_load 'zerotier'
  75. config_foreach stop_instance 'zerotier'
  76. rm -f ${CONFIG_PATH}
  77. }