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.

109 lines
2.2 KiB

  1. #!/bin/sh
  2. . /usr/share/wginstaller/rpcd_ubus.sh
  3. . /usr/share/wginstaller/wg.sh
  4. DEFAULT_NAMESPACE=0
  5. CMD=$1
  6. shift
  7. while true; do
  8. case "$1" in
  9. -h | --help)
  10. echo "help"
  11. shift 1
  12. ;;
  13. --endpoint)
  14. ENDPOINT=$2
  15. shift 2
  16. ;;
  17. --user)
  18. USER=$2
  19. shift 2
  20. ;;
  21. --password)
  22. PASSWORD=$2
  23. shift 2
  24. ;;
  25. --mtu)
  26. WG_MTU=$2
  27. shift 2
  28. ;;
  29. --wg-key-file)
  30. WG_KEY_FILE=$2
  31. shift 2
  32. ;;
  33. --lookup-default-namespace)
  34. DEFAULT_NAMESPACE=1
  35. shift 1
  36. ;;
  37. '')
  38. break
  39. ;;
  40. *)
  41. break
  42. ;;
  43. esac
  44. done
  45. register_client_interface () {
  46. local endpoint=$2
  47. local mtu_client=$3
  48. local privkey=$4
  49. local pubkey=$5
  50. local gw_port=$6
  51. local def_namespace=$7
  52. port_start=$(uci get wgclient.@client[0].port_start)
  53. port_end=$(uci get wgclient.@client[0].port_end)
  54. if [ "$def_namespace" -eq "1" ]; then
  55. [ -f /var/run/netns/default ] || ln -s /proc/1/ns/net /var/run/netns/default
  56. port=$(ip netns exec default /usr/share/wginstaller/wg.sh next_port "$port_start" "$port_end")
  57. else
  58. port=$(next_port "$port_start" "$port_end")
  59. fi
  60. ifname="wg_$port"
  61. ip link add dev "$ifname" type wireguard
  62. ip -6 addr add dev "$ifname" fe80::2/64
  63. wg set "$ifname" listen-port "$port" private-key "$privkey" peer "$pubkey" allowed-ips 0.0.0.0/0,::0/0 endpoint "${endpoint}:${gw_port}"
  64. ip link set up dev "$ifname"
  65. ip link set mtu "$mtu_client" dev "$ifname"
  66. export "$1=$ifname"
  67. }
  68. # rpc login
  69. token="$(request_token "$ENDPOINT" "$USER" "$PASSWORD")"
  70. if [ $? -ne 0 ]; then
  71. logger -t "wg-client-installer" "Failed to register token!"
  72. exit 1
  73. fi
  74. # now call procedure
  75. case $CMD in
  76. "get_usage")
  77. wg_rpcd_get_usage "$token" "$ENDPOINT"
  78. ;;
  79. "register")
  80. if [ -n "$WG_KEY_FILE" ]; then
  81. wg_priv_key_file="$WG_KEY_FILE"
  82. wg_pub_key=$(wg pubkey < "$WG_KEY_FILE")
  83. fi
  84. wg_rpcd_register __gw_pubkey __gw_ipv4 __gw_ipv6 __gw_port "$token" "$ENDPOINT" "$WG_MTU" "$wg_pub_key"
  85. if [ $? -ne 0 ]; then
  86. logger -t "wg-client-installer" "Failed to Register!"
  87. exit 1
  88. fi
  89. register_client_interface __interface "$ENDPOINT" "$WG_MTU" "$wg_priv_key_file" "$__gw_pubkey" "$__gw_port" "$DEFAULT_NAMESPACE"
  90. logger -t "wg-client-installer" "Registered: $__interface"
  91. echo $__interface
  92. ;;
  93. *) echo "Usage: wg-client-installer [cmd] --endpoint [2001::1] --mtu 1500 --user wginstaller --password wginstaller" ;;
  94. esac