unetd.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/sh
  2. [ -x /usr/sbin/unetd ] || exit 0
  3. . /lib/functions.sh
  4. . /lib/functions/network.sh
  5. . ../netifd-proto.sh
  6. init_proto "$@"
  7. proto_unet_init_config() {
  8. proto_config_add_string device
  9. proto_config_add_string type
  10. proto_config_add_string auth_key
  11. proto_config_add_string key
  12. proto_config_add_string file
  13. proto_config_add_int keepalive
  14. proto_config_add_string domain
  15. proto_config_add_boolean dht
  16. proto_config_add_array "tunnels:list(string)"
  17. proto_config_add_array "connect:list(string)"
  18. proto_config_add_array "peer_data:list(string)"
  19. no_device=1
  20. available=1
  21. no_proto_task=1
  22. }
  23. proto_unet_setup() {
  24. local config="$1"
  25. local device type key file keepalive domain tunnels
  26. json_get_vars device type auth_key key file keepalive domain dht
  27. json_get_values tunnels tunnels
  28. json_get_values connect connect
  29. json_get_values peer_data peer_data
  30. device="${device:-$config}"
  31. [ -n "$auth_key" ] && type="${type:-dynamic}"
  32. [ -n "$file" ] && type="${type:-file}"
  33. json_init
  34. json_add_string name "$device"
  35. json_add_string type "$type"
  36. json_add_string interface "$config"
  37. json_add_string auth_key "$auth_key"
  38. json_add_string key "$key"
  39. json_add_string file "$file"
  40. [ -n "$keepalive" ] && json_add_int keepalive "$keepalive"
  41. [ -n "$dht" ] && json_add_boolean dht "$dht"
  42. json_add_string domain "$domain"
  43. json_add_object tunnels
  44. for t in $tunnels; do
  45. local ifname="${t%%=*}"
  46. local service="${t#*=}"
  47. [ -n "$ifname" -a -n "$service" -a "$ifname" != "$t" ] || continue
  48. json_add_string "$ifname" "$service"
  49. done
  50. json_close_object
  51. json_add_array auth_connect
  52. for c in $connect; do
  53. json_add_string "" "$c"
  54. done
  55. json_close_array
  56. json_add_array peer_data
  57. for c in $peer_data; do
  58. json_add_string "" "$c"
  59. done
  60. json_close_array
  61. ip link del dev "$device" >/dev/null 2>&1
  62. ip link add dev "$device" type wireguard || {
  63. echo "Could not create wireguard device $device"
  64. proto_setup_failed "$config"
  65. exit 1
  66. }
  67. ubus call unetd network_add "$(json_dump)"
  68. }
  69. proto_unet_teardown() {
  70. local config="$1"
  71. local iface="$2"
  72. local device
  73. json_get_vars device
  74. device="${device:-$iface}"
  75. json_init
  76. json_add_string name "$device"
  77. ip link del dev "$device"
  78. ubus call unetd network_del "$(json_dump)"
  79. }
  80. add_protocol unet