Skip to content

Commit 643061e

Browse files
authored
Merge pull request #2 from tan-wei/dev
Bypass cloudflare
2 parents 745fa25 + fc22dfe commit 643061e

File tree

5 files changed

+63
-18
lines changed

5 files changed

+63
-18
lines changed

.env.sample

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
LEETCODE_COOKIE=

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ target/
88
Cargo.lock
99

1010
# These are backup files generated by rustfmt
11-
**/*.rs.bk
11+
**/*.rs.bk
12+
13+
# Dotenv file
14+
.env

Cargo.toml

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ authors = ["alei <[email protected]>"]
55
edition = "2018"
66

77
[dependencies]
8-
reqwest = { version = "0.11", features = ["blocking", "json"] }
8+
reqwest = { version = "0.11", features = [
9+
"blocking",
10+
"json",
11+
"native-tls-alpn",
12+
"gzip",
13+
] }
914
tokio = { version = "1", features = ["full"] }
1015
serde = "1.0"
1116
serde_json = "1.0"
@@ -14,6 +19,7 @@ rand = "0.6.5"
1419
regex = "1.3.4"
1520
futures = { version = "0.3.28", features = ["thread-pool"] }
1621
surf = "2.3.2"
22+
dotenv = "0.15.0"
1723

1824
[lib]
1925
doctest = false

src/fetcher.rs

+48-16
Original file line numberDiff line numberDiff line change
@@ -99,45 +99,77 @@ pub async fn get_problem_async(problem_stat: StatWithStatus) -> Option<Problem>
9999
pub fn get_problems() -> Option<Problems> {
100100
let headers = {
101101
let mut h = reqwest::header::HeaderMap::new();
102+
h.insert(
103+
"Accept",
104+
reqwest::header::HeaderValue::from_static(
105+
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
106+
),
107+
);
108+
h.insert(
109+
"Accept-Encoding",
110+
reqwest::header::HeaderValue::from_static("gzip, deflate, br"),
111+
);
112+
h.insert(
113+
"Accept-Language",
114+
reqwest::header::HeaderValue::from_static("zh-CN,en-US;q=0.7,en;q=0.3"),
115+
);
116+
h.insert(
117+
"Connection",
118+
reqwest::header::HeaderValue::from_static("keep-alive"),
119+
);
102120
h.insert(
103121
"User-Agent",
104122
reqwest::header::HeaderValue::from_static(
105-
"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0",
123+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:123.0) Gecko/20100101 Firefox/123.0",
106124
),
107125
);
108126
h.insert(
109-
"Referer",
110-
reqwest::header::HeaderValue::from_static(PROBLEMS_URL),
127+
"Sec-Fetch-Dest",
128+
reqwest::header::HeaderValue::from_static("document"),
111129
);
112130
h.insert(
113-
"Origin",
114-
reqwest::header::HeaderValue::from_static(PROBLEMS_URL),
131+
"Sec-Fetch-Mode",
132+
reqwest::header::HeaderValue::from_static("navigate"),
115133
);
116134
h.insert(
117-
"Content-Type",
118-
reqwest::header::HeaderValue::from_static("application/json"),
135+
"Sec-Fetch-Site",
136+
reqwest::header::HeaderValue::from_static("none"),
119137
);
120138
h.insert(
121-
"Accept",
122-
reqwest::header::HeaderValue::from_static("application/json"),
139+
"Sec-Fetch-User",
140+
reqwest::header::HeaderValue::from_static("?1"),
123141
);
142+
h.insert(
143+
"Upgrade-Insecure-Requests",
144+
reqwest::header::HeaderValue::from_static("1"),
145+
);
146+
124147
h.insert(
125148
"Host",
126-
reqwest::header::HeaderValue::from_static(PROBLEMS_URL),
149+
reqwest::header::HeaderValue::from_static("leetcode.com"),
127150
);
128151
h.insert(
129-
"X-Requested-With",
130-
reqwest::header::HeaderValue::from_static("XMLHttpRequest"),
152+
"Cookie",
153+
reqwest::header::HeaderValue::from_str(
154+
&std::env::var("LEETCODE_COOKIE")
155+
.expect("Please set LEETCODE_COOKIE in .env file or environment"),
156+
)
157+
.unwrap(),
131158
);
132159
h
133160
};
134161
let client = reqwest::blocking::Client::builder()
135-
.default_headers(headers)
162+
.connection_verbose(true)
163+
.http2_prior_knowledge()
164+
.gzip(true)
136165
.build()
137166
.unwrap();
138-
let res = client.get(PROBLEMS_URL).send().unwrap().json();
139-
println!("{:?}", res);
140-
res.unwrap()
167+
let get = client.get(PROBLEMS_URL).headers(headers);
168+
// println!("Get: {:?}", get);
169+
let reponse = get.send().unwrap();
170+
// println!("Response: {:?}", reponse);
171+
let result = reponse.json();
172+
result.unwrap()
141173
}
142174

143175
#[derive(Serialize, Deserialize)]

src/main.rs

+3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ use futures::executor::ThreadPool;
1919
use futures::future::join_all;
2020
use futures::stream::StreamExt;
2121
use futures::task::SpawnExt;
22+
extern crate dotenv;
2223
use std::sync::{Arc, Mutex};
2324

2425
/// main() helps to generate the submission template .rs
2526
fn main() {
27+
dotenv::dotenv().ok();
28+
2629
println!("Welcome to leetcode-rust system.\n");
2730
let mut initialized_ids = get_initialized_ids();
2831
loop {

0 commit comments

Comments
 (0)