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.

134 lines
2.7 KiB

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