From c837a131218bbe5acf5cdc11e6e97a515d9ccbd4 Mon Sep 17 00:00:00 2001 From: Cecylia Bocovich Date: Fri, 20 Jan 2023 16:01:03 -0500 Subject: [PATCH] Initial commit --- crates/rdsys-backend/.gitignore | 2 + crates/rdsys-backend/Cargo.toml | 10 ++ crates/rdsys-backend/src/lib.rs | 157 ++++++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 crates/rdsys-backend/.gitignore create mode 100644 crates/rdsys-backend/Cargo.toml create mode 100644 crates/rdsys-backend/src/lib.rs diff --git a/crates/rdsys-backend/.gitignore b/crates/rdsys-backend/.gitignore new file mode 100644 index 0000000..4fffb2f --- /dev/null +++ b/crates/rdsys-backend/.gitignore @@ -0,0 +1,2 @@ +/target +/Cargo.lock diff --git a/crates/rdsys-backend/Cargo.toml b/crates/rdsys-backend/Cargo.toml new file mode 100644 index 0000000..14adce3 --- /dev/null +++ b/crates/rdsys-backend/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "rdsys-backend" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serde_json = "1" +serde = { version = "1", features = ["derive"]} diff --git a/crates/rdsys-backend/src/lib.rs b/crates/rdsys-backend/src/lib.rs new file mode 100644 index 0000000..8900381 --- /dev/null +++ b/crates/rdsys-backend/src/lib.rs @@ -0,0 +1,157 @@ +use std::collections::HashMap; + +use serde::{Deserialize, Serialize}; + +#[derive(Serialize)] +pub struct ResourceRequest { + request_origin: String, + resource_types: Vec, +} + +#[derive(Deserialize,PartialEq,Debug)] +pub struct Resource { + r#type: String, + blocked_in: HashMap, + protocol: String, + address: String, + port: u16, + fingerprint: String, + #[serde(rename="or-addresses")] + or_addresses: Option>, + distribution: String, + flags: Option>, + params: Option>, +} + +#[derive(Deserialize)] +pub struct ResourceDiff { + new: Option>>, + changed: Option>>, + gone: Option>>, + full_update: bool, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn serialize_resource_request() { + let req = ResourceRequest { + request_origin: String::from("https"), + resource_types: vec![String::from("obfs2"), String::from("scramblesuit")], + }; + let json = serde_json::to_string(&req).unwrap(); + assert_eq!(json, "{\"request_origin\":\"https\",\"resource_types\":[\"obfs2\",\"scramblesuit\"]}") + } + + #[test] + fn deserialize_resource() { + let mut flags = HashMap::new(); + flags.insert(String::from("fast"), true); + flags.insert(String::from("stable"), true); + let mut params = HashMap::new(); + params.insert(String::from("password"), String::from("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567")); + let bridge = Resource { + r#type: String::from("scramblesuit"), + blocked_in: HashMap::new(), + protocol: String::from("tcp"), + address: String::from("216.117.3.62"), + port: 63174, + fingerprint: String::from("BE84A97D02130470A1C77839954392BA979F7EE1"), + or_addresses: None, + distribution: String::from("https"), + flags: Some(flags), + params: Some(params), + }; + + let data = r#" + { + "type": "scramblesuit", + "blocked_in": {}, + "protocol": "tcp", + "address": "216.117.3.62", + "port": 63174, + "fingerprint": "BE84A97D02130470A1C77839954392BA979F7EE1", + "or-addresses": null, + "distribution": "https", + "flags": { + "fast": true, + "stable": true + }, + "params": { + "password": "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" + } + }"#; + let res: Resource = serde_json::from_str(data).unwrap(); + assert_eq!(bridge, res); + } + + #[test] + fn deserialize_resource_diff() { + let data = r#" + { + "new": { + "obfs2": [ + { + "type": "obfs2", + "blocked_in": {}, + "protocol": "tcp", + "address": "176.247.216.207", + "port": 42810, + "fingerprint": "10282810115283F99ADE5CFE42D49644F45D715D", + "or-addresses": null, + "distribution": "https", + "flags": { + "fast": true, + "stable": true, + "running": true, + "valid": true + } + }, + { + "type": "obfs2", + "blocked_in": {}, + "protocol": "tcp", + "address": "133.69.16.145", + "port": 58314, + "fingerprint": "BE84A97D02130470A1C77839954392BA979F7EE1", + "or-addresses": null, + "distribution": "https", + "flags": { + "fast": true, + "stable": true, + "running": true, + "valid": true + } + } + ], + "scramblesuit": [ + { + "type": "scramblesuit", + "blocked_in": {}, + "protocol": "tcp", + "address": "216.117.3.62", + "port": 63174, + "fingerprint": "BE84A97D02130470A1C77839954392BA979F7EE1", + "or-addresses": null, + "distribution": "https", + "flags": { + "fast": true, + "stable": true, + "running": true, + "valid": true + }, + "params": { + "password": "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" + } + } + ] + }, + "changed": null, + "gone": null, + "full_update": true + }"#; + let _diff: ResourceDiff = serde_json::from_str(data).unwrap(); + } +}