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