Skip to content

Commit 29c0b48

Browse files
committed
feat: Add bot_management to Cf
1 parent 5f06bb0 commit 29c0b48

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

worker-sys/src/types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod ai;
22
mod analytics_engine;
3+
mod bot_management;
34
mod context;
45
mod crypto;
56
#[cfg(feature = "d1")]
@@ -24,6 +25,7 @@ mod websocket_request_response_pair;
2425

2526
pub use ai::*;
2627
pub use analytics_engine::*;
28+
pub use bot_management::*;
2729
pub use context::*;
2830
pub use crypto::*;
2931
#[cfg(feature = "d1")]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use wasm_bindgen::prelude::*;
2+
3+
#[wasm_bindgen]
4+
extern "C" {
5+
#[derive(Debug, Clone, PartialEq)]
6+
pub type BotManagement;
7+
8+
#[wasm_bindgen(method, catch, getter, js_name=score)]
9+
pub fn score(this: &BotManagement) -> Result<u32, JsValue>;
10+
11+
#[wasm_bindgen(method, catch, getter, js_name=verifiedBot)]
12+
pub fn verified_bot(this: &BotManagement) -> Result<bool, JsValue>;
13+
14+
#[wasm_bindgen(method, catch, getter, js_name=staticResource)]
15+
pub fn static_resource(this: &BotManagement) -> Result<String, JsValue>;
16+
17+
#[wasm_bindgen(method, catch, getter, js_name=ja3Hash)]
18+
pub fn ja3_hash(this: &BotManagement) -> Result<String, JsValue>;
19+
20+
#[wasm_bindgen(method, catch, getter, js_name=ja4)]
21+
pub fn ja4(this: &BotManagement) -> Result<String, JsValue>;
22+
23+
#[wasm_bindgen(method, catch, getter, js_name=jsDetection)]
24+
pub fn js_detection(this: &BotManagement) -> Result<JsDetection, JsValue>;
25+
26+
#[wasm_bindgen(method, catch, getter, js_name=detectionIds)]
27+
pub fn detection_ids(this: &BotManagement) -> Result<Vec<String>, JsValue>;
28+
29+
#[wasm_bindgen(method, catch, getter, js_name=verifiedBotCategory)]
30+
pub fn verified_bot_category(this: &BotManagement) -> Result<String, JsValue>;
31+
}
32+
33+
#[wasm_bindgen]
34+
extern "C" {
35+
#[derive(Debug, Clone, PartialEq)]
36+
pub type JsDetection;
37+
38+
#[wasm_bindgen(method, catch, getter, js_name=passed)]
39+
pub fn passed(this: &JsDetection) -> Result<bool, JsValue>;
40+
}

worker-sys/src/types/incoming_request_cf_properties.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
use wasm_bindgen::prelude::*;
22

33
use crate::types::TlsClientAuth;
4+
use crate::types::BotManagement;
45

56
#[wasm_bindgen]
67
extern "C" {
78
#[wasm_bindgen(extends=js_sys::Object)]
89
#[derive(Debug, Clone, PartialEq, Eq)]
910
pub type IncomingRequestCfProperties;
1011

12+
#[wasm_bindgen(method, catch, getter, js_name=botManagement)]
13+
pub fn bot_management(this: &IncomingRequestCfProperties) -> Result<Option<BotManagement>, JsValue>;
14+
1115
#[wasm_bindgen(method, catch, getter)]
1216
pub fn colo(this: &IncomingRequestCfProperties) -> Result<String, JsValue>;
1317

worker/src/cf.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ impl Cf {
2626
&self.inner
2727
}
2828

29+
/// Information about bot management.
30+
pub fn bot_management(&self) -> Option<BotManagement> {
31+
self.inner.bot_management().unwrap().map(Into::into)
32+
}
33+
2934
/// The three-letter airport code (e.g. `ATX`, `LUX`) representing
3035
/// the colocation which processed the request
3136
pub fn colo(&self) -> String {
@@ -274,6 +279,68 @@ impl From<worker_sys::TlsClientAuth> for TlsClientAuth {
274279
}
275280
}
276281

282+
#[derive(Debug)]
283+
pub struct BotManagement {
284+
inner: worker_sys::BotManagement,
285+
}
286+
287+
impl BotManagement {
288+
pub fn score(&self) -> u32 {
289+
self.inner.score().unwrap()
290+
}
291+
292+
pub fn verified_bot(&self) -> bool {
293+
self.inner.verified_bot().unwrap()
294+
}
295+
296+
pub fn static_resource(&self) -> String {
297+
self.inner.static_resource().unwrap()
298+
}
299+
300+
pub fn ja3_hash(&self) -> String {
301+
self.inner.ja3_hash().unwrap()
302+
}
303+
304+
pub fn ja4(&self) -> String {
305+
self.inner.ja4().unwrap()
306+
}
307+
308+
pub fn js_detection(&self) -> JsDetection {
309+
self.inner.js_detection().map(Into::into).unwrap()
310+
}
311+
312+
pub fn detection_ids(&self) -> Vec<String> {
313+
self.inner.detection_ids().unwrap()
314+
}
315+
316+
pub fn verified_bot_category(&self) -> String {
317+
self.inner.verified_bot_category().unwrap()
318+
}
319+
}
320+
321+
impl From<worker_sys::BotManagement> for BotManagement {
322+
fn from(inner: worker_sys::BotManagement) -> Self {
323+
Self { inner }
324+
}
325+
}
326+
327+
#[derive(Debug)]
328+
pub struct JsDetection {
329+
inner: worker_sys::JsDetection,
330+
}
331+
332+
impl JsDetection {
333+
pub fn passed(&self) -> bool {
334+
self.inner.passed().unwrap()
335+
}
336+
}
337+
338+
impl From<worker_sys::JsDetection> for JsDetection {
339+
fn from(inner: worker_sys::JsDetection) -> Self {
340+
Self { inner }
341+
}
342+
}
343+
277344
#[derive(Clone, Debug)]
278345
pub struct CfResponseProperties(pub(crate) js_sys::Object);
279346

0 commit comments

Comments
 (0)