feature: Add support for eslint.
This commit is contained in:
commit
d107fff996
41 changed files with 3627 additions and 704 deletions
|
@ -1,6 +1,17 @@
|
|||
#!/usr/bin/env bash
|
||||
shopt -s globstar
|
||||
"$(git rev-parse --show-toplevel)"/api/node_modules/.bin/prettier --ignore-unknown --write $(git rev-parse --show-toplevel)/{api,lom}/src/**/*.ts
|
||||
set -e
|
||||
|
||||
pushd $(git rev-parse --show-toplevel)/api
|
||||
npx eslint src
|
||||
popd
|
||||
|
||||
pushd $(git rev-parse --show-toplevel)/lom
|
||||
npx eslint src
|
||||
popd
|
||||
|
||||
# Formatting step
|
||||
"$(git rev-parse --show-toplevel)"/api/node_modules/.bin/prettier --ignore-unknown --write $(git rev-parse --show-toplevel)/{api,lom}/{eslint.config.js,src/**/*.ts}
|
||||
rustfmt $(git rev-parse --show-toplevel)/gui/src/**/*.rs
|
||||
git update-index --again
|
||||
exit 0
|
4
.github/labeler.yml
vendored
4
.github/labeler.yml
vendored
|
@ -5,9 +5,11 @@ modifies ci:
|
|||
modifies docker:
|
||||
- '**/Dockerfile'
|
||||
- '**/docker-compose.yml'
|
||||
- '**/*.env'
|
||||
- '**/prod-docker.env'
|
||||
modifies api:
|
||||
- api/**/*
|
||||
modifies lom:
|
||||
- lom/**/*
|
||||
modifies gui:
|
||||
- gui/**/*
|
||||
modifies nix:
|
||||
|
|
61
.github/workflows/api-testing.yml
vendored
Normal file
61
.github/workflows/api-testing.yml
vendored
Normal file
|
@ -0,0 +1,61 @@
|
|||
name: CI Testing (API)
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
paths:
|
||||
- "api/**"
|
||||
push:
|
||||
paths:
|
||||
- "api/**"
|
||||
|
||||
defaults:
|
||||
run:
|
||||
working-directory: api
|
||||
|
||||
env:
|
||||
DATABASE_URL: "postgresql://nextnet:nextnet@localhost:5432/nextnet?schema=nextnet"
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: postgres
|
||||
env:
|
||||
POSTGRES_PASSWORD: nextnet
|
||||
POSTGRES_USER: nextnet
|
||||
POSTGRES_DB: nextnet
|
||||
options: >-
|
||||
--health-cmd pg_isready
|
||||
--health-interval 10s
|
||||
--health-timeout 5s
|
||||
--health-retries 5
|
||||
ports:
|
||||
- 5432:5432
|
||||
|
||||
steps:
|
||||
- name: Checkout code using Git
|
||||
uses: actions/checkout@main
|
||||
|
||||
- name: Install Node
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 20.x
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm install --save-dev
|
||||
|
||||
- name: Install prisma
|
||||
run: npx prisma migrate dev
|
||||
|
||||
- name: Build source
|
||||
run: npm run build
|
||||
|
||||
- name: Run eslint
|
||||
run: npx eslint src
|
||||
|
||||
- name: Run prettier to verify if we're formatted or not
|
||||
uses: creyD/prettier_action@v4.3
|
||||
with:
|
||||
dry: true
|
2
.github/workflows/label.yml
vendored
2
.github/workflows/label.yml
vendored
|
@ -1,4 +1,4 @@
|
|||
name: Labeler
|
||||
name: Label Issues / Pull Requests
|
||||
on: [pull_request_target]
|
||||
|
||||
jobs:
|
||||
|
|
40
.github/workflows/lom-testing.yml
vendored
Normal file
40
.github/workflows/lom-testing.yml
vendored
Normal file
|
@ -0,0 +1,40 @@
|
|||
name: CI Testing (LOM)
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
paths:
|
||||
- "api/**"
|
||||
push:
|
||||
paths:
|
||||
- "api/**"
|
||||
|
||||
defaults:
|
||||
run:
|
||||
working-directory: lom
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code using Git
|
||||
uses: actions/checkout@main
|
||||
|
||||
- name: Install Node
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 20.x
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm install --save-dev
|
||||
|
||||
- name: Build source
|
||||
run: npm run build
|
||||
|
||||
- name: Run eslint
|
||||
run: npx eslint src
|
||||
|
||||
- name: Run prettier to verify if we're formatted or not
|
||||
uses: creyD/prettier_action@v4.3
|
||||
with:
|
||||
dry: true
|
2
.github/workflows/release.yml
vendored
2
.github/workflows/release.yml
vendored
|
@ -1,4 +1,4 @@
|
|||
name: release
|
||||
name: Release code
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
|
|
19
api/eslint.config.js
Normal file
19
api/eslint.config.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
import globals from "globals";
|
||||
import pluginJs from "@eslint/js";
|
||||
import tseslint from "typescript-eslint";
|
||||
|
||||
export default [
|
||||
pluginJs.configs.recommended,
|
||||
...tseslint.configs.recommended,
|
||||
|
||||
{
|
||||
languageOptions: {
|
||||
globals: globals.node,
|
||||
},
|
||||
|
||||
rules: {
|
||||
"no-unused-vars": "off",
|
||||
"@typescript-eslint/no-unused-vars": "off",
|
||||
},
|
||||
},
|
||||
];
|
1327
api/package-lock.json
generated
1327
api/package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -14,14 +14,18 @@
|
|||
"author": "greysoh",
|
||||
"license": "BSD-3-Clause",
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.2.0",
|
||||
"@types/bcrypt": "^5.0.2",
|
||||
"@types/node": "^20.12.7",
|
||||
"@types/ssh2": "^1.15.0",
|
||||
"@types/ws": "^8.5.10",
|
||||
"eslint": "^8.57.0",
|
||||
"globals": "^15.2.0",
|
||||
"nodemon": "^3.0.3",
|
||||
"prettier": "^3.2.5",
|
||||
"prisma": "^5.13.0",
|
||||
"typescript": "^5.3.3"
|
||||
"typescript": "^5.3.3",
|
||||
"typescript-eslint": "^7.8.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@fastify/websocket": "^10.0.1",
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// @eslint-ignore-file
|
||||
|
||||
export type ParameterReturnedValue = {
|
||||
success: boolean;
|
||||
message?: string;
|
||||
|
|
|
@ -10,4 +10,4 @@ export const backendProviders: Record<string, typeof BackendBaseClass> = {
|
|||
|
||||
if (process.env.NODE_ENV != "production") {
|
||||
backendProviders["dummy"] = BackendBaseClass;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -55,7 +55,7 @@ function parseBackendProviderString(data: string): BackendParsedProviderString {
|
|||
|
||||
if (typeof jsonData.ip != "string")
|
||||
throw new Error("IP field is not a string");
|
||||
|
||||
|
||||
if (typeof jsonData.port != "number") throw new Error("Port is not a number");
|
||||
|
||||
if (
|
||||
|
@ -63,7 +63,7 @@ function parseBackendProviderString(data: string): BackendParsedProviderString {
|
|||
typeof jsonData.publicPort != "number"
|
||||
)
|
||||
throw new Error("(optional field) Proxied port is not a number");
|
||||
|
||||
|
||||
if (
|
||||
typeof jsonData.isProxied != "undefined" &&
|
||||
typeof jsonData.isProxied != "boolean"
|
||||
|
@ -216,7 +216,7 @@ export class PassyFireBackendProvider implements BackendBaseClass {
|
|||
static checkParametersBackendInstance(data: string): ParameterReturnedValue {
|
||||
try {
|
||||
parseBackendProviderString(data);
|
||||
// @ts-ignore
|
||||
// @ts-expect-error: We write the function, and we know we're returning an error
|
||||
} catch (e: Error) {
|
||||
return {
|
||||
success: false,
|
||||
|
|
|
@ -69,7 +69,7 @@ export function route(instance: PassyFireBackendProvider) {
|
|||
},
|
||||
},
|
||||
(req, res) => {
|
||||
// @ts-ignore
|
||||
// @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-ignore
|
||||
// @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-ignore
|
||||
// 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.
|
||||
|
|
|
@ -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-ignore
|
||||
let connectedClient: ConnectedClientExt = {};
|
||||
// @ts-expect-error: FIXME because this is a mess
|
||||
const connectedClient: ConnectedClientExt = {};
|
||||
|
||||
ws.on("close", () => {
|
||||
instance.clients.splice(
|
||||
|
|
|
@ -92,7 +92,7 @@ export class SSHBackendProvider implements BackendBaseClass {
|
|||
this.logs.push(`Failed to start SSHBackendProvider! Error: '${e}'`);
|
||||
this.state = "stopped";
|
||||
|
||||
// @ts-ignore
|
||||
// @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-ignore
|
||||
// @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-ignore
|
||||
// @ts-expect-error: We write the function, and we know we're returning an error
|
||||
} catch (e: Error) {
|
||||
return {
|
||||
success: false,
|
||||
|
|
|
@ -27,7 +27,7 @@ export const permissionListDisabled: Record<string, boolean> = {
|
|||
};
|
||||
|
||||
// FIXME: This solution fucking sucks.
|
||||
export let permissionListEnabled: Record<string, boolean> = JSON.parse(
|
||||
export const permissionListEnabled: Record<string, boolean> = JSON.parse(
|
||||
JSON.stringify(permissionListDisabled),
|
||||
);
|
||||
|
||||
|
|
|
@ -35,12 +35,12 @@ export function route(routeOptions: RouteOptions) {
|
|||
},
|
||||
},
|
||||
async (req, res) => {
|
||||
// @ts-ignore
|
||||
// @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;
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@ export function route(routeOptions: RouteOptions) {
|
|||
},
|
||||
},
|
||||
async (req, res) => {
|
||||
// @ts-ignore
|
||||
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
|
||||
const body: {
|
||||
token: string;
|
||||
id?: number;
|
||||
|
@ -69,7 +69,7 @@ export function route(routeOptions: RouteOptions) {
|
|||
success: true,
|
||||
data: prismaBackends.map(i => ({
|
||||
id: i.id,
|
||||
|
||||
|
||||
name: i.name,
|
||||
description: i.description,
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@ export function route(routeOptions: RouteOptions) {
|
|||
},
|
||||
},
|
||||
async (req, res) => {
|
||||
// @ts-ignore
|
||||
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
|
||||
const body: {
|
||||
token: string;
|
||||
id: number;
|
||||
|
|
|
@ -27,7 +27,7 @@ export function route(routeOptions: RouteOptions) {
|
|||
},
|
||||
},
|
||||
async (req, res) => {
|
||||
// @ts-ignore
|
||||
// @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
|
||||
);
|
||||
}),
|
||||
};
|
||||
},
|
||||
|
|
|
@ -50,7 +50,7 @@ export function route(routeOptions: RouteOptions) {
|
|||
},
|
||||
},
|
||||
async (req, res) => {
|
||||
// @ts-ignore
|
||||
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
|
||||
const body: {
|
||||
token: string;
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ export function route(routeOptions: RouteOptions) {
|
|||
},
|
||||
},
|
||||
async (req, res) => {
|
||||
// @ts-ignore
|
||||
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
|
||||
const body: {
|
||||
token: string;
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@ export function route(routeOptions: RouteOptions) {
|
|||
},
|
||||
},
|
||||
async (req, res) => {
|
||||
// @ts-ignore
|
||||
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
|
||||
const body: {
|
||||
token: string;
|
||||
id: number;
|
||||
|
|
|
@ -30,7 +30,7 @@ export function route(routeOptions: RouteOptions) {
|
|||
},
|
||||
},
|
||||
async (req, res) => {
|
||||
// @ts-ignore
|
||||
// @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-ignore
|
||||
// @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(
|
||||
|
|
|
@ -30,7 +30,7 @@ export function route(routeOptions: RouteOptions) {
|
|||
},
|
||||
},
|
||||
async (req, res) => {
|
||||
// @ts-ignore
|
||||
// @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-ignore
|
||||
// @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(
|
||||
|
|
|
@ -22,7 +22,7 @@ export function route(routeOptions: RouteOptions) {
|
|||
},
|
||||
},
|
||||
async (req, res) => {
|
||||
// @ts-ignore
|
||||
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
|
||||
const body: {
|
||||
token: string;
|
||||
} = req.body;
|
||||
|
|
|
@ -29,7 +29,7 @@ export function route(routeOptions: RouteOptions) {
|
|||
},
|
||||
},
|
||||
async (req, res) => {
|
||||
// @ts-ignore
|
||||
// @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-ignore
|
||||
// @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-ignore
|
||||
// @ts-expect-error: Read above.
|
||||
userData.isRootServiceAccount = true;
|
||||
}
|
||||
|
||||
|
|
|
@ -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-ignore
|
||||
// @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,
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ export function route(routeOptions: RouteOptions) {
|
|||
},
|
||||
},
|
||||
async (req, res) => {
|
||||
// @ts-ignore
|
||||
// @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,
|
||||
})),
|
||||
};
|
||||
},
|
||||
|
|
|
@ -30,7 +30,7 @@ export function route(routeOptions: RouteOptions) {
|
|||
},
|
||||
},
|
||||
async (req, res) => {
|
||||
// @ts-ignore
|
||||
// @ts-expect-error: Fastify routes schema parsing is trustworthy, so we can "assume" invalid types
|
||||
const body: {
|
||||
token: string;
|
||||
uid: number;
|
||||
|
|
149
lom/diff.diff
Normal file
149
lom/diff.diff
Normal file
|
@ -0,0 +1,149 @@
|
|||
diff --git a/api/src/backendimpl/passyfire-reimpl/routes.ts b/api/src/backendimpl/passyfire-reimpl/routes.ts
|
||||
index 2961483..4519a87 100644
|
||||
--- a/api/src/backendimpl/passyfire-reimpl/routes.ts
|
||||
+++ b/api/src/backendimpl/passyfire-reimpl/routes.ts
|
||||
@@ -47,25 +47,6 @@ export function route(instance: PassyFireBackendProvider) {
|
||||
|
||||
for (const spoofedRoute of unsupportedSpoofedRoutes) {
|
||||
fastify.post(spoofedRoute, (req, res) => {
|
||||
- if (typeof req.body != "string")
|
||||
- return res.status(400).send({
|
||||
- error: "Invalid token",
|
||||
- });
|
||||
-
|
||||
- try {
|
||||
- JSON.parse(req.body);
|
||||
- } catch (e) {
|
||||
- return res.status(400).send({
|
||||
- error: "Invalid token",
|
||||
- });
|
||||
- }
|
||||
-
|
||||
- // @ts-expect-error
|
||||
- if (!req.body.token)
|
||||
- return res.status(400).send({
|
||||
- error: "Invalid token",
|
||||
- });
|
||||
-
|
||||
return res.status(403).send({
|
||||
error: "Invalid scope(s)",
|
||||
});
|
||||
diff --git a/lom/src/commands/backends.ts b/lom/src/commands/backends.ts
|
||||
index baba3d1..d16cac1 100644
|
||||
--- a/lom/src/commands/backends.ts
|
||||
+++ b/lom/src/commands/backends.ts
|
||||
@@ -16,6 +16,18 @@ type BackendLookupSuccess = {
|
||||
}[];
|
||||
};
|
||||
|
||||
+const addRequiredOptions = {
|
||||
+ ssh: [
|
||||
+ "sshKey",
|
||||
+ "username",
|
||||
+ "host",
|
||||
+ ],
|
||||
+
|
||||
+ passyfire: [
|
||||
+ "host"
|
||||
+ ]
|
||||
+};
|
||||
+
|
||||
export async function run(
|
||||
argv: string[],
|
||||
println: PrintLine,
|
||||
@@ -58,12 +70,12 @@ export async function run(
|
||||
);
|
||||
|
||||
addBackend.option(
|
||||
- "-u, --username",
|
||||
+ "-u, --username <user>",
|
||||
"(SSH, PassyFire) Username to authenticate with. With PassyFire, it's the username you create",
|
||||
);
|
||||
|
||||
addBackend.option(
|
||||
- "-h, --host",
|
||||
+ "-h, --host <host>",
|
||||
"(SSH, PassyFire) Host to connect to. With PassyFire, it's what you listen on",
|
||||
);
|
||||
|
||||
@@ -86,10 +98,70 @@ export async function run(
|
||||
);
|
||||
|
||||
addBackend.option(
|
||||
- "-p, --password",
|
||||
+ "-p, --password <password>",
|
||||
"(PassyFire) What password you want to use for the primary user",
|
||||
);
|
||||
|
||||
+ addBackend.action(async(name: string, provider: string, options: {
|
||||
+ description?: string,
|
||||
+ forceCustomParameters?: boolean,
|
||||
+ customParameters?: string,
|
||||
+
|
||||
+ // SSH (mostly)
|
||||
+ sshKey?: string,
|
||||
+ username?: string,
|
||||
+ host?: string,
|
||||
+
|
||||
+ // PassyFire (mostly)
|
||||
+ isProxied?: boolean,
|
||||
+ proxiedPortStr?: number,
|
||||
+ guest?: boolean,
|
||||
+ userAsk?: boolean,
|
||||
+ password?: string
|
||||
+ }) => {
|
||||
+ // Yes it can index for what we need it to do.
|
||||
+ // @ts-expect-error
|
||||
+ const isUnsupportedPlatform: boolean = !addRequiredOptions[provider];
|
||||
+
|
||||
+ if (isUnsupportedPlatform) {
|
||||
+ println("WARNING: Platform is not natively supported by the LOM yet!\n");
|
||||
+ }
|
||||
+
|
||||
+ let connectionDetails: string = "";
|
||||
+
|
||||
+ if (options.forceCustomParameters || isUnsupportedPlatform) {
|
||||
+ if (typeof options.customParameters != "string") {
|
||||
+ return println("ERROR: You are missing the custom parameters option!\n");
|
||||
+ }
|
||||
+
|
||||
+ connectionDetails = options.customParameters;
|
||||
+ } else if (provider == "ssh") {
|
||||
+ for (const argument of addRequiredOptions["ssh"]) {
|
||||
+ // No.
|
||||
+ // @ts-expect-error
|
||||
+ const hasArgument = options[argument] as any;
|
||||
+
|
||||
+ if (!hasArgument) {
|
||||
+ return println("ERROR: Missing argument '%s'\n", hasArgument);
|
||||
+ };
|
||||
+ };
|
||||
+
|
||||
+ // todo!
|
||||
+ } else if (provider == "passyfire") {
|
||||
+ for (const argument of addRequiredOptions["passyfire"]) {
|
||||
+ // No.
|
||||
+ // @ts-expect-error
|
||||
+ const hasArgument = options[argument];
|
||||
+
|
||||
+ if (!hasArgument) {
|
||||
+ return println("ERROR: Missing argument '%s'\n", hasArgument);
|
||||
+ };
|
||||
+ };
|
||||
+
|
||||
+ // todo!
|
||||
+ }
|
||||
+ });
|
||||
+
|
||||
const removeBackend = new SSHCommand(println, "rm");
|
||||
removeBackend.description("Removes a backend");
|
||||
removeBackend.argument("<id>", "ID of the backend");
|
||||
@@ -269,7 +341,7 @@ export async function run(
|
||||
|
||||
// It would make sense to check this, then parse argv, however this causes issues with
|
||||
// the application name not displaying correctly.
|
||||
-
|
||||
+
|
||||
if (argv.length == 1) {
|
||||
println("No arguments specified!\n\n");
|
||||
program.help();
|
19
lom/eslint.config.js
Normal file
19
lom/eslint.config.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
import globals from "globals";
|
||||
import pluginJs from "@eslint/js";
|
||||
import tseslint from "typescript-eslint";
|
||||
|
||||
export default [
|
||||
pluginJs.configs.recommended,
|
||||
...tseslint.configs.recommended,
|
||||
|
||||
{
|
||||
languageOptions: {
|
||||
globals: globals.node,
|
||||
},
|
||||
|
||||
rules: {
|
||||
"@typescript-eslint/no-explicit-any": "off",
|
||||
"no-constant-condition": "warn",
|
||||
},
|
||||
},
|
||||
];
|
1243
lom/package-lock.json
generated
1243
lom/package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -14,11 +14,15 @@
|
|||
"author": "greysoh",
|
||||
"license": "BSD-3-Clause",
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.2.0",
|
||||
"@types/node": "^20.12.8",
|
||||
"@types/ssh2": "^1.15.0",
|
||||
"@types/yargs": "^17.0.32",
|
||||
"eslint": "^8.57.0",
|
||||
"globals": "^15.2.0",
|
||||
"nodemon": "^3.0.3",
|
||||
"typescript": "^5.3.3"
|
||||
"typescript": "^5.3.3",
|
||||
"typescript-eslint": "^7.8.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^1.6.8",
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
];
|
||||
|
|
|
@ -4,28 +4,22 @@ import { SSHCommand } from "../libs/patchCommander.js";
|
|||
import type { PrintLine, KeyboardRead } from "../commands.js";
|
||||
|
||||
type BackendLookupSuccess = {
|
||||
success: boolean,
|
||||
success: boolean;
|
||||
data: {
|
||||
id: number,
|
||||
id: number;
|
||||
|
||||
name: string,
|
||||
description: string,
|
||||
backend: string,
|
||||
connectionDetails?: string,
|
||||
logs: string[]
|
||||
name: string;
|
||||
description: string;
|
||||
backend: string;
|
||||
connectionDetails?: string;
|
||||
logs: string[];
|
||||
}[];
|
||||
};
|
||||
|
||||
const addRequiredOptions = {
|
||||
ssh: [
|
||||
"sshKey",
|
||||
"username",
|
||||
"host",
|
||||
],
|
||||
ssh: ["sshKey", "username", "host"],
|
||||
|
||||
passyfire: [
|
||||
"host"
|
||||
]
|
||||
passyfire: ["host"],
|
||||
};
|
||||
|
||||
export async function run(
|
||||
|
@ -33,7 +27,7 @@ export async function run(
|
|||
println: PrintLine,
|
||||
axios: Axios,
|
||||
token: string,
|
||||
readKeyboard: KeyboardRead
|
||||
readKeyboard: KeyboardRead,
|
||||
) {
|
||||
const program = new SSHCommand(println);
|
||||
program.description("Manages backends for NextNet");
|
||||
|
@ -103,218 +97,238 @@ export async function run(
|
|||
"(PassyFire) What password you want to use for the primary user",
|
||||
);
|
||||
|
||||
addBackend.action(async(name: string, provider: string, options: {
|
||||
description?: string,
|
||||
forceCustomParameters?: boolean,
|
||||
customParameters?: string,
|
||||
|
||||
// SSH (mostly)
|
||||
sshKey?: string,
|
||||
username?: string,
|
||||
host?: string,
|
||||
addBackend.action(
|
||||
async (
|
||||
name: string,
|
||||
provider: string,
|
||||
options: {
|
||||
description?: string;
|
||||
forceCustomParameters?: boolean;
|
||||
customParameters?: string;
|
||||
|
||||
// PassyFire (mostly)
|
||||
isProxied?: boolean,
|
||||
proxiedPort?: string,
|
||||
guest?: boolean,
|
||||
userAsk?: boolean,
|
||||
password?: string
|
||||
}) => {
|
||||
// Yes it can index for what we need it to do.
|
||||
// @ts-ignore
|
||||
const isUnsupportedPlatform: boolean = !addRequiredOptions[provider];
|
||||
|
||||
if (isUnsupportedPlatform) {
|
||||
println("WARNING: Platform is not natively supported by the LOM yet!\n");
|
||||
}
|
||||
// SSH (mostly)
|
||||
sshKey?: string;
|
||||
username?: string;
|
||||
host?: string;
|
||||
|
||||
let connectionDetails: string = "";
|
||||
// PassyFire (mostly)
|
||||
isProxied?: boolean;
|
||||
proxiedPort?: string;
|
||||
guest?: boolean;
|
||||
userAsk?: boolean;
|
||||
password?: string;
|
||||
},
|
||||
) => {
|
||||
// @ts-expect-error: Yes it can index for what we need it to do.
|
||||
const isUnsupportedPlatform: boolean = !addRequiredOptions[provider];
|
||||
|
||||
if (options.forceCustomParameters || isUnsupportedPlatform) {
|
||||
if (typeof options.customParameters != "string") {
|
||||
return println("ERROR: You are missing the custom parameters option!\n");
|
||||
if (isUnsupportedPlatform) {
|
||||
println(
|
||||
"WARNING: Platform is not natively supported by the LOM yet!\n",
|
||||
);
|
||||
}
|
||||
|
||||
connectionDetails = options.customParameters;
|
||||
} else if (provider == "ssh") {
|
||||
for (const argument of addRequiredOptions["ssh"]) {
|
||||
// No.
|
||||
// @ts-ignore
|
||||
const hasArgument = options[argument] as any;
|
||||
|
||||
if (!hasArgument) {
|
||||
return println("ERROR: Missing argument '%s'\n", argument);
|
||||
};
|
||||
};
|
||||
let connectionDetails: string = "";
|
||||
|
||||
const unstringifiedArguments: {
|
||||
ip?: string,
|
||||
port?: number,
|
||||
username?: string,
|
||||
privateKey?: string
|
||||
} = {};
|
||||
|
||||
if (options.host) {
|
||||
const sourceSplit: string[] = options.host.split(":");
|
||||
|
||||
const sourceIP: string = sourceSplit[0];
|
||||
const sourcePort: number = sourceSplit.length >= 2 ? parseInt(sourceSplit[1]) : 22;
|
||||
|
||||
unstringifiedArguments.ip = sourceIP;
|
||||
unstringifiedArguments.port = sourcePort;
|
||||
}
|
||||
|
||||
unstringifiedArguments.username = options.username;
|
||||
unstringifiedArguments.privateKey = options.sshKey?.replaceAll("\\n", "\n");
|
||||
|
||||
connectionDetails = JSON.stringify(unstringifiedArguments);
|
||||
} else if (provider == "passyfire") {
|
||||
for (const argument of addRequiredOptions["passyfire"]) {
|
||||
// No.
|
||||
// @ts-ignore
|
||||
const hasArgument = options[argument];
|
||||
|
||||
if (!hasArgument) {
|
||||
return println("ERROR: Missing argument '%s'\n", argument);
|
||||
};
|
||||
};
|
||||
|
||||
const unstringifiedArguments: {
|
||||
ip?: string,
|
||||
port?: number,
|
||||
publicPort?: number,
|
||||
isProxied?: boolean,
|
||||
users: {
|
||||
username: string,
|
||||
password: string
|
||||
}[]
|
||||
} = {
|
||||
users: []
|
||||
};
|
||||
|
||||
if (options.guest) {
|
||||
unstringifiedArguments.users.push({
|
||||
username: "guest",
|
||||
password: "guest"
|
||||
});
|
||||
};
|
||||
|
||||
if (options.username) {
|
||||
if (!options.password) {
|
||||
return println("Password must not be left blank\n");
|
||||
if (options.forceCustomParameters || isUnsupportedPlatform) {
|
||||
if (typeof options.customParameters != "string") {
|
||||
return println(
|
||||
"ERROR: You are missing the custom parameters option!\n",
|
||||
);
|
||||
}
|
||||
|
||||
unstringifiedArguments.users.push({
|
||||
username: options.username,
|
||||
password: options.password
|
||||
});
|
||||
};
|
||||
connectionDetails = options.customParameters;
|
||||
} else if (provider == "ssh") {
|
||||
for (const argument of addRequiredOptions["ssh"]) {
|
||||
// @ts-expect-error: No.
|
||||
const hasArgument = options[argument];
|
||||
|
||||
if (options.userAsk) {
|
||||
while (true) {
|
||||
println("Creating a user.\nUsername: ");
|
||||
const username = await readKeyboard();
|
||||
if (!hasArgument) {
|
||||
return println("ERROR: Missing argument '%s'\n", argument);
|
||||
}
|
||||
}
|
||||
|
||||
let passwordConfirmOne = "a";
|
||||
let passwordConfirmTwo = "b";
|
||||
const unstringifiedArguments: {
|
||||
ip?: string;
|
||||
port?: number;
|
||||
username?: string;
|
||||
privateKey?: string;
|
||||
} = {};
|
||||
|
||||
println("\n");
|
||||
if (options.host) {
|
||||
const sourceSplit: string[] = options.host.split(":");
|
||||
|
||||
while (passwordConfirmOne != passwordConfirmTwo) {
|
||||
println("Password: ");
|
||||
passwordConfirmOne = await readKeyboard(true);
|
||||
|
||||
println("\nConfirm password: ");
|
||||
passwordConfirmTwo = await readKeyboard(true);
|
||||
const sourceIP: string = sourceSplit[0];
|
||||
const sourcePort: number =
|
||||
sourceSplit.length >= 2 ? parseInt(sourceSplit[1]) : 22;
|
||||
|
||||
println("\n");
|
||||
|
||||
if (passwordConfirmOne != passwordConfirmTwo) {
|
||||
println("Passwords do not match! Try again.\n\n");
|
||||
}
|
||||
unstringifiedArguments.ip = sourceIP;
|
||||
unstringifiedArguments.port = sourcePort;
|
||||
}
|
||||
|
||||
unstringifiedArguments.username = options.username;
|
||||
unstringifiedArguments.privateKey = options.sshKey?.replaceAll(
|
||||
"\\n",
|
||||
"\n",
|
||||
);
|
||||
|
||||
connectionDetails = JSON.stringify(unstringifiedArguments);
|
||||
} else if (provider == "passyfire") {
|
||||
for (const argument of addRequiredOptions["passyfire"]) {
|
||||
// @ts-expect-error: No.
|
||||
const hasArgument = options[argument];
|
||||
|
||||
if (!hasArgument) {
|
||||
return println("ERROR: Missing argument '%s'\n", argument);
|
||||
}
|
||||
}
|
||||
|
||||
const unstringifiedArguments: {
|
||||
ip?: string;
|
||||
port?: number;
|
||||
publicPort?: number;
|
||||
isProxied?: boolean;
|
||||
users: {
|
||||
username: string;
|
||||
password: string;
|
||||
}[];
|
||||
} = {
|
||||
users: [],
|
||||
};
|
||||
|
||||
if (options.guest) {
|
||||
unstringifiedArguments.users.push({
|
||||
username: "guest",
|
||||
password: "guest",
|
||||
});
|
||||
}
|
||||
|
||||
if (options.username) {
|
||||
if (!options.password) {
|
||||
return println("Password must not be left blank\n");
|
||||
}
|
||||
|
||||
unstringifiedArguments.users.push({
|
||||
username,
|
||||
password: passwordConfirmOne
|
||||
username: options.username,
|
||||
password: options.password,
|
||||
});
|
||||
|
||||
println("\nShould we continue creating users? (y/n) ");
|
||||
const shouldContinueAsking = (await readKeyboard()).toLowerCase().trim().startsWith("y");
|
||||
|
||||
println("\n\n");
|
||||
|
||||
if (!shouldContinueAsking) break;
|
||||
}
|
||||
}
|
||||
|
||||
if (unstringifiedArguments.users.length == 0) {
|
||||
return println("No users will be created with your current arguments! You must have users set up.\n");
|
||||
}
|
||||
|
||||
unstringifiedArguments.isProxied = Boolean(options.isProxied);
|
||||
|
||||
if (options.proxiedPort) {
|
||||
unstringifiedArguments.publicPort = parseInt(options.proxiedPort ?? "");
|
||||
|
||||
if (Number.isNaN(unstringifiedArguments.publicPort)) {
|
||||
println("UID (%s) is not a number.\n", options.proxiedPort);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (options.host) {
|
||||
const sourceSplit: string[] = options.host.split(":");
|
||||
|
||||
if (sourceSplit.length != 2) {
|
||||
return println("Source could not be splitted down (are you missing the ':' in the source to specify port?)\n");
|
||||
}
|
||||
|
||||
const sourceIP: string = sourceSplit[0];
|
||||
const sourcePort: number = parseInt(sourceSplit[1]);
|
||||
if (options.userAsk) {
|
||||
let shouldContinueAsking: boolean = true;
|
||||
|
||||
if (Number.isNaN(sourcePort)) {
|
||||
println("UID (%s) is not a number.\n", sourcePort);
|
||||
return;
|
||||
while (shouldContinueAsking) {
|
||||
println("Creating a user.\nUsername: ");
|
||||
const username = await readKeyboard();
|
||||
|
||||
let passwordConfirmOne = "a";
|
||||
let passwordConfirmTwo = "b";
|
||||
|
||||
println("\n");
|
||||
|
||||
while (passwordConfirmOne != passwordConfirmTwo) {
|
||||
println("Password: ");
|
||||
passwordConfirmOne = await readKeyboard(true);
|
||||
|
||||
println("\nConfirm password: ");
|
||||
passwordConfirmTwo = await readKeyboard(true);
|
||||
|
||||
println("\n");
|
||||
|
||||
if (passwordConfirmOne != passwordConfirmTwo) {
|
||||
println("Passwords do not match! Try again.\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
unstringifiedArguments.users.push({
|
||||
username,
|
||||
password: passwordConfirmOne,
|
||||
});
|
||||
|
||||
println("\nShould we continue creating users? (y/n) ");
|
||||
shouldContinueAsking = (await readKeyboard())
|
||||
.toLowerCase()
|
||||
.trim()
|
||||
.startsWith("y");
|
||||
|
||||
println("\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
unstringifiedArguments.ip = sourceIP;
|
||||
unstringifiedArguments.port = sourcePort;
|
||||
if (unstringifiedArguments.users.length == 0) {
|
||||
return println(
|
||||
"No users will be created with your current arguments! You must have users set up.\n",
|
||||
);
|
||||
}
|
||||
|
||||
unstringifiedArguments.isProxied = Boolean(options.isProxied);
|
||||
|
||||
if (options.proxiedPort) {
|
||||
unstringifiedArguments.publicPort = parseInt(
|
||||
options.proxiedPort ?? "",
|
||||
);
|
||||
|
||||
if (Number.isNaN(unstringifiedArguments.publicPort)) {
|
||||
println("UID (%s) is not a number.\n", options.proxiedPort);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (options.host) {
|
||||
const sourceSplit: string[] = options.host.split(":");
|
||||
|
||||
if (sourceSplit.length != 2) {
|
||||
return println(
|
||||
"Source could not be splitted down (are you missing the ':' in the source to specify port?)\n",
|
||||
);
|
||||
}
|
||||
|
||||
const sourceIP: string = sourceSplit[0];
|
||||
const sourcePort: number = parseInt(sourceSplit[1]);
|
||||
|
||||
if (Number.isNaN(sourcePort)) {
|
||||
println("UID (%s) is not a number.\n", sourcePort);
|
||||
return;
|
||||
}
|
||||
|
||||
unstringifiedArguments.ip = sourceIP;
|
||||
unstringifiedArguments.port = sourcePort;
|
||||
}
|
||||
|
||||
connectionDetails = JSON.stringify(unstringifiedArguments);
|
||||
}
|
||||
|
||||
connectionDetails = JSON.stringify(unstringifiedArguments);
|
||||
}
|
||||
const response = await axios.post("/api/v1/backends/create", {
|
||||
token,
|
||||
|
||||
const response = await axios.post("/api/v1/backends/create", {
|
||||
token,
|
||||
|
||||
name,
|
||||
description: options.description,
|
||||
backend: provider,
|
||||
name,
|
||||
description: options.description,
|
||||
backend: provider,
|
||||
|
||||
connectionDetails
|
||||
});
|
||||
connectionDetails,
|
||||
});
|
||||
|
||||
if (response.status != 200) {
|
||||
if (process.env.NODE_ENV != "production") console.log(response);
|
||||
if (response.status != 200) {
|
||||
if (process.env.NODE_ENV != "production") console.log(response);
|
||||
|
||||
if (response.data.error) {
|
||||
println(`Error: ${response.data.error}\n`);
|
||||
} else {
|
||||
println("Error creating a backend!\n");
|
||||
if (response.data.error) {
|
||||
println(`Error: ${response.data.error}\n`);
|
||||
} else {
|
||||
println("Error creating a backend!\n");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
println("Successfully created the backend.\n");
|
||||
});
|
||||
println("Successfully created the backend.\n");
|
||||
},
|
||||
);
|
||||
|
||||
const removeBackend = new SSHCommand(println, "rm");
|
||||
removeBackend.description("Removes a backend");
|
||||
removeBackend.argument("<id>", "ID of the backend");
|
||||
|
||||
removeBackend.action(async(idStr: string) => {
|
||||
removeBackend.action(async (idStr: string) => {
|
||||
const id: number = parseInt(idStr);
|
||||
|
||||
if (Number.isNaN(id)) {
|
||||
|
@ -324,7 +338,7 @@ export async function run(
|
|||
|
||||
const response = await axios.post("/api/v1/backends/remove", {
|
||||
token,
|
||||
id
|
||||
id,
|
||||
});
|
||||
|
||||
if (response.status != 200) {
|
||||
|
@ -338,7 +352,7 @@ export async function run(
|
|||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
println("Backend has been successfully deleted.\n");
|
||||
});
|
||||
|
||||
|
@ -359,96 +373,101 @@ export async function run(
|
|||
|
||||
lookupBackend.option(
|
||||
"-e, --parse-connection-details",
|
||||
"If specified, we automatically parse the connection details to make them human readable, if standard JSON."
|
||||
"If specified, we automatically parse the connection details to make them human readable, if standard JSON.",
|
||||
);
|
||||
|
||||
lookupBackend.action(async(options: {
|
||||
name?: string,
|
||||
provider?: string,
|
||||
description?: string,
|
||||
parseConnectionDetails?: boolean
|
||||
}) => {
|
||||
const response = await axios.post("/api/v1/backends/lookup", {
|
||||
token,
|
||||
|
||||
name: options.name,
|
||||
description: options.description,
|
||||
lookupBackend.action(
|
||||
async (options: {
|
||||
name?: string;
|
||||
provider?: string;
|
||||
description?: string;
|
||||
parseConnectionDetails?: boolean;
|
||||
}) => {
|
||||
const response = await axios.post("/api/v1/backends/lookup", {
|
||||
token,
|
||||
|
||||
backend: options.provider
|
||||
});
|
||||
name: options.name,
|
||||
description: options.description,
|
||||
|
||||
if (response.status != 200) {
|
||||
if (process.env.NODE_ENV != "production") console.log(response);
|
||||
backend: options.provider,
|
||||
});
|
||||
|
||||
if (response.data.error) {
|
||||
println(`Error: ${response.data.error}\n`);
|
||||
} else {
|
||||
println("Error looking up backends!\n");
|
||||
}
|
||||
if (response.status != 200) {
|
||||
if (process.env.NODE_ENV != "production") console.log(response);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const { data }: BackendLookupSuccess = response.data;
|
||||
|
||||
for (const backend of data) {
|
||||
println("ID: %s:\n", backend.id);
|
||||
println(" - Name: %s\n", backend.name);
|
||||
println(" - Description: %s\n", backend.description);
|
||||
println(" - Using Backend: %s\n", backend.backend);
|
||||
|
||||
if (backend.connectionDetails) {
|
||||
if (options.parseConnectionDetails) {
|
||||
// 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 :(
|
||||
|
||||
let parsedJSONData: any | undefined;
|
||||
|
||||
try {
|
||||
parsedJSONData = JSON.parse(backend.connectionDetails);
|
||||
} catch (e) {
|
||||
println(" - Connection Details: %s\n", backend.connectionDetails);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!parsedJSONData) {
|
||||
// Not really an assertion but I don't care right now
|
||||
println("Assertion failed: parsedJSONData should not be undefined\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
println(" - Connection details:\n");
|
||||
|
||||
for (const key of Object.keys(parsedJSONData)) {
|
||||
let value: string | number = parsedJSONData[key];
|
||||
|
||||
if (typeof value == "string") {
|
||||
value = value.replaceAll("\n", "\n" + " ".repeat(16));
|
||||
}
|
||||
|
||||
if (typeof value == "object") {
|
||||
// TODO: implement?
|
||||
value = JSON.stringify(value);
|
||||
}
|
||||
|
||||
println(" - %s: %s\n", key, value);
|
||||
}
|
||||
if (response.data.error) {
|
||||
println(`Error: ${response.data.error}\n`);
|
||||
} else {
|
||||
println(" - Connection Details: %s\n", backend.connectionDetails);
|
||||
println("Error looking up backends!\n");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
println("\n");
|
||||
}
|
||||
const { data }: BackendLookupSuccess = response.data;
|
||||
|
||||
println("%s backends found.\n", data.length);
|
||||
});
|
||||
for (const backend of data) {
|
||||
println("ID: %s:\n", backend.id);
|
||||
println(" - Name: %s\n", backend.name);
|
||||
println(" - Description: %s\n", backend.description);
|
||||
println(" - Using Backend: %s\n", backend.backend);
|
||||
|
||||
if (backend.connectionDetails) {
|
||||
if (options.parseConnectionDetails) {
|
||||
// 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 {
|
||||
parsedJSONData = JSON.parse(backend.connectionDetails);
|
||||
} catch (e) {
|
||||
println(" - Connection Details: %s\n", backend.connectionDetails);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!parsedJSONData) {
|
||||
// Not really an assertion but I don't care right now
|
||||
println(
|
||||
"Assertion failed: parsedJSONData should not be undefined\n",
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
println(" - Connection details:\n");
|
||||
|
||||
for (const key of Object.keys(parsedJSONData)) {
|
||||
let value: string | number = parsedJSONData[key];
|
||||
|
||||
if (typeof value == "string") {
|
||||
value = value.replaceAll("\n", "\n" + " ".repeat(16));
|
||||
}
|
||||
|
||||
if (typeof value == "object") {
|
||||
// TODO: implement?
|
||||
value = JSON.stringify(value);
|
||||
}
|
||||
|
||||
println(" - %s: %s\n", key, value);
|
||||
}
|
||||
} else {
|
||||
println(" - Connection Details: %s\n", backend.connectionDetails);
|
||||
}
|
||||
}
|
||||
|
||||
println("\n");
|
||||
}
|
||||
|
||||
println("%s backends found.\n", data.length);
|
||||
},
|
||||
);
|
||||
|
||||
const logsCommand = new SSHCommand(println, "logs");
|
||||
logsCommand.description("View logs for a backend");
|
||||
logsCommand.argument("<id>", "ID of the backend");
|
||||
|
||||
logsCommand.action(async(idStr: string) => {
|
||||
logsCommand.action(async (idStr: string) => {
|
||||
const id: number = parseInt(idStr);
|
||||
|
||||
if (Number.isNaN(id)) {
|
||||
|
@ -458,7 +477,7 @@ export async function run(
|
|||
|
||||
const response = await axios.post("/api/v1/backends/lookup", {
|
||||
token,
|
||||
id
|
||||
id,
|
||||
});
|
||||
|
||||
if (response.status != 200) {
|
||||
|
@ -474,10 +493,10 @@ export async function run(
|
|||
}
|
||||
|
||||
const { data }: BackendLookupSuccess = response.data;
|
||||
const ourBackend = data.find((i) => i.id == id);
|
||||
const ourBackend = data.find(i => i.id == id);
|
||||
|
||||
if (!ourBackend) return println("Could not find the backend!\n");
|
||||
ourBackend.logs.forEach((log) => println("%s\n", log));
|
||||
ourBackend.logs.forEach(log => println("%s\n", log));
|
||||
});
|
||||
|
||||
program.addCommand(addBackend);
|
||||
|
@ -496,5 +515,5 @@ export async function run(
|
|||
return;
|
||||
}
|
||||
|
||||
await new Promise((resolve) => program.onExit(resolve));
|
||||
await new Promise(resolve => program.onExit(resolve));
|
||||
}
|
||||
|
|
|
@ -5,36 +5,36 @@ import type { PrintLine } from "../commands.js";
|
|||
|
||||
// https://stackoverflow.com/questions/37938504/what-is-the-best-way-to-find-all-items-are-deleted-inserted-from-original-arra
|
||||
function difference(a: any[], b: any[]) {
|
||||
return a.filter(x => b.indexOf(x) < 0);
|
||||
};
|
||||
return a.filter(x => b.indexOf(x) < 0);
|
||||
}
|
||||
|
||||
type InboundConnectionSuccess = {
|
||||
success: true,
|
||||
success: true;
|
||||
data: {
|
||||
ip: string,
|
||||
port: number,
|
||||
ip: string;
|
||||
port: number;
|
||||
|
||||
connectionDetails: {
|
||||
sourceIP: string,
|
||||
sourcePort: number,
|
||||
destPort: number,
|
||||
enabled: boolean
|
||||
}
|
||||
}[]
|
||||
sourceIP: string;
|
||||
sourcePort: number;
|
||||
destPort: number;
|
||||
enabled: boolean;
|
||||
};
|
||||
}[];
|
||||
};
|
||||
|
||||
type LookupCommandSuccess = {
|
||||
success: true,
|
||||
success: true;
|
||||
data: {
|
||||
id: number,
|
||||
name: string,
|
||||
description: string,
|
||||
sourceIP: string,
|
||||
sourcePort: number,
|
||||
destPort: number,
|
||||
providerID: number,
|
||||
autoStart: boolean
|
||||
}[]
|
||||
id: number;
|
||||
name: string;
|
||||
description: string;
|
||||
sourceIP: string;
|
||||
sourcePort: number;
|
||||
destPort: number;
|
||||
providerID: number;
|
||||
autoStart: boolean;
|
||||
}[];
|
||||
};
|
||||
|
||||
export async function run(
|
||||
|
@ -43,7 +43,11 @@ export async function run(
|
|||
axios: Axios,
|
||||
token: string,
|
||||
) {
|
||||
if (argv.length == 1) return println("error: no arguments specified! run %s --help to see commands.\n", argv[0]);
|
||||
if (argv.length == 1)
|
||||
return println(
|
||||
"error: no arguments specified! run %s --help to see commands.\n",
|
||||
argv[0],
|
||||
);
|
||||
|
||||
const program = new SSHCommand(println);
|
||||
program.description("Manages connections for NextNet");
|
||||
|
@ -68,71 +72,82 @@ export async function run(
|
|||
addCommand.argument("<dest_port>", "Destination port to use");
|
||||
addCommand.option("-d, --description", "Description for the tunnel");
|
||||
|
||||
addCommand.action(async(providerIDStr: string, name: string, protocolRaw: string, source: string, destPortRaw: string, options: {
|
||||
description?: string
|
||||
}) => {
|
||||
const providerID = parseInt(providerIDStr);
|
||||
addCommand.action(
|
||||
async (
|
||||
providerIDStr: string,
|
||||
name: string,
|
||||
protocolRaw: string,
|
||||
source: string,
|
||||
destPortRaw: string,
|
||||
options: {
|
||||
description?: string;
|
||||
},
|
||||
) => {
|
||||
const providerID = parseInt(providerIDStr);
|
||||
|
||||
if (Number.isNaN(providerID)) {
|
||||
println("ID (%s) is not a number\n", providerIDStr);
|
||||
return;
|
||||
};
|
||||
|
||||
const protocol = protocolRaw.toLowerCase().trim();
|
||||
|
||||
if (protocol != "tcp" && protocol != "udp") {
|
||||
return println("Protocol is not a valid option (not tcp or udp)\n");
|
||||
};
|
||||
|
||||
const sourceSplit: string[] = source.split(":");
|
||||
|
||||
if (sourceSplit.length != 2) {
|
||||
return println("Source could not be splitted down (are you missing the ':' in the source to specify port?)\n");
|
||||
}
|
||||
|
||||
const sourceIP: string = sourceSplit[0];
|
||||
const sourcePort: number = parseInt(sourceSplit[1]);
|
||||
|
||||
if (Number.isNaN(sourcePort)) {
|
||||
return println("Port splitted is not a number\n");
|
||||
}
|
||||
|
||||
const destinationPort: number = parseInt(destPortRaw);
|
||||
|
||||
if (Number.isNaN(destinationPort)) {
|
||||
return println("Destination port could not be parsed into a number\n");
|
||||
}
|
||||
|
||||
const response = await axios.post("/api/v1/forward/create", {
|
||||
token,
|
||||
|
||||
name,
|
||||
description: options.description,
|
||||
|
||||
protocol,
|
||||
|
||||
sourceIP,
|
||||
sourcePort,
|
||||
|
||||
destinationPort,
|
||||
|
||||
providerID
|
||||
});
|
||||
|
||||
if (response.status != 200) {
|
||||
if (process.env.NODE_ENV != "production") console.log(response);
|
||||
|
||||
if (response.data.error) {
|
||||
println(`Error: ${response.data.error}\n`);
|
||||
} else {
|
||||
println("Error creating a connection!\n");
|
||||
if (Number.isNaN(providerID)) {
|
||||
println("ID (%s) is not a number\n", providerIDStr);
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
const protocol = protocolRaw.toLowerCase().trim();
|
||||
|
||||
println("Successfully created connection.\n");
|
||||
});
|
||||
if (protocol != "tcp" && protocol != "udp") {
|
||||
return println("Protocol is not a valid option (not tcp or udp)\n");
|
||||
}
|
||||
|
||||
const sourceSplit: string[] = source.split(":");
|
||||
|
||||
if (sourceSplit.length != 2) {
|
||||
return println(
|
||||
"Source could not be splitted down (are you missing the ':' in the source to specify port?)\n",
|
||||
);
|
||||
}
|
||||
|
||||
const sourceIP: string = sourceSplit[0];
|
||||
const sourcePort: number = parseInt(sourceSplit[1]);
|
||||
|
||||
if (Number.isNaN(sourcePort)) {
|
||||
return println("Port splitted is not a number\n");
|
||||
}
|
||||
|
||||
const destinationPort: number = parseInt(destPortRaw);
|
||||
|
||||
if (Number.isNaN(destinationPort)) {
|
||||
return println("Destination port could not be parsed into a number\n");
|
||||
}
|
||||
|
||||
const response = await axios.post("/api/v1/forward/create", {
|
||||
token,
|
||||
|
||||
name,
|
||||
description: options.description,
|
||||
|
||||
protocol,
|
||||
|
||||
sourceIP,
|
||||
sourcePort,
|
||||
|
||||
destinationPort,
|
||||
|
||||
providerID,
|
||||
});
|
||||
|
||||
if (response.status != 200) {
|
||||
if (process.env.NODE_ENV != "production") console.log(response);
|
||||
|
||||
if (response.data.error) {
|
||||
println(`Error: ${response.data.error}\n`);
|
||||
} else {
|
||||
println("Error creating a connection!\n");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
println("Successfully created connection.\n");
|
||||
},
|
||||
);
|
||||
|
||||
const lookupCommand = new SSHCommand(println, "find");
|
||||
|
||||
|
@ -164,111 +179,124 @@ export async function run(
|
|||
"Description for the tunnel",
|
||||
);
|
||||
|
||||
lookupCommand.action(async(options: {
|
||||
backendId?: string,
|
||||
destPort?: string,
|
||||
name?: string,
|
||||
protocol?: string,
|
||||
source?: string,
|
||||
description?: string
|
||||
}) => {
|
||||
let numberBackendID: number | undefined;
|
||||
lookupCommand.action(
|
||||
async (options: {
|
||||
backendId?: string;
|
||||
destPort?: string;
|
||||
name?: string;
|
||||
protocol?: string;
|
||||
source?: string;
|
||||
description?: string;
|
||||
}) => {
|
||||
let numberBackendID: number | undefined;
|
||||
|
||||
let sourceIP: string | undefined;
|
||||
let sourcePort: number | undefined;
|
||||
let sourceIP: string | undefined;
|
||||
let sourcePort: number | undefined;
|
||||
|
||||
let destPort: number | undefined;
|
||||
let destPort: number | undefined;
|
||||
|
||||
if (options.backendId) {
|
||||
numberBackendID = parseInt(options.backendId);
|
||||
if (options.backendId) {
|
||||
numberBackendID = parseInt(options.backendId);
|
||||
|
||||
if (Number.isNaN(numberBackendID)) {
|
||||
println("ID (%s) is not a number\n", options.backendId);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (options.source) {
|
||||
const sourceSplit: string[] = options.source.split(":");
|
||||
|
||||
if (sourceSplit.length != 2) {
|
||||
return println(
|
||||
"Source could not be splitted down (are you missing the ':' in the source to specify port?)\n",
|
||||
);
|
||||
}
|
||||
|
||||
sourceIP = sourceSplit[0];
|
||||
sourcePort = parseInt(sourceSplit[1]);
|
||||
|
||||
if (Number.isNaN(sourcePort)) {
|
||||
return println("Port splitted is not a number\n");
|
||||
}
|
||||
}
|
||||
|
||||
if (options.destPort) {
|
||||
destPort = parseInt(options.destPort);
|
||||
|
||||
if (Number.isNaN(destPort)) {
|
||||
println("ID (%s) is not a number\n", options.destPort);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const response = await axios.post("/api/v1/forward/lookup", {
|
||||
token,
|
||||
|
||||
name: options.name,
|
||||
description: options.description,
|
||||
|
||||
protocol: options.protocol,
|
||||
|
||||
sourceIP,
|
||||
sourcePort,
|
||||
|
||||
destinationPort: destPort,
|
||||
});
|
||||
|
||||
if (response.status != 200) {
|
||||
if (process.env.NODE_ENV != "production") console.log(response);
|
||||
|
||||
if (response.data.error) {
|
||||
println(`Error: ${response.data.error}\n`);
|
||||
} else {
|
||||
println("Error requesting connections!\n");
|
||||
}
|
||||
|
||||
if (Number.isNaN(numberBackendID)) {
|
||||
println("ID (%s) is not a number\n", options.backendId);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (options.source) {
|
||||
const sourceSplit: string[] = options.source.split(":");
|
||||
const { data }: LookupCommandSuccess = response.data;
|
||||
|
||||
if (sourceSplit.length != 2) {
|
||||
return println("Source could not be splitted down (are you missing the ':' in the source to specify port?)\n");
|
||||
}
|
||||
|
||||
sourceIP = sourceSplit[0];
|
||||
sourcePort = parseInt(sourceSplit[1]);
|
||||
|
||||
if (Number.isNaN(sourcePort)) {
|
||||
return println("Port splitted is not a number\n");
|
||||
}
|
||||
}
|
||||
for (const connection of data) {
|
||||
println(
|
||||
"ID: %s%s:\n",
|
||||
connection.id,
|
||||
connection.autoStart ? " (automatically starts)" : "",
|
||||
);
|
||||
println(" - Backend ID: %s\n", connection.providerID);
|
||||
println(" - Name: %s\n", connection.name);
|
||||
if (connection.description)
|
||||
println(" - Description: %s\n", connection.description);
|
||||
println(
|
||||
" - Source: %s:%s\n",
|
||||
connection.sourceIP,
|
||||
connection.sourcePort,
|
||||
);
|
||||
println(" - Destination port: %s\n", connection.destPort);
|
||||
|
||||
if (options.destPort) {
|
||||
destPort = parseInt(options.destPort);
|
||||
|
||||
if (Number.isNaN(destPort)) {
|
||||
println("ID (%s) is not a number\n", options.destPort);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const response = await axios.post("/api/v1/forward/lookup", {
|
||||
token,
|
||||
|
||||
name: options.name,
|
||||
description: options.description,
|
||||
|
||||
protocol: options.protocol,
|
||||
|
||||
sourceIP,
|
||||
sourcePort,
|
||||
|
||||
destinationPort: destPort
|
||||
});
|
||||
|
||||
if (response.status != 200) {
|
||||
if (process.env.NODE_ENV != "production") console.log(response);
|
||||
|
||||
if (response.data.error) {
|
||||
println(`Error: ${response.data.error}\n`);
|
||||
} else {
|
||||
println("Error requesting connections!\n");
|
||||
println("\n");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const { data }: LookupCommandSuccess = response.data;
|
||||
|
||||
for (const connection of data) {
|
||||
println("ID: %s%s:\n", connection.id, (connection.autoStart ? " (automatically starts)" : ""));
|
||||
println(" - Backend ID: %s\n", connection.providerID);
|
||||
println(" - Name: %s\n", connection.name);
|
||||
if (connection.description) println(" - Description: %s\n", connection.description);
|
||||
println(" - Source: %s:%s\n", connection.sourceIP, connection.sourcePort);
|
||||
println(" - Destination port: %s\n", connection.destPort);
|
||||
|
||||
println("\n");
|
||||
}
|
||||
|
||||
println("%s connections found.\n", data.length);
|
||||
});
|
||||
println("%s connections found.\n", data.length);
|
||||
},
|
||||
);
|
||||
|
||||
const startTunnel = new SSHCommand(println, "start");
|
||||
startTunnel.description("Starts a tunnel");
|
||||
startTunnel.argument("<id>", "Tunnel ID to start");
|
||||
|
||||
startTunnel.action(async(idStr: string) => {
|
||||
startTunnel.action(async (idStr: string) => {
|
||||
const id = parseInt(idStr);
|
||||
|
||||
if (Number.isNaN(id)) {
|
||||
println("ID (%s) is not a number\n", idStr);
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
||||
const response = await axios.post("/api/v1/forward/start", {
|
||||
token,
|
||||
id
|
||||
id,
|
||||
});
|
||||
|
||||
if (response.status != 200) {
|
||||
|
@ -291,17 +319,17 @@ export async function run(
|
|||
stopTunnel.description("Stops a tunnel");
|
||||
stopTunnel.argument("<id>", "Tunnel ID to stop");
|
||||
|
||||
stopTunnel.action(async(idStr: string) => {
|
||||
stopTunnel.action(async (idStr: string) => {
|
||||
const id = parseInt(idStr);
|
||||
|
||||
if (Number.isNaN(id)) {
|
||||
println("ID (%s) is not a number\n", idStr);
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
||||
const response = await axios.post("/api/v1/forward/stop", {
|
||||
token,
|
||||
id
|
||||
id,
|
||||
});
|
||||
|
||||
if (response.status != 200) {
|
||||
|
@ -323,107 +351,130 @@ export async function run(
|
|||
getInbound.description("Shows all current connections");
|
||||
getInbound.argument("<id>", "Tunnel ID to view inbound connections of");
|
||||
getInbound.option("-t, --tail", "Live-view of connection list");
|
||||
getInbound.option("-s, --tail-pull-rate <ms>", "Controls the speed to pull at (in ms)");
|
||||
getInbound.option(
|
||||
"-s, --tail-pull-rate <ms>",
|
||||
"Controls the speed to pull at (in ms)",
|
||||
);
|
||||
|
||||
getInbound.action(async(idStr: string, options: {
|
||||
tail?: boolean,
|
||||
tailPullRate?: string
|
||||
}): Promise<void> => {
|
||||
const pullRate: number = options.tailPullRate ? parseInt(options.tailPullRate) : 2000;
|
||||
const id = parseInt(idStr);
|
||||
getInbound.action(
|
||||
async (
|
||||
idStr: string,
|
||||
options: {
|
||||
tail?: boolean;
|
||||
tailPullRate?: string;
|
||||
},
|
||||
): Promise<void> => {
|
||||
const pullRate: number = options.tailPullRate
|
||||
? parseInt(options.tailPullRate)
|
||||
: 2000;
|
||||
const id = parseInt(idStr);
|
||||
|
||||
if (Number.isNaN(id)) {
|
||||
println("ID (%s) is not a number\n", idStr);
|
||||
return;
|
||||
}
|
||||
if (Number.isNaN(id)) {
|
||||
println("ID (%s) is not a number\n", idStr);
|
||||
return;
|
||||
}
|
||||
|
||||
if (Number.isNaN(pullRate)) {
|
||||
println("Pull rate is not a number\n");
|
||||
return;
|
||||
}
|
||||
if (Number.isNaN(pullRate)) {
|
||||
println("Pull rate is not a number\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (options.tail) {
|
||||
let previousEntries: string[] = [];
|
||||
if (options.tail) {
|
||||
let previousEntries: string[] = [];
|
||||
|
||||
while (true) {
|
||||
// FIXME?
|
||||
// eslint-disable-next-line no-constant-condition
|
||||
while (true) {
|
||||
const response = await axios.post("/api/v1/forward/connections", {
|
||||
token,
|
||||
id,
|
||||
});
|
||||
|
||||
if (response.status != 200) {
|
||||
if (process.env.NODE_ENV != "production") console.log(response);
|
||||
|
||||
if (response.data.error) {
|
||||
println(`Error: ${response.data.error}\n`);
|
||||
} else {
|
||||
println("Error requesting inbound connections!\n");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const { data }: InboundConnectionSuccess = response.data;
|
||||
const simplifiedArray: string[] = data.map(i => `${i.ip}:${i.port}`);
|
||||
|
||||
const insertedItems: string[] = difference(
|
||||
simplifiedArray,
|
||||
previousEntries,
|
||||
);
|
||||
|
||||
const removedItems: string[] = difference(
|
||||
previousEntries,
|
||||
simplifiedArray,
|
||||
);
|
||||
|
||||
insertedItems.forEach(i => println("CONNECTED: %s\n", i));
|
||||
removedItems.forEach(i => println("DISCONNECTED: %s\n", i));
|
||||
|
||||
previousEntries = simplifiedArray;
|
||||
|
||||
await new Promise(i => setTimeout(i, pullRate));
|
||||
}
|
||||
} else {
|
||||
const response = await axios.post("/api/v1/forward/connections", {
|
||||
token,
|
||||
id
|
||||
id,
|
||||
});
|
||||
|
||||
if (response.status != 200) {
|
||||
if (process.env.NODE_ENV != "production") console.log(response);
|
||||
|
||||
|
||||
if (response.data.error) {
|
||||
println(`Error: ${response.data.error}\n`);
|
||||
} else {
|
||||
println("Error requesting inbound connections!\n");
|
||||
println("Error requesting connections!\n");
|
||||
}
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const { data }: InboundConnectionSuccess = response.data;
|
||||
const simplifiedArray: string[] = data.map((i) => `${i.ip}:${i.port}`);
|
||||
|
||||
const insertedItems: string[] = difference(simplifiedArray, previousEntries);
|
||||
const removedItems: string[] = difference(previousEntries, simplifiedArray);
|
||||
|
||||
insertedItems.forEach((i) => println("CONNECTED: %s\n", i));
|
||||
removedItems.forEach((i) => println("DISCONNECTED: %s\n", i));
|
||||
|
||||
previousEntries = simplifiedArray;
|
||||
|
||||
await new Promise((i) => setTimeout(i, pullRate));
|
||||
}
|
||||
} else {
|
||||
const response = await axios.post("/api/v1/forward/connections", {
|
||||
token,
|
||||
id
|
||||
});
|
||||
|
||||
if (response.status != 200) {
|
||||
if (process.env.NODE_ENV != "production") console.log(response);
|
||||
|
||||
if (response.data.error) {
|
||||
println(`Error: ${response.data.error}\n`);
|
||||
} else {
|
||||
println("Error requesting connections!\n");
|
||||
if (data.length == 0) {
|
||||
println("There are currently no connected clients.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
println(
|
||||
"Connected clients (for source: %s:%s):\n",
|
||||
data[0].connectionDetails.sourceIP,
|
||||
data[0].connectionDetails.sourcePort,
|
||||
);
|
||||
|
||||
for (const entry of data) {
|
||||
println(" - %s:%s\n", entry.ip, entry.port);
|
||||
}
|
||||
}
|
||||
|
||||
const { data }: InboundConnectionSuccess = response.data;
|
||||
|
||||
if (data.length == 0) {
|
||||
println("There are currently no connected clients.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
println("Connected clients (for source: %s:%s):\n", data[0].connectionDetails.sourceIP, data[0].connectionDetails.sourcePort);
|
||||
|
||||
for (const entry of data) {
|
||||
println(" - %s:%s\n", entry.ip, entry.port);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
const removeTunnel = new SSHCommand(println, "rm");
|
||||
removeTunnel.description("Removes a tunnel");
|
||||
removeTunnel.argument("<id>", "Tunnel ID to remove");
|
||||
|
||||
removeTunnel.action(async(idStr: string) => {
|
||||
removeTunnel.action(async (idStr: string) => {
|
||||
const id = parseInt(idStr);
|
||||
|
||||
if (Number.isNaN(id)) {
|
||||
println("ID (%s) is not a number\n", idStr);
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
||||
const response = await axios.post("/api/v1/forward/remove", {
|
||||
token,
|
||||
id
|
||||
id,
|
||||
});
|
||||
|
||||
if (response.status != 200) {
|
||||
|
@ -436,7 +487,7 @@ export async function run(
|
|||
}
|
||||
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
||||
println("Successfully deleted connection.\n");
|
||||
});
|
||||
|
@ -449,5 +500,5 @@ export async function run(
|
|||
program.addCommand(removeTunnel);
|
||||
|
||||
program.parse(argv);
|
||||
await new Promise((resolve) => program.onExit(resolve));
|
||||
await new Promise(resolve => program.onExit(resolve));
|
||||
}
|
||||
|
|
|
@ -6,11 +6,11 @@ import type { PrintLine, KeyboardRead } from "../commands.js";
|
|||
type UserLookupSuccess = {
|
||||
success: true;
|
||||
data: {
|
||||
id: number,
|
||||
isServiceAccount: boolean,
|
||||
username: string,
|
||||
name: string,
|
||||
email: string
|
||||
id: number;
|
||||
isServiceAccount: boolean;
|
||||
username: string;
|
||||
name: string;
|
||||
email: string;
|
||||
}[];
|
||||
};
|
||||
|
||||
|
@ -19,9 +19,13 @@ export async function run(
|
|||
println: PrintLine,
|
||||
axios: Axios,
|
||||
apiKey: string,
|
||||
readKeyboard: KeyboardRead
|
||||
readKeyboard: KeyboardRead,
|
||||
) {
|
||||
if (argv.length == 1) return println("error: no arguments specified! run %s --help to see commands.\n", argv[0]);
|
||||
if (argv.length == 1)
|
||||
return println(
|
||||
"error: no arguments specified! run %s --help to see commands.\n",
|
||||
argv[0],
|
||||
);
|
||||
|
||||
const program = new SSHCommand(println);
|
||||
program.description("Manages users for NextNet");
|
||||
|
@ -39,69 +43,75 @@ export async function run(
|
|||
"Asks for a password. Hides output",
|
||||
);
|
||||
|
||||
addCommand.action(async(username: string, email: string, name: string, options: {
|
||||
password?: string,
|
||||
askPassword?: boolean
|
||||
}) => {
|
||||
if (!options.password && !options.askPassword) {
|
||||
println("No password supplied, and askpass has not been supplied.\n");
|
||||
return;
|
||||
};
|
||||
addCommand.action(
|
||||
async (
|
||||
username: string,
|
||||
email: string,
|
||||
name: string,
|
||||
options: {
|
||||
password?: string;
|
||||
askPassword?: boolean;
|
||||
},
|
||||
) => {
|
||||
if (!options.password && !options.askPassword) {
|
||||
println("No password supplied, and askpass has not been supplied.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
let password: string = "";
|
||||
let password: string = "";
|
||||
|
||||
if (options.askPassword) {
|
||||
let passwordConfirmOne = "a";
|
||||
let passwordConfirmTwo = "b";
|
||||
if (options.askPassword) {
|
||||
let passwordConfirmOne = "a";
|
||||
let passwordConfirmTwo = "b";
|
||||
|
||||
while (passwordConfirmOne != passwordConfirmTwo) {
|
||||
println("Password: ");
|
||||
passwordConfirmOne = await readKeyboard(true);
|
||||
|
||||
println("\nConfirm password: ");
|
||||
passwordConfirmTwo = await readKeyboard(true);
|
||||
while (passwordConfirmOne != passwordConfirmTwo) {
|
||||
println("Password: ");
|
||||
passwordConfirmOne = await readKeyboard(true);
|
||||
|
||||
println("\n");
|
||||
println("\nConfirm password: ");
|
||||
passwordConfirmTwo = await readKeyboard(true);
|
||||
|
||||
if (passwordConfirmOne != passwordConfirmTwo) {
|
||||
println("Passwords do not match! Try again.\n\n");
|
||||
println("\n");
|
||||
|
||||
if (passwordConfirmOne != passwordConfirmTwo) {
|
||||
println("Passwords do not match! Try again.\n\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
password = passwordConfirmOne;
|
||||
} else {
|
||||
// From the first check we do, we know this is safe (you MUST specify a password)
|
||||
// @ts-ignore
|
||||
password = options.password;
|
||||
}
|
||||
|
||||
const response = await axios.post("/api/v1/users/create", {
|
||||
name,
|
||||
username,
|
||||
email,
|
||||
password
|
||||
});
|
||||
|
||||
if (response.status != 200) {
|
||||
if (process.env.NODE_ENV != "production") console.log(response);
|
||||
|
||||
if (response.data.error) {
|
||||
println(`Error: ${response.data.error}\n`);
|
||||
password = passwordConfirmOne;
|
||||
} else {
|
||||
println("Error creating users!\n");
|
||||
// @ts-expect-error: From the first check we do, we know this is safe (you MUST specify a password)
|
||||
password = options.password;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
const response = await axios.post("/api/v1/users/create", {
|
||||
name,
|
||||
username,
|
||||
email,
|
||||
password,
|
||||
});
|
||||
|
||||
println("User created successfully.\n");
|
||||
})
|
||||
if (response.status != 200) {
|
||||
if (process.env.NODE_ENV != "production") console.log(response);
|
||||
|
||||
if (response.data.error) {
|
||||
println(`Error: ${response.data.error}\n`);
|
||||
} else {
|
||||
println("Error creating users!\n");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
println("User created successfully.\n");
|
||||
},
|
||||
);
|
||||
|
||||
const removeCommand = new SSHCommand(println, "rm");
|
||||
removeCommand.description("Remove a user");
|
||||
removeCommand.argument("<uid>", "ID of user to remove");
|
||||
|
||||
removeCommand.action(async(uidStr: string) => {
|
||||
removeCommand.action(async (uidStr: string) => {
|
||||
const uid = parseInt(uidStr);
|
||||
|
||||
if (Number.isNaN(uid)) {
|
||||
|
@ -109,9 +119,9 @@ 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
|
||||
uid,
|
||||
});
|
||||
|
||||
if (response.status != 200) {
|
||||
|
@ -125,7 +135,7 @@ export async function run(
|
|||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
println("User has been successfully deleted.\n");
|
||||
});
|
||||
|
||||
|
@ -137,7 +147,7 @@ export async function run(
|
|||
lookupCommand.option("-e, --email <email>", "Email of User");
|
||||
lookupCommand.option("-s, --service", "The user is a service account");
|
||||
|
||||
lookupCommand.action(async(options) => {
|
||||
lookupCommand.action(async options => {
|
||||
// FIXME: redundant parseInt calls
|
||||
|
||||
if (options.id) {
|
||||
|
@ -146,7 +156,7 @@ export async function run(
|
|||
if (Number.isNaN(uid)) {
|
||||
println("UID (%s) is not a number.\n", uid);
|
||||
return;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const response = await axios.post("/api/v1/users/lookup", {
|
||||
|
@ -155,7 +165,7 @@ export async function run(
|
|||
name: options.name,
|
||||
username: options.username,
|
||||
email: options.email,
|
||||
service: Boolean(options.service)
|
||||
service: Boolean(options.service),
|
||||
});
|
||||
|
||||
if (response.status != 200) {
|
||||
|
@ -173,7 +183,11 @@ export async function run(
|
|||
const { data }: UserLookupSuccess = response.data;
|
||||
|
||||
for (const user of data) {
|
||||
println("UID: %s%s:\n", user.id, (user.isServiceAccount ? " (service)" : ""));
|
||||
println(
|
||||
"UID: %s%s:\n",
|
||||
user.id,
|
||||
user.isServiceAccount ? " (service)" : "",
|
||||
);
|
||||
println("- Username: %s\n", user.username);
|
||||
println("- Name: %s\n", user.name);
|
||||
println("- Email: %s\n", user.email);
|
||||
|
@ -182,12 +196,12 @@ export async function run(
|
|||
}
|
||||
|
||||
println("%s users found.\n", data.length);
|
||||
})
|
||||
});
|
||||
|
||||
program.addCommand(addCommand);
|
||||
program.addCommand(removeCommand);
|
||||
program.addCommand(lookupCommand);
|
||||
|
||||
program.parse(argv);
|
||||
await new Promise((resolve) => program.onExit(resolve));
|
||||
await new Promise(resolve => program.onExit(resolve));
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ const serverBaseURL: string =
|
|||
|
||||
const axios = baseAxios.create({
|
||||
baseURL: serverBaseURL,
|
||||
validateStatus: () => true
|
||||
validateStatus: () => true,
|
||||
});
|
||||
|
||||
try {
|
||||
|
@ -39,13 +39,15 @@ if (!keyFile) throw new Error("Somehow failed to fetch the key file!");
|
|||
|
||||
const server: ssh2.Server = new ssh2.Server({
|
||||
hostKeys: [keyFile],
|
||||
banner: "NextNet-LOM (c) NextNet project et al."
|
||||
banner: "NextNet-LOM (c) NextNet project et al.",
|
||||
});
|
||||
|
||||
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 => {
|
||||
|
@ -54,11 +56,11 @@ server.on("connection", client => {
|
|||
username: auth.username,
|
||||
password: auth.password,
|
||||
});
|
||||
|
||||
|
||||
if (response.status == 403) {
|
||||
return auth.reject(["password"]);
|
||||
}
|
||||
|
||||
|
||||
token = response.data.token;
|
||||
|
||||
username = auth.username;
|
||||
|
@ -69,44 +71,44 @@ server.on("connection", client => {
|
|||
return auth.reject();
|
||||
// todo
|
||||
} else {
|
||||
return auth.reject(["password", "publickey"]);
|
||||
return auth.reject(["password", "publickey"]);
|
||||
}
|
||||
});
|
||||
|
||||
client.on("ready", () => {
|
||||
client.on("session", (accept, reject) => {
|
||||
client.on("session", accept => {
|
||||
const conn = accept();
|
||||
|
||||
conn.on("exec", async (accept, reject, info) => {
|
||||
const stream = accept();
|
||||
|
||||
// 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);
|
||||
|
||||
|
||||
if (argv[0] == "exit") {
|
||||
stream.close();
|
||||
} else {
|
||||
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),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return stream.close();
|
||||
});
|
||||
|
||||
|
@ -123,26 +125,28 @@ 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");
|
||||
|
||||
|
||||
if (line == "") {
|
||||
stream.write(`~$ `);
|
||||
continue;
|
||||
}
|
||||
|
||||
const argv = parseArgsStringToArgv(line);
|
||||
|
||||
|
||||
if (argv[0] == "exit") {
|
||||
stream.close();
|
||||
} else {
|
||||
const command = commands.find(i => i.name == argv[0]);
|
||||
|
||||
|
||||
if (!command) {
|
||||
stream.write(
|
||||
`Unknown command ${argv[0]}. Run 'help' to see commands.\r\n~$ `,
|
||||
|
@ -150,8 +154,10 @@ 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("~$ ");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ export class SSHCommand extends Command {
|
|||
hasRecievedExitSignal: boolean;
|
||||
println: PrintLine;
|
||||
|
||||
exitEventHandlers: ((...any: any[]) => void)[];
|
||||
exitEventHandlers: ((...any: unknown[]) => void)[];
|
||||
parent: SSHCommand | null;
|
||||
|
||||
/**
|
||||
|
@ -53,22 +53,22 @@ export class SSHCommand extends Command {
|
|||
|
||||
recvExitDispatch() {
|
||||
this.hasRecievedExitSignal = true;
|
||||
this.exitEventHandlers.forEach((eventHandler) => eventHandler());
|
||||
this.exitEventHandlers.forEach(eventHandler => eventHandler());
|
||||
|
||||
let parentElement = this.parent;
|
||||
|
||||
while (parentElement instanceof SSHCommand) {
|
||||
parentElement.hasRecievedExitSignal = true;
|
||||
parentElement.exitEventHandlers.forEach((eventHandler) => eventHandler());
|
||||
parentElement.exitEventHandlers.forEach(eventHandler => eventHandler());
|
||||
|
||||
parentElement = parentElement.parent;
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
onExit(callback: (...any: any[]) => void) {
|
||||
this.exitEventHandlers.push(callback);
|
||||
if (this.hasRecievedExitSignal) callback();
|
||||
};
|
||||
}
|
||||
|
||||
_exit() {
|
||||
this.recvExitDispatch();
|
||||
|
@ -81,11 +81,11 @@ export class SSHCommand extends Command {
|
|||
action(fn: (...args: any[]) => void | Promise<void>): this {
|
||||
super.action(fn);
|
||||
|
||||
// @ts-ignore
|
||||
// @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-ignore
|
||||
// @ts-expect-error: Overriding private parameters (but this works)
|
||||
this._actionHandler = async (...args: any[]): Promise<void> => {
|
||||
if (this.hasRecievedExitSignal) return;
|
||||
await oldActionHandler(...args);
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
import type { ServerChannel } from "ssh2";
|
||||
|
||||
const pullRate = process.env.KEYBOARD_PULLING_RATE ? parseInt(process.env.KEYBOARD_PULLING_RATE) : 5;
|
||||
const pullRate = process.env.KEYBOARD_PULLING_RATE
|
||||
? parseInt(process.env.KEYBOARD_PULLING_RATE)
|
||||
: 5;
|
||||
|
||||
const leftEscape = "\x1B[D";
|
||||
const rightEscape = "\x1B[C";
|
||||
|
@ -33,19 +35,20 @@ export async function readFromKeyboard(
|
|||
} else if (character == clientBackspace) {
|
||||
if (line.length == 0) return setTimeout(eventLoop, pullRate); // Here because if we do it in the parent if statement, shit breaks
|
||||
line = line.substring(0, lineIndex - 1) + line.substring(lineIndex);
|
||||
|
||||
|
||||
if (!disableEcho) {
|
||||
const deltaCursor = line.length - lineIndex;
|
||||
|
||||
if (deltaCursor == line.length) return setTimeout(eventLoop, pullRate);
|
||||
|
||||
|
||||
if (deltaCursor == line.length)
|
||||
return setTimeout(eventLoop, pullRate);
|
||||
|
||||
if (deltaCursor < 0) {
|
||||
// Use old technique if the delta is < 0, as the new one is tailored to the start + 1 to end - 1
|
||||
stream.write(ourBackspace + " " + ourBackspace);
|
||||
} else {
|
||||
// Jump forward to the front, and remove the last character
|
||||
stream.write(rightEscape.repeat(deltaCursor) + " " + ourBackspace);
|
||||
|
||||
|
||||
// Go backwards & rerender text & go backwards again (wtf?)
|
||||
stream.write(
|
||||
leftEscape.repeat(deltaCursor + 1) +
|
||||
|
@ -53,12 +56,13 @@ export async function readFromKeyboard(
|
|||
leftEscape.repeat(deltaCursor + 1),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
lineIndex -= 1;
|
||||
}
|
||||
} else if (character == "\x1B") {
|
||||
if (character == rightEscape) {
|
||||
if (lineIndex + 1 > line.length) return setTimeout(eventLoop, pullRate);
|
||||
if (lineIndex + 1 > line.length)
|
||||
return setTimeout(eventLoop, pullRate);
|
||||
lineIndex += 1;
|
||||
} else if (character == leftEscape) {
|
||||
if (lineIndex - 1 < 0) return setTimeout(eventLoop, pullRate);
|
||||
|
@ -66,20 +70,20 @@ export async function readFromKeyboard(
|
|||
} else {
|
||||
return setTimeout(eventLoop, pullRate);
|
||||
}
|
||||
|
||||
|
||||
if (!disableEcho) stream.write(character);
|
||||
} else {
|
||||
lineIndex += 1;
|
||||
|
||||
|
||||
// There isn't a splice method for String prototypes. So, ugh:
|
||||
line =
|
||||
line.substring(0, lineIndex - 1) +
|
||||
character +
|
||||
line.substring(lineIndex - 1);
|
||||
|
||||
|
||||
if (!disableEcho) {
|
||||
let deltaCursor = line.length - lineIndex;
|
||||
|
||||
|
||||
// wtf?
|
||||
if (deltaCursor < 0) {
|
||||
console.log(
|
||||
|
@ -87,7 +91,7 @@ export async function readFromKeyboard(
|
|||
);
|
||||
deltaCursor = 0;
|
||||
}
|
||||
|
||||
|
||||
stream.write(
|
||||
line.substring(lineIndex - 1) + leftEscape.repeat(deltaCursor),
|
||||
);
|
||||
|
@ -96,7 +100,7 @@ export async function readFromKeyboard(
|
|||
}
|
||||
|
||||
setTimeout(eventLoop, pullRate);
|
||||
};
|
||||
}
|
||||
|
||||
return new Promise(resolve => {
|
||||
setTimeout(eventLoop, pullRate);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue