/
wlansurvey.cc
280 lines (235 loc) · 7.74 KB
/
wlansurvey.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* davep 20181230 ; poke at cradlepoint router wifi survey : work in progress */
#include <iostream>
#include <cstdlib>
#include <optional>
// http://fmtlib.net/latest/api.html#format
#include <fmt/format.h>
#include <cpprest/http_client.h>
#include "netrc.hpp"
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
// TODO get username+password from cli, env var, etc
std::string username { U("admin") };
//std::string password { U("00000000") };
std::string password { U("12345") };
static std::optional<json::value> get(http_client& client, http::uri& uri)
{
web::http::http_response data_response;
try {
// blocking wait for the http
data_response = client.request(methods::GET, uri.to_string()).get();
}
catch (web::http::http_exception & err) {
std::cerr << "Error! " << err.what() << std::endl;
std::cerr << "Error! " << err.error_code() << std::endl;
return {};
}
// extract_json() returns a pplx::task<json::value>
// blocking wait for task to finish
// return the contents of the task (the template thingy)
json::value value = data_response.extract_json().get();
// std::cout << "value=" << value << std::endl;
return value;
}
void object_introspect(json::object& jobj)
{
// dump the key+value pairs in a json::object
for (auto fields: jobj) {
utility::string_t key { fields.first };
json::value value { fields.second };
std::cout << key << "=" << value << " type="<< value.type() << "\n";
}
}
int channel_to_frequency(int f)
{
// # https://en.wikipedia.org/wiki/List_of_WLAN_channels
if (f >= 36)
return f*5 + 5000;
else if (f < 14)
return ((f-1)*5) +2412;
else if (f == 14)
return 2484;
else
return 0;
}
void check_netrc(std::string host, std::string& username_, std::string& password_)
{
netrc_map netrc;
try {
netrc = netrc_parse_default_file();
}
catch (file_error& err) {
return;
}
while(1) {
try {
auto [netrc_login, netrc_password, netrc_account] = netrc.at(host);
username_ = std::move(netrc_login);
password_ = std::move(netrc_password);
return;
}
catch (file_error& err) {
}
catch (std::out_of_range & err) {
std::clog << "netrc has no entry for " << host << "\n";
}
if (host == "default") {
return;
}
host = "default";
}
}
bool wlansurvey(std::string& target)
{
web::uri target_uri = web::uri{target};
// read netrc before trying higher priority methods
check_netrc(target_uri.host(), username, password);
const char * p = std::getenv("CP_PASSWORD");
if (p) {
password.assign(p);
}
credentials cred{username, password};
http_client_config config;
config.set_credentials(cred);
http_client client(target, config);
uri_builder builder(U("/api/status/wan/connection_state"));
web::http::http_response data_response;
try {
auto response = client.request(methods::GET, builder.to_string());
// blocking wait for the http
response.wait();
data_response = response.get();
}
catch (web::http::http_exception & err) {
std::cerr << "Error! " << err.what() << std::endl;
std::cerr << "Error! " << err.error_code() << std::endl;
return EXIT_FAILURE;
}
// extract_json() returns a pplx::task<json::value>
auto json_response = data_response.extract_json();
// blocking wait for task to finish
json_response.wait();
// return the contents of the task (the template thingy)
json::value value = json_response.get();
std::cout << "value=" << value << std::endl;
// pull out the contents the router sends back
json::value success = value.at(U("success"));
json::value data = value.at(U("data"));
builder.clear();
builder.set_path(U("/api/shallow/status/wlan/radio"));
http::uri uri = builder.to_uri();
std::optional<json::value> get_value = get(client, uri);
if (!get_value) {
return EXIT_FAILURE;
}
success = get_value.value().at(U("success"));
data = get_value.value().at(U("data"));
std::cout << "type=" << data.type() << " data=" << data << "\n";
/* davep 20190117 ; single radio products return a dict. Multiple radio
* products return an array of dicts.
*/
int num_radios;
if (data.type() == 3) {
num_radios = 1;
}
else {
json::array wlan = data.as_array();
num_radios = wlan.size();
}
std::cout << "num_radios=" << num_radios << "\n";
for (int i=0 ; i<num_radios ; i++) {
builder.clear();
builder.set_path(U("/api/status/wlan/radio/" + std::to_string(i) ));
uri = builder.to_uri();
get_value = get(client, uri);
if (!get_value) {
continue;
}
success = get_value.value().at(U("success"));
data = get_value.value().at(U("data"));
json::object status = data.as_object();;
for (auto fields: status) {
utility::string_t key = fields.first;
value = fields.second;
// std::cout << key << "=" << value << "\n";
}
json::value survey = status.at(U("survey"));
std::cout << "found " << survey.as_array().size() << " networks\n";
for (auto network: survey.as_array()) {
json::object element = network.as_object();
// object_introspect(element);
utility::string_t bssid = network.at(U("bssid")).as_string();
utility::string_t ssid = network.at(U("ssid")).as_string();
int channel = network.at(U("channel")).as_integer();
int frequency = channel_to_frequency(channel);
int rssi = network.at(U("rssi")).as_integer();
utility::string_t seen = network.at(U("seen")).as_string();
fmt::print("{0:36s} {1:10s} {2:3d} {3:4d} {4:5d} {5:20s}\n",
ssid, bssid, rssi, channel, frequency, seen);
}
}
return true;
}
void trigger_scan(std::string& target)
{
std::cout << target << "\n";
// let's learn how to PUT
std::string path {"/api/control/wlan/radio/0"};
http::uri uri = http::uri_builder(target).append_path(path).to_uri();
// http::uri uri = http::uri(target);
credentials cred{username, password};
http_client_config config;
config.set_credentials(cred);
http_client client(target, config);
// maketh the json (stupid to go through all this for something as simple
// as {"survey":True} but I'm learning how to use the library
// json::value value {true};
// json::object obj;
// obj["survey"] = value;
utility::string_t k {"survey"};
json::value v {true};
// this seems awfully complicated
std::vector fields {std::make_pair(k,v)};
json::value value = json::value::object(fields);
object_introspect(value.as_object());
utility::string_t s = value.serialize();
std::cout << s << "\n";
// this might be simpler
value = json::value::parse("{\"survey\":true}");
object_introspect(value.as_object());
std::cout << value.serialize() << "\n";
// our firmware wants payload URL encoded
// my python also sends Content-Type: application/x-www-form-urlencoded
//
// (using BlackJackClient.cpp as example on building this string)
utility::ostringstream_t buf;
buf << "data=" << value.serialize() ;
// buf << "data={\"survey\":true}" ;
// buf << "data=%7B%22survey%22%3A+true%7D";
http_request request(methods::PUT);
request.set_request_uri(uri);
request.set_body(buf.str());
request.headers()["Content-Type"] = "application/x-www-form-urlencoded";
std::cout << "request=" << request.to_string() << "\n";
http_response response = client.request(request).get();
// http_response response = client.request(methods::PUT, path, buf.str()).get();
json::value response_json = response.extract_json().get();
std::cout << "value=" << response_json << std::endl;
}
int main(int argc, char* argv[])
{
if (argc < 2) {
return EXIT_FAILURE;
}
std::string target{argv[1]};
std::cout << "target=" << target << "\n";
if (!web::uri::validate(target)) {
std::cerr << "invalid target\n";
return EXIT_FAILURE;
}
// trigger_scan(target);
wlansurvey(target);
return EXIT_SUCCESS;
}