Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions worker-sys/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod ai;
mod analytics_engine;
mod bot_management;
mod context;
mod crypto;
#[cfg(feature = "d1")]
Expand All @@ -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")]
Expand Down
40 changes: 40 additions & 0 deletions worker-sys/src/types/bot_management.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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<usize, JsValue>;

#[wasm_bindgen(method, catch, getter, js_name=verifiedBot)]
pub fn verified_bot(this: &BotManagement) -> Result<bool, JsValue>;

#[wasm_bindgen(method, catch, getter, js_name=staticResource)]
pub fn static_resource(this: &BotManagement) -> Result<bool, JsValue>;

#[wasm_bindgen(method, catch, getter, js_name=ja3Hash)]
pub fn ja3_hash(this: &BotManagement) -> Result<String, JsValue>;

#[wasm_bindgen(method, catch, getter, js_name=ja4)]
pub fn ja4(this: &BotManagement) -> Result<String, JsValue>;

#[wasm_bindgen(method, catch, getter, js_name=jsDetection)]
pub fn js_detection(this: &BotManagement) -> Result<JsDetection, JsValue>;

#[wasm_bindgen(method, catch, getter, js_name=detectionIds)]
pub fn detection_ids(this: &BotManagement) -> Result<Vec<usize>, JsValue>;

#[wasm_bindgen(method, catch, getter, js_name=verifiedBotCategory)]
pub fn verified_bot_category(this: &BotManagement) -> Result<String, 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<bool, JsValue>;
}
4 changes: 4 additions & 0 deletions worker-sys/src/types/incoming_request_cf_properties.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use wasm_bindgen::prelude::*;

use crate::types::TlsClientAuth;
use crate::types::BotManagement;

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

#[wasm_bindgen(method, catch, getter, js_name=botManagement)]
pub fn bot_management(this: &IncomingRequestCfProperties) -> Result<Option<BotManagement>, JsValue>;

#[wasm_bindgen(method, catch, getter)]
pub fn colo(this: &IncomingRequestCfProperties) -> Result<String, JsValue>;

Expand Down
67 changes: 67 additions & 0 deletions worker/src/cf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ impl Cf {
&self.inner
}

/// Information about bot management.
pub fn bot_management(&self) -> Option<BotManagement> {
self.inner.bot_management().unwrap().map(Into::into)
}

/// The three-letter airport code (e.g. `ATX`, `LUX`) representing
/// the colocation which processed the request
pub fn colo(&self) -> String {
Expand Down Expand Up @@ -274,6 +279,68 @@ impl From<worker_sys::TlsClientAuth> 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<usize> {
self.inner.detection_ids().unwrap()
}

pub fn verified_bot_category(&self) -> String {
self.inner.verified_bot_category().unwrap()
}
}

impl From<worker_sys::BotManagement> 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<worker_sys::JsDetection> for JsDetection {
fn from(inner: worker_sys::JsDetection) -> Self {
Self { inner }
}
}

#[derive(Clone, Debug)]
pub struct CfResponseProperties(pub(crate) js_sys::Object);

Expand Down