Skip to content

Commit

Permalink
Add RequestCode::TrustedUserAgentList
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanbeom kim authored and msk committed Jun 15, 2023
1 parent d454e74 commit 0458dac
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ file is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and
this project adheres to [Semantic
Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Introduced new request code: `RequestCode:TrustedUserAgentList`
- `RequestCode::TrustedUserAgentList`: This new request code facilitates the
REview sending a list of trusted `user-agents` to the agent. This request code
is followed by a `Vec<String>` parameter, which is a list of trusted user-agents.

## [0.8.1] - 2023-06-05

### Added
Expand Down Expand Up @@ -209,6 +218,7 @@ without relying on the content of the response.

- `send_frame` and `recv_frame` to send and receive length-delimited frames.

[Unreleased]: https://github.com/petabi/oinq/compare/0.8.1...main
[0.8.1]: https://github.com/petabi/oinq/compare/0.8.0...0.8.1
[0.8.0]: https://github.com/petabi/oinq/compare/0.7.1...0.8.0
[0.7.1]: https://github.com/petabi/oinq/compare/0.7.0...0.7.1
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ pub enum RequestCode {
/// Request Echo (for ping)
EchoRequest = 17,

/// Update the list of trusted User-agent
TrustedUserAgentList = 18,

/// Update the list of trusted domains
TrustedDomainList = 0,

Expand Down
40 changes: 40 additions & 0 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,15 @@ pub trait Handler: Send {
async fn block_list(&mut self, _list: HostNetworkGroup) -> Result<(), String> {
return Err("not supported".to_string());
}

// This feature is supported for all daemons that use oinq.
async fn echo_request(&mut self) -> Result<(), String> {
return Ok(());
}

async fn trusted_user_agent_list(&mut self, _list: &[&str]) -> Result<(), String> {
return Err("not supported".to_string());
}
}

/// Handles requests to an agent.
Expand Down Expand Up @@ -255,6 +260,13 @@ pub async fn handle<H: Handler>(
RequestCode::EchoRequest => {
send_response(send, &mut buf, handler.echo_request().await).await?;
}
RequestCode::TrustedUserAgentList => {
let user_agent_list = codec
.deserialize::<Vec<&str>>(body)
.map_err(frame::RecvError::DeserializationFailure)?;
let result = handler.trusted_user_agent_list(&user_agent_list).await;
send_response(send, &mut buf, result).await?;
}
RequestCode::ReloadFilterRule => {
let rules = codec
.deserialize::<Vec<IpNet>>(body)
Expand Down Expand Up @@ -320,6 +332,7 @@ mod tests {
allow_list: usize,
block_list: usize,
echo_request_count: usize,
trusted_user_agents: usize,
}

#[async_trait]
Expand Down Expand Up @@ -368,6 +381,11 @@ mod tests {
self.echo_request_count += 1;
Ok(())
}

async fn trusted_user_agent_list(&mut self, list: &[&str]) -> Result<(), String> {
self.trusted_user_agents = list.len();
Ok(())
}
}

let mut handler = TestHandler::default();
Expand Down Expand Up @@ -480,6 +498,26 @@ mod tests {
.await;
assert!(res.is_ok());

let trusted_user_agent_list: Vec<String> = vec![
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0"
.to_string(),
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113."
.to_string(),
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.35"
.to_string(),
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.42"
.to_string(),
];
let mut buf = Vec::new();
let res = message::send_request(
&mut channel.client.send,
&mut buf,
RequestCode::TrustedUserAgentList,
trusted_user_agent_list,
)
.await;
assert!(res.is_ok());

channel.client.send.finish().await.unwrap();

assert_eq!(handler.reboot_count, 0);
Expand All @@ -489,6 +527,7 @@ mod tests {
assert_eq!(handler.allow_list, 0);
assert_eq!(handler.block_list, 0);
assert_eq!(handler.echo_request_count, 0);
assert_eq!(handler.trusted_user_agents, 0);
let res = super::handle(
&mut handler,
&mut channel.server.send,
Expand All @@ -503,6 +542,7 @@ mod tests {
assert_eq!(handler.allow_list, 2);
assert_eq!(handler.block_list, 1);
assert_eq!(handler.echo_request_count, 1);
assert_eq!(handler.trusted_user_agents, 4);

frame::recv_raw(&mut channel.client.recv, &mut buf)
.await
Expand Down

0 comments on commit 0458dac

Please sign in to comment.