1 | | - | use serde::{Deserialize, Serialize}; |
2 | | - | |
3 | 1 | | use crate::Error; |
4 | 2 | | |
5 | | - | #[derive(Deserialize)] |
6 | | - | pub(crate) struct DebugManifest { |
7 | | - | #[serde(rename(deserialize = "webSocketDebuggerUrl"))] |
8 | | - | pub ws_debugger_url: String, |
9 | | - | } |
| 3 | + | pub(crate) mod requests { |
| 4 | + | use lazy_static::lazy_static; |
| 5 | + | use serde::Serialize; |
| 6 | + | use std::collections::HashMap; |
| 7 | + | use std::sync::{ |
| 8 | + | atomic::{AtomicUsize, Ordering}, |
| 9 | + | Arc, |
| 10 | + | }; |
10 | 11 | | |
11 | | - | #[derive(Deserialize, Debug)] |
12 | | - | pub(crate) struct DomainCommandParam { |
13 | | - | pub name: String, |
14 | | - | } |
| 12 | + | lazy_static! { |
| 13 | + | static ref REQUEST_ID: Arc<AtomicUsize> = Arc::new(AtomicUsize::new(0)); |
| 14 | + | } |
15 | 15 | | |
16 | | - | #[derive(Deserialize, Debug)] |
17 | | - | pub(crate) struct DomainCommand { |
18 | | - | pub name: String, |
19 | | - | pub parameters: Option<Vec<DomainCommandParam>>, |
20 | | - | } |
| 16 | + | #[derive(Serialize)] |
| 17 | + | #[serde(untagged)] |
| 18 | + | pub(crate) enum ParamValue { |
| 19 | + | String(String), |
| 20 | + | Bool(bool), |
| 21 | + | } |
21 | 22 | | |
22 | | - | impl DomainCommand { |
23 | | - | pub fn display(&self) -> String { |
24 | | - | let args = match &self.parameters { |
25 | | - | Some(params) => params |
26 | | - | .iter() |
27 | | - | .map(|p| p.name.to_owned()) |
28 | | - | .collect::<Vec<String>>() |
29 | | - | .join(", "), |
30 | | - | None => "".to_owned(), |
31 | | - | }; |
32 | | - | format!(".{}({})", self.name, args) |
| 23 | + | #[derive(Serialize)] |
| 24 | + | pub(crate) struct MethodCall { |
| 25 | + | pub id: usize, |
| 26 | + | pub method: String, |
| 27 | + | pub params: HashMap<String, ParamValue>, |
33 | 28 | | } |
34 | | - | } |
35 | 29 | | |
36 | | - | #[derive(Deserialize, Debug)] |
37 | | - | pub(crate) struct Domain { |
38 | | - | pub domain: String, |
39 | | - | pub commands: Vec<DomainCommand>, |
40 | | - | } |
| 30 | + | impl MethodCall { |
| 31 | + | pub fn new(method: String, params: HashMap<String, ParamValue>) -> Self { |
| 32 | + | let id = REQUEST_ID.fetch_add(1, Ordering::SeqCst); |
| 33 | + | Self { id, method, params } |
| 34 | + | } |
| 35 | + | } |
41 | 36 | | |
42 | | - | #[derive(Deserialize)] |
43 | | - | pub(crate) struct Protocol { |
44 | | - | pub domains: Vec<Domain>, |
45 | | - | } |
| 37 | + | #[derive(Serialize)] |
| 38 | + | pub(crate) struct RuntimeEval(MethodCall); |
46 | 39 | | |
47 | | - | #[derive(Serialize)] |
48 | | - | pub(crate) struct EvalParams { |
49 | | - | #[serde(rename(serialize = "awaitPromise"))] |
50 | | - | pub await_promise: bool, |
51 | | - | #[serde(rename(serialize = "includeCommandLineAPI"))] |
52 | | - | pub include_commandline_api: bool, |
53 | | - | #[serde(rename(serialize = "allowUnsafeEvalBlockedByCSP"))] |
54 | | - | pub unsafe_eval: bool, |
55 | | - | pub expression: String, |
| 40 | + | impl RuntimeEval { |
| 41 | + | pub fn new(expression: &str) -> Self { |
| 42 | + | let params = HashMap::from([ |
| 43 | + | ("awaitPromise".to_owned(), ParamValue::Bool(true)), |
| 44 | + | ("includeCommandLineAPI".to_owned(), ParamValue::Bool(true)), |
| 45 | + | ( |
| 46 | + | "allowUnsafeEvalBlockedByCSP".to_owned(), |
| 47 | + | ParamValue::Bool(true), |
| 48 | + | ), |
| 49 | + | ( |
| 50 | + | "expression".to_owned(), |
| 51 | + | ParamValue::String(expression.to_owned()), |
| 52 | + | ), |
| 53 | + | ]); |
| 54 | + | Self(MethodCall::new("Runtime.evaluate".to_owned(), params)) |
| 55 | + | } |
| 56 | + | } |
56 | 57 | | } |
57 | 58 | | |
58 | | - | #[derive(Serialize)] |
59 | | - | pub(crate) struct EvalRequest { |
60 | | - | pub id: usize, |
61 | | - | pub method: String, |
62 | | - | pub params: EvalParams, |
63 | | - | } |
| 59 | + | pub(crate) mod responses { |
| 60 | + | use serde::Deserialize; |
64 | 61 | | |
65 | | - | impl EvalRequest { |
66 | | - | pub fn new(expression: &str) -> Self { |
67 | | - | Self { |
68 | | - | id: 0, |
69 | | - | method: "Runtime.evaluate".to_owned(), |
70 | | - | params: EvalParams { |
71 | | - | await_promise: true, |
72 | | - | include_commandline_api: true, |
73 | | - | unsafe_eval: true, |
74 | | - | expression: expression.to_owned(), |
75 | | - | }, |
| 62 | + | #[derive(Deserialize)] |
| 63 | + | pub(crate) struct DebugManifest { |
| 64 | + | #[serde(rename(deserialize = "webSocketDebuggerUrl"))] |
| 65 | + | pub ws_debugger_url: String, |
| 66 | + | } |
| 67 | + | |
| 68 | + | #[derive(Deserialize, Debug)] |
| 69 | + | pub(crate) struct DomainCommandParam { |
| 70 | + | pub name: String, |
| 71 | + | } |
| 72 | + | |
| 73 | + | #[derive(Deserialize, Debug)] |
| 74 | + | pub(crate) struct DomainCommand { |
| 75 | + | pub name: String, |
| 76 | + | pub parameters: Option<Vec<DomainCommandParam>>, |
| 77 | + | } |
| 78 | + | |
| 79 | + | impl DomainCommand { |
| 80 | + | pub fn display(&self) -> String { |
| 81 | + | let args = match &self.parameters { |
| 82 | + | Some(params) => params |
| 83 | + | .iter() |
| 84 | + | .map(|p| p.name.to_owned()) |
| 85 | + | .collect::<Vec<String>>() |
| 86 | + | .join(", "), |
| 87 | + | None => "".to_owned(), |
| 88 | + | }; |
| 89 | + | format!(".{}({})", self.name, args) |
76 | 90 | | } |
77 | 91 | | } |
| 92 | + | |
| 93 | + | #[derive(Deserialize, Debug)] |
| 94 | + | pub(crate) struct Domain { |
| 95 | + | pub domain: String, |
| 96 | + | pub commands: Vec<DomainCommand>, |
| 97 | + | } |
| 98 | + | |
| 99 | + | #[derive(Deserialize)] |
| 100 | + | pub(crate) struct Protocol { |
| 101 | + | pub domains: Vec<Domain>, |
| 102 | + | } |
78 | 103 | | } |
79 | 104 | | |
80 | | - | pub(crate) fn get_domains(port: u16) -> Result<Vec<Domain>, Error> { |
| 105 | + | pub(crate) fn get_domains(port: u16) -> Result<Vec<responses::Domain>, Error> { |
81 | 106 | | let json_url = format!("http://127.0.0.1:{}/json/protocol", port); |
82 | 107 | | println!( |
83 | 108 | | "inspection enabled on port {}, requesting available domains from {} ...", |
| skipped 1 lines |
85 | 110 | | ); |
86 | 111 | | |
87 | 112 | | let body = reqwest::blocking::get(json_url).unwrap().bytes().unwrap(); |
88 | | - | let proto: Protocol = serde_json::from_slice(&body).map_err(|e| { |
| 113 | + | let proto: responses::Protocol = serde_json::from_slice(&body).map_err(|e| { |
89 | 114 | | format!( |
90 | 115 | | "could not parse protocol definitiont: {:?}\n\nDATA:\n {:?}", |
91 | 116 | | e, &body |
| skipped 12 lines |
104 | 129 | | ); |
105 | 130 | | |
106 | 131 | | let body = reqwest::blocking::get(json_url).unwrap().bytes().unwrap(); |
107 | | - | let debug_manifests: Vec<DebugManifest> = serde_json::from_slice(&body).map_err(|e| { |
108 | | - | format!( |
109 | | - | "could not parse debug manifest: {:?}\n\nDATA:\n {:?}", |
110 | | - | e, &body |
111 | | - | ) |
112 | | - | })?; |
| 132 | + | let debug_manifests: Vec<responses::DebugManifest> = |
| 133 | + | serde_json::from_slice(&body).map_err(|e| { |
| 134 | + | format!( |
| 135 | + | "could not parse debug manifest: {:?}\n\nDATA:\n {:?}", |
| 136 | + | e, &body |
| 137 | + | ) |
| 138 | + | })?; |
113 | 139 | | |
114 | 140 | | if debug_manifests.is_empty() { |
115 | 141 | | return Err("no debug manifests found".to_owned()); |
| skipped 5 lines |