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.

120 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. ip -6 a a dev $ifname fe80::2/64
  74. wg set $ifname listen-port $port private-key $gw_key peer $pubkey allowed-ips ::/0 endpoint "${endpoint}:${gw_port}"
  75. ip link set up dev $ifname
  76. ip link set mtu $mtu_client dev $ifname # configure mtu here!
  77. }
  78. # rpc login
  79. token="$(request_token $IP $USER $PASSWORD)"
  80. if [ $? != 0 ]; then
  81. echo "failed to register token"
  82. exit 1
  83. fi
  84. # now call procedure
  85. case $CMD in
  86. "get_usage")
  87. wg_rpcd_get_usage $token $IP
  88. ;;
  89. "register")
  90. gw_pub=$(uci get wgclient.@client[0].wg_pub)
  91. gw_pub_string=$(cat $gw_pub)
  92. register_output=$(wg_rpcd_register $token $IP $BANDWIDTH $WG_MTU $gw_pub_string)
  93. if [ $? != 0 ]; then
  94. echo "Failed to Register!"
  95. exit 1
  96. fi
  97. pubkey=$(echo $register_output | awk '{print $2}')
  98. ip_addr=$(echo $register_output | awk '{print $4}')
  99. port=$(echo $register_output | awk '{print $6}')
  100. client_ip=$(echo $register_output | awk '{print $8}')
  101. register_client_interface $pubkey $ip_addr $port $IP $WG_MTU
  102. ;;
  103. *) echo "Usage: wg-client-installer [cmd] --ip [2001::1] --user wginstaller --password wginstaller" ;;
  104. esac