File size: 9,136 Bytes
fd211b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**

 * protocol.js - ChatAIBot.pro ็บฏ HTTP ๅ่ฎฎๆณจๅ†Œๆจกๅ—

 *

 * ๅทฒ็กฎ่ฎค็š„ API ็ซฏ็‚น (Express + Apache/Ubuntu):

 *   POST /api/register         โ†’ 201 {"success":true}  (ๆณจๅ†Œ, ่ฟ”ๅ›ž connect.sid session cookie)

 *   POST /api/register/verify  โ†’ 200 (้ชŒ่ฏ, ้œ€่ฆ {email, token})

 *   POST /api/login            โ†’ 200 (็™ปๅฝ•, ้œ€่ฆ {email, password})

 *   POST /api/logout           โ†’ 200 OK

 *

 * ๆณจๅ†Œๆต็จ‹ (้€†ๅ‘่‡ชๅ‰็ซฏ sign-up chunk + module 40933):

 *   1. POST /api/register  { email, password, isAdvertisingAccepted, mainSiteUrl,

 *                             utmSource, utmCampaign, connectBusiness, yandexClientId }

 *      Headers: Content-Type: application/json, Accept-Language: en

 *      โ†’ 201 {"success":true} + Set-Cookie: connect.sid

 *   2. ้‚ฎไปถไธญๅŒ…ๅซ 6 ไฝๆ•ฐๅญ—้ชŒ่ฏ็  (token)

 *      ๆ ผๅผ: "Your code: 528135"

 *   3. POST /api/register/verify  { email, token, connectBusiness, syncToken }

 *      โ†’ ้ชŒ่ฏๅฎŒๆˆ๏ผŒ่ดฆๅทๆฟ€ๆดป

 *   4. POST /api/login  { email, password }

 *      โ†’ ่Žทๅ– session

 */

import { request, get, post, sleep } from './http.js';
import config from './config.js';

const API_BASE = config.siteBase;

// ==================== ๅทฅๅ…ทๅ‡ฝๆ•ฐ ====================

function humanDelay(baseMs) {
  return Math.floor(baseMs * (0.8 + Math.random() * 1.7));
}

// ==================== ๆณจๅ†Œๆญฅ้ชค ====================

/**

 * Step 1: ๆณจๅ†Œ่ดฆๅท

 * POST /api/register

 * ้€†ๅ‘่‡ชๅ‰็ซฏ sign-up-d2a668c82094de73.js + module 40933

 * ๆต่งˆๅ™จๅ‘้€็š„ๅฎŒๆ•ดๅญ—ๆฎตๅ’Œ headers

 */
async function stepRegister(account) {
  console.log('  [Step 1] ๆไบคๆณจๅ†Œ...');

  const resp = await post(`${API_BASE}/api/register`, {
    email: account.email,
    password: account.password,
    isAdvertisingAccepted: false,
    mainSiteUrl: `${config.siteBase}/api`,
    utmSource: '',
    utmCampaign: '',
    connectBusiness: '',
    yandexClientId: '',
  }, {
    headers: {
      'Origin': config.siteBase,
      'Referer': config.signupUrl,
      'Accept-Language': 'en',
    },
  });

  const body = resp.text();
  console.log(`    ็Šถๆ€: ${resp.status}`);
  console.log(`    ๅ“ๅบ”: ${body.substring(0, 200)}`);

  if (resp.status === 201 || resp.ok) {
    const sessionCookie = resp.cookies.get('connect.sid');
    console.log(`    Session: ${sessionCookie ? sessionCookie.substring(0, 50) + '...' : 'ๆ— '}`);
    return { success: true, cookies: resp.cookies };
  }

  let errorMsg = body;
  try {
    const json = JSON.parse(body);
    errorMsg = json.message || json.error || body;
  } catch {}

  throw new Error(`ๆณจๅ†Œๅคฑ่ดฅ (${resp.status}): ${errorMsg}`);
}

/**

 * Step 2: ไปŽ้‚ฎ็ฎฑ่Žทๅ– 6 ไฝ้ชŒ่ฏ็ 

 * chataibot.pro ็š„้ชŒ่ฏ็ ๅฐฑๆ˜ฏ token๏ผŒๆ˜ฏไธ€ไธช 6 ไฝๆ•ฐๅญ—

 */
async function stepGetVerifyToken(mailProvider, senderFilter) {
  console.log('  [Step 2] ็ญ‰ๅพ…้ชŒ่ฏ้‚ฎไปถ...');

  const pollOptions = {
    initialDelay: 8000,
    maxAttempts: 15,
    pollInterval: 5000,
  };

  const code = await mailProvider.fetchVerificationCode(senderFilter, pollOptions);
  if (code) {
    console.log(`    ้ชŒ่ฏ็ : ${code}`);
    return code;
  }

  return null;
}

/**

 * Step 3: ๆไบค้ชŒ่ฏ็ 

 * POST /api/register/verify  { email, token, connectBusiness, syncToken }

 * ้€†ๅ‘่‡ชๅ‰็ซฏ: verify ่ฟ˜ๅ‘้€ connectBusiness ๅ’Œ syncToken

 */
async function stepVerify(email, token, cookies) {
  console.log('  [Step 3] ๆไบค้ชŒ่ฏ...');

  const resp = await post(`${API_BASE}/api/register/verify`, {
    email,
    token,
    connectBusiness: '',
    syncToken: '',
  }, {
    cookies,
    headers: {
      'Origin': config.siteBase,
      'Referer': `${config.siteBase}/app/verify`,
      'Accept-Language': 'en',
    },
  });

  const body = resp.text();
  console.log(`    ็Šถๆ€: ${resp.status}`);
  console.log(`    ๅ“ๅบ”: ${body.substring(0, 300)}`);

  if (resp.ok) {
    let data;
    try { data = JSON.parse(body); } catch { data = { raw: body }; }
    return { success: true, data, cookies: resp.cookies };
  }

  throw new Error(`้ชŒ่ฏๅคฑ่ดฅ (${resp.status}): ${body.substring(0, 200)}`);
}

/**

 * Step 4: ็™ปๅฝ•่Žทๅ– session

 * POST /api/login

 */
async function stepLogin(email, password) {
  console.log('  [Step 4] ็™ปๅฝ•...');

  await sleep(humanDelay(1000));

  const resp = await post(`${API_BASE}/api/login`, {
    email,
    password,
  }, {
    headers: {
      'Origin': config.siteBase,
      'Referer': `${config.siteBase}/app/auth/sign-in`,
      'Accept-Language': 'en',
    },
  });

  const body = resp.text();
  console.log(`    ็Šถๆ€: ${resp.status}`);
  console.log(`    ๅ“ๅบ”: ${body.substring(0, 300)}`);

  if (resp.ok) {
    let data;
    try { data = JSON.parse(body); } catch { data = { raw: body }; }
    return {
      success: true,
      data,
      cookies: resp.cookies,
      sessionCookie: resp.cookies.get('connect.sid'),
    };
  }

  console.log('    ็™ปๅฝ•ๅคฑ่ดฅ๏ผˆๅฏ่ƒฝ้œ€่ฆๅ…ˆ้ชŒ่ฏ้‚ฎ็ฎฑ๏ผ‰');
  return { success: false, status: resp.status, body };
}

// ==================== ไธปๆณจๅ†Œๆต็จ‹ ====================

/**

 * ็บฏๅ่ฎฎๆณจๅ†Œ ChatAIBot.pro ่ดฆๅท

 *

 * @param {object} account - { email, password, firstName, lastName, fullName }

 * @param {object} mailProvider - ้‚ฎ็ฎฑ provider ๅฎžไพ‹

 * @returns {object} { success, account, session, error }

 */
export async function register(account, mailProvider) {
  console.log(`\n  โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•`);
  console.log(`  ๆณจๅ†Œ: ${account.email}`);
  console.log(`  โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•\n`);

  try {
    // Step 1: ๆณจๅ†Œ
    await sleep(humanDelay(500));
    const regResult = await stepRegister(account);

    // Step 2: ่Žทๅ– 6 ไฝ้ชŒ่ฏ็ 
    const senderFilter = config.senderFilter || 'chataibot';
    const token = await stepGetVerifyToken(mailProvider, senderFilter);
    if (!token) {
      console.log('\n  [่ญฆๅ‘Š] ๆœช่Žทๅ–ๅˆฐ้ชŒ่ฏ็ ๏ผŒ่ดฆๅทๅทฒๅˆ›ๅปบไฝ†ๆœช้ชŒ่ฏ');
      console.log('         ่ฏทๆ‰‹ๅŠจๆฃ€ๆŸฅ้‚ฎ็ฎฑๅฎŒๆˆ้ชŒ่ฏ');
      return {
        success: true,
        verified: false,
        account,
        session: { cookies: regResult.cookies },
        cookies: regResult.cookies,
      };
    }

    // Step 3: ๆไบค้ชŒ่ฏ
    await sleep(humanDelay(1000));
    const verifyResult = await stepVerify(account.email, token, regResult.cookies);

    // Step 4: ็™ปๅฝ•
    await sleep(humanDelay(1500));
    const loginResult = await stepLogin(account.email, account.password);

    return {
      success: true,
      verified: true,
      account,
      session: loginResult.data || verifyResult.data,
      cookies: loginResult.cookies || verifyResult.cookies || regResult.cookies,
      loginSuccess: loginResult.success,
    };
  } catch (e) {
    console.log(`\n  [้”™่ฏฏ] ${e.message}`);
    return {
      success: false,
      account,
      error: e.message,
    };
  }
}

/**

 * ๆŽขๆต‹ API (ไฟ็•™ไพ›่ฐƒ่ฏ•็”จ)

 */
export async function probe() {
  console.log('\n  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”');
  console.log('  โ”‚       ChatAIBot.pro API ๆŽขๆต‹                     โ”‚');
  console.log('  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜\n');

  const endpoints = [
    ['GET', '/api/register', null],
    ['POST', '/api/register', { email: 'probe@test.com', password: 'Probe123!' }],
    ['POST', '/api/register/verify', { email: 'probe@test.com', token: 'test' }],
    ['POST', '/api/login', { email: 'probe@test.com', password: 'Probe123!' }],
    ['POST', '/api/logout', {}],
  ];

  const results = [];

  for (const [method, path, body] of endpoints) {
    try {
      const opts = {
        headers: {
          'Origin': config.siteBase,
          'Referer': config.signupUrl,
        },
        followRedirect: false,
        timeout: 10000,
      };

      let resp;
      if (method === 'POST') {
        resp = await post(`${API_BASE}${path}`, body, opts);
      } else {
        resp = await get(`${API_BASE}${path}`, opts);
      }

      const text = resp.text();
      const status = resp.status;

      console.log(`  ${status} ${method} ${path}`);
      console.log(`    ${text.substring(0, 200)}`);
      console.log(`    Cookies: ${[...resp.cookies.keys()].join(', ') || 'ๆ— '}`);

      results.push({ method, path, status, body: text.substring(0, 200) });
    } catch (e) {
      results.push({ method, path, status: 0, error: e.message });
    }
  }

  return results;
}