Skip to content

Commit 52b694e

Browse files
authored
Merge pull request #1 from woung717/fix-cloudflare-issue
fix: added some request headers and make it always.
2 parents df69a65 + c3e45d8 commit 52b694e

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

lib/plugins/leetcode.cn.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,28 @@ plugin.init = function() {
3939

4040
// FIXME: refactor those
4141
// update options with user credentials
42-
function signOpts(opts, user) {
42+
function setupHeaders(opts, user) {
4343
opts.headers.Cookie = 'LEETCODE_SESSION=' + user.sessionId +
4444
';csrftoken=' + user.sessionCSRF + ';';
4545
opts.headers['X-CSRFToken'] = user.sessionCSRF;
46+
opts.headers['x-csrftoken'] = user.sessionCSRF;
4647
opts.headers['X-Requested-With'] = 'XMLHttpRequest';
48+
49+
opts.headers['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0';
50+
opts.headers['Referer'] = 'https://leetcode.cn';
51+
opts.headers['Origin'] = 'https://leetcode.cn/';
52+
opts.headers['Host'] = 'leetcode.cn';
53+
opts.headers['Content-Type'] = 'application/json';
54+
opts.headers['Accept'] = 'application/json';
4755
}
4856

4957
function makeOpts(url) {
5058
const opts = {};
5159
opts.url = url;
5260
opts.headers = {};
5361

54-
if (session.isLogin())
55-
signOpts(opts, session.getUser());
62+
setupHeaders(opts, session.getUser());
63+
5664
return opts;
5765
}
5866

lib/plugins/leetcode.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,24 @@ plugin.signOpts = function(opts, user) {
2323
opts.headers.Cookie = 'LEETCODE_SESSION=' + user.sessionId +
2424
';csrftoken=' + user.sessionCSRF + ';';
2525
opts.headers['X-CSRFToken'] = user.sessionCSRF;
26+
opts.headers['x-csrftoken'] = user.sessionCSRF;
2627
opts.headers['X-Requested-With'] = 'XMLHttpRequest';
28+
29+
opts.headers['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0';
30+
opts.headers['Referer'] = 'https://leetcode.com';
31+
opts.headers['Origin'] = 'https://leetcode.com/';
32+
opts.headers['Host'] = 'leetcode.com';
33+
opts.headers['Content-Type'] = 'application/json';
34+
opts.headers['Accept'] = 'application/json';
2735
};
2836

2937
plugin.makeOpts = function(url) {
3038
const opts = {};
3139
opts.url = url;
3240
opts.headers = {};
3341

34-
if (session.isLogin())
35-
plugin.signOpts(opts, session.getUser());
42+
plugin.signOpts(opts, session.getUser());
43+
3644
return opts;
3745
};
3846

lib/plugins/solution.discuss.js

+20
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ var plugin = new Plugin(200, 'solution.discuss', '2019.02.03',
1616
var URL_DISCUSSES = 'https://leetcode.com/graphql';
1717
var URL_DISCUSS = 'https://leetcode.com/problems/$slug/discuss/$id';
1818

19+
function setupHeaders(opts, user) {
20+
opts.headers.Cookie = 'LEETCODE_SESSION=' + user.sessionId +
21+
';csrftoken=' + user.sessionCSRF + ';';
22+
opts.headers['X-CSRFToken'] = user.sessionCSRF;
23+
opts.headers['x-csrftoken'] = user.sessionCSRF;
24+
opts.headers['X-Requested-With'] = 'XMLHttpRequest';
25+
26+
opts.headers['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0';
27+
opts.headers['Referer'] = 'https://leetcode.cn';
28+
opts.headers['Origin'] = 'https://leetcode.cn/';
29+
opts.headers['Host'] = 'leetcode.cn';
30+
opts.headers['Content-Type'] = 'application/json';
31+
opts.headers['Accept'] = 'application/json';
32+
}
33+
1934
function getSolution(problem, lang, cb) {
2035
if (!problem) return cb();
2136

@@ -60,6 +75,11 @@ function getSolution(problem, lang, cb) {
6075
})
6176
}
6277
};
78+
79+
opts.headers = {};
80+
81+
setupHeaders(opts, session.getUser());
82+
6383
request(opts, function(e, resp, body) {
6484
if (e) return cb(e);
6585
if (resp.statusCode !== 200)

0 commit comments

Comments
 (0)