File size: 9,923 Bytes
b238c4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
const deviceID = require('uuid');
const adid = require('uuid');
const { TOTP } = require("totp-generator"); 
const axios = require("axios");
const logger = require("./b");

const ANDROID_DEVICES = [
    { model: "Pixel 6", build: "SP2A.220505.002" },
    { model: "Pixel 5", build: "RQ3A.210805.001.A1" },
    { model: "Samsung Galaxy S21", build: "G991USQU4AUDA" },
    { model: "OnePlus 9", build: "LE2115_11_C.48" },
    { model: "Xiaomi Mi 11", build: "RKQ1.200826.002" }
];

function getRandomDevice() {
    const device = ANDROID_DEVICES[Math.floor(Math.random() * ANDROID_DEVICES.length)];
    return {
        userAgent: `Dalvik/2.1.0 (Linux; U; Android 11; ${device.model} Build/${device.build})`,
        device
    };
}

async function getCookie({ username, password, twofactor = '0', _2fa, i_user }) {
    try {
        const androidDevice = getRandomDevice();
        const device_id = deviceID.v4();
        const family_device_id = deviceID.v4();
        const machine_id = randomString(24);
        const form = {
            adid: adid.v4(),
            email: username,
            password: password,
            format: 'json',
            device_id: device_id,
            cpl: 'true',
            family_device_id: family_device_id,
            locale: 'en_US',
            client_country_code: 'US',
            credentials_type: 'device_based_login_password',
            generate_session_cookies: '1',
            generate_analytics_claim: '1',
            generate_machine_id: '1',
            currently_logged_in_userid: '0',
            irisSeqID: 1,
            try_num: "1",
            enroll_misauth: "false",
            meta_inf_fbmeta: "NO_FILE",
            source: 'login',
            machine_id: machine_id,
            fb_api_req_friendly_name: 'authenticate',
            fb_api_caller_class: 'com.facebook.account.login.protocol.Fb4aAuthHandler',
            api_key: '882a8490361da98702bf97a021ddc14d',
            access_token: '350685531728|62f8ce9f74b12f84c123cc23437a4a32',
            advertiser_id: adid.v4(),
            device_platform: 'android',
            app_version: '392.0.0.0.66',
            network_type: 'WIFI'
        };

        form.sig = encodesig(sort(form));

        const options = {
            url: 'https://b-graph.facebook.com/auth/login',
            method: 'post',
            data: form,
            transformRequest: [(data) => require('querystring').stringify(data)],
            headers: {
                'content-type': 'application/x-www-form-urlencoded',
                'x-fb-friendly-name': form["fb_api_req_friendly_name"],
                'x-fb-http-engine': 'Liger',
                'user-agent': androidDevice.userAgent,
                'x-fb-connection-type': 'WIFI',
                'authorization': `OAuth ${form.access_token}`
            }
        };

        return new Promise((resolve) => {
            axios.request(options).then(async(response) => {
                try {
                    const appstate = response.data.session_cookies.map(cookie => ({
                        key: cookie.name,
                        value: cookie.value,
                        domain: cookie.domain,
                        path: cookie.path
                    }));

                    // ✅ Chỉ thêm i_user nếu được truyền vào
                    if (i_user) {
                        appstate.push({
                            key: 'i_user',
                            value: i_user,
                            domain: '.facebook.com',
                            path: '/'
                        });
                    }

                    const tokenOptions = {
                        url: `https://api.facebook.com/method/auth.getSessionforApp?format=json&access_token=${response.data.access_token}&new_app_id=275254692598279`,
                        method: 'get',
                        headers: {
                            'user-agent': androidDevice.userAgent,
                            'authorization': `OAuth ${response.data.access_token}`
                        }
                    };

                    const tokenV6D7Response = await axios.request(tokenOptions);

                    resolve({
                        appstate,
                        access_token: response.data.access_token,
                        access_token_eaad6v7: tokenV6D7Response.data.access_token,
                        device_info: {
                            model: androidDevice.device.model,
                            user_agent: androidDevice.userAgent
                        }
                    });
                } catch (e) {
                    logger(`Login error: ${e.message}`, '[ LOGIN ERROR ]');
                    resolve({ status: false, message: "Please enable 2FA and try again!" });
                }
            }).catch(async (error) => {
                try {
                    const data = error.response.data.error.error_data;
                    let twoFactorCode;
                    if (_2fa && _2fa !== "0") {
                        twoFactorCode = _2fa;
                    } else if (twofactor && twofactor !== "0") {
                        try {
                            logger('Đang xử lý 2FA', '[ AUTO LOGIN ]');
                            const cleanSecret = decodeURI(twofactor).replace(/\s+/g, '').toUpperCase(); 
                            const { otp } = TOTP.generate(cleanSecret);
                            twoFactorCode = otp;
                        } catch (e) {
                            resolve({ status: false, message: 'Invalid 2FA secret key' });
                            return;
                        }
                    } else {
                        resolve({ status: false, message: 'Please provide 2FA code or secret key' });
                        return;
                    }

                    const twoFactorForm = {
                        ...form,
                        twofactor_code: twoFactorCode,
                        encrypted_msisdn: "",
                        userid: data.uid,
                        machine_id: data.machine_id || machine_id,
                        first_factor: data.login_first_factor,
                        credentials_type: "two_factor"
                    };
                    twoFactorForm.sig = encodesig(sort(twoFactorForm));
                    options.data = twoFactorForm;

                    try {
                        const twoFactorResponse = await axios.request(options);
                        const appstate = twoFactorResponse.data.session_cookies.map(cookie => ({
                            key: cookie.name,
                            value: cookie.value,
                            domain: cookie.domain,
                            path: cookie.path
                        }));

                        if (i_user) {
                            appstate.push({
                                key: 'i_user',
                                value: i_user,
                                domain: '.facebook.com',
                                path: '/'
                            });
                        }

                        const tokenOptions = {
                            url: `https://api.facebook.com/method/auth.getSessionforApp?format=json&access_token=${twoFactorResponse.data.access_token}&new_app_id=275254692598279`,
                            method: 'get',
                            headers: {
                                'user-agent': androidDevice.userAgent,
                                'authorization': `OAuth ${twoFactorResponse.data.access_token}`
                            }
                        };

                        const tokenV6D7Response = await axios.request(tokenOptions);

                        resolve({
                            appstate,
                            access_token: twoFactorResponse.data.access_token,
                            access_token_eaad6v7: tokenV6D7Response.data.access_token,
                            device_info: {
                                model: androidDevice.device.model,
                                user_agent: androidDevice.userAgent
                            }
                        });
                    } catch (requestError) {
                        logger(`Lỗi request 2FA: ${requestError.message}`, '[ 2FA ERROR ]');
                        resolve({ status: false, message: 'Failed to authenticate with 2FA code. Please try again.' });
                    }
                } catch (twoFactorError) {
                    logger(`Lỗi xử lý 2FA: ${twoFactorError.message}`, '[ 2FA ERROR ]');
                    resolve({ status: false, message: 'Two-factor authentication failed. Check your credentials.' });
                }
            });
        });
    } catch (e) {
        logger(`Lỗi chung: ${e.message}`, '[ ERROR ]');
        return { status: false, message: 'Incorrect username or password.' };
    }
}

function randomString(length) {
    length = length || 10;
    let char = 'abcdefghijklmnopqrstuvwxyz';
    let result = char.charAt(Math.floor(Math.random() * char.length));
    for (let i = 0; i < length - 1; i++) {
        result += 'abcdefghijklmnopqrstuvwxyz0123456789'.charAt(Math.floor(36 * Math.random()));
    }
    return result;
}

function encodesig(string) {
    let data = '';
    Object.keys(string).forEach((key) => {
        data += `${key}=${string[key]}`;
    });
    return md5(data + '62f8ce9f74b12f84c123cc23437a4a32');
}

function md5(string) {
    return require('crypto').createHash('md5').update(string).digest('hex');
}

function sort(string) {
    const sortedKeys = Object.keys(string).sort();
    let sortedData = {};
    for (const key of sortedKeys) {
        sortedData[key] = string[key];
    }
    return sortedData;
}

module.exports = getCookie;