import base64
import ctypes
import hashlib
import hmac
import json
import math
import random
from datetime import datetime


def left_shift(based, counter):
    # a << 5
    val = based << (counter % 32)
    maxint = 2147483647
    if not -maxint - 1 <= val <= maxint:
        val = (val + (maxint + 1)) % (2 * (maxint + 1)) - maxint - 1
    return val


def unsigned_right_shift(n, i):
    """
    js中 >>>
    :param n:
    :param i:
    :return:
    """
    x, y = ctypes.c_uint32(n).value, i % 32
    return ctypes.c_uint32(x >> y).value


def Y0(Z0, h0):
    # a |= b
    return unsigned_right_shift(Z0, h0) | left_shift(Z0, (32 - h0))


def createPx1(t):
    t = '' + str(t)
    e = 0
    n = 0
    for r in range(len(t)):
        # print(n, ord(t[r]))
        n = left_shift(n, 5) - n + ord(t[r])
        n = Y0(n, 0)
        # print(r, n)

    e = n
    e |= 0
    if e < 0:
        e += 4294967296
    return hex(e)[2:]


def encrypt(data, param):
    n = bytearray()
    if isinstance(data, str):
        bytess = data.encode()
    else:
        bytess = data
    for i in bytess:
        n.append(i ^ param)
    return n


def randomInt(param, param1):
    return random.randint(param, param1)


def randomFloat(a, b):
    return random.random() * (b - a) + a


class PxEnv():
    def __init__(self, url, sid, vid, ts, num1, num2, str1, uuid, startTs, startTs2, performanceNow, px11280):
        self.ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36';

        # 初始化参数
        self.url = url
        self.sid = sid
        self.vid = vid
        self.ts = ts
        self.num1 = num1
        self.num2 = num2
        self.str1 = str1
        self.uuid = uuid
        self.startTs = startTs
        self.startTs2 = startTs2
        self.performanceNow = performanceNow
        self.px11280 = px11280
        totalJSHeapSize = randomInt(35903195, 39903195)
        self.memory_data = dict(
            totalJSHeapSize=totalJSHeapSize,
            usedJSHeapSize=math.floor(randomFloat(0.65, 0.9) * totalJSHeapSize),
            jsHeapSizeLimit=4294705152
        )

    @property
    def memory(self):
        self.memory_data['usedJSHeapSize'] = self.memory_data['usedJSHeapSize'] + randomInt(1903195, 2903195)
        return self.memory_data

    def _to_json(self, data):
        return json.dumps(data, separators=(',', ':'))

    def get_px11590(self):
        px11590_d = {"PX11431": int(self.ts),
                     "PX12454": int(self.num1),
                     "PX11701": self.num2[:80],
                     "PX11529": self.memory_data.get('usedJSHeapSize'),
                     "PX11555": self.memory_data.get('jsHeapSizeLimit'),
                     "PX11833": self.memory_data.get('totalJSHeapSize'),
                     "PX11840": datetime.now().strftime('%a %b %d %Y %H:%M:%S GMT+0800 (中国标准时间)'),
                     "PX12573": self.getPX12573(math.floor(int(self.ts) / 1e3), self.vid, self.uuid),
                     "PX11804": self.createPX2(self.ua, self.uuid),
                     "PX12118": self.str1,
                     "PX11746": self.createPX2(self.ua, self.vid),
                     "PX11371": self.createPX2(self.ua, self.sid),
                     "PX12501": self.md5(self.vid),
                     "PX12169": 1, "PX11902": 1,
                     "PX11560": self.performanceNow + randomInt(1000, 2500),
                     "PX11332": self.startTs + randomInt(1000, 2500),
                     "PX12248": 3600,
                     "PX11385": self.startTs,
                     "PX12280": self.px11280,
                     "PX11496": self.uuid,
                     "PX12330": "109|66|66|70|80",
                     "PX11705": random.choice([571, 1182]),
                     "PX11938": True, "PX11602": True, "PX12021": "False", "PX12421": "False", "PX12124": 1,
                     "PX11609": 1,
                     "PX12291": "", "PX11881": ["app", "csi", "loadTimes"], "PX12207": 0, "PX11538": 2,
                     "PX11984": "TypeError: Cannot read properties of None (reading '0')\n    at he (https://client.px-cloud.net/PXVb73hTEg/main.min.js:1136:13)\n    at Zo (https://client.px-cloud.net/PXVb73hTEg/main.min.js:3320:22)\n    at Eo (https://client.px-cloud.net/PXVb73hTEg/main.min.js:2892:23)\n    at https://client.px-cloud.net/PXVb73hTEg/main.min.js:3265:9",
                     "PX11645": "https://www.flyfrontier.com", "PX11597": [], "PX12023": "", "PX11337": False,
                     "PX12544": True, "PX12589": "succeeded", "PX11524": True,
                     "PX11843": 1920,
                     "PX11781": 1080,
                     "PX12121": 1920,
                     "PX12128": 1055,
                     "PX12387": "1920X1080",
                     "PX12003": 24,
                     "PX11380": 24,
                     "PX11494": 906,
                     "PX12411": 853, "PX12443": 0, "PX12447": 0, "PX11533": True, "PX12079": False, "PX12278": True,
                     "PX11694": False, "PX12294": False, "PX12514": True,
                     "PX12515": "TypeError: Cannot read properties of undefined (reading 'width')", "PX12516": "webkit",
                     "PX12517": 33, "PX12518": False, "PX12545": False, "PX12593": False,
                     "PX12595": "AudioData.SVGAnimatedAngle.SVGMetadataElement",
                     "PX12069": ["Chrome PDF Viewer", "Chromium PDF Viewer", "Microsoft Edge PDF Viewer", "PDF Viewer",
                                 "WebKit built-in PDF"], "PX12286": 5, "PX11576": True, "PX12318": True,
                     "PX11384": True,
                     "PX11886": True, "PX11583": "en-US", "PX12458": "MacIntel", "PX11681": ["en-US"],
                     "PX11754": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
                     "PX12037": True, "PX11390": -480, "PX11621": 8, "PX11657": 1, "PX12081": "Gecko",
                     "PX11908": "20030107",
                     "PX12314": "5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
                     "PX11829": True, "PX11464": True, "PX12054": 2, "PX11821": "Netscape", "PX11479": "Mozilla",
                     "PX11674": True, "PX12241": 400, "PX11372": False, "PX11683": 1.5, "PX11561": "3g",
                     "PX11877": True,
                     "PX12100": True, "PX12506": "arm", "PX12507": "64", "PX12509": False, "PX12510": "",
                     "PX12511": "macOS",
                     "PX12512": "13.5.0", "PX12513": "117.0.5938.149", "PX12548": True, "PX12578": {},
                     "PX12579": {"support": True,
                                 "status": {"effectiveType": "3g", "rtt": 400, "downlink": 1.5, "saveData": False}},
                     "PX12508": [{"brand": "Google Chrome", "version": "117"}, {"brand": "Not;A=Brand", "version": "8"},
                                 {"brand": "Chromium", "version": "117"}], "PX12549": True, "PX11539": "831efc5e",
                     "PX11528": "", "PX12271": "10207b2f", "PX11849": "10207b2f", "PX12464": "90e65465",
                     "PX11356": True,
                     "PX12426": True, "PX11791": True, "PX11517": True, "PX12520": True,
                     "PX12524": "4YC14YCd4YCd4YCV4YCe4YCX4YGS5J256aus7r266YaI5oCR7r27",
                     "PX12527": "3207084bd110f1ac964863e23aa78e04",
                     "PX12260": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
                     "PX12249": False, "PX11897": "90e65465", "PX12597": 2, "PX11526": False, "PX11684": False,
                     "PX11812": False, "PX12335": True, "PX12080": 0, "PX11678": False, "PX11349": "visible",
                     "PX12397": False, "PX11387": 0, "PX12150": 1920, "PX12304": True, "PX11651": 970,
                     "PX11867": "missing",
                     "PX12254": True, "PX11540": True, "PX11548": False, "PX11446": True, "PX12550": 1, "PX12431": 0,
                     "PX11991": 10, "PX11837": 34, "PX11632": 0, "PX11409": 9, "PX11508": "49e5084e",
                     "PX11452": "7c5f9724",
                     "PX12218": "65d826e0", "PX12481": "a9269e00", "PX11780": "50a5ec55", "PX12551": "https:",
                     "PX12553": "Asia/Shanghai", "PX12567": "w3c", "PX12576": "screen",
                     "PX12554": "function getOwnPropertyDescriptors() { [native code] }",
                     "PX12577": "function query() { [native code] }", "PX12594": False, "PX12566": False,
                     "PX12571": "74d9c66", "PX12581": "default", "PX11303": False, "PX11515": False, "PX12133": False,
                     "PX12340": False, "PX11738": False, "PX11723": False, "PX11389": False, "PX11839": False,
                     "PX11460": False, "PX12102": False, "PX11378": False, "PX12317": False, "PX12564": None,
                     "PX12565": -1,
                     }
        noPx1, noPx2 = self.getNoPx(px11590_d)
        px11590_d[noPx1] = noPx2
        data = {"t": "PX11590",
                "d": px11590_d}
        return data

    def getPX12573(self, a, b, c):
        t = a * 2866 / ord(b[2])
        return createPx1(t)

    def createPX2(self, e, t):
        h = hmac.new(e.encode(), t.encode(), hashlib.md5)
        t = h.hexdigest()
        return t

    def md5(self, vid):
        m = hashlib.md5()
        m.update(vid.encode())
        return m.hexdigest()

    def getNoPx(self, data):
        return encrypt(data['PX11701'], data['PX11431'] % 10 + 2).decode(), encrypt(data['PX11701'],
                                                                                    data['PX11431'] % 10 + 1).decode()

    def get_px12123(self):
        px12123_d = {
            "PX12108": randomInt(401, 601),
            "PX12414": randomInt(15, 61),
            "PX11984": "TypeError: Cannot read properties of null (reading '0')\n    at he (https://client.px-cloud.net/PXVb73hTEg/main.min.js:1136:13)\n    at Zo (https://client.px-cloud.net/PXVb73hTEg/main.min.js:3320:22)\n    at Eo (https://client.px-cloud.net/PXVb73hTEg/main.min.js:2892:23)\n    at https://client.px-cloud.net/PXVb73hTEg/main.min.js:3265:9",
            "PX12303": "mouseover",
            "PX11699": 58848,
            "PX11987": "True",
            "PX12461": True,
            "PX11652": "#wisepops-instance-429858",
            "PX11902": 3,
            "PX11560": self.performanceNow,
            "PX12280": self.px11280,
            "PX11496": self.uuid,
            "PX12564": None,
            "PX12565": -1,
            # "PX11379": False,
            # "PX11645": url
        }
        return {
            "t": "PX12123",
            "d": px12123_d
        }

    def get_px11891(self):
        px11891_d = {
            "PX11913": True,
            "PX11900": 240000,
            "PX12159": 1,
            "PX11754": self.ua,
            "PX12256": 0,
            "PX12445": 0,
            "PX11804": self.createPX2(self.ua, self.uuid),
            "PX11746": self.createPX2(self.ua, self.vid),
            "PX11371": self.createPX2(self.ua, self.sid),
            "PX11902": 4,
            "PX11560": self.performanceNow,
            "PX12280": self.px11280,
            "PX11496": self.uuid,
            "PX12564": None,
            "PX12565": -1,
            # // "PX11379": False
        }
        return {
            "t": "PX11891",
            "d": px11891_d
        }

    def get_px11547(self):
        px11547_d = {
            "PX12492": "c505c10e26a1b7a7741437db9f82916b",
            "PX12570": "78f763eab3a2082c63c66cf47f9e05a0",
            "PX11352": "e089aa3d700a35da7244dd4da7a68f9c",
            "PX12292": "WebKit",
            "PX11811": [],
            "PX11567": "WebKit WebGL",
            "PX12032": "WebGL 1.0 (OpenGL ES 2.0 Chromium)",
            "PX11536": [
                "ANGLE_instanced_arrays",
                "EXT_blend_minmax",
                "EXT_color_buffer_half_float",
                "EXT_disjoint_timer_query",
                "EXT_float_blend",
                "EXT_frag_depth",
                "EXT_shader_texture_lod",
                "EXT_texture_compression_rgtc",
                "EXT_texture_filter_anisotropic",
                "EXT_sRGB",
                "KHR_parallel_shader_compile",
                "OES_element_index_uint",
                "OES_fbo_render_mipmap",
                "OES_standard_derivatives",
                "OES_texture_float",
                "OES_texture_float_linear",
                "OES_texture_half_float",
                "OES_texture_half_float_linear",
                "OES_vertex_array_object",
                "WEBGL_color_buffer_float",
                "WEBGL_compressed_texture_s3tc",
                "WEBGL_compressed_texture_s3tc_srgb",
                "WEBGL_debug_renderer_info",
                "WEBGL_debug_shaders",
                "WEBGL_depth_texture",
                "WEBGL_draw_buffers",
                "WEBGL_lose_context",
                "WEBGL_multi_draw"
            ],
            "PX12149": [
                "[1, 1]",
                "[1, 64]",
                8,
                "yes",
                8,
                24,
                8,
                16,
                32,
                16384,
                1024,
                16384,
                16,
                16384,
                31,
                16,
                16,
                1024,
                "[16384, 16384]",
                "no_fp",
                23,
                127,
                127,
                23,
                127,
                127,
                23,
                127,
                127,
                23,
                127,
                127,
                23,
                127,
                127,
                23,
                127,
                127,
                23,
                127,
                127,
                23,
                127,
                127,
                23,
                127,
                127,
                23,
                127,
                127,
                23,
                127,
                127,
                23,
                127,
                127
            ],
            "PX12352": "Google Inc. (Apple)",
            "PX11455": "ANGLE (Apple, Apple M1 Max, OpenGL 4.1)",
            "PX11534": "WebGL GLSL ES 1.0 (OpenGL ES GLSL ES 1.0 Chromium)",
            "PX12503": "d9153fa5509ac1e988e36776dd8d598a",
            "PX12502": "32fe0262ba56767dfd83466855e47bec",
            "PX11927": "a02e9e2368744e62770d4a78862f75fa",
            "PX12572": "7ec700f02277463745cc71774c12c8aa",
            "PX11477": "126.8696939282454",
            "PX12109": "b908567e93d9ed24c036da7986349e97",
            "PX12362": "5a86e326549d99b7d916101a127153a1",
            "PX12491": "be9996f96141c689cda7a1e4e1c28787",
            "PX12130": [
                "Sha256",
                "_init",
                "_open",
                "_close",
                "F9Storage",
                "$",
                "F9Qubit",
                "F9Social",
                "html5",
                "$A",
                "_fbq",
                "_uxa",
                "__core-js_shared__",
                "_",
                "__f9publicCache",
                "ueto_9786ebd4fb",
                "Native2JSBridge",
                "_jelly_sdks",
                "wisepops429858JsonP",
                "_upliftFlag"
            ],
            "PX12351": [
                "jQuery3510402196117429823861",
                "_html5shiv",
                "AccDC1698805894620"
            ],
            "PX11386": [
                "webdriver"
            ],
            "PX12525": "b1199c2b7aa9aa43f16fc345099c3f04",
            "PX12526": "c9a6cb64a9c6b14c5c489fd18fc4c35d",
            "PX11948": 1,
            "PX11685": 10,
            "PX11986": True,
            "PX12299": True,
            "PX12331": True,
            "PX11316": False,
            "PX11448": True,
            "PX12196": "missing",
            "PX12427": [
                "_init",
                "_open",
                "_close",
                "$",
                "$A",
                "_pxCustomAbrDomains",
                "_pxAppId",
                "_Vb73hTEghandler",
                "_fbq",
                "_uxa",
                "__core-js_shared__",
                "_",
                "__f9publicCache"
            ],
            "PX11842": [
                "_html5shiv"
            ],
            "PX12439": [
                "PDF Viewer::Portable Document Format::application/pdf~pdf::text/pdf~pdf",
                "Chrome PDF Viewer::Portable Document Format::application/pdf~pdf::text/pdf~pdf",
                "Chromium PDF Viewer::Portable Document Format::application/pdf~pdf::text/pdf~pdf",
                "Microsoft Edge PDF Viewer::Portable Document Format::application/pdf~pdf::text/pdf~pdf",
                "WebKit built-in PDF::Portable Document Format::application/pdf~pdf::text/pdf~pdf"
            ],
            "PX11993": self.ts,
            "PX12228": "TypeError: Cannot read properties of null (reading '0') at de (https://client.px-cloud.net/PXVb73hTEg/main.min.js:796:13) at func (https://client.px-cloud.net/PXVb73hTEg/main.min.js:4899:19) at ve (https://client.px-cloud.net/PXVb73hTEg/main.min.js:790:13) at https://client.px-cloud.net/PXVb73hTEg/main.min.js:5048:13",
            "PX12288": True,
            "PX12446": 33,
            "PX12236": "fd7149bbfb316699ef918fa7bb7510a8",
            "PX11309": "d41d8cd98f00b204e9800998ecf8427e",
            "PX11551": "fd7149bbfb316699ef918fa7bb7510a8",
            "PX12586": 1,
            "PX11843": 1920,
            "PX11781": 1080,
            "PX12121": 1920,
            "PX12128": 1055,
            "PX12387": "1920X1080",
            "PX12003": 24,
            "PX11380": 24,
            "PX11849": "10207b2f",
            "PX11583": "en-US",
            "PX12458": "MacIntel",
            "PX11681": [
                "en-US"
            ],
            "PX11754": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
            "PX12037": True,
            "PX11621": 8,
            "PX11390": -480,
            "PX11678": False,
            "PX11840": datetime.now().strftime('%a %b %d %Y %H:%M:%S GMT+0800 (中国标准时间)'),
            "PX11540": True,
            "PX11539": "64556c77",
            "PX11555": 4294705152,
            "PX11452": "7c5f9724",
            "PX12527": "3207084bd110f1ac964863e23aa78e04",
            "PX11484": [],
            "PX12501": self.md5(self.vid),
            "PX11902": 2,
            "PX11560": self.performanceNow,
            "PX12280": self.px11280,
            "PX11496": self.uuid,
            "PX12564": None,
            "PX12565": -1
        }
        return {
            "t": "PX11547",
            "d": px11547_d
        }


if __name__ == '__main__':
    a2 = "B1kHB1kHFF4JX1kODVAKRVBQXF1FWVkNDUUKXVpfRQteDVFRDFhcXF8MWBRbWV1bXlhYWBQcGh0NFhYWFllZWQcHWRQLCxReWBQ9Wi4cMj4mGAwvPVE8Ly5cJx9VVRYWFhYHWVlZBwcHWRRZX1hYXV1cWlpZXVpdFhYWFllZB1lZWRQMWw5bX19fCQldXlALWw1eUFoJX1kOX1tbUQ4JXwwJXgpYX1wMXApQDVwLXQsKDFtQDVpfXl4LCllRXAtQWlpcFhYWFgdZWQdZWQcHFF4JX1pYCloORVBQXF1FWVkNDUUKXVpQRQ5fCl8ODA5ZDFwOWxQcGh0NFhYWFlkHBwcHWRQLBA1eChpRXA9eBgFYWVoDXAAAWBYWFhYHWVkHWVkUXglfWlhQXFBFUFBcXUVZWQ0NRQpdWlBFDl8KXw4MDlkMXA5bFhYWFgdZB1lZWRQLHRYWFhZZWVkHWQcUWVBYXF9QWlhfW11bXF1dWFhbWlwWFhYWB1lZWQcHBwcUWV1ZWw=="

    b2 = base64.b64decode(a2.encode())
    for i in bytes(encrypt(b2, 872 % 128)).decode().split('~~~~'):
        print('o11o111o' in i, i)
        tmp = i.split('|')
        if tmp[0] == 'o111ooo1':
            ts = tmp[1]
        if tmp[0] == 'o111oooo':
            num1 = int(tmp[1])
        if tmp[0] == '111o1o':
            num2 = tmp[1]

        if tmp[0] == '1oooo1':
            str1 = tmp[1]
        if tmp[0] == 'o1oo1o':
            vid = tmp[1]

        if tmp[0] == 'o11o11':
            sid = tmp[1]
        if tmp[0] == '11o111':
            cs = tmp[1]
        if tmp[0] == 'o11o11oo':
            cts = tmp[1]
    uuid = '31fafcb0-8844-11ee-8d49-97ed9bc8f96f'
    px = PxEnv('https://www.flyfrontier.com/',
               sid, vid, ts, num1, num2, str1, uuid

               , 1700552863875, 1700464712550
               , 1700552863903, 1700552863903)
    print(px.get_px11590())
