feature: Adds eslint rules.

This commit is contained in:
greysoh 2024-05-10 17:37:04 -04:00
parent 57a82a15e9
commit abc60691c6
No known key found for this signature in database
GPG key ID: FE0F173B8FC01571
26 changed files with 123 additions and 94 deletions

View file

@ -2,9 +2,18 @@ import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";
export default [
{languageOptions: { globals: globals.node }},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
{
languageOptions: {
globals: globals.node
},
rules: {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "off"
}
},
];

View file

@ -216,7 +216,7 @@ export class PassyFireBackendProvider implements BackendBaseClass {
static checkParametersBackendInstance(data: string): ParameterReturnedValue {
try {
parseBackendProviderString(data);
// @ts-expect-error
// @ts-expect-error: We write the function, and we know we're returning an error
} catch (e: Error) {
return {
success: false,

View file

@ -69,7 +69,7 @@ export function route(instance: PassyFireBackendProvider) {
},
},
(req, res) => {
// @ts-expect-error
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
const body: {
username: string;
password: string;
@ -115,7 +115,7 @@ export function route(instance: PassyFireBackendProvider) {
},
},
async (req, res) => {
// @ts-expect-error
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
const body: {
token: string;
} = req.body;
@ -132,8 +132,7 @@ export function route(instance: PassyFireBackendProvider) {
req.hostname.indexOf(":") + 1,
);
// @ts-expect-error
// parseInt(...) can take a number just fine, at least in Node.JS
// @ts-expect-error: parseInt(...) can take a number just fine, at least in Node.JS
const port = parseInt(unparsedPort == "" ? proxiedPort : unparsedPort);
// This protocol is so confusing. I'm sorry.

View file

@ -14,7 +14,7 @@ function authenticateSocket(
ws: WebSocket,
message: string,
state: ConnectedClientExt,
): Boolean {
): boolean {
if (!message.startsWith("Accept: ")) {
ws.send("400 Bad Request");
return false;
@ -57,8 +57,8 @@ export function requestHandler(
let state: "authentication" | "data" = "authentication";
let socket: dgram.Socket | net.Socket | undefined;
// @ts-expect-error
let connectedClient: ConnectedClientExt = {};
// @ts-expect-error: FIXME because this is a mess
const connectedClient: ConnectedClientExt = {};
ws.on("close", () => {
instance.clients.splice(

View file

@ -92,7 +92,7 @@ export class SSHBackendProvider implements BackendBaseClass {
this.logs.push(`Failed to start SSHBackendProvider! Error: '${e}'`);
this.state = "stopped";
// @ts-expect-error
// @ts-expect-error: We know that stuff will be initialized in order, so this will be safe
this.sshInstance = null;
return false;
@ -112,7 +112,7 @@ export class SSHBackendProvider implements BackendBaseClass {
this.sshInstance.dispose();
// @ts-expect-error
// @ts-expect-error: We know that stuff will be initialized in order, so this will be safe
this.sshInstance = null;
this.logs.push("Successfully stopped SSHBackendProvider.");
@ -255,7 +255,7 @@ export class SSHBackendProvider implements BackendBaseClass {
static checkParametersBackendInstance(data: string): ParameterReturnedValue {
try {
parseBackendProviderString(data);
// @ts-expect-error
// @ts-expect-error: We write the function, and we know we're returning an error
} catch (e: Error) {
return {
success: false,

View file

@ -35,12 +35,12 @@ export function route(routeOptions: RouteOptions) {
},
},
async (req, res) => {
// @ts-expect-error
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
const body: {
token: string;
name: string;
description?: string;
connectionDetails: any;
connectionDetails: unknown;
backend: string;
} = req.body;

View file

@ -33,7 +33,7 @@ export function route(routeOptions: RouteOptions) {
},
},
async (req, res) => {
// @ts-expect-error
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
const body: {
token: string;
id?: number;

View file

@ -30,7 +30,7 @@ export function route(routeOptions: RouteOptions) {
},
},
async (req, res) => {
// @ts-expect-error
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
const body: {
token: string;
id: number;

View file

@ -27,7 +27,7 @@ export function route(routeOptions: RouteOptions) {
},
},
async (req, res) => {
// @ts-expect-error
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
const body: {
token: string;
id: number;
@ -59,8 +59,12 @@ export function route(routeOptions: RouteOptions) {
return {
success: true,
data: backends[forward.destProviderID].getAllConnections().filter((i) => {
return i.connectionDetails.sourceIP == forward.sourceIP && i.connectionDetails.sourcePort == forward.sourcePort && i.connectionDetails.destPort == forward.destPort;
data: backends[forward.destProviderID].getAllConnections().filter(i => {
return (
i.connectionDetails.sourceIP == forward.sourceIP &&
i.connectionDetails.sourcePort == forward.sourcePort &&
i.connectionDetails.destPort == forward.destPort
);
}),
};
},

View file

@ -50,7 +50,7 @@ export function route(routeOptions: RouteOptions) {
},
},
async (req, res) => {
// @ts-expect-error
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
const body: {
token: string;

View file

@ -41,7 +41,7 @@ export function route(routeOptions: RouteOptions) {
},
},
async (req, res) => {
// @ts-expect-error
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
const body: {
token: string;

View file

@ -30,7 +30,7 @@ export function route(routeOptions: RouteOptions) {
},
},
async (req, res) => {
// @ts-expect-error
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
const body: {
token: string;
id: number;

View file

@ -30,7 +30,7 @@ export function route(routeOptions: RouteOptions) {
},
},
async (req, res) => {
// @ts-expect-error
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
const body: {
token: string;
id: number;
@ -58,8 +58,7 @@ export function route(routeOptions: RouteOptions) {
error: "Backend not found",
});
// Other restrictions in place make it so that it MUST be either TCP or UDP
// @ts-expect-error
// @ts-expect-error: Other restrictions in place make it so that it MUST be either TCP or UDP
const protocol: "tcp" | "udp" = forward.protocol;
backends[forward.destProviderID].addConnection(

View file

@ -30,7 +30,7 @@ export function route(routeOptions: RouteOptions) {
},
},
async (req, res) => {
// @ts-expect-error
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
const body: {
token: string;
id: number;
@ -58,8 +58,7 @@ export function route(routeOptions: RouteOptions) {
error: "Backend not found",
});
// Other restrictions in place make it so that it MUST be either TCP or UDP
// @ts-expect-error
// @ts-expect-error: Other restrictions in place make it so that it MUST be either TCP or UDP
const protocol: "tcp" | "udp" = forward.protocol;
backends[forward.destProviderID].removeConnection(

View file

@ -22,7 +22,7 @@ export function route(routeOptions: RouteOptions) {
},
},
async (req, res) => {
// @ts-expect-error
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
const body: {
token: string;
} = req.body;

View file

@ -29,7 +29,7 @@ export function route(routeOptions: RouteOptions) {
},
},
async (req, res) => {
// @ts-expect-error
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
const body: {
name: string;
email: string;
@ -87,9 +87,9 @@ export function route(routeOptions: RouteOptions) {
}
if (options.allowUnsafeGlobalTokens) {
// @ts-expect-error
// @ts-expect-error: Setting this correctly is a goddamn mess, but this is safe to an extent. It won't crash at least
userData.rootToken = generateRandomData();
// @ts-expect-error
// @ts-expect-error: Read above.
userData.isRootServiceAccount = true;
}

View file

@ -20,27 +20,28 @@ export function route(routeOptions: RouteOptions) {
properties: {
email: { type: "string" },
username: { type: "string" },
password: { type: "string" }
password: { type: "string" },
},
},
},
},
async (req, res) => {
// @ts-expect-error
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
const body: {
email?: string;
username?: string;
password: string;
} = req.body;
if (!body.email && !body.username) return res.status(400).send({
error: "missing both email and username. please supply at least one."
if (!body.email && !body.username)
return res.status(400).send({
error: "missing both email and username. please supply at least one.",
});
const userSearch = await prisma.user.findFirst({
where: {
email: body.email,
username: body.username
username: body.username,
},
});

View file

@ -31,7 +31,7 @@ export function route(routeOptions: RouteOptions) {
},
},
async (req, res) => {
// @ts-expect-error
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
const body: {
token: string;
id?: number;
@ -64,7 +64,7 @@ export function route(routeOptions: RouteOptions) {
name: i.name,
email: i.email,
isServiceAccount: i.isRootServiceAccount,
username: i.username
username: i.username,
})),
};
},

View file

@ -30,7 +30,7 @@ export function route(routeOptions: RouteOptions) {
},
},
async (req, res) => {
// @ts-expect-error
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
const body: {
token: string;
uid: number;

View file

@ -4,7 +4,17 @@ import tseslint from "typescript-eslint";
export default [
{languageOptions: { globals: globals.node }},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
{
languageOptions: {
globals: globals.node,
},
rules: {
"@typescript-eslint/no-explicit-any": "off",
"no-constant-condition": "warn"
}
},
];

View file

@ -4,7 +4,7 @@ import { run as connection } from "./commands/connections.js";
import { run as backends } from "./commands/backends.js";
import { run as users } from "./commands/users.js";
export type PrintLine = (...str: any[]) => void;
export type PrintLine = (...str: unknown[]) => void;
export type KeyboardRead = (disableEcho?: boolean) => Promise<string>;
type Command = (
@ -12,7 +12,7 @@ type Command = (
println: PrintLine,
axios: Axios,
apiKey: string,
keyboardRead: KeyboardRead
keyboardRead: KeyboardRead,
) => Promise<void>;
type Commands = {
@ -30,7 +30,9 @@ export const commands: Commands = [
printf(`${command.name}: ${command.description}\n`);
});
printf("\nRun a command of your choosing with --help to see more options.\n");
printf(
"\nRun a command of your choosing with --help to see more options.\n",
);
},
},
{
@ -43,21 +45,21 @@ export const commands: Commands = [
{
name: "conn",
description: "Manages connections for NextNet",
run: connection
run: connection,
},
{
name: "user",
description: "Manages users for NextNet",
run: users
run: users,
},
{
name: "backend",
description: "Manages backends for NextNet",
run: backends
run: backends,
},
{
name: "back",
description: "(alias) Manages backends for NextNet",
run: backends
}
run: backends,
},
];

View file

@ -119,8 +119,7 @@ export async function run(
password?: string;
},
) => {
// Yes it can index for what we need it to do.
// @ts-expect-error
// @ts-expect-error: Yes it can index for what we need it to do.
const isUnsupportedPlatform: boolean = !addRequiredOptions[provider];
if (isUnsupportedPlatform) {
@ -141,9 +140,8 @@ export async function run(
connectionDetails = options.customParameters;
} else if (provider == "ssh") {
for (const argument of addRequiredOptions["ssh"]) {
// No.
// @ts-expect-error
const hasArgument = options[argument] as any;
// @ts-expect-error: No.
const hasArgument = options[argument];
if (!hasArgument) {
return println("ERROR: Missing argument '%s'\n", argument);
@ -177,8 +175,7 @@ export async function run(
connectionDetails = JSON.stringify(unstringifiedArguments);
} else if (provider == "passyfire") {
for (const argument of addRequiredOptions["passyfire"]) {
// No.
// @ts-expect-error
// @ts-expect-error: No.
const hasArgument = options[argument];
if (!hasArgument) {
@ -218,7 +215,9 @@ export async function run(
}
if (options.userAsk) {
while (true) {
let shouldContinueAsking: boolean = true;
while (shouldContinueAsking) {
println("Creating a user.\nUsername: ");
const username = await readKeyboard();
@ -247,14 +246,12 @@ export async function run(
});
println("\nShould we continue creating users? (y/n) ");
const shouldContinueAsking = (await readKeyboard())
shouldContinueAsking = (await readKeyboard())
.toLowerCase()
.trim()
.startsWith("y");
println("\n\n");
if (!shouldContinueAsking) break;
}
}
@ -420,6 +417,7 @@ export async function run(
// We don't know what we're recieving. We just try to parse it (hence the any type)
// {} is more accurate but TS yells at us if we do that :(
// eslint-disable-next-line
let parsedJSONData: any | undefined;
try {

View file

@ -382,6 +382,8 @@ export async function run(
if (options.tail) {
let previousEntries: string[] = [];
// FIXME?
// eslint-disable-next-line no-constant-condition
while (true) {
const response = await axios.post("/api/v1/forward/connections", {
token,
@ -407,6 +409,7 @@ export async function run(
simplifiedArray,
previousEntries,
);
const removedItems: string[] = difference(
previousEntries,
simplifiedArray,

View file

@ -80,8 +80,7 @@ export async function run(
password = passwordConfirmOne;
} else {
// From the first check we do, we know this is safe (you MUST specify a password)
// @ts-expect-error
// @ts-expect-error: From the first check we do, we know this is safe (you MUST specify a password)
password = options.password;
}
@ -120,7 +119,7 @@ export async function run(
return;
}
let response = await axios.post("/api/v1/users/remove", {
const response = await axios.post("/api/v1/users/remove", {
token: apiKey,
uid,
});

View file

@ -31,7 +31,7 @@ const serverBaseURL: string =
const axios = baseAxios.create({
baseURL: serverBaseURL,
validateStatus: () => true
validateStatus: () => true,
});
try {
@ -70,7 +70,9 @@ const server: ssh2.Server = new ssh2.Server({
server.on("connection", client => {
let token: string = "";
// eslint-disable-next-line
let username: string = "";
// eslint-disable-next-line
let password: string = "";
client.on("authentication", async auth => {
@ -138,7 +140,7 @@ server.on("connection", client => {
});
client.on("ready", () => {
client.on("session", (accept, reject) => {
client.on("session", accept => {
const conn = accept();
conn.on("exec", async (accept, reject, info) => {
@ -149,11 +151,11 @@ server.on("connection", client => {
}
// Matches on ; and &&
const commandsRecv = info.command.split(/;|&&/).map((i) => i.trim());
const commandsRecv = info.command.split(/;|&&/).map(i => i.trim());
function println(...data: any[]) {
function println(...data: unknown[]) {
stream.write(format(...data).replaceAll("\n", "\r\n"));
};
}
for (const command of commandsRecv) {
const argv = parseArgsStringToArgv(command);
@ -164,14 +166,14 @@ server.on("connection", client => {
const command = commands.find(i => i.name == argv[0]);
if (!command) {
stream.write(
`Unknown command ${argv[0]}.\r\n`,
);
stream.write(`Unknown command ${argv[0]}.\r\n`);
continue;
}
await command.run(argv, println, axios, token, (disableEcho) => readFromKeyboard(stream, disableEcho));
await command.run(argv, println, axios, token, disableEcho =>
readFromKeyboard(stream, disableEcho),
);
}
}
@ -191,10 +193,12 @@ server.on("connection", client => {
"Welcome to NextNet LOM. Run 'help' to see commands.\r\n\r\n~$ ",
);
function println(...data: any[]) {
function println(...data: unknown[]) {
stream.write(format(...data).replaceAll("\n", "\r\n"));
};
}
// FIXME (greysoh): wtf? this isn't setting correctly.
// @eslint-disable-next-line
while (true) {
const line = await readFromKeyboard(stream);
stream.write("\r\n");
@ -219,7 +223,9 @@ server.on("connection", client => {
continue;
}
await command.run(argv, println, axios, token, (disableEcho) => readFromKeyboard(stream, disableEcho));
await command.run(argv, println, axios, token, disableEcho =>
readFromKeyboard(stream, disableEcho),
);
stream.write("~$ ");
}
}

View file

@ -5,7 +5,7 @@ export class SSHCommand extends Command {
hasRecievedExitSignal: boolean;
println: PrintLine;
exitEventHandlers: ((...any: any[]) => void)[];
exitEventHandlers: ((...any: unknown[]) => void)[];
parent: SSHCommand | null;
/**
@ -81,11 +81,11 @@ export class SSHCommand extends Command {
action(fn: (...args: any[]) => void | Promise<void>): this {
super.action(fn);
// @ts-expect-error
// @ts-expect-error: This parameter is private, but we need control over it.
// prettier-ignore
const oldActionHandler: (...args: any[]) => void | Promise<void> = this._actionHandler;
// @ts-expect-error
// @ts-expect-error: Overriding private parameters (but this works)
this._actionHandler = async (...args: any[]): Promise<void> => {
if (this.hasRecievedExitSignal) return;
await oldActionHandler(...args);