zerotier.init 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 interface config_path
  13. local ARGS=""
  14. section_enabled "$cfg" || return 1
  15. config_get config_path $cfg 'config_path'
  16. config_get_bool port $cfg 'port'
  17. config_get secret $cfg 'secret'
  18. config_get interface $cfg 'interface'
  19. # Remove existing link or folder
  20. rm -rf $CONFIG_PATH
  21. # Create link from CONFIG_PATH to config_path
  22. if [ -n "$config_path" -a $config_path != $CONFIG_PATH ]; then
  23. if [ ! -d "$config_path" ]; then
  24. echo "ZeroTier config_path does not exist: $config_path"
  25. return
  26. fi
  27. ln -s $config_path $CONFIG_PATH
  28. fi
  29. mkdir -p $CONFIG_PATH/networks.d
  30. if [ -n "$port" ]; then
  31. ARGS="$ARGS -p$port"
  32. fi
  33. if [ "$secret" = "generate" ]; then
  34. echo "Generate secret - please wait..."
  35. local tmp="/tmp/zt.$cfg.secret"
  36. zerotier-idtool generate "$tmp" > /dev/null
  37. secret="$(cat $tmp)"
  38. rm "$tmp"
  39. uci set zerotier.$cfg.secret="$secret"
  40. uci commit zerotier
  41. fi
  42. if [ -n "$secret" ]; then
  43. echo "$secret" > $CONFIG_PATH/identity.secret
  44. # make sure there is not previous identity.public
  45. rm -f $CONFIG_PATH/identity.public
  46. fi
  47. add_join() {
  48. # an (empty) config file will cause ZT to join a network
  49. touch $CONFIG_PATH/networks.d/$1.conf
  50. }
  51. config_list_foreach $cfg 'join' add_join
  52. procd_open_instance
  53. procd_add_reload_interface_trigger "$interface"
  54. procd_set_param command $PROG $ARGS $CONFIG_PATH
  55. procd_close_instance
  56. }
  57. service_triggers() {
  58. procd_add_reload_trigger zerotier
  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. }