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.

119 lines
2.4 KiB

  1. #!/bin/sh
  2. . /usr/share/wginstaller/rpcd_ubus.sh
  3. . /usr/share/wginstaller/wg.sh
  4. CMD=$1
  5. shift
  6. while true; do
  7. case "$1" in
  8. -h | --help)
  9. echo "help"
  10. shift 1
  11. ;;
  12. -i | --ip)
  13. IP=$2
  14. shift 2
  15. ;;
  16. --user)
  17. USER=$2
  18. shift 2
  19. ;;
  20. --password)
  21. PASSWORD=$2
  22. shift 2
  23. ;;
  24. --bandwidth)
  25. BANDWIDTH=$2
  26. shift 2
  27. ;;
  28. --mtu)
  29. WG_MTU=$2
  30. shift 2
  31. ;;
  32. '')
  33. break
  34. ;;
  35. *)
  36. break
  37. ;;
  38. esac
  39. done
  40. escape_ip () {
  41. local gw_ip=$1
  42. # ipv4 processing
  43. ret_ip=$(echo $gw_ip | tr '.' '_')
  44. # ipv6 processing
  45. ret_ip=$(echo $ret_ip | tr ':' '_')
  46. ret_ip=$(echo $ret_ip | cut -d '[' -f 2)
  47. ret_ip=$(echo $ret_ip | cut -d ']' -f 1)
  48. echo $ret_ip
  49. }
  50. register_client_interface () {
  51. local pubkey=$1
  52. local gw_ip=$2
  53. local gw_port=$3
  54. local endpoint=$4
  55. local mtu_client=$5
  56. gw_key=$(uci get wgclient.@client[0].wg_key)
  57. interface_name="gw_$(escape_ip $endpoint)"
  58. port_start=$(uci get wgclient.@client[0].port_start)
  59. port_end=$(uci get wgclient.@client[0].port_end)
  60. base_prefix=$(uci get wgclient.@client[0].base_prefix)
  61. port=$(next_port $port_start $port_end)
  62. ifname="wg_$port"
  63. offset=$(($port - $port_start))
  64. client_ip=$(owipcalc $base_prefix add $offset next 128)
  65. client_ip_assign="${client_ip}/128"
  66. echo "Installing Interface With:"
  67. echo "Endpoint ${endpoint}"
  68. echo "Client IP ${client_ip}"
  69. echo "Port ${port}"
  70. echo "Pubkey ${pubkey}"
  71. ip link add dev $ifname type wireguard
  72. ip -6 a a dev $ifname $client_ip
  73. wg set $ifname listen-port $port private-key $gw_key peer $pubkey allowed-ips ::/0 endpoint "${endpoint}:${gw_port}"
  74. ip link set up dev $ifname
  75. ip link set mtu $mtu_client dev $ifname # configure mtu here!
  76. }
  77. # rpc login
  78. token="$(request_token $IP $USER $PASSWORD)"
  79. if [ $? != 0 ]; then
  80. echo "failed to register token"
  81. exit 1
  82. fi
  83. # now call procedure
  84. case $CMD in
  85. "get_usage")
  86. wg_rpcd_get_usage $token $IP
  87. ;;
  88. "register")
  89. gw_pub=$(uci get wgclient.@client[0].wg_pub)
  90. gw_pub_string=$(cat $gw_pub)
  91. register_output=$(wg_rpcd_register $token $IP $BANDWIDTH $WG_MTU $gw_pub_string)
  92. if [ $? != 0 ]; then
  93. echo "Failed to Register!"
  94. exit 1
  95. fi
  96. pubkey=$(echo $register_output | awk '{print $2}')
  97. ip_addr=$(echo $register_output | awk '{print $4}')
  98. port=$(echo $register_output | awk '{print $6}')
  99. client_ip=$(echo $register_output | awk '{print $8}')
  100. register_client_interface $pubkey $ip_addr $port $IP $WG_MTU
  101. ;;
  102. *) echo "Usage: wg-client-installer [cmd] --ip [2001::1] --user wginstaller --password wginstaller" ;;
  103. esac