feat: Update (see description)

```diff
+ fix: Discord Ban (invalid headers)
+ fix: acceptInvite not working (invalid captcha data)
+ feat: automod update
- feat: remove `nopecha`
- feat: remove Client#updateCookie & ClientOptions#autoCookie
```
This commit is contained in:
March 7th
2022-12-27 17:27:34 +07:00
parent e407700865
commit affcf5e166
12 changed files with 118 additions and 126 deletions

View File

@@ -48,26 +48,32 @@ class APIRequest {
'Accept-Language': 'en-US,en;q=0.9',
'Sec-Ch-Ua': `"Not?A_Brand";v="8", "Chromium";v="${chromeVersion}", "Google Chrome";v="${chromeVersion}"`,
'Sec-Ch-Ua-Mobile': '?0',
'Sec-Ch-Ua-Platform': '"Windows"',
'Sec-Ch-Ua-Platform': 'Windows',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'X-Debug-Options': 'bugReporterEnabled',
'X-Super-Properties': `${Buffer.from(
this.client.options.jsonTransformer(this.client.options.ws.properties),
this.client.options.jsonTransformer({
...this.client.options.ws.properties,
browser_user_agent: this.client.options.http.headers['User-Agent'],
}),
'ascii',
).toString('base64')}`,
'X-Discord-Locale': 'en-US',
'User-Agent': this.client.options.http.headers['User-Agent'],
Origin: 'https://discord.com',
Connection: 'keep-alive',
};
/* Remove
this.client.options.http.headers['User-Agent'] = this.fullUserAgent;
*/
if (this.options.auth !== false) headers.Authorization = this.rest.getAuth();
if (this.options.reason) headers['X-Audit-Log-Reason'] = encodeURIComponent(this.options.reason);
if (this.options.headers) headers = Object.assign(headers, this.options.headers);
if (this.options.webhook === true) {
headers = {
'User-Agent': this.client.options.http.headers['User-Agent'],
};
}
let body;
if (this.options.files?.length) {

View File

@@ -16,10 +16,17 @@ module.exports = class CaptchaSolver {
const lib = require('2captcha');
this.service = '2captcha';
this.solver = new lib.Solver(key);
this.solve = siteKey =>
this.solve = (data, userAgent) =>
new Promise((resolve, reject) => {
const siteKey = data.captcha_sitekey;
const postD = data.captcha_rqdata
? {
data: data.captcha_rqdata,
userAgent,
}
: undefined;
this.solver
.hcaptcha(siteKey, 'discord.com')
.hcaptcha(siteKey, 'https://discord.com/channels/@me', postD)
.then(res => {
resolve(res.data);
})
@@ -30,31 +37,6 @@ module.exports = class CaptchaSolver {
throw this._missingModule('2captcha');
}
}
case 'nopecha': {
if (!key || typeof key !== 'string') throw new Error('NopeCHA key is not provided');
try {
const { Configuration, NopeCHAApi } = require('nopecha');
const configuration = new Configuration({
apiKey: key,
});
this.service = 'nopecha';
this.solver = new NopeCHAApi(configuration);
this.solve = sitekey =>
new Promise((resolve, reject) => {
this.solver
.solveToken({
type: 'hcaptcha',
sitekey,
url: 'https://discord.com',
})
.then(res => (res ? resolve(res) : reject(new Error('Captcha could not be solved'))))
.catch(reject);
});
break;
} catch (e) {
throw this._missingModule('nopecha');
}
}
}
}
solve() {}

View File

@@ -11,6 +11,25 @@ const {
Events: { DEBUG, RATE_LIMIT, INVALID_REQUEST_WARNING, API_RESPONSE, API_REQUEST },
} = require('../util/Constants');
const cookieFilter = str => {
const blackList = ['expires', 'path', 'domain', 'httponly', 'secure', 'max-age', 'samesite'];
if (blackList.some(s => str.toLowerCase().includes(`${s}`))) return false;
return true;
};
function parseCookie(str, old) {
const oldProps = old.split(';').filter(cookieFilter);
const allProps = str.split(';').filter(cookieFilter);
// Update data from all to old
allProps.forEach(prop => {
const key = prop.split('=')[0];
const index = oldProps.findIndex(s => s.startsWith(key));
if (index !== -1) oldProps[index] = prop;
else oldProps.push(prop);
});
return oldProps.filter(s => s).join('; ');
}
const captchaMessage = [
'incorrect-captcha',
'response-already-used',
@@ -240,6 +259,20 @@ class RequestHandler {
let sublimitTimeout;
if (res.headers) {
// Cookie:
const cookie = res.headers.get('set-cookie');
if (cookie) {
if (typeof cookie == 'string') {
this.manager.client.options.http.headers.Cookie = parseCookie(
cookie,
this.manager.client.options.http.headers.Cookie || '',
);
this.manager.client.emit(
'debug',
`[REST] Set new cookie: ${this.manager.client.options.http.headers.Cookie}`,
);
}
}
const serverDate = res.headers.get('date');
const limit = res.headers.get('x-ratelimit-limit');
const remaining = res.headers.get('x-ratelimit-remaining');
@@ -368,7 +401,10 @@ class RequestHandler {
Route : ${request.route}
Info : ${inspect(data, { depth: null })}`,
);
const captcha = await this.manager.captchaService.solve(data.captcha_sitekey);
const captcha = await this.manager.captchaService.solve(
data,
this.manager.client.options.http.headers['User-Agent'],
);
this.manager.client.emit(
DEBUG,
`Captcha solved.