convertified
This commit is contained in:
7
pyapi/start.sh
Executable file
7
pyapi/start.sh
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
|
||||
# FastAPI Proxy Server Startup Script
|
||||
# This script starts the FastAPI proxy server that bypasses Cloudflare
|
||||
|
||||
echo "Starting FastAPI Proxy Server on port 3070..."
|
||||
uv run python -m fastapi dev --port=3070 main.py
|
||||
47
src/lib/server/external/api.scraping.helpers.ts
vendored
47
src/lib/server/external/api.scraping.helpers.ts
vendored
@@ -1,4 +1,4 @@
|
||||
import { getRandomUserAgent, getULID, sleep } from "$lib/utils";
|
||||
import { getULID, sleep } from "$lib/utils";
|
||||
import { constants } from "$lib/utils/constants";
|
||||
import type { BookingEntry, Draw, LooseApiUser } from "$lib/utils/data.types";
|
||||
import { rng } from "$lib/utils/rng";
|
||||
@@ -15,12 +15,10 @@ import { rng } from "$lib/utils/rng";
|
||||
export const testIfSessionIsValid = async (jwt: string) => {
|
||||
try {
|
||||
const res = await fetch(
|
||||
`${constants.SCRAP_API_URL}/v1/user/get-balance?userId=6339`,
|
||||
`${constants.PROXY_API_URL}/v1/user/get-balance?userId=6339&authorization=${encodeURIComponent(jwt)}`,
|
||||
{
|
||||
headers: {
|
||||
...constants.SCRAP_API_BASE_HEADERS,
|
||||
Authorization: jwt,
|
||||
"User-Agent": getRandomUserAgent(),
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
},
|
||||
);
|
||||
@@ -49,11 +47,9 @@ export const getSessionToken = async (payload: {
|
||||
userType: number;
|
||||
}): Promise<{ ok: boolean; message: string }> => {
|
||||
console.log("Requesting...");
|
||||
const res = await fetch(`${constants.SCRAP_API_URL}/v1/auth/login`, {
|
||||
const res = await fetch(`${constants.PROXY_API_URL}/v1/auth/login`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
...constants.SCRAP_API_BASE_HEADERS,
|
||||
"User-Agent": getRandomUserAgent(),
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
@@ -68,12 +64,10 @@ export const getSessionToken = async (payload: {
|
||||
export async function getUsersBalance(userId: number, jwt: string) {
|
||||
try {
|
||||
const res = await fetch(
|
||||
`${constants.SCRAP_API_URL}/v1/user/get-balance?userId=${userId}`,
|
||||
`${constants.PROXY_API_URL}/v1/user/get-balance?userId=${userId}&authorization=${encodeURIComponent(jwt)}`,
|
||||
{
|
||||
headers: {
|
||||
...constants.SCRAP_API_BASE_HEADERS,
|
||||
Authorization: jwt,
|
||||
"User-Agent": getRandomUserAgent(),
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
},
|
||||
);
|
||||
@@ -85,9 +79,7 @@ export async function getUsersBalance(userId: number, jwt: string) {
|
||||
time: string;
|
||||
};
|
||||
if (res.status !== 200 || rj.code !== 200 || !rj.success) {
|
||||
console.log(
|
||||
`[!] Error getting balance for ${userId} :: ${JSON.stringify(rj)}`,
|
||||
);
|
||||
console.log(`[!] Error getting balance for ${userId} :: ${JSON.stringify(rj)}`);
|
||||
return false;
|
||||
}
|
||||
return rj.data.balance;
|
||||
@@ -103,14 +95,11 @@ export const getDealers = async (jwt: string, distributor_ids: string[]) => {
|
||||
const requests = distributor_ids.map(async (did) => {
|
||||
await sleep(rng(100, 10000));
|
||||
const res = await fetch(
|
||||
`${constants.SCRAP_API_URL}/v1/user/dealer-list`,
|
||||
`${constants.PROXY_API_URL}/v1/user/dealer-list?authorization=${encodeURIComponent(jwt)}`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
...constants.SCRAP_API_BASE_HEADERS,
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": getRandomUserAgent(),
|
||||
Authorization: jwt,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
page: 1,
|
||||
@@ -175,14 +164,11 @@ export const getDealers = async (jwt: string, distributor_ids: string[]) => {
|
||||
|
||||
export const getDistributors = async (jwt: string) => {
|
||||
const res = await fetch(
|
||||
`${constants.SCRAP_API_URL}/v1/user/distributor-list`,
|
||||
`${constants.PROXY_API_URL}/v1/user/distributor-list?authorization=${encodeURIComponent(jwt)}`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
...constants.SCRAP_API_BASE_HEADERS,
|
||||
Authorization: jwt,
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": getRandomUserAgent(),
|
||||
},
|
||||
body: JSON.stringify({
|
||||
page: 1,
|
||||
@@ -218,14 +204,11 @@ export const getDistributors = async (jwt: string) => {
|
||||
|
||||
export const getDraws = async (jwt: string) => {
|
||||
const res = await fetch(
|
||||
`${constants.SCRAP_API_URL}/v1/draw/list-my?userId=15`,
|
||||
`${constants.PROXY_API_URL}/v1/draw/list-my?userId=15&authorization=${encodeURIComponent(jwt)}`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
...constants.SCRAP_API_BASE_HEADERS,
|
||||
Authorization: jwt,
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": getRandomUserAgent(),
|
||||
},
|
||||
},
|
||||
);
|
||||
@@ -253,13 +236,12 @@ export const getData = async (
|
||||
drawId: number,
|
||||
chosenDate: string,
|
||||
) => {
|
||||
const res = await fetch(`${constants.SCRAP_API_URL}/v1/book/list2`, {
|
||||
const res = await fetch(
|
||||
`${constants.PROXY_API_URL}/v1/book/list2?authorization=${encodeURIComponent(jwt)}`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
...constants.SCRAP_API_BASE_HEADERS,
|
||||
Authorization: jwt,
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": getRandomUserAgent(),
|
||||
},
|
||||
body: JSON.stringify({
|
||||
userType: 3,
|
||||
@@ -271,7 +253,8 @@ export const getData = async (
|
||||
containImported: false,
|
||||
keyword: "",
|
||||
}),
|
||||
});
|
||||
},
|
||||
);
|
||||
type J = {
|
||||
code: number;
|
||||
success: boolean;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { getRandomUserAgent, getULID, sleep } from "$lib/utils";
|
||||
import { getULID, sleep } from "$lib/utils";
|
||||
import { constants } from "$lib/utils/constants";
|
||||
import type {
|
||||
ApiPostUserWithParent,
|
||||
@@ -7,8 +7,6 @@ import type {
|
||||
PostDataEntry,
|
||||
ServerError,
|
||||
} from "$lib/utils/data.types";
|
||||
import Fetch from "node-fetch";
|
||||
import { HttpsProxyAgent } from "https-proxy-agent";
|
||||
|
||||
export type APIResponse<T> = {
|
||||
code: number;
|
||||
@@ -72,9 +70,7 @@ export async function postDataToApi(payload: {
|
||||
return {
|
||||
ok: false,
|
||||
detail: "User not found to post data with",
|
||||
errors: [
|
||||
{ message: "User not found for request" },
|
||||
] as ServerError,
|
||||
errors: [{ message: "User not found for request" }] as ServerError,
|
||||
};
|
||||
}
|
||||
if (!dataByUser[userId]) {
|
||||
@@ -84,14 +80,11 @@ export async function postDataToApi(payload: {
|
||||
}
|
||||
|
||||
try {
|
||||
const userPromises = Object.entries(dataByUser).map(
|
||||
async ([userId, userData]) => {
|
||||
const userPromises = Object.entries(dataByUser).map(async ([userId, userData]) => {
|
||||
const session = payload.sessions[userId];
|
||||
const usr = payload.users.find((u) => u.userId === userId);
|
||||
if (!usr) {
|
||||
throw new Error(
|
||||
`User ${userId} not found for posting to api`,
|
||||
);
|
||||
throw new Error(`User ${userId} not found for posting to api`);
|
||||
}
|
||||
|
||||
const distId = usr.parentDistributor ?? 0;
|
||||
@@ -116,13 +109,7 @@ export async function postDataToApi(payload: {
|
||||
drawId,
|
||||
date,
|
||||
);
|
||||
const res = await sendBatchRequest(
|
||||
session,
|
||||
dealerId,
|
||||
payload.draw,
|
||||
total,
|
||||
message,
|
||||
);
|
||||
const res = await sendBatchRequest(session, dealerId, payload.draw, total, message);
|
||||
let rj:
|
||||
| APIResponse<{
|
||||
bookDtos: {
|
||||
@@ -134,9 +121,7 @@ export async function postDataToApi(payload: {
|
||||
try {
|
||||
rj = (await res.json()) as any;
|
||||
} catch (err) {
|
||||
console.log(
|
||||
"Encountered error while parsing post response",
|
||||
);
|
||||
console.log("Encountered error while parsing post response");
|
||||
console.log(res.status, err);
|
||||
}
|
||||
if (rj && rj.code === 200 && res.status === 200) {
|
||||
@@ -167,14 +152,11 @@ export async function postDataToApi(payload: {
|
||||
});
|
||||
console.log(await out.text());
|
||||
}
|
||||
throw new Error(
|
||||
`Failed to send data to api for user ${userId}`,
|
||||
);
|
||||
throw new Error(`Failed to send data to api for user ${userId}`);
|
||||
}
|
||||
}
|
||||
return userResponseIds;
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
// Wait for all user processes to complete
|
||||
const results = await Promise.allSettled(userPromises);
|
||||
@@ -195,9 +177,7 @@ export async function postDataToApi(payload: {
|
||||
return {
|
||||
ok: false,
|
||||
detail: "Failed to post data to API for some users",
|
||||
errors: [
|
||||
{ message: "Failed to post data to API for some users" },
|
||||
] as ServerError,
|
||||
errors: [{ message: "Failed to post data to API for some users" }] as ServerError,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -225,14 +205,12 @@ async function sendBatchRequest(
|
||||
changedBalance: number,
|
||||
body: string,
|
||||
) {
|
||||
return Fetch(`${constants.SCRAP_API_URL}/v1/book/add-multiple`, {
|
||||
agent: new HttpsProxyAgent(`http://${session.ip}`),
|
||||
return fetch(
|
||||
`${constants.PROXY_API_URL}/v1/book/add-multiple?authorization=${encodeURIComponent(session.sessionToken)}`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
...constants.SCRAP_API_BASE_HEADERS,
|
||||
"Content-Type": "application/json;charset=UTF-8",
|
||||
Authorization: session.sessionToken,
|
||||
"User-Agent": getRandomUserAgent(),
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
dealerId,
|
||||
@@ -242,7 +220,8 @@ async function sendBatchRequest(
|
||||
changedBalance,
|
||||
insertData: body,
|
||||
}),
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async function mockSendBatchRequest(
|
||||
@@ -313,14 +292,12 @@ async function deleteAllBookedEntries({
|
||||
drawId: number;
|
||||
closeTime: string;
|
||||
}) {
|
||||
return Fetch(`${constants.SCRAP_API_URL}/v1/book/delete-multiple`, {
|
||||
agent: new HttpsProxyAgent(`http://${session.ip}`),
|
||||
return fetch(
|
||||
`${constants.PROXY_API_URL}/v1/book/delete-multiple?authorization=${encodeURIComponent(session.sessionToken)}`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
...constants.SCRAP_API_BASE_HEADERS,
|
||||
"Content-Type": "application/json;charset=UTF-8",
|
||||
Authorization: session.sessionToken,
|
||||
"User-Agent": getRandomUserAgent(),
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
bookIds: data.map((e) => e.bookId),
|
||||
@@ -328,7 +305,8 @@ async function deleteAllBookedEntries({
|
||||
dealerId,
|
||||
drawId,
|
||||
}),
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// export async function postDataToApi(payload: {
|
||||
|
||||
@@ -1,30 +1,26 @@
|
||||
import { getSessionToken } from "$lib/server/external/api.scraping.helpers";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { createTRPCRouter, protectedProcedure } from "../t";
|
||||
import { constants } from "$lib/utils/constants";
|
||||
import { getUUID } from "$lib/utils";
|
||||
import { z } from "zod";
|
||||
import { dbApiUser } from "$lib/server/db/apiuser.db";
|
||||
import type { ServerError } from "$lib/utils/data.types";
|
||||
import { getSessionToken } from "$lib/server/external/api.scraping.helpers";
|
||||
import {
|
||||
isSessionValidInStore,
|
||||
removeSessionFromStore,
|
||||
setSessionToRedis,
|
||||
} from "$lib/server/utils/session.service";
|
||||
import { getUUID } from "$lib/utils";
|
||||
import { constants } from "$lib/utils/constants";
|
||||
import type { ServerError } from "$lib/utils/data.types";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { z } from "zod";
|
||||
import { createTRPCRouter, protectedProcedure } from "../t";
|
||||
|
||||
export const apiAuthRouter = createTRPCRouter({
|
||||
getCaptcha: protectedProcedure.mutation(async () => {
|
||||
try {
|
||||
const uuid = getUUID();
|
||||
const res = await fetch(
|
||||
`${constants.SCRAP_API_URL}/verify/image?uuid=${uuid}`,
|
||||
{
|
||||
const res = await fetch(`${constants.PROXY_API_URL}/verify/image?uuid=${uuid}`, {
|
||||
headers: {
|
||||
...constants.SCRAP_API_BASE_HEADERS,
|
||||
Accept: "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
},
|
||||
);
|
||||
});
|
||||
const bloob = await res.blob();
|
||||
const imageBuffer = Buffer.from(await bloob.arrayBuffer());
|
||||
const base64String = imageBuffer.toString("base64");
|
||||
@@ -50,8 +46,7 @@ export const apiAuthRouter = createTRPCRouter({
|
||||
console.log("[=] Getting new session... ", input);
|
||||
try {
|
||||
const { captchaId, captchaAnswer } = input;
|
||||
let { userId, userType, password } =
|
||||
await dbApiUser.getRandomDistributor();
|
||||
let { userId, userType, password } = await dbApiUser.getRandomDistributor();
|
||||
|
||||
if (input.userId) {
|
||||
let _user = await dbApiUser.getUserById(input.userId);
|
||||
@@ -74,10 +69,7 @@ export const apiAuthRouter = createTRPCRouter({
|
||||
userType: userType,
|
||||
password: password,
|
||||
});
|
||||
console.log(
|
||||
"[=] Token Response :: ",
|
||||
JSON.stringify(token, null, 2),
|
||||
);
|
||||
console.log("[=] Token Response :: ", JSON.stringify(token, null, 2));
|
||||
if (!token.ok) {
|
||||
return {
|
||||
success: false,
|
||||
|
||||
@@ -4,6 +4,7 @@ export const constants = {
|
||||
POST_SESSION_KEY: "postsession",
|
||||
LAST_FETCHED_KEY: "LAST_FETCHED",
|
||||
SCRAP_API_URL: "https://gamebooking24.com/lottery-api",
|
||||
PROXY_API_URL: "http://localhost:3070",
|
||||
SCRAP_API_SESSION_KEY: "SRAJWT",
|
||||
SCRAP_API_BASE_HEADERS: {
|
||||
Host: "gamebooking24.com",
|
||||
|
||||
Reference in New Issue
Block a user