Skip to content

Commit

Permalink
Try bypass cf captcha
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc authored Jul 2, 2024
1 parent a55b6ff commit 12ca08b
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 142 deletions.
235 changes: 94 additions & 141 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ json = "0.12"
lazy_static = "1.4"
log = "*"
log4rs = "1"
markup5ever_rcdom = "0.2"
markup5ever_rcdom = "0.3"
md5 = "0.7"
modular-bitfield = "0.11"
multipart = { features = ["server"], git = 'https://github.com/lifegpc/multipart', optional = true, default-features = false }
Expand Down
5 changes: 5 additions & 0 deletions src/fanbox_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ impl FanboxClientInternal {
self.client.set_header("User-Agent", &helper.user_agent());
self.client.set_header("referer", "https://www.fanbox.cc/");
self.client.set_header("origin", "https://www.fanbox.cc");
let headers = helper.fanbox_http_headers();
for (k, v) in headers.iter() {
self.client.set_header(k, v);
}
self.inited.qstore(true);
true
}
Expand Down Expand Up @@ -116,6 +120,7 @@ impl FanboxClientInternal {
let code = status.as_u16();
if code >= 400 {
log::error!(target: "fanbox_api", "{} {}", gettext("Failed to get fanbox main page:"), status);
log::debug!(target: "fanbox_api", "{}", r.text_with_charset("UTF-8").await.unwrap_or(String::from("")));
return false;
}
match r.text_with_charset("UTF-8").await {
Expand Down
34 changes: 34 additions & 0 deletions src/opt/header_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::error::PixivDownloaderError;
use crate::ext::json::{FromJson, ToJson};
use json::JsonValue;
use std::collections::HashMap;

pub type HeaderMap = HashMap<String, String>;

impl FromJson for HeaderMap {
type Err = PixivDownloaderError;
fn from_json<T: ToJson>(v: T) -> Result<Self, Self::Err> {
let j = v.to_json();
match &j {
Some(j) => {
let mut map = HeaderMap::new();
for (k, v) in j.entries() {
if v.is_string() {
map.insert(k.to_owned(), v.as_str().unwrap().to_owned());
} else {
return Err(PixivDownloaderError::from(format!(
"Key {} is not string.",
k
)));
}
}
Ok(map)
}
None => Ok(HeaderMap::new()),
}
}
}

pub fn check_header_map(obj: &JsonValue) -> bool {
HeaderMap::from_json(obj).is_ok()
}
2 changes: 2 additions & 0 deletions src/opt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pub mod author_name_filter;
#[cfg(feature = "ugoira")]
/// libx264 Constant Rate Factor settings
pub mod crf;
/// HTTP Header Map
pub mod header_map;
/// Proxy settings
pub mod proxy;
pub mod size;
Expand Down
12 changes: 12 additions & 0 deletions src/opthelper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::ext::use_or_not::ToBool;
use crate::ext::use_or_not::UseOrNot;
use crate::list::NonTailList;
use crate::opt::author_name_filter::AuthorNameFilter;
use crate::opt::header_map::HeaderMap;
use crate::opt::proxy::ProxyChain;
use crate::opt::size::parse_u32_size;
use crate::opt::use_progress_bar::UseProgressBar;
Expand Down Expand Up @@ -48,6 +49,7 @@ pub struct OptHelper {
_proxy_chain: RwLock<ProxyChain>,
#[cfg(feature = "server")]
_cors_entries: RwLock<Vec<CorsEntry>>,
_fanbox_http_headers: RwLock<HeaderMap>,
}

impl OptHelper {
Expand Down Expand Up @@ -198,6 +200,10 @@ impl OptHelper {
self.download_multiple_files() && self.max_download_tasks() > 1
}

pub fn fanbox_http_headers(&self) -> HeaderMap {
self._fanbox_http_headers.get_ref().clone()
}

#[cfg(feature = "ugoira")]
/// Return whether to force yuv420p as output pixel format when converting ugoira(GIF) to video.
pub fn force_yuv420p(&self) -> bool {
Expand Down Expand Up @@ -345,6 +351,11 @@ impl OptHelper {
self._cors_entries
.replace_with2(parse_cors_entries(&settings.get("cors-entries").unwrap()).unwrap());
}
if settings.have("fanbox-http-headers") {
self._fanbox_http_headers.replace_with2(
HeaderMap::from_json(&settings.get("fanbox-http-headers").unwrap()).unwrap(),
);
}
self.opt.replace_with2(opt);
self.settings.replace_with2(settings);
}
Expand Down Expand Up @@ -638,6 +649,7 @@ impl Default for OptHelper {
_proxy_chain: RwLock::new(ProxyChain::default()),
#[cfg(feature = "server")]
_cors_entries: RwLock::new(Vec::new()),
_fanbox_http_headers: RwLock::new(HeaderMap::new()),
};
s.init_log();
s
Expand Down
2 changes: 2 additions & 0 deletions src/settings_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::settings::JsonValueType;
use crate::opt::author_name_filter::check_author_name_filters;
#[cfg(feature = "ugoira")]
use crate::opt::crf::check_crf;
use crate::opt::header_map::check_header_map;
use crate::opt::proxy::check_proxy;
use crate::opt::size::parse_u32_size;
#[cfg(feature = "server")]
Expand Down Expand Up @@ -73,6 +74,7 @@ pub fn get_settings_list() -> Vec<SettingDes> {
SettingDes::new("push-task-max-count", gettext("The maximum number of push tasks running at the same time."), JsonValueType::Number, Some(check_nozero_usize)).unwrap(),
#[cfg(feature = "server")]
SettingDes::new("push-task-max-push-count", gettext("The maximum number of tasks to push to client at the same time."), JsonValueType::Number, Some(check_nozero_usize)).unwrap(),
SettingDes::new("fanbox-http-headers", gettext("Extra http headers for fanbox.cc."), JsonValueType::Object, Some(check_header_map)).unwrap(),
]
}

Expand Down
3 changes: 3 additions & 0 deletions src/webclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,17 +360,20 @@ impl WebClient {
let mut r = self.client.get(s);
for (k, v) in self.get_headers().iter() {
r = r.header(k, v);
log::debug!(target: "webclient", "{}: {}", k, v);
}
let headers = headers.to_headers();
if headers.is_some() {
let h = headers.unwrap();
for (k, v) in h.iter() {
r = r.header(k, v);
log::debug!(target: "webclient", "{}: {}", k, v);
}
}
let c = gen_cookie_header(&self, s);
if c.len() > 0 {
r = r.header("Cookie", c.as_str());
log::debug!(target: "webclient", "Cookie: {}", c.as_str());
}
self.handle_req_middlewares(r.build()?)
}
Expand Down

0 comments on commit 12ca08b

Please sign in to comment.