more inits
This commit is contained in:
parent
8be74278f3
commit
73dbf48c44
7 changed files with 170 additions and 2 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
|||
.env
|
||||
dist/
|
||||
node_modules/
|
||||
state.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": "",
|
||||
|
|
44
src/grokCookies.ts
Normal file
44
src/grokCookies.ts
Normal file
|
@ -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
|
||||
}
|
||||
};
|
||||
}
|
25
src/index.ts
25
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);
|
||||
}
|
60
src/utils/Yapper.ts
Normal file
60
src/utils/Yapper.ts
Normal file
|
@ -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
|
||||
}
|
27
src/utils/state.ts
Normal file
27
src/utils/state.ts
Normal file
|
@ -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;
|
||||
}
|
||||
});
|
10
src/utils/types.ts
Normal file
10
src/utils/types.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
export interface GrokMeta {
|
||||
userAgent: string;
|
||||
cookies: {
|
||||
[name: string]: string;
|
||||
};
|
||||
expiresIn: {
|
||||
time: number;
|
||||
requests: 0 | 1 | 2 | 3;
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue