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.

148 lines
3.1 KiB

  1. #!/bin/sh
  2. . /usr/share/libubox/jshn.sh
  3. query_gw () {
  4. local ip=$1
  5. local req=$2
  6. # first try https
  7. ret=$(curl https://"$ip"/ubus -d "$req") 2>/dev/null
  8. if [ $? -eq 0 ]; then
  9. echo "$ret"
  10. return 0
  11. fi
  12. # try with --insecure
  13. if [ "$(uci get wgclient.@client[0].try_insecure)" -eq '1' ]; then
  14. ret=$(curl --insecure https://"$ip"/ubus -d "$req") 2>/dev/null
  15. if [ $? -eq 0 ]; then
  16. echo "$ret"
  17. return 0
  18. fi
  19. fi
  20. # try with http
  21. if [ "$(uci get wgclient.@client[0].try_http)" -eq '1' ]; then
  22. ret=$(curl http://"$ip"/ubus -d "$req") 2>/dev/null
  23. if [ $? -eq 0 ]; then
  24. echo "$ret"
  25. return 0
  26. fi
  27. fi
  28. return 1
  29. }
  30. request_token () {
  31. local ip=$1
  32. local user=$2
  33. local password=$3
  34. json_init
  35. json_add_string "jsonrpc" "2.0"
  36. json_add_int "id" "1"
  37. json_add_string "method" "call"
  38. json_add_array "params"
  39. json_add_string "" "00000000000000000000000000000000"
  40. json_add_string "" "session"
  41. json_add_string "" "login"
  42. json_add_object
  43. json_add_string "username" "$user"
  44. json_add_string "password" "$password"
  45. json_close_object
  46. json_close_array
  47. req=$(json_dump)
  48. ret=$(query_gw "$ip" "$req") 2>/dev/null
  49. if [ $? -ne 0 ]; then
  50. return 1
  51. fi
  52. json_load "$ret"
  53. json_get_vars result result
  54. json_select result
  55. json_select 2
  56. json_get_var ubus_rpc_session ubus_rpc_session
  57. echo "$ubus_rpc_session"
  58. }
  59. wg_rpcd_get_usage () {
  60. local token=$1
  61. local ip=$2
  62. local secret=$3
  63. json_init
  64. json_add_string "jsonrpc" "2.0"
  65. json_add_int "id" "1"
  66. json_add_string "method" "call"
  67. json_add_array "params"
  68. json_add_string "" "$token"
  69. json_add_string "" "wginstaller"
  70. json_add_string "" "get_usage"
  71. json_add_object
  72. json_close_object
  73. json_close_array
  74. req=$(json_dump)
  75. ret=$(query_gw "$ip" "$req") 2>/dev/null
  76. if [ $? -ne 0 ]; then
  77. return 1
  78. fi
  79. # return values
  80. json_load "$ret"
  81. json_get_vars result result
  82. json_select result
  83. json_select 2
  84. json_get_var num_interfaces num_interfaces
  85. echo "num_interfaces: ${num_interfaces}"
  86. }
  87. wg_error_handling () {
  88. local response_code=$1
  89. case "$response_code" in
  90. 1) logger -t "wginstaller" "Server rejected request since the public key is already used!" ;;
  91. *) logger -t "wginstaller" "Unknown Error Code!";;
  92. esac
  93. }
  94. wg_rpcd_register () {
  95. local token=$5
  96. local ip=$6
  97. local mtu=$7
  98. local public_key=$8
  99. json_init
  100. json_add_string "jsonrpc" "2.0"
  101. json_add_int "id" "1"
  102. json_add_string "method" "call"
  103. json_add_array "params"
  104. json_add_string "" "$token"
  105. json_add_string "" "wginstaller"
  106. json_add_string "" "register"
  107. json_add_object
  108. json_add_int "mtu" "$mtu"
  109. json_add_string "public_key" "$public_key"
  110. json_close_object
  111. json_close_array
  112. req=$(json_dump)
  113. ret=$(query_gw "$ip" "$req") 2>/dev/null
  114. if [ $? -ne 0 ]; then
  115. return 1
  116. fi
  117. json_load "$ret"
  118. json_get_vars result result
  119. json_select result
  120. json_select 2
  121. json_get_var response_code response_code
  122. if [ "$response_code" -ne 0 ]; then
  123. wg_error_handling "$response_code"
  124. return 1
  125. fi
  126. json_get_var gw_pubkey gw_pubkey
  127. json_get_var gw_ipv4 gw_ipv4
  128. json_get_var gw_ipv6 gw_ipv6
  129. json_get_var gw_port gw_port
  130. export "$1=$gw_pubkey"
  131. export "$2=$gw_ipv4"
  132. export "$3=$gw_ipv6"
  133. export "$4=$gw_port"
  134. }