From 73dbf48c444afbe288cea756b7b5f0a57e755e61 Mon Sep 17 00:00:00 2001 From: nin0 Date: Sat, 3 May 2025 08:38:12 -0400 Subject: [PATCH] more inits --- .gitignore | 3 ++- package.json | 3 ++- src/grokCookies.ts | 44 +++++++++++++++++++++++++++++++++ src/index.ts | 25 +++++++++++++++++++ src/utils/Yapper.ts | 60 +++++++++++++++++++++++++++++++++++++++++++++ src/utils/state.ts | 27 ++++++++++++++++++++ src/utils/types.ts | 10 ++++++++ 7 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 src/grokCookies.ts create mode 100644 src/utils/Yapper.ts create mode 100644 src/utils/state.ts create mode 100644 src/utils/types.ts diff --git a/.gitignore b/.gitignore index c949905..eb777b6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .env dist/ -node_modules/ \ No newline at end of file +node_modules/ +state.json \ No newline at end of file diff --git a/package.json b/package.json index 20fe073..89ff0df 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "dist/index.js", "scripts": { "dev": "tsx --env-file=.env --watch src/index.ts", - "build": "node build.mjs" + "build": "node build.mjs", + "start": "node build.mjs && node --env-file=.env dist/index.js" }, "keywords": [], "author": "", diff --git a/src/grokCookies.ts b/src/grokCookies.ts new file mode 100644 index 0000000..23324ae --- /dev/null +++ b/src/grokCookies.ts @@ -0,0 +1,44 @@ +import { state } from "./utils/state"; + +export async function getGrokAuthedCookies() { + if (!process.env.FLARESOLVERR_BASE_URL) + throw new Error("No flaresolverr found"); + + const req = await fetch( + `${process.env.FLARESOLVERR_BASE_URL!.replace(/\/(v1)?$/, "")}/v1`, + { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ + cmd: "request.get", + url: "https://grok.com/", + maxTimeout: 60000 + }) + } + ); + + if (!req.ok) throw new Error(await req.text()); + const data = await req.json(); + if (data.status !== "ok") { + console.error(data); + throw new Error(data); + } + + const { + solution: { userAgent, cookies } + } = data; + + state.grokMeta = { + userAgent, + cookies: (cookies as any[]).reduce((acc, { name, value }) => { + acc[name] = value; + return acc; + }, {}), + expiresIn: { + time: Date.now() + 60 * 60 * 1000, + requests: 3 + } + }; +} diff --git a/src/index.ts b/src/index.ts index e69de29..a38fce7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -0,0 +1,25 @@ +import Fastify from "fastify"; +import { Yapper } from "./utils/Yapper"; +import { getGrokAuthedCookies } from "./grokCookies"; + +const fastify = Fastify({ + loggerInstance: new Yapper(), + disableRequestLogging: true +}); + +fastify.get("/", async (request, reply) => { + reply.type("text/html"); + return "Hiiiiiii"; +}); + +try { + getGrokAuthedCookies(); + + fastify.listen({ + port: 4935, + host: "0.0.0.0" + }); +} catch (err) { + fastify.log.error(err); + process.exit(1); +} diff --git a/src/utils/Yapper.ts b/src/utils/Yapper.ts new file mode 100644 index 0000000..a10c3df --- /dev/null +++ b/src/utils/Yapper.ts @@ -0,0 +1,60 @@ +function getDateTime() { + const now = new Date(); + const hours = String(now.getHours()).padStart(2, "0"); + const minutes = String(now.getMinutes()).padStart(2, "0"); + const seconds = String(now.getSeconds()).padStart(2, "0"); + const day = String(now.getDate()).padStart(2, "0"); + const month = String(now.getMonth() + 1).padStart(2, "0"); + const year = now.getFullYear(); + + return `${hours}:${minutes}:${seconds} ${day}-${month}-${year}`; +} + +export class Yapper { + constructor() {} + trace(obj) { + console.log("\x1b[90;1m TRC\x1b[90;1m \x1b[0;90m", getDateTime(), obj); + } + debug(obj) { + console.log( + "\x1b[90;1;47m DBG", + `\x1b[0m \x1b[90m${getDateTime()}`, + obj, + "\x1b[0m" + ); + } + info(obj) { + console.log( + "\x1b[44;1m INF", + `\x1b[0m \x1b[90m${getDateTime()}\x1b[0m`, + obj + ); + } + warn(obj) { + console.log( + "\x1b[43;1m WRN", + `\x1b[0m \x1b[90m${getDateTime()}\x1b[0m`, + obj + ); + } + error(obj) { + console.log( + "\x1b[41;1m ERR", + `\x1b[0m \x1b[90m${getDateTime()}\x1b[0m`, + obj + ); + } + fatal(obj) { + console.log( + "\x1b[40;31;1m FTL", + `\x1b[0m \x1b[31;1m${getDateTime()}\x1b[0;31m`, + obj, + "\x1b[0m" + ); + } + child() { + return new Yapper(); + } + level; // unused + silent; // unused +} diff --git a/src/utils/state.ts b/src/utils/state.ts new file mode 100644 index 0000000..a1e8187 --- /dev/null +++ b/src/utils/state.ts @@ -0,0 +1,27 @@ +import { existsSync, readFileSync, writeFileSync } from "fs"; +import path from "path"; +import { GrokMeta } from "./types"; + +export const STATE_PATH = path.join( + __dirname, + __dirname.includes("index.js") ? ".." : "../..", + "state.json" +); + +if (!existsSync(STATE_PATH)) + writeFileSync(STATE_PATH, JSON.stringify({}), "utf-8"); + +export const state = new Proxy<{ + grokMeta: GrokMeta; +}>(JSON.parse(readFileSync(STATE_PATH, "utf-8")), { + get(target, prop) { + const data = readFileSync(STATE_PATH, "utf-8"); + return JSON.parse(data)[prop]; + }, + set(target, prop, value) { + const data = JSON.parse(readFileSync(STATE_PATH, "utf-8")); + data[prop] = value; + writeFileSync(STATE_PATH, JSON.stringify(data)); + return true; + } +}); diff --git a/src/utils/types.ts b/src/utils/types.ts new file mode 100644 index 0000000..407d6c6 --- /dev/null +++ b/src/utils/types.ts @@ -0,0 +1,10 @@ +export interface GrokMeta { + userAgent: string; + cookies: { + [name: string]: string; + }; + expiresIn: { + time: number; + requests: 0 | 1 | 2 | 3; + }; +}