summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/npm/node_modules/funcaptcha/src/session.ts
blob: 9244daaef7f627f615e3463f2d8343daef06caa1 (plain) (blame)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { GetTokenResult } from "./api";
import { Challenge, Challenge1, Challenge3, Challenge4 } from "./challenge";
import http from "./http";
import util from "./util";

export interface TokenInfo {
    token: string;
    r: string;
    metabgclr: string;
    mainbgclr: string;
    guitextcolor: string;
    metaiconclr: string;
    meta_height: string;
    meta_width: string;
    meta: string;
    pk: string;
    dc: string;
    at: string;
    cdn_url: string;
    lurl: string;
    surl: string;
    smurl: string;
    // Enable keyboard biometrics
    kbio: boolean;
    // Enable mouse biometrics
    mbio: boolean;
    // Enable touch biometrics
    tbio: boolean;
}

export interface SessionOptions {
    userAgent?: string;
    proxy?: string;
}

let parseToken = (token: string): TokenInfo =>
    Object.fromEntries(
        token
            .split("|")
            .map((v) => v.split("=").map((v) => decodeURIComponent(v)))
    );

export class Session {
    public token: string;
    public tokenInfo: TokenInfo;
    private userAgent: string;
    private proxy: string;

    constructor(
        token: string | GetTokenResult,
        sessionOptions?: SessionOptions
    ) {
        if (typeof token === "string") {
            this.token = token;
        } else {
            this.token = token.token;
        }
        if (!this.token.startsWith("token="))
            this.token = "token=" + this.token;

        this.tokenInfo = parseToken(this.token);
        this.tokenInfo.mbio = typeof(token) !== "string" ? token.mbio ?? false : false
        this.userAgent = sessionOptions?.userAgent || util.DEFAULT_USER_AGENT;
        this.proxy = sessionOptions?.proxy;
    }

    async getChallenge(): Promise<Challenge> {
        let res = await http(
            this.tokenInfo.surl,
            {
                path: "/fc/gfct/",
                method: "POST",
                body: util.constructFormData({
                    sid: this.tokenInfo.r,
                    render_type: "canvas",
                    token: this.tokenInfo.token,
                    analytics_tier: this.tokenInfo.at,
                    "data%5Bstatus%5D": "init",
                    lang: "en",
                    apiBreakerVersion: "green"
                }),
                headers: {
                    "User-Agent": this.userAgent,
                    "Content-Type": "application/x-www-form-urlencoded",
                    "Accept-Language": "en-US,en;q=0.9",
                    "Sec-Fetch-Site": "same-origin",
                    "Referer": this.getEmbedUrl()
                },
            },
            this.proxy
        );

        let data = JSON.parse(res.body.toString());
        data.token = this.token;
        data.tokenInfo = this.tokenInfo;

        if (data.game_data.gameType == 1) {
            return new Challenge1(data, {
                proxy: this.proxy,
                userAgent: this.userAgent,
            });
        } else if (data.game_data.gameType == 3) {
            return new Challenge3(data, {
                proxy: this.proxy,
                userAgent: this.userAgent,
            });
        }  else if (data.game_data.gameType == 4) {
            return new Challenge4(data, {
                proxy: this.proxy,
                userAgent: this.userAgent,
            });
        } else {
            throw new Error(
                "Unsupported game type: " + data.game_data.gameType
            );
        }
        //return res.body.toString()
    }

    getEmbedUrl(): string {
        return `${this.tokenInfo.surl}/fc/gc/?${util.constructFormData(
            this.tokenInfo
        )}`;
    }
}