Skip to content
This repository has been archived by the owner on Aug 10, 2023. It is now read-only.

Experimental pure Python Cloudflare bypass #1382

Merged
merged 4 commits into from
May 28, 2023

Conversation

hldr4
Copy link
Contributor

@hldr4 hldr4 commented May 22, 2023

Been playing around with this to avoid using the GO proxy method for TLS spoofing. It uses curl-cffi module to spoof the TLS fingerprint (I set chrome110). But, since that module doesn't support stream=True on requests, a workaround is to write content_callback to a temp file, then read from it, then delete. For async it is the same principle, just uses AsyncSession instead.
Works well enough from my various tests, but can never be 100% sure. For example, the following outputs every task result correctly with no parts missing or any weirdness like that.

import asyncio
import sys
from revChatGPT.V1 import Chatbot, AsyncChatbot
from const import access_token, conversation_id
    
# Needed on my Windows machine for asyncio, idk why
if sys.version_info >= (3, 8) and sys.platform.lower().startswith("win"):
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

async def run_prompt(prompt: str, idx: int):
    bot = AsyncChatbot(config={'access_token': access_token}, conversation_id=conversation_id)
    print(f'Task {idx}:\n') 
    prev_text = ""
    async for data in bot.ask(prompt):
        message = data["message"][len(prev_text):]
        print(message, end="", flush=True)
        prev_text = data["message"]
    print('\n'*2)

def start_tasks(tasks: list):
    for i, task in enumerate(tasks):
        asyncio.run(run_prompt(task, i+1))
    
tasks = ['Calculate the sum of the first 10 prime numbers', 'Tell me what distance is the moon from earth', 'Write "Hello World" in C']    

def main():
    print('\nAsync Taskbot:\n')
    start_tasks(tasks)

if __name__ == '__main__':
    main()

So this change allows to use the official https://chat.openai.com/backend-api/conversation endpoint without having to spin up a local proxy or using a bypass site, but only on those IPs where the CF challenge isn't triggered for whatever reason.

hldr4 added 4 commits May 22, 2023 01:09
uses the curl-cffi module for tls fingerprint spoofing and writes content_callback to a temp file as a workaround for the aforementioned module not supporting streamed requests
the official endpoint can only be bypassed with tls spoofing on some IPs
@acheong08
Copy link
Owner

This is good. Removing the need for a proxy server has pretty high demand but I could never figure out how to do it in Python.

Is curl-cffi installation reliable?

If it does not work, you may need to compile and install curl-impersonate first.

src/revChatGPT/V1.py Show resolved Hide resolved
@hldr4
Copy link
Contributor Author

hldr4 commented May 23, 2023

Is curl-cffi installation reliable?

I cannot speak for everyone, but it works without any additional setup on my Windows PC and remote Linux server

src/revChatGPT/V1.py Show resolved Hide resolved
@acheong08
Copy link
Owner

Um there are conflicts now. I'll merge right after they are resolved. Will be on the next release 6.0.0

@acheong08
Copy link
Owner

I'll make the changes myself. Merging

@acheong08 acheong08 merged commit 86d6123 into acheong08:main May 28, 2023
5 checks passed
@18870
Copy link

18870 commented May 29, 2023

did you try io.StringIO or something similar, i mean you probably don't need a regular file to do this, it's stupid to write into a file then read from a file in hard disk

@acheong08
Copy link
Owner

I'll look into this. There are streaming issues with file due to buffers

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants