-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Piotr Czajka
committed
Sep 17, 2023
1 parent
4aef5c8
commit 2776fe4
Showing
7 changed files
with
238 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
**/*.rs.bk | ||
Cargo.lock | ||
tags | ||
cookies.sqlite-shm | ||
cookies.sqlite-wal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,88 @@ | ||
use browsercookie::{Attribute, Browser, CookieFinder}; | ||
use clap::{App, Arg}; | ||
use regex::Regex; | ||
use clap::{Arg, App}; | ||
use browsercookie::{Browser, Browsercookies}; | ||
|
||
#[macro_use] | ||
extern crate clap; | ||
|
||
fn curl_output(bc: &Browsercookies, domain_regex: &Regex) { | ||
print!("Cookie: {}", bc.to_header(domain_regex).unwrap()); | ||
async fn curl_output(cookie_finder: &CookieFinder) { | ||
let cookie_jar = cookie_finder.find().await; | ||
let cookie = cookie_jar.iter().last().expect("Cookie not found"); | ||
print!("Cookie: {}", cookie); | ||
} | ||
|
||
fn python_output(bc: &Browsercookies, domain_regex: &Regex) { | ||
print!("{{'Cookie': '{}'}}", bc.to_header(domain_regex).unwrap()); | ||
async fn python_output(cookie_finder: &CookieFinder) { | ||
let cookie_jar = cookie_finder.find().await; | ||
let cookie = cookie_jar.iter().last().expect("Cookie not found"); | ||
print!("{{'Cookie': '{}'}}", cookie); | ||
} | ||
|
||
fn main() { | ||
#[tokio::main] | ||
async fn main() { | ||
let matches = App::new("browsercookies") | ||
.version(crate_version!()) | ||
.author(crate_authors!()) | ||
.about(crate_description!()) | ||
.arg(Arg::with_name("domain") | ||
.short("d") | ||
.long("domain") | ||
.value_name("DOMAIN_REGEX") | ||
.required(true) | ||
.help("Sets a domain filter for cookies") | ||
.takes_value(true)) | ||
.arg(Arg::with_name("browser") | ||
.short("b") | ||
.long("browser") | ||
.value_name("BROWSER") | ||
.multiple(true) | ||
.default_value("firefox") | ||
.help("Accepted values: firefox (only one can be provided)") | ||
.takes_value(true)) | ||
.arg(Arg::with_name("name") | ||
.short("n") | ||
.long("name") | ||
.conflicts_with("output") | ||
.value_name("COOKIE_NAME") | ||
.help("Specify a cookie name to output only that value") | ||
.takes_value(true)) | ||
.arg(Arg::with_name("output") | ||
.short("o") | ||
.long("output") | ||
.value_name("OUTPUT_FORMAT") | ||
.help("Accepted values: curl,python (only one can be provided)") | ||
.default_value("curl") | ||
.takes_value(true)) | ||
.get_matches(); | ||
.version(crate_version!()) | ||
.author(crate_authors!()) | ||
.about(crate_description!()) | ||
.arg( | ||
Arg::with_name("domain") | ||
.short("d") | ||
.long("domain") | ||
.value_name("DOMAIN_REGEX") | ||
.required(true) | ||
.help("Sets a domain filter for cookies") | ||
.takes_value(true), | ||
) | ||
.arg( | ||
Arg::with_name("browser") | ||
.short("b") | ||
.long("browser") | ||
.value_name("BROWSER") | ||
.multiple(true) | ||
.default_value("firefox") | ||
.help("Accepted values: firefox (only one can be provided)") | ||
.takes_value(true), | ||
) | ||
.arg( | ||
Arg::with_name("name") | ||
.short("n") | ||
.long("name") | ||
.conflicts_with("output") | ||
.value_name("COOKIE_NAME") | ||
.help("Specify a cookie name to output only that value") | ||
.takes_value(true), | ||
) | ||
.arg( | ||
Arg::with_name("output") | ||
.short("o") | ||
.long("output") | ||
.value_name("OUTPUT_FORMAT") | ||
.help("Accepted values: curl,python (only one can be provided)") | ||
.default_value("curl") | ||
.takes_value(true), | ||
) | ||
.get_matches(); | ||
|
||
let mut bc = Browsercookies::new(); | ||
let domain_regex = Regex::new(matches.value_of("domain").unwrap()).unwrap(); | ||
|
||
for b in matches.values_of("browser").unwrap() { | ||
if b == "firefox" { | ||
bc.from_browser(Browser::Firefox, &domain_regex).expect("Failed to get cookies from firefox"); | ||
let mut builder = CookieFinder::builder().with_regexp(domain_regex, Attribute::Domain); | ||
|
||
for b in matches.values_of("browser").unwrap() { | ||
if b == "firefox" { | ||
builder = builder.with_browser(Browser::Firefox); | ||
} | ||
} | ||
|
||
if let Some(cookie_name) = matches.value_of("name") { | ||
print!("{}", bc.cj.get(cookie_name).expect("Cookie not present").value()); | ||
builder.build().find().await.iter().for_each(|c| { | ||
if c.name() == cookie_name { | ||
println!("{}", c.value()); | ||
} | ||
}); | ||
} else { | ||
match matches.value_of("output").unwrap() { | ||
"curl" => curl_output(&bc, &domain_regex), | ||
"python" => python_output(&bc, &domain_regex), | ||
_ => () | ||
"curl" => curl_output(&builder.build()).await, | ||
"python" => python_output(&builder.build()).await, | ||
_ => (), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.