Skip to content

Commit

Permalink
merge functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Nov 6, 2023
1 parent d0ff72f commit ad4334c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 44 deletions.
63 changes: 32 additions & 31 deletions src/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,50 +71,49 @@ pub struct BrowserConnection {
pub webkit_version: String,
#[serde(rename = "webSocketDebuggerUrl")]
/// Remote debugging address
pub web_socket_debugger_url: String
pub web_socket_debugger_url: String,
}

impl Browser {
/// Connect to an already running chromium instance via websocket
/// Connect to an already running chromium instance via the given URL.
///
/// If the URL is a http(s) URL, it will first attempt to retrieve the Websocket URL from the `json/version` endpoint.
pub async fn connect(debug_ws_url: impl Into<String>) -> Result<(Self, Handler)> {
let debug_ws_url = debug_ws_url.into();
let conn = Connection::<CdpEventMessage>::connect(&debug_ws_url).await?;

let (tx, rx) = channel(1);

let fut = Handler::new(conn, rx, HandlerConfig::default());
let browser_context = fut.default_browser_context().clone();

let browser = Self {
sender: tx,
config: None,
child: None,
debug_ws_url,
browser_context,
};
Ok((browser, fut))
}

/// Connect to an already running chromium instance via url
pub async fn connect_url(debug_ws_url: impl Into<String>) -> Result<(Self, Handler)> {
let mut debug_ws_url = debug_ws_url.into();
let mut debug_ws_url = debug_url.into();

if debug_ws_url.starts_with("http") {
match reqwest::Client::new()
.get(if debug_ws_url.ends_with("/json/version") { debug_ws_url.to_string() } else { format!("{}{}json/version", &debug_ws_url, if debug_ws_url.ends_with("/") { "" } else { "/" }) })
.header("content-type", "application/json")
.send()
.await {
.get(
if debug_ws_url.ends_with("/json/version")
|| debug_ws_url.ends_with("/json/version/")
{
debug_ws_url.clone()
} else {
format!(
"{}{}json/version",
&debug_ws_url,
if debug_ws_url.ends_with("/") { "" } else { "/" }
)
},
)
.header("content-type", "application/json")
.send()
.await
{
Ok(req) => {
let socketaddr = req.remote_addr().unwrap();
let connection: BrowserConnection = serde_json::from_slice(&req.bytes().await.unwrap_or_default()).unwrap_or_default();

let connection: BrowserConnection =
serde_json::from_slice(&req.bytes().await.unwrap_or_default())
.unwrap_or_default();

if !connection.web_socket_debugger_url.is_empty() {
// prevent proxy interfaces from returning local ips to connect to the exact machine
debug_ws_url = connection.web_socket_debugger_url.replace("127.0.0.1", &socketaddr.ip().to_string());
debug_ws_url = connection
.web_socket_debugger_url
.replace("127.0.0.1", &socketaddr.ip().to_string());
}
}
Err(_) => return Err(CdpError::NoResponse)
Err(_) => return Err(CdpError::NoResponse),
}
}

Expand Down Expand Up @@ -914,6 +913,8 @@ pub fn default_executable() -> Result<std::path::PathBuf, String> {
detection::default_executable(options)
}

fn fetch_websocket_url(url: &str) -> Result<String> {}

/// These are passed to the Chrome binary by default.
/// Via https://github.com/puppeteer/puppeteer/blob/4846b8723cf20d3551c0d755df394cc5e0c82a94/src/node/Launcher.ts#L157
static DEFAULT_ARGS: [&str; 25] = [
Expand Down
15 changes: 3 additions & 12 deletions src/detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,10 @@ fn get_by_name(options: &DetectionOptions) -> Option<PathBuf> {
fn get_by_path(options: &DetectionOptions) -> Option<PathBuf> {
#[cfg(all(unix, not(target_os = "macos")))]
let default_paths: [(&str, bool); 3] = [
(
"/opt/chromium.org/chromium",
true
),
(
"/opt/google/chrome",
true
),
("/opt/chromium.org/chromium", true),
("/opt/google/chrome", true),
// test for lambda
(
"/tmp/aws/lib",
true
)
("/tmp/aws/lib", true),
];
#[cfg(windows)]
let default_paths = [(
Expand Down
2 changes: 1 addition & 1 deletion src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ impl Page {
.await?
.into_value()?)
}

/// Returns source for the script with given id.
///
/// Debugger must be enabled.
Expand Down

0 comments on commit ad4334c

Please sign in to comment.