From 9a29b9909a590e761488c1192c53fcdddf76cf17 Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Fri, 29 Nov 2019 12:04:47 +0800 Subject: [PATCH 1/3] add leetcode user -c that user can login with cookie --- README.md | 2 ++ lib/commands/user.js | 24 +++++++++++++++++++++++- lib/plugins/leetcode.js | 15 +++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 13eabbb7..56bf57d8 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ # leetcode-cli > Note: This repository is forked from [leetcode-cli](https://github.com/skygragon/leetcode-cli) for temporary usage. +> Note: Copy cookie from webbrowser and Using **leetcode user -c** can temporary fix can't [login problem](https://github.com/jdneo/vscode-leetcode/issues/478). @@ -39,6 +40,7 @@ Great thanks to leetcode.com, a really awesome website! Read help first $ leetcode help Login with your leetcode account $ leetcode user -l + Cookie login with cookie $ leetcode user -c Browse all questions $ leetcode list Choose one question $ leetcode show 1 -g -l cpp Coding it! diff --git a/lib/commands/user.js b/lib/commands/user.js index 4cd903cd..f5ae86b7 100644 --- a/lib/commands/user.js +++ b/lib/commands/user.js @@ -21,6 +21,12 @@ const cmd = { default: false, describe: 'Login' }) + .option('c', { + alias: 'cookie', + type: 'boolean', + default: false, + describe: 'cookieLogin' + }) .option('L', { alias: 'logout', type: 'boolean', @@ -29,6 +35,7 @@ const cmd = { }) .example(chalk.yellow('leetcode user'), 'Show current user') .example(chalk.yellow('leetcode user -l'), 'User login') + .example(chalk.yellow('leetcode user -c'), 'User Cookie login') .example(chalk.yellow('leetcode user -L'), 'User logout'); } }; @@ -59,7 +66,22 @@ cmd.handler = function(argv) { log.info('Successfully logout as', chalk.yellow(user.name)); else log.fail('You are not login yet?'); - } else { + } else if (argv.cookie) { + // session + prompt.colors = false; + prompt.message = ''; + prompt.start(); + prompt.get([ + {name: 'login', required: true}, + {name: 'cookie', required: true} + ], function(e, user) { + if (e) return log.fail(e) + core.cookieLogin(user, function(e, user) { + if (e) return log.fail(e); + log.info('Successfully cookie login as', chalk.yellow(user.name)); + }); + }); + } else { // show current user user = session.getUser(); if (user) { diff --git a/lib/plugins/leetcode.js b/lib/plugins/leetcode.js index 880c9fba..e537c102 100644 --- a/lib/plugins/leetcode.js +++ b/lib/plugins/leetcode.js @@ -538,4 +538,19 @@ plugin.login = function(user, cb) { }); }; +plugin.cookieLogin = function(user, cb) { + // re pattern for cookie chrome or firefox + const SessionPattern = /LEETCODE_SESSION=(.*);?$/im; + const csrfPattern = /csrftoken=(.*);?$/im; + const reSessionResult = SessionPattern.exec(user.cookie); + const reCsrfResult = csrfPattern.exec(user.cookie); + if (reSessionResult === null || reCsrfResult === null) { + return cb('invalid cookie?') + } + user.sessionId = reSessionResult[1]; + user.sessionCSRF = reCsrfResult[1]; + session.saveUser(user); + plugin.getUser(user, cb); +} + module.exports = plugin; From 6f2b68f0d3aef5e6ddbeafa08a000087ec522c1a Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Fri, 29 Nov 2019 16:38:20 +0800 Subject: [PATCH 2/3] change regex bug --- lib/plugins/leetcode.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/plugins/leetcode.js b/lib/plugins/leetcode.js index e537c102..f177c20b 100644 --- a/lib/plugins/leetcode.js +++ b/lib/plugins/leetcode.js @@ -540,15 +540,17 @@ plugin.login = function(user, cb) { plugin.cookieLogin = function(user, cb) { // re pattern for cookie chrome or firefox - const SessionPattern = /LEETCODE_SESSION=(.*);?$/im; - const csrfPattern = /csrftoken=(.*);?$/im; + const SessionPattern = /LEETCODE_SESSION=(.+?)(;|$)/; + const csrfPattern = /csrftoken=(.+?)(;|$)/; const reSessionResult = SessionPattern.exec(user.cookie); const reCsrfResult = csrfPattern.exec(user.cookie); + console.log(reCsrfResult); if (reSessionResult === null || reCsrfResult === null) { return cb('invalid cookie?') } user.sessionId = reSessionResult[1]; user.sessionCSRF = reCsrfResult[1]; + console.log(user.sessionCSRF); session.saveUser(user); plugin.getUser(user, cb); } From 906e083592057febdea9ddf2ed5dda29646f1b90 Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Fri, 29 Nov 2019 16:40:12 +0800 Subject: [PATCH 3/3] drop log --- lib/plugins/leetcode.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/plugins/leetcode.js b/lib/plugins/leetcode.js index f177c20b..924b40d7 100644 --- a/lib/plugins/leetcode.js +++ b/lib/plugins/leetcode.js @@ -544,13 +544,11 @@ plugin.cookieLogin = function(user, cb) { const csrfPattern = /csrftoken=(.+?)(;|$)/; const reSessionResult = SessionPattern.exec(user.cookie); const reCsrfResult = csrfPattern.exec(user.cookie); - console.log(reCsrfResult); if (reSessionResult === null || reCsrfResult === null) { return cb('invalid cookie?') } user.sessionId = reSessionResult[1]; user.sessionCSRF = reCsrfResult[1]; - console.log(user.sessionCSRF); session.saveUser(user); plugin.getUser(user, cb); }