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.

111 lines
2.8 KiB

  1. #!/bin/sh
  2. . /usr/share/libubox/jshn.sh
  3. . /usr/share/wginstaller/wg.sh
  4. wg_key_exists () {
  5. local key=$1
  6. wg show | grep -q "$key"
  7. }
  8. wg_timeout () {
  9. local int=$1
  10. handshake=$(wg show "$int" latest-handshakes | awk '{print $2}')
  11. timeout=$(uci get wgserver.@server[0].timeout_handshake)
  12. if [ "$handshake" -ge "$timeout" ]; then
  13. echo "1"
  14. else
  15. echo "0"
  16. fi
  17. }
  18. wg_check_interface () {
  19. local int=$1
  20. if [ "$(wg_timeout "$int")" -eq "1" ]; then
  21. ip link del dev "$int"
  22. fi
  23. }
  24. wg_check_interfaces () {
  25. wg_interfaces=$(wg show interfaces)
  26. for interface in $wg_interfaces; do
  27. wg_check_interface "$interface"
  28. done
  29. }
  30. wg_get_usage () {
  31. num_interfaces=$(wg show interfaces | wc -w)
  32. json_init
  33. json_add_int "num_interfaces" "$num_interfaces"
  34. json_dump
  35. }
  36. wg_register () {
  37. local uplink_bw=$1
  38. local mtu=$2
  39. local public_key=$3
  40. if wg_key_exists $public_key; then
  41. logger -t "wginstaller" "Rejecting request since the public key is already used!" "$public_key"
  42. json_init
  43. json_add_int "response_code" 1
  44. json_dump
  45. return 1
  46. fi
  47. base_prefix_ipv6=$(uci get wgserver.@server[0].base_prefix_ipv6)
  48. port_start=$(uci get wgserver.@server[0].port_start)
  49. port_end=$(uci get wgserver.@server[0].port_end)
  50. port=$(next_port "$port_start" "$port_end")
  51. ifname="wg_$port"
  52. offset=$((port - port_start))
  53. gw_ipv6=$(owipcalc "$base_prefix_ipv6" add "$offset" next 128) # gateway ip
  54. gw_ipv6_assign="${gw_ipv6}/128"
  55. gw_key=$(uci get wgserver.@server[0].wg_key)
  56. gw_pub=$(uci get wgserver.@server[0].wg_pub)
  57. if [ "$(uci get wgserver.@server[0].wg_tmp_key)" -eq 1 ]; then
  58. [ -d "/tmp/run/wgserver" ] || mkdir -p /tmp/run/wgserver
  59. gw_key="/tmp/run/wgserver/${ifname}.key"
  60. gw_pub="/tmp/run/wgserver/${ifname}.pub"
  61. wg genkey | tee "$gw_key" | wg pubkey > "$gw_pub"
  62. else
  63. [ -d "$(dirname $gw_key)" ] || mkdir -p "$(dirname $gw_key)"
  64. [ -f "$gw_key" ] || wg genkey | tee "$gw_key" | wg pubkey > "$gw_pub"
  65. fi
  66. wg_server_pubkey=$(cat "$gw_pub")
  67. # create wg tunnel
  68. ip link add dev "$ifname" type wireguard
  69. wg set "$ifname" listen-port "$port" private-key "$gw_key" peer "$public_key" allowed-ips 0.0.0.0/0,::0/0
  70. ip -6 addr add "$gw_ipv6_assign" dev "$ifname"
  71. ip -6 addr add fe80::1/64 dev "$ifname"
  72. base_prefix_ipv4=$(uci get wgserver.@server[0].base_prefix_ipv4)
  73. if [ $? -eq 0 ]; then
  74. gw_ipv4=$(owipcalc "$base_prefix_ipv4" add "$offset" next 32) # gateway ip
  75. gw_ipv4_assign="${gw_ipv4}/32"
  76. ip addr add "$gw_ipv4_assign" broadcast 255.255.255.255 dev "$ifname"
  77. fi
  78. ip link set up dev "$ifname"
  79. ip link set mtu "$mtu" dev "$ifname"
  80. # craft return address
  81. json_init
  82. json_add_int "response_code" 0
  83. json_add_string "gw_pubkey" "$wg_server_pubkey"
  84. if test -n "${gw_ipv4_assign-}"; then
  85. json_add_string "gw_ipv4" "$gw_ipv4_assign"
  86. fi
  87. json_add_string "gw_ipv6" "$gw_ipv6_assign"
  88. json_add_int "gw_port" "$port"
  89. json_dump
  90. }