Skip to content

Commit

Permalink
fix: color, functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamyplayer committed Sep 28, 2022
1 parent 2aeacc4 commit c98a291
Showing 1 changed file with 42 additions and 46 deletions.
88 changes: 42 additions & 46 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/* eslint-disable no-control-regex */
/* eslint-disable no-sequences */

const { sync } = require('mkdirp');
const ms = require('ms');
const ProxyAgent = require('proxy-agent');
const { yellow, green, bgBlue, red, bgRed, bgGreen, cyan, blue } = require('colorette');
const { performance } = require('node:perf_hooks');
const fetch = require('cross-fetch');
const {
existsSync,
Expand All @@ -11,9 +16,10 @@ const {
unlinkSync,
createWriteStream,
} = require('node:fs');
const { sync } = require('mkdirp');
const { Welcome, ProxyAgent, performance, chalk, LogoMain } = require('../utils/modules');
const { wait, duration, datetocompact, numberFormat } = require('../utils/functions');
const { setTimeout } = require('node:timers');

const { Welcome, LogoMain } = require('../utils/modules');
const { wait, compactMode } = require('../utils/functions');
const { interval, autoGrabProxies, updateRate, proxy, proxiesType, proxiesfile, debug, codesfile, bURL, params } =
require('../config').checker;
const { length, random } = require('../config').generator;
Expand Down Expand Up @@ -83,7 +89,7 @@ function log(e) {
}

function dbug(o) {
debug && log(`${chalk.yellow('[DBUG]')} ${o}`);
debug && log(`${yellow('[DBUG]')} ${o}`);
}

function formatCode(o) {
Expand All @@ -94,30 +100,27 @@ async function actualizeCodes() {
let e = codes
.filter(e => !e.c || e.c === 'ongoing')
.map(e => (Date.now() - e.t > 3e4 && ((e.c = !1), (e.t = 1 / 0)), e));
dbug(`${chalk.yellow(codes.length - e.length)} codes purged.`);
dbug(`${yellow(codes.length - e.length)} codes purged.`);
const o = codesMaxSize - e.length;
if (o > 0) {
const c = await gen(length, random, o);
e.push(...c.codes.map(e => formatCode(e))), log(`${chalk.bgBlue('[ADDED]')} ${chalk.yellow(o)} more codes.`);
e.push(...c.codes.map(e => formatCode(e))), log(`${bgBlue('[ADDED]')} ${yellow(o)} more codes.`);
}
return e;
}

proxy &&
setInterval(() => {
const e = numberFormat(proxies.filter(e => e.working && e.readyAt <= Date.now()).length);
const n = numberFormat(proxies.filter(e => e.working).length);
const r = numberFormat(proxies.filter(e => !e.working).length);
const e = compactMode({
date: proxies.filter(e => e.working && e.readyAt <= Date.now()).length,
formatType: 'Number',
});
const n = compactMode({ date: proxies.filter(e => e.working).length, formatType: 'Number' });
const r = compactMode({ date: proxies.filter(e => !e.working).length, formatType: 'Number' });
r <= 0
? log(
`\n\n${chalk.bgGreen('Proxies')} \nUP: ${chalk.green(e)}\nALIVE: ${chalk.yellow(n)}\nDEAD: ${chalk.red(
r,
)}\n\n`,
)
? log(`\n\n${bgGreen('Proxies')} \nUP: ${green(e)}\nALIVE: ${yellow(n)}\nDEAD: ${red(r)}\n\n`)
: log(
`\n\n${chalk.bgGreen('Proxies')} \nUP: ${chalk.green(e)}\nALIVE: ${chalk.yellow(n)}\nDEAD: ${chalk.red(
r,
)} ${chalk.bgRed('[purging]')}\n\n`,
`\n\n${bgGreen('Proxies')} \nUP: ${green(e)}\nALIVE: ${yellow(n)}\nDEAD: ${red(r)} ${bgRed('[purging]')}\n\n`,
),
(pauseLog = 2500),
(proxies = proxies.filter(e => e.working));
Expand Down Expand Up @@ -151,7 +154,7 @@ class Proxy {
};

this.used(1);
this.debug(chalk.yellow(`Checking ${code}`));
this.debug(yellow(`Checking ${code}`));

try {
const body = await (
Expand All @@ -164,36 +167,36 @@ class Proxy {
).json();

if (body?.redeemed === false && new Date(body?.expires_at) > Date.now()) {
log(`${chalk.bgGreen('[HIT]')} ${chalk.cyan(`Check succeed, code: ${code}`)}.`);
log(`${bgGreen('[HIT]')} ${cyan(`Check succeed, code: ${code}`)}.`);
return {
c: true,
v: true,
};
} else {
if (body.message === 'Unknown Gift Code') {
this.debug(chalk.red(`Check failed (404)`));
this.debug(red(`Check failed (404)`));
return {
c: true,
v: false,
};
} else if (body.message === 'You are being rate limited.') {
let int = body.retry_after * 1000;
this.debug(chalk.red(`Check missed (429), waiting ${numberFormat(int)}ms.`));
this.debug(red(`Check missed (429), waiting ${compactMode({ date: int, formatType: 'Number' })}ms.`));
this.used(5, int);
return {
c: false,
v: null,
};
} else {
this.debug(chalk.bgRedBright(`Unknow message (${body})`));
this.debug(bgRed(`Unknow message (${body})`));
return {
c: null,
v: null,
};
}
}
} catch (e) {
this.debug(chalk.red(`Fetch missed (${e})`));
this.debug(red(`Fetch missed (${e})`));
if (!e.toString().toLowerCase().replace(/ +/g, '').includes('timedout')) this.working = false;
return {
c: false,
Expand All @@ -203,7 +206,7 @@ class Proxy {
}

debug(str) {
if (this.working) dbug(`${chalk.yellow(`{${this.id}}`)} ${str}`);
if (this.working) dbug(`${yellow(`{${this.id}}`)} ${str}`);
}

used(n = 1, time = 60000) {
Expand Down Expand Up @@ -237,7 +240,7 @@ proxy &&

async function grabProxies() {
if (Date.now() - lastGrab < 10000) {
dbug(chalk.red(`Codes actualization in cooldown, waiting ${duration(Date.now() - lastGrab, true, true)}.`));
dbug(red(`Codes actualization in cooldown, waiting ${ms(Date.now() - lastGrab)}.`));
await wait(1000);
return codes;
}
Expand All @@ -249,7 +252,7 @@ async function grabProxies() {
).then(async res => {
const body = await res.text();
const lines = body.split('\n').filter(line => !proxies.find(p => p.proxy === line));
log(`${chalk.bgBlue('[AUTO]')} grabbed ${chalk.yellow(lines.length)} proxies.`);
log(`${bgBlue('[AUTO]')} grabbed ${yellow(lines.length)} proxies.`);
for (let line of lines) {
proxies.push(new Proxy(line, (proxies[proxies.length - 1]?.id || 0) + 1));
}
Expand All @@ -261,9 +264,9 @@ async function main() {
const e = () =>
proxy ? ((max - c) / (5 * proxies.filter(e => e.working).length)) * 6e4 : ((max - c).length / 5) * 6e4;
console.info(
`${chalk.bgGreen('[Launching]')} ${chalk.yellow(numberFormat(max))} ${chalk.green('checks')}, ${chalk.red(
`${bgGreen('[Launching]')} ${yellow(compactMode({ date: max, formatType: 'Number' }))} ${green('checks')}, ${red(
'estimated time:',
)} ${chalk.blue(duration(e(), !0, !0))} | ${chalk.blue(datetocompact(e() + Date.now()))}`,
)} ${blue(ms(e()))} | ${blue(compactMode({ date: e() + Date.now(), formatType: 'Date' }))}`,
);
let a = 0;
let t = c;
Expand Down Expand Up @@ -296,11 +299,11 @@ async function main() {
}
t !== c &&
(log(
`${chalk.bgGreen('[ETL]')} ${chalk.green(`${duration(e(), !1, !0)}`)} Analyzed ${chalk.green(
`${numberFormat(c)}`,
)}/${chalk.yellow(`${numberFormat(max)}`)} [${chalk.green(valids.length)}] - continuing ${chalk.cyan(
numberFormat(max - c),
)} codes`,
`${bgGreen('[ETL]')} ${green(`${ms(e())}`)} Analyzed ${green(
`${compactMode({ date: c, formatType: 'Number' })}`,
)}/${yellow(`${compactMode({ date: max, formatType: 'Number' })}`)} [${green(
valids.length,
)}] - continuing ${cyan(compactMode({ date: max - c, formatType: 'Number' }))} codes`,
),
(t = c)),
await wait(pause ? pauseMs : interval);
Expand All @@ -321,24 +324,19 @@ async function tryCode() {
if (proxy && !o) {
let r = proxies.sort((r, e) => r.readyAt - e.readyAt).filter(r => r.working)[0];
return (
dbug(chalk.red(`No more proxy available | Next ready in ${duration(r.readyAt - Date.now(), !0, !0)}.`)),
grabProxies(),
r.readyAt
dbug(red(`No more proxy available | Next ready in ${ms(r.readyAt - Date.now())}.`)), grabProxies(), r.readyAt
);
}
if (!proxy && !localProxy.ready)
return (
dbug(chalk.red(`Rate-limited | Ready in ${duration(localProxy.readyAt - Date.now(), !0, !0)}.`)),
localProxy.readyAt
);
return dbug(red(`Rate-limited | Ready in ${ms(localProxy.readyAt - Date.now())}.`)), localProxy.readyAt;
const i = proxy ? await o.check(a, t) : await localProxy.check(a, t);
return (r.c = i.c), (r.t = 1 / 0), i.c && c++, i.v && valids.push(t), !0;
}

function end(e) {
(pauseMs = 6e4), (pause = !0), (pauseLog = 6e4);
let i = '';
let n = codesfile.match(/.*(\/|\\)/g)[0] + 'valids.txt';
let n = codesfile.match(/.*(\/|\\)/g)[0] + 'valid_codes.txt';
sync(n.match(/.*(\/|\\)/g)[0]), existsSync(n) && ((i = readFileSync(n, { encoding: 'utf-8' })), unlinkSync(n));
let s = createWriteStream(n, { encoding: 'utf-8' });
s.write(i + valids.join('\n')),
Expand All @@ -358,11 +356,9 @@ function end(e) {
s.close(),
wait(2e3).then(() => {
console.info(
`\n\n${chalk.bgBlue('[END]')} Coffee Break ${chalk.blue(
duration(e - start, !0, !0),
)} 🍵 - Checked ${chalk.green(`${numberFormat(c)}`)} ⚡ - Valid ${chalk.green(
numberFormat(valids.length),
)} ✔️ `,
`\n\n${bgBlue('[END]')} Coffee Break ${blue(ms(e - start))} 🍵 - Checked ${green(
`${compactMode({ date: c, formatType: 'Number' })}`,
)} ⚡ - Valid ${green(compactMode({ date: valids.length, formatType: 'Number' }))} ✔️ `,
);
}),
wait(4e3).then(() => process.exit());
Expand Down

0 comments on commit c98a291

Please sign in to comment.