From 0bcd27bba40cf83c703a9da20a2c167224cf2ec8 Mon Sep 17 00:00:00 2001 From: Henry Chu Date: Sat, 31 Jan 2026 23:11:57 +0800 Subject: [PATCH] feat: Add bot_management to Cf --- worker-sys/src/types.rs | 2 + worker-sys/src/types/bot_management.rs | 37 ++++++++++ .../types/incoming_request_cf_properties.rs | 9 +++ worker/src/cf.rs | 67 +++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 worker-sys/src/types/bot_management.rs diff --git a/worker-sys/src/types.rs b/worker-sys/src/types.rs index d6e63d4db..afef5bc11 100644 --- a/worker-sys/src/types.rs +++ b/worker-sys/src/types.rs @@ -1,5 +1,6 @@ mod ai; mod analytics_engine; +mod bot_management; mod context; mod crypto; #[cfg(feature = "d1")] @@ -24,6 +25,7 @@ mod websocket_request_response_pair; pub use ai::*; pub use analytics_engine::*; +pub use bot_management::*; pub use context::*; pub use crypto::*; #[cfg(feature = "d1")] diff --git a/worker-sys/src/types/bot_management.rs b/worker-sys/src/types/bot_management.rs new file mode 100644 index 000000000..792246402 --- /dev/null +++ b/worker-sys/src/types/bot_management.rs @@ -0,0 +1,37 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +extern "C" { + #[derive(Debug, Clone, PartialEq)] + pub type BotManagement; + + #[wasm_bindgen(method, catch, getter, js_name=score)] + pub fn score(this: &BotManagement) -> Result; + + #[wasm_bindgen(method, catch, getter, js_name=verifiedBot)] + pub fn verified_bot(this: &BotManagement) -> Result; + + #[wasm_bindgen(method, catch, getter, js_name=staticResource)] + pub fn static_resource(this: &BotManagement) -> Result; + + #[wasm_bindgen(method, catch, getter, js_name=ja3Hash)] + pub fn ja3_hash(this: &BotManagement) -> Result; + + #[wasm_bindgen(method, catch, getter, js_name=ja4)] + pub fn ja4(this: &BotManagement) -> Result; + + #[wasm_bindgen(method, catch, getter, js_name=jsDetection)] + pub fn js_detection(this: &BotManagement) -> Result; + + #[wasm_bindgen(method, catch, getter, js_name=detectionIds)] + pub fn detection_ids(this: &BotManagement) -> Result, JsValue>; +} + +#[wasm_bindgen] +extern "C" { + #[derive(Debug, Clone, PartialEq)] + pub type JsDetection; + + #[wasm_bindgen(method, catch, getter, js_name=passed)] + pub fn passed(this: &JsDetection) -> Result; +} diff --git a/worker-sys/src/types/incoming_request_cf_properties.rs b/worker-sys/src/types/incoming_request_cf_properties.rs index b7044013b..8e91546b1 100644 --- a/worker-sys/src/types/incoming_request_cf_properties.rs +++ b/worker-sys/src/types/incoming_request_cf_properties.rs @@ -1,5 +1,6 @@ use wasm_bindgen::prelude::*; +use crate::types::BotManagement; use crate::types::TlsClientAuth; #[wasm_bindgen] @@ -8,6 +9,14 @@ extern "C" { #[derive(Debug, Clone, PartialEq, Eq)] pub type IncomingRequestCfProperties; + #[wasm_bindgen(method, catch, getter, js_name=botManagement)] + pub fn bot_management( + this: &IncomingRequestCfProperties, + ) -> Result, JsValue>; + + #[wasm_bindgen(method, catch, getter, js_name=verifiedBotCategory)] + pub fn verified_bot_category(this: &IncomingRequestCfProperties) -> Result; + #[wasm_bindgen(method, catch, getter)] pub fn colo(this: &IncomingRequestCfProperties) -> Result; diff --git a/worker/src/cf.rs b/worker/src/cf.rs index dd29ca181..18b45aa16 100644 --- a/worker/src/cf.rs +++ b/worker/src/cf.rs @@ -26,6 +26,15 @@ impl Cf { &self.inner } + /// Information about bot management. + pub fn bot_management(&self) -> Option { + self.inner.bot_management().unwrap().map(Into::into) + } + + pub fn verified_bot_category(&self) -> String { + self.inner.verified_bot_category().unwrap() + } + /// The three-letter airport code (e.g. `ATX`, `LUX`) representing /// the colocation which processed the request pub fn colo(&self) -> String { @@ -274,6 +283,64 @@ impl From for TlsClientAuth { } } +#[derive(Debug)] +pub struct BotManagement { + inner: worker_sys::BotManagement, +} + +impl BotManagement { + pub fn score(&self) -> usize { + self.inner.score().unwrap() + } + + pub fn verified_bot(&self) -> bool { + self.inner.verified_bot().unwrap() + } + + pub fn static_resource(&self) -> bool { + self.inner.static_resource().unwrap() + } + + pub fn ja3_hash(&self) -> String { + self.inner.ja3_hash().unwrap() + } + + pub fn ja4(&self) -> String { + self.inner.ja4().unwrap() + } + + pub fn js_detection(&self) -> JsDetection { + self.inner.js_detection().map(Into::into).unwrap() + } + + pub fn detection_ids(&self) -> Vec { + self.inner.detection_ids().unwrap() + } +} + +impl From for BotManagement { + fn from(inner: worker_sys::BotManagement) -> Self { + Self { inner } + } +} + +#[derive(Debug)] +pub struct JsDetection { + inner: worker_sys::JsDetection, +} + +impl JsDetection { + pub fn passed(&self) -> bool { + self.inner.passed().unwrap() + } +} + +impl From for JsDetection { + fn from(inner: worker_sys::JsDetection) -> Self { + Self { inner } + } +} + #[derive(Clone, Debug)] pub struct CfResponseProperties(pub(crate) js_sys::Object);