Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/bin/lc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fn main() {
.expect("Build tokio runtime failed")
.block_on(cli::main())
{
println!("{:?}", err);
eprintln!("{err}");
std::process::exit(1);
}
}
8 changes: 6 additions & 2 deletions src/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ pub enum Error {
"~/.leetcode/leetcode.toml".yellow().bold().underline(),
)]
Config(#[from] toml::de::Error),
#[error("Maybe you not login on the Chrome, you can login and retry")]
ChromeNotLogin,
#[error(
"No LeetCode cookies found in Chrome. \
Please log in at https://leetcode.com in Chrome, \
or set `csrf` and `session` in `~/.leetcode/leetcode.toml`."
)]
CookieNotFound,
#[error(transparent)]
ParseInt(#[from] std::num::ParseIntError),
#[error(transparent)]
Expand Down
19 changes: 11 additions & 8 deletions src/plugins/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! (`LEETCODE_SESSION`, `csrftoken`) from one browser, so this reuses the SQLite
//! stack already pulled in by `diesel` to read the cookie DB and implements just
//! Chrome's value decryption — no second SQLite client and no dbus linkage.
#![allow(clippy::redundant_field_names)] // diesel derive trips this in generated code
use crate::{Error, Result};
use cbc::cipher::{BlockDecryptMut, KeyIvInit, block_padding::Pkcs7};
use diesel::{prelude::*, sql_query, sql_types, sqlite::SqliteConnection};
Expand Down Expand Up @@ -42,11 +43,11 @@ pub fn cookies() -> Result<Ident> {
Ok(Ident {
csrf: jar
.get("csrftoken")
.ok_or(Error::ChromeNotLogin)?
.ok_or(Error::CookieNotFound)?
.to_string(),
session: jar
.get("LEETCODE_SESSION")
.ok_or(Error::ChromeNotLogin)?
.ok_or(Error::CookieNotFound)?
.to_string(),
})
}
Expand All @@ -61,7 +62,7 @@ struct CookieRow {

/// Read Chrome's cookie DB for `domain` and decrypt the matching values.
fn chrome_cookies(domain: &str) -> Result<HashMap<String, String>> {
let db = cookie_db_path().ok_or(Error::ChromeNotLogin)?;
let db = cookie_db_path().ok_or(Error::CookieNotFound)?;

// Chrome keeps the DB locked while running; read from a snapshot copy.
let tmp = std::env::temp_dir().join(format!("leetcode-cli-{}.sqlite", std::process::id()));
Expand Down Expand Up @@ -123,7 +124,8 @@ fn derive(password: &[u8], iterations: u32) -> Vec<u8> {

#[cfg(target_os = "macos")]
fn cookie_db_path() -> Option<PathBuf> {
Some(dirs::home_dir()?.join("Library/Application Support/Google/Chrome/Default/Cookies"))
let p = dirs::home_dir()?.join("Library/Application Support/Google/Chrome/Default/Cookies");
p.exists().then_some(p)
}

#[cfg(target_os = "macos")]
Expand All @@ -141,7 +143,7 @@ fn encryption_key() -> Result<Vec<u8>> {
let pw = String::from_utf8_lossy(&out.stdout);
let pw = pw.trim();
if pw.is_empty() {
return Err(Error::ChromeNotLogin);
return Err(Error::CookieNotFound);
}
Ok(derive(pw.as_bytes(), 1003))
}
Expand Down Expand Up @@ -170,15 +172,16 @@ fn encryption_key() -> Result<Vec<u8>> {

#[cfg(target_os = "windows")]
fn cookie_db_path() -> Option<PathBuf> {
Some(dirs::data_local_dir()?.join("Google/Chrome/User Data/Default/Network/Cookies"))
let p = dirs::data_local_dir()?.join("Google/Chrome/User Data/Default/Network/Cookies");
p.exists().then_some(p)
}

#[cfg(target_os = "windows")]
fn encryption_key() -> Result<Vec<u8>> {
// Windows uses DPAPI + AES-256-GCM (and app-bound encryption on Chrome
// 127+), a separate code path that isn't implemented yet. Until then,
// Windows users should set `csrf`/`session` in the config manually.
Err(Error::ChromeNotLogin)
Err(Error::CookieNotFound)
}

// for headless installations stub functions are defined
Expand All @@ -190,5 +193,5 @@ fn cookie_db_path() -> Option<PathBuf> {

#[cfg(target_os = "android")]
fn encryption_key() -> Result<Vec<u8>> {
Err(Error::ChromeNotLogin)
Err(Error::CookieNotFound)
}
Loading