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.

73 lines
1.6 KiB

  1. . /usr/share/libubox/jshn.sh
  2. . /usr/share/wginstaller/wg.sh
  3. wg_timeout () {
  4. local int=$1
  5. handshake=$(wg show $int latest-handshakes | awk '{print $2}')
  6. timeout=$(uci get wgserver.@server[0].timeout_handshake)
  7. if [ $handshake -ge $timeout ]; then
  8. echo "1"
  9. else
  10. echo "0"
  11. fi
  12. }
  13. wg_check_interface () {
  14. local int=$1
  15. if [ $(wg_timeout $int) -eq "1" ]; then
  16. ip link del dev $int
  17. fi
  18. }
  19. wg_check_interfaces () {
  20. wg_interfaces=$(wg show interfaces)
  21. for interface in $wg_interfaces; do
  22. wg_check_interface $interface
  23. done
  24. }
  25. wg_get_usage () {
  26. num_interfaces=$(wg show interfaces | wc -w)
  27. json_init
  28. json_add_int "num_interfaces" $num_interfaces
  29. echo $(json_dump)
  30. }
  31. wg_register () {
  32. local uplink_bw=$1
  33. local mtu=$2
  34. local public_key=$3
  35. base_prefix=$(uci get wgserver.@server[0].base_prefix)
  36. port_start=$(uci get wgserver.@server[0].port_start)
  37. port_end=$(uci get wgserver.@server[0].port_end)
  38. port=$(next_port $port_start $port_end)
  39. ifname="wg_$port"
  40. offset=$(($port - $port_start))
  41. gw_ip=$(owipcalc $base_prefix add $offset next 128) # gateway ip
  42. gw_ip_assign="${gw_ip}/128"
  43. gw_key=$(uci get wgserver.@server[0].wg_key)
  44. gw_pub=$(uci get wgserver.@server[0].wg_pub)
  45. wg_server_pubkey=$(cat $gw_pub)
  46. # create wg tunnel
  47. ip link add dev $ifname type wireguard
  48. wg set $ifname listen-port $port private-key $gw_key peer $public_key allowed-ips ::0/0
  49. ip -6 a a $gw_ip_assign dev $ifname
  50. ip -6 a a fe80::1/64 dev $ifname
  51. ip link set up dev $ifname
  52. ip link set mtu $mtu dev $ifname
  53. # craft return address
  54. json_init
  55. json_add_string "pubkey" $wg_server_pubkey
  56. json_add_string "gw_ip" $gw_ip_assign
  57. json_add_int "port" $port
  58. echo $(json_dump)
  59. }