-
Notifications
You must be signed in to change notification settings - Fork 25
/
client-info.cpp
239 lines (191 loc) · 6.41 KB
/
client-info.cpp
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
#include "builtins/client-info.h"
#include "core/geo_ip.h"
#include "host_interface/host_api.h"
#include "openssl/evp.h"
#include "js/JSON.h"
#include <arpa/inet.h>
namespace builtins {
namespace {
JSString *address(JSObject *obj) {
JS::Value val = JS::GetReservedSlot(obj, static_cast<uint32_t>(ClientInfo::Slots::Address));
return val.isString() ? val.toString() : nullptr;
}
JSString *geo_info(JSObject *obj) {
JS::Value val = JS::GetReservedSlot(obj, static_cast<uint32_t>(ClientInfo::Slots::GeoInfo));
return val.isString() ? val.toString() : nullptr;
}
static JSString *retrieve_address(JSContext *cx, JS::HandleObject self) {
auto res = HttpReq::downstream_client_ip_addr();
if (auto *err = res.to_err()) {
HANDLE_ERROR(cx, *err);
return nullptr;
}
auto octets = std::move(res.unwrap());
char address_chars[INET6_ADDRSTRLEN];
int addr_family = 0;
socklen_t size = 0;
switch (octets.len) {
case 0: {
// No address to be had, leave `address` as a nullptr.
break;
}
case 4: {
addr_family = AF_INET;
size = INET_ADDRSTRLEN;
break;
}
case 16: {
addr_family = AF_INET6;
size = INET6_ADDRSTRLEN;
break;
}
}
JS::RootedString address(cx);
if (octets.len > 0) {
// TODO: do we need to do error handling here, or can we depend on the
// host giving us a valid address?
inet_ntop(addr_family, octets.begin(), address_chars, size);
address = JS_NewStringCopyZ(cx, address_chars);
if (!address) {
return nullptr;
}
}
JS::SetReservedSlot(self, static_cast<uint32_t>(ClientInfo::Slots::Address),
JS::StringValue(address));
return address;
}
JSString *retrieve_geo_info(JSContext *cx, JS::HandleObject self) {
JS::RootedString address_str(cx, address(self));
if (!address_str) {
address_str = retrieve_address(cx, self);
if (!address_str)
return nullptr;
}
JS::RootedString geo(cx, get_geo_info(cx, address_str));
if (!geo)
return nullptr;
JS::SetReservedSlot(self, static_cast<uint32_t>(ClientInfo::Slots::GeoInfo),
JS::StringValue(geo));
return geo;
}
} // namespace
bool ClientInfo::address_get(JSContext *cx, unsigned argc, JS::Value *vp) {
METHOD_HEADER(0)
JS::RootedString address_str(cx, address(self));
if (!address_str) {
address_str = retrieve_address(cx, self);
if (!address_str)
return false;
}
args.rval().setString(address_str);
return true;
}
bool ClientInfo::geo_get(JSContext *cx, unsigned argc, JS::Value *vp) {
METHOD_HEADER(0)
JS::RootedString geo_info_str(cx, geo_info(self));
if (!geo_info_str) {
geo_info_str = retrieve_geo_info(cx, self);
if (!geo_info_str)
return false;
}
return JS_ParseJSON(cx, geo_info_str, args.rval());
}
bool ClientInfo::tls_cipher_openssl_name_get(JSContext *cx, unsigned argc, JS::Value *vp) {
METHOD_HEADER(0);
auto res = HttpReq::http_req_downstream_tls_cipher_openssl_name();
if (auto *err = res.to_err()) {
HANDLE_ERROR(cx, *err);
return false;
}
HostString cipher = std::move(res.unwrap());
JS::RootedString result(cx, JS_NewStringCopyN(cx, cipher.ptr.get(), cipher.len));
args.rval().setString(result);
return true;
}
bool ClientInfo::tls_ja3_md5_get(JSContext *cx, unsigned argc, JS::Value *vp) {
METHOD_HEADER(0);
auto res = HttpReq::http_req_downstream_tls_ja3_md5();
if (auto *err = res.to_err()) {
HANDLE_ERROR(cx, *err);
return false;
}
auto ja3 = std::move(res.unwrap());
JS::UniqueChars hex{OPENSSL_buf2hexstr(ja3.ptr.get(), ja3.len)};
std::string ja3hex{hex.get(), std::remove(hex.get(), hex.get() + strlen(hex.get()), ':')};
JS::RootedString s(cx, JS_NewStringCopyN(cx, ja3hex.c_str(), ja3hex.length()));
args.rval().setString(s);
return true;
}
bool ClientInfo::tls_client_hello_get(JSContext *cx, unsigned argc, JS::Value *vp) {
METHOD_HEADER(0);
auto res = HttpReq::http_req_downstream_tls_client_hello();
if (auto *err = res.to_err()) {
HANDLE_ERROR(cx, *err);
return false;
}
HostBytes hello = std::move(res.unwrap());
JS::RootedObject buffer(cx, JS::NewArrayBufferWithContents(cx, hello.len, hello.ptr.get()));
if (!buffer) {
// We can be here if the array buffer was too large -- if that was the case then a
// JSMSG_BAD_ARRAY_LENGTH will have been created.
return false;
}
// `hello` is now owned by `buffer`
static_cast<void>(hello.ptr.release());
args.rval().setObject(*buffer);
return true;
}
bool ClientInfo::tls_client_certificate_get(JSContext *cx, unsigned argc, JS::Value *vp) {
METHOD_HEADER(0);
auto res = HttpReq::http_req_downstream_tls_raw_client_certificate();
if (auto *err = res.to_err()) {
HANDLE_ERROR(cx, *err);
return false;
}
HostBytes cert = std::move(res.unwrap());
JS::RootedObject buffer(cx, JS::NewArrayBufferWithContents(cx, cert.len, cert.ptr.get()));
if (!buffer) {
// We can be here if the array buffer was too large -- if that was the case then a
// JSMSG_BAD_ARRAY_LENGTH will have been created.
return false;
}
// `cert` is now owned by `buffer`
static_cast<void>(cert.ptr.release());
args.rval().setObject(*buffer);
return true;
}
bool ClientInfo::tls_protocol_get(JSContext *cx, unsigned argc, JS::Value *vp) {
METHOD_HEADER(0);
auto res = HttpReq::http_req_downstream_tls_protocol();
if (auto *err = res.to_err()) {
HANDLE_ERROR(cx, *err);
return false;
}
HostString protocol = std::move(res.unwrap());
JS::RootedString result(cx, JS_NewStringCopyN(cx, protocol.ptr.get(), protocol.len));
args.rval().setString(result);
return true;
}
const JSFunctionSpec ClientInfo::static_methods[] = {
JS_FS_END,
};
const JSPropertySpec ClientInfo::static_properties[] = {
JS_PS_END,
};
const JSFunctionSpec ClientInfo::methods[] = {
JS_FS_END,
};
const JSPropertySpec ClientInfo::properties[] = {
JS_PSG("address", address_get, JSPROP_ENUMERATE),
JS_PSG("geo", geo_get, JSPROP_ENUMERATE),
JS_PSG("tlsCipherOpensslName", tls_cipher_openssl_name_get, JSPROP_ENUMERATE),
JS_PSG("tlsProtocol", tls_protocol_get, JSPROP_ENUMERATE),
JS_PSG("tlsJA3MD5", tls_ja3_md5_get, JSPROP_ENUMERATE),
JS_PSG("tlsClientCertificate", tls_client_certificate_get, JSPROP_ENUMERATE),
JS_PSG("tlsClientHello", tls_client_hello_get, JSPROP_ENUMERATE),
JS_PS_END,
};
JSObject *ClientInfo::create(JSContext *cx) {
return JS_NewObjectWithGivenProto(cx, &class_, proto_obj);
}
} // namespace builtins