Skip to content

Commit b5d8f2b

Browse files
committed
Integrate curl-impersonate to bypass Cloudflare JS challenge
1 parent acb67f5 commit b5d8f2b

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

src/cache/mod.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,22 @@ impl Cache {
352352
let (json, [url, refer]) = self.pre_run_code(run.clone(), rfid, test_case).await?;
353353
trace!("Pre-run code result {:#?}, {}, {}", json, url, refer);
354354

355-
let text = self
355+
let response = self
356356
.0
357357
.clone()
358358
.run_code(json.clone(), url.clone(), refer.clone())
359-
.await?
360-
.text()
361359
.await?;
360+
let response_status = response.status();
361+
362+
let mut text = response.text().await?;
363+
if !response_status.is_success() {
364+
println!("Response is not success! Error Code {:?}", response_status);
365+
println!("If error code is 403, it's likely caught by Cloudflare, trying to bypass it");
366+
let output = self
367+
.0
368+
.clone().execute_curl_impersonate(json.clone(), url.clone(), refer.clone())?;
369+
text = String::from_utf8_lossy(&output.stdout).to_string();
370+
}
362371

363372
let run_res: RunCode = serde_json::from_str(&text).map_err(|e| {
364373
anyhow!("JSON error: {e}, please double check your session and csrf config.")

src/plugins/leetcode.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use reqwest::{
88
Client, ClientBuilder, Response,
99
};
1010
use std::{collections::HashMap, str::FromStr, time::Duration};
11+
use std::process::{Command, Output};
1112

1213
/// LeetCode API set
1314
#[derive(Clone)]
@@ -43,7 +44,6 @@ impl LeetCode {
4344
vec![
4445
("Cookie", &cookie),
4546
("x-csrftoken", &csrf),
46-
("x-requested-with", "XMLHttpRequest"),
4747
("Origin", &conf.sys.urls.base),
4848
],
4949
)?;
@@ -232,6 +232,40 @@ impl LeetCode {
232232
.send(&self.client)
233233
.await
234234
}
235+
236+
pub fn execute_curl_impersonate(self, j: Json, url: String, refer: String) -> Result<Output> {
237+
let header_csrf_arg = match self.default_headers.get("x-csrftoken") {
238+
Some(value) => format!("x-csrftoken: {}", value.to_str().unwrap()),
239+
None => String::new()
240+
};
241+
242+
let header_referer_arg = format!("Referer: {}", refer);
243+
244+
let cookie_arg = match self.default_headers.get("Cookie") {
245+
Some(value) => format!("{}", value.to_str().unwrap()),
246+
None => String::new()
247+
};
248+
249+
let data_arg = match serde_json::to_string(&j) {
250+
Ok(value) => format!("{}", value),
251+
Err(_) => String::new()
252+
};
253+
254+
let output = Command::new("curl_ff117")
255+
.arg("--header")
256+
.arg(&header_csrf_arg)
257+
.arg("--header")
258+
.arg(&header_referer_arg)
259+
.arg("--cookie")
260+
.arg(&cookie_arg)
261+
.arg("--data")
262+
.arg(&data_arg)
263+
.arg(&url)
264+
.output()
265+
.expect("Failed to execute process");
266+
trace!("{:?}", output);
267+
Ok(output)
268+
}
235269

236270
/// Get the result of submission / testing
237271
pub async fn verify_result(self, id: String) -> Result<Response> {

0 commit comments

Comments
 (0)