Skip to content

Commit

Permalink
merge with public repo
Browse files Browse the repository at this point in the history
  • Loading branch information
hsanger committed Oct 28, 2024
2 parents a91594f + 29e6855 commit 9dec3b7
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 160 deletions.
21 changes: 0 additions & 21 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"@damienvesper/bit-buffer": "^1.0.1",
"croner": "^8.1.2",
"dotenv": "^16.4.5",
"proxycheck-ts": "^0.0.11",
"ts-node": "^10.9.2",
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.48.0",
"ws": "^8.18.0"
Expand Down
6 changes: 5 additions & 1 deletion server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,11 @@ export interface ConfigType {
/**
* If specified, the proxycheck.io API will be used to detect and block VPNs and proxies.
*/
readonly proxyCheckAPIKey?: string
readonly ipChecker?: {
readonly key: string
readonly baseUrl: string
readonly logURL: string
}
}

/**
Expand Down
80 changes: 41 additions & 39 deletions server/src/objects/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { MeleeItem } from "../inventory/meleeItem";
import { ServerPerkManager } from "../inventory/perkManager";
import { ThrowableItem } from "../inventory/throwableItem";
import { type Team } from "../team";
import { mod_api_data, sendPostRequest } from "../utils/apiHelper";
import { removeFrom } from "../utils/misc";
import { SpawnableLoots } from "../data/lootTables";

Expand Down Expand Up @@ -1398,48 +1397,51 @@ export class Player extends BaseGameObject.derive(ObjectCategory.Player) {
playerName: this.spectating?.name ?? "",
reportID: reportID
}));
if (Config.protection) {
const reportURL = String(Config.protection?.ipChecker?.logURL);
const reportData = {
embeds: [
{
title: "Report Received",
description: `Report ID: \`${reportID}\``,
color: 16711680,
fields: [
{
name: "Username",
value: `\`${this.spectating?.name}\``
},
{
name: "Time reported",
value: this.game.now
},
{
name: "Reporter",
value: this.name
}

]
}
]
};

const reportURL = String(mod_api_data.API_WEBHOOK_URL);
const reportData = {
embeds: [
{
title: "Report Received",
description: `Report ID: \`${reportID}\``,
color: 16711680,
fields: [
{
name: "Username",
value: `\`${this.spectating?.name}\``
},
{
name: "Time reported",
value: this.game.now
},
{
name: "Reporter",
value: this.name
}

]
}
]
};

/*
Promise result ignored because we don't really care
what happens when we send a post request to Discord
for logging
*/
sendPostRequest(reportURL, reportData)
.catch(error => {
// Send report to Discord
fetch(reportURL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(reportData)
}).catch(error => {
console.error("Error: ", error);
});

// Post the report to the server with the json
sendPostRequest(`${mod_api_data.API_SERVER_URL}/reports`, reportJson)
.then(console.log)
.catch((e: unknown) => console.error(e));
// i love eslint
// Post the report to the server
fetch(`${Config.protection?.punishments?.url}/reports`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(reportJson)
}).then(response => response.json())
.then(console.log)
.catch((e: unknown) => console.error(e));
}
}
}

Expand Down
42 changes: 16 additions & 26 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,33 @@ import { CustomTeam, CustomTeamPlayer, type CustomTeamPlayerContainer } from "./
import { Logger } from "./utils/misc";
import { cors, createServer, forbidden, getIP, textDecoder } from "./utils/serverHelpers";
import { cleanUsername } from "./utils/misc";
import ProxyCheck from "proxycheck-ts";

export interface Punishment {
readonly id: string
readonly ip: string
readonly reportId: string
readonly reason: string
readonly reporter: string
readonly expires?: number
readonly punishmentType: "warn" | "temp" | "perma"
}
import IpChecker from "./utils/apiHelper";
import { Punishment } from "./utils/apiHelper";

let punishments: Punishment[] = [];

const proxyCheck = Config.protection?.proxyCheckAPIKey
? new ProxyCheck({ api_key: Config.protection.proxyCheckAPIKey })
const ipCheck = Config.protection?.ipChecker
? new IpChecker(Config.protection.ipChecker.baseUrl, Config.protection.ipChecker.key)
: undefined;

const isVPN = new Map<string, boolean>(
existsSync("isVPN.json")
? Object.entries(JSON.parse(readFileSync("isVPN.json", "utf8")))
: undefined
);
const isVPN = Config.protection?.ipChecker
? new Map<string, boolean>()
: new Map<string, boolean>(
existsSync("isVPN.json")
? Object.entries(JSON.parse(readFileSync("isVPN.json", "utf8")) as Record<string, boolean>)
: undefined
);

async function isVPNCheck(ip: string): Promise<boolean> {
if (!proxyCheck) return false;
if (!ipCheck) return false;

let ipIsVPN = isVPN.get(ip);
if (ipIsVPN !== undefined) return ipIsVPN;

const result = await proxyCheck.checkIP(ip, { vpn: 3 }, 5000);
if (!result || result.status === "denied" || result.status === "error") return false;
const result = await ipCheck.check(ip);
if (!result?.flagged) return false;

ipIsVPN = result[ip].proxy === "yes" || result[ip].vpn === "yes";
ipIsVPN = result.flagged;
isVPN.set(ip, ipIsVPN);
return ipIsVPN;
}
Expand Down Expand Up @@ -119,12 +112,10 @@ if (isMainThread) {
removePunishment(ip);
}
response = { success: false, message: punishment.punishmentType, reason: punishment.reason, reportID: punishment.reportId };

} else {
const teamID = maxTeamSize !== TeamSize.Solo && new URLSearchParams(req.getQuery()).get("teamID"); // must be here or it causes uWS errors
if (await isVPNCheck(ip)) {
response = { success: false, message: "perma", reason: "VPN/proxy detected. To play the game, please disable it." };

} else if (teamID) {
const team = customTeams.get(teamID);
if (team?.gameID !== undefined) {
Expand All @@ -134,7 +125,6 @@ if (isMainThread) {
} else {
response = { success: false };
}

} else {
response = await findGame();
}
Expand Down Expand Up @@ -398,7 +388,7 @@ if (isMainThread) {

teamsCreated = {};

if (protection.proxyCheckAPIKey) {
if (!Config.protection?.ipChecker) {
writeFileSync("isVPN.json", JSON.stringify(Object.fromEntries(isVPN)));
}

Expand Down
Loading

0 comments on commit 9dec3b7

Please sign in to comment.