Skip to main content

relay_core_script/
engine_trait.rs

1use async_trait::async_trait;
2use relay_core_api::flow::{Flow, WebSocketMessage};
3use relay_core_lib::interceptor::{
4    BoxError, ConnectAction, ConnectionInfo, ConnectionStats, HttpBody, RequestAction,
5    ResponseAction, WebSocketMessageAction,
6};
7
8#[async_trait]
9pub trait ScriptEngineTrait: Send + Sync {
10    async fn load_script(&mut self, script: &str) -> Result<(), BoxError>;
11
12    async fn on_connect(&self, _conn: &ConnectionInfo) -> Result<ConnectAction, BoxError> {
13        Ok(ConnectAction::Allow)
14    }
15
16    async fn on_disconnect(
17        &self,
18        _conn: &ConnectionInfo,
19        _stats: &ConnectionStats,
20    ) -> Result<(), BoxError> {
21        Ok(())
22    }
23
24    async fn on_request_headers(&self, _flow: &mut Flow) -> Result<Option<Flow>, BoxError> {
25        Ok(None)
26    }
27
28    async fn on_request(&self, flow: &mut Flow, body: HttpBody) -> Result<RequestAction, BoxError>;
29
30    async fn on_response_headers(&self, _flow: &mut Flow) -> Result<Option<Flow>, BoxError> {
31        Ok(None)
32    }
33
34    async fn on_response(
35        &self,
36        flow: &mut Flow,
37        body: HttpBody,
38    ) -> Result<ResponseAction, BoxError>;
39
40    async fn on_websocket_message(
41        &self,
42        _flow: &mut Flow,
43        _message: &mut WebSocketMessage,
44    ) -> Result<WebSocketMessageAction, BoxError> {
45        Ok(WebSocketMessageAction::Continue(_message.clone()))
46    }
47
48    async fn on_websocket_start(&self, _flow: &mut Flow) -> Result<(), BoxError> {
49        Ok(())
50    }
51
52    async fn on_websocket_end(
53        &self,
54        _flow: &mut Flow,
55        _close_code: u16,
56        _close_reason: &str,
57    ) -> Result<(), BoxError> {
58        Ok(())
59    }
60
61    async fn on_websocket_error(&self, _flow: &mut Flow, _error: &str) -> Result<(), BoxError> {
62        Ok(())
63    }
64}