j178 / leetgo

Best LeetCode friend for geek. :snowboarder:
MIT License
527 stars 33 forks source link

[Bug] Unable to login using cookies #282

Closed g-kartik closed 4 months ago

g-kartik commented 4 months ago

leetgo debug

Leetgo version info :

1.4.1
commit: 34b9f1c6693f0b0de95aee203db99bdb3b4c4c08
built at: 2024-01-27T05:35:18Z
goos: linux
goarch: amd64

Home dir : /home/gkrj/.config/leetgo Project root : /mnt/ssd/work Working dir : /mnt/ssd/work Project config file : /mnt/ssd/work/leetgo.yaml Project configuration:

# Leetgo configuration file, see more at https://github.com/j178/leetgo

# Your name
author: karthik
# Language of the question description: zh or en
language: en
code:
  # Language of code generated for questions: go, cpp, python, java... 
  # (will be overridden by command line flag -l/--lang)
  lang: en
  # The default template to generate filename (without extension), e.g. {{.Id}}.{{.Slug}}
  # Available attributes: Id, Slug, Title, Difficulty, Lang, SlugIsMeaningful
  # Available functions: lower, upper, trim, padWithZero, toUnderscore, group
  filename_template: '{{ .Id | padWithZero 4 }}{{ if .SlugIsMeaningful }}.{{ .Slug }}{{ end }}'
  # Generate question description into a separate question.md file
  separate_description_file: true
  # Default modifiers for all languages
  modifiers:
    - name: removeUselessComments
  go:
    out_dir: go
    # Functions that modify the generated code
    modifiers:
      - name: removeUselessComments
      - name: changeReceiverName
      - name: addNamedReturn
      - name: addMod
  python3:
    out_dir: python
    # Python executable that creates the venv
    executable: python3
  cpp:
    out_dir: cpp
    # C++ compiler
    cxx: g++
    # C++ compiler flags (our Leetcode I/O library implementation requires C++17)
    cxxflags: -O2 -std=c++17
  rust:
    out_dir: rust
  java:
    out_dir: java
leetcode:
  # LeetCode site, https://leetcode.com or https://leetcode.cn
  site: https://leetcode.com
  # Credentials to access LeetCode
  credentials:
    # How to provide credentials: browser, cookies, password or none
    from: cookies
    # Browsers to get cookies from: chrome, safari, edge or firefox. If empty, all browsers will be tried
    browsers: []
contest:
  # Base dir to put generated contest questions
  out_dir: contest
  # Template to generate filename of the question
  filename_template: '{{ .ContestShortSlug }}/{{ .Id }}{{ if .SlugIsMeaningful }}.{{ .Slug }}{{ end }}'
  # Open the contest page in browser after generating
  open_in_browser: true
# Editor settings to open generated files
editor:
  # Use a predefined editor: vim, vscode, goland
  # Set to 'none' to disable, set to 'custom' to provide your own command
  use: none
  # Custom command to open files
  command: "vscode"
  # Arguments to the command.
  # String contains {{.CodeFile}}, {{.TestFile}}, {{.DescriptionFile}}, {{.TestCasesFile}} will be replaced with corresponding file path.
  # {{.Folder}} will be substituted with the output directory.
  # {{.Files}} will be substituted with the list of all file paths.
  args: ""

Full configuration :

author: karthik
language: en
code:
  lang: en
  filename_template: '{{ .Id | padWithZero 4 }}{{ if .SlugIsMeaningful }}.{{ .Slug }}{{ end }}'
  separate_description_file: true
  modifiers:
    - name: removeUselessComments
  go:
    out_dir: go
    modifiers:
      - name: removeUselessComments
      - name: changeReceiverName
      - name: addNamedReturn
      - name: addMod
  python3:
    out_dir: python
    executable: python3
  cpp:
    out_dir: cpp
    cxx: g++
    cxxflags: -O2 -std=c++17
  rust:
    out_dir: rust
  java:
    out_dir: java
leetcode:
  site: https://leetcode.com
  credentials:
    from: cookies
    browsers: []
contest:
  out_dir: contest
  filename_template: '{{ .ContestShortSlug }}/{{ .Id }}{{ if .SlugIsMeaningful }}.{{ .Slug }}{{ end }}'
  open_in_browser: true
editor:
  use: none
  command: vscode
  args: ""

Debug log

2024/03/08 23:53:12 DEBU cache loaded path=/home/gkrj/.config/leetgo/cache/leetcode-questions.json elapsed=48.299µs
2024/03/08 23:53:12 DEBU get by id elapsed=118.734µs
2024/03/08 23:53:12 DEBU request method=POST url=https://leetcode.com/graphql
  body=
  │ {"operationName":"questionData","query":"\n\tquery questionData($titleSlug: String!) {\n\t\tquestion(titleSlug: $titleSlug) {\n\t\t\tquestionId\n\t\t\tquestionFrontendId\n\t\t\tcategoryTitle\n\t\t\ttitle\n\t\t\ttitleSlug\n\t\t\tcontent\n\t\t\tisPaidOnly\n\t\t\ttranslatedTitle\n\t\t\ttranslatedContent\n\t\t\tdifficulty\n\t\t\tstatus\n\t\t\tstats\n\t\t\thints\n\t\t\tsimilarQuestions\n\t\t\tsampleTestCase\n\t\t\texampleTestcases\n\t\t\texampleTestcaseList\n\t\t\tmetaData\n\t\t\tcodeSnippets {\n\t\t\t\tlang\n\t\t\t\tlangSlug\n\t\t\t\tcode\n\t\t\t}\n\t\t\ttopicTags {\n\t\t\t\tname\n\t\t\t\tslug\n\t\t\t\ttranslatedName\n\t\t\t}\n\t\t}\n\t}","variables":{"titleSlug":"1"}}

2024/03/08 23:53:12 FATA invalid qid: user not signed in, your cookies may have expired

Description

I ran leetgo pick 1. I have set the LEETCODE_SESSION and LEETCODE_CSRFTOKEN as environment variables from my logged in Leetcode account and yet I am unable to login.

Cvaniak commented 4 months ago

I think it is duplicate of #256.

I also have problem to use leetgo on leetcode.com.
I am able to pick problems with leetcode.cn.

I tried both via cookies and browser (which worked well before). Since 06.03.2024 all my tries end up with same:

invalid qid: user not signed in, your cookies may have expired

When I try to

leetgo pick today
# output:
invalid qid: user not signed in, your cookies may have expired

but when I try to use:

leetgo pick name-of-task
# output:
× failed to load cache, try updating with `leetgo cache update` err="stat /home/name/.config/leetgo/cache/leetcode-questions.json: no such file or directory"
● read LeetCode cookies browser=firefox domain=leetcode.com
× invalid qid: user not signed in, your cookies may have expired

# or if I have no variables setup for cookies or I am not login on leetcode
× failed to load cache, try updating with `leetgo cache update` err="stat /home/name/.config/leetgo/cache/leetcode-questions.json: no such file or directory"
● add credentials failed, continue requesting without credentials err="cookies not found"
× invalid qid: user not signed in, your cookies may have expired

So I think we can assume that it reads cookies properly.

I tried with v1.3.13 and v1.4.1.

g-kartik commented 4 months ago

Any idea on how can we make it work on Leetcode us site? I don't understand Chinese.

thorvn commented 4 months ago

I have the same issue. I try to use graphql with insomnia, it still was blocked by cloudflare. Still don't know how to bypass this one.

j178 commented 4 months ago

Hi, @th0j @g-kartik @Cvaniak I just pushed a commit attempting to resolve this. Please see if it works for you:

go run github.com/j178/leetgo@master whoami

If you're providing cookies manually, grab a cf_clearance cookie and set it to LEETCODE_CFCLEARANCE env var. Let me know how it goes!

whjum commented 4 months ago

The issue might related to this discussion in Reddit. It seems that Leetcode did restrict basic crawler 4 days ago. It should be the same date @Cvaniak got their issue.

Here is the curl script that works sometime:

curl \
--location "https://leetcode.com/graphql/" \
--header "Content-Type: application/json" \
--header "User-Agent: ME" \
--header "Cookie: LEETCODE_SESSION=<LEETCODE_SESSION>; csrftoken=<csrftoken>" \
--data '{"query":"query problemsetQuestionList($categorySlug: String, $limit: Int, $skip: Int, $filters: QuestionListFilterInput) {\n  problemsetQuestionList: questionList(\n    categorySlug: $categorySlug\n    limit: $limit\n   skip: $skip\n    filters: $filters\n  ) {\n    total: totalNum\n     questions: data {\n      acRate\n      difficulty\n      freqBar\n      frontendQuestionId: questionFrontendId\n      isFavor\n      paidOnly: isPaidOnly\n      status\n      title\n      titleSlug\n      topicTags {\n        name\n        id\n        slug\n      }\n      hasSolution\n      hasVideoSolution\n    }\n  }\n}","variables":{"categorySlug":"","skip":0,"limit":1,"filters":{}}}' \

It works with my environment occasionally while mostly it will return something like "just a moment".

I noticed that Leetgo already add User-Agent in all request except authentication. However, it doesn't work even once under the same env with either browser or cookies option. There might be some filtering behind while I am not sure what that is.

j178 commented 4 months ago

I figure out that turning off http2 seems to do the trick; now leetcode.com is consistently working for me. Could someone else confirm it by running?

go run github.com/j178/leetgo@master whoami
rgbygv commented 4 months ago

Can pick problem, but can't submit.

go run github.com/j178/leetgo@master whoami
× [403 Forbidden] user not signed in, your cookies may have expired
exit status 1
lc test last -Ls --lang rust --site us
● running test locally question=custom-sort-string
● building file=rust/src/0791.custom-sort-string/solution.rs
● Case 1:    Skipped: no output
● submitting solution user=aaaaaa@leetcode.com
× failed to submit solution err="failed to submit solution: user not signed in, your cookies may have expired"
whjum commented 4 months ago

I figure out that turning off http2 seems to do the trick; now leetcode.com is consistently working for me. Could someone else confirm it by running?

go run github.com/j178/leetgo@master whoami

It works for me at this moment.

Cvaniak commented 4 months ago

I figure out that turning off http2 seems to do the trick; now leetcode.com is consistently working for me. Could someone else confirm it by running?

go run github.com/j178/leetgo@master whoami

It also works for me. I hope it will solve this problem for good.

Thank you very much! As always - quick and working solution!

g-kartik commented 4 months ago

Thank you @j178 for such quick fixes. It have tried setting environment variables as you told. It works absolutely fine.

Hi, @th0j @g-kartik @Cvaniak I just pushed a commit attempting to resolve this. Please see if it works for you:

go run github.com/j178/leetgo@master whoami

If you're providing cookies manually, grab a cf_clearance cookie and set it to LEETCODE_CFCLEARANCE env var. Let me know how it goes!

j178 commented 4 months ago

Hi everyone, I've released v1.4.2 which might mitigate this issue. Interestingly, this morning leetgo can access leetcode.com successfully even without those changes :) It's werid, so I can not promoise this issue will not come again, but rest assured, I'll be keeping watch on this.