File size: 2,645 Bytes
709c473
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import contextlib
import json
import re
from pathlib import Path
import time

import api
import misc


def parse_curl_string(curl_string: str) -> dict[str, str]:
    headers = {}
    header_lines = curl_string.strip().splitlines()

    for header_line in header_lines:
        found = re.search(r"-H\s*'(.*?)\s*:\s*(.*)'", header_line)
        if not found:
            continue

        headers[found.group(1)] = found.group(2)

    return headers


def prompt_for_headers() -> dict[str, str]:
    while True:
        print(
            "Follow these directions to bypass CloudFlare anti-bot restrictions.\n"
            "  1. Go to https://neal.fun/infinite-craft/\n"
            "  2. Open the developer tools, and select the Network tab\n"
            "  3. Make any pair\n"
            "  4. In the Network tab, find the network request that was made (search 'pair')\n"
            "  5. Right click on it -> Copy -> Copy as cURL (bash)\n"
            "  6. Come back to the terminal, and press 'Enter'.",
        )
        curl_string = misc.header_bank_and_impersonate()
        if curl_string.startswith("curl 'https://neal.fun/api/infinite-craft/pair?first="):
            headers = parse_curl_string(curl_string)
            if headers:
                return headers

        print(
            "Hmm... it doesn't look like your clipboard contains the right data. Try again.\n\n",
        )


def verify_headers(headers: dict[str, str]) -> Exception | None:
    try:
        api.raw_make_pair("Fire", "Water", headers)
    except Exception as e:
        return e

    return None


def get_headers(verify: bool = True) -> dict[str, str]:
    filename = Path(__file__).parent / ".cloudflare-headers-cache.json"
    try:
        with filename.open() as f:
            headers = json.load(f)
    except Exception as e:
        pass
    else:
        if verify and verify_headers(headers) is None:
            return headers
        with contextlib.suppress(Exception):
            filename.unlink()

    headers = prompt_for_headers()
    if verify:
        error = verify_headers(headers)
        if error is not None:
            print(f"Those headers are not valid! Error: {error!r}")
            print("Try again.\n")
            time.sleep(90)
            return get_headers(verify)

    with contextlib.suppress(Exception), filename.open("w") as f:
        json.dump(headers, f, indent=2)

    return headers


if __name__ == "__main__":
    headers = get_headers()
    print("Your (working) headers:")
    print(json.dumps(headers, indent=2))