Skip to content

Commit

Permalink
chore: update dependencies and migrate to ES modules #383
Browse files Browse the repository at this point in the history
  • Loading branch information
billchurch committed Dec 14, 2024
1 parent 626e581 commit 56a6ce1
Show file tree
Hide file tree
Showing 23 changed files with 355 additions and 322 deletions.
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

99 changes: 0 additions & 99 deletions .eslintrc.js

This file was deleted.

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ssl/*

bigip/*

config.json
# config.json

# Logs
logs
Expand Down
23 changes: 12 additions & 11 deletions app/app.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// server
// app/app.js

const express = require("express")
const config = require("./config")
const socketHandler = require("./socket")
const sshRoutes = require("./routes")(config)
const { applyMiddleware } = require("./middleware")
const { createServer, startServer } = require("./server")
const { configureSocketIO } = require("./io")
const { handleError, ConfigError } = require("./errors")
const { createNamespacedDebug } = require("./logger")
const { DEFAULTS, MESSAGES } = require("./constants")
import express from 'express'
import config from './config.js'
import socketHandler from './socket.js'
import { createRoutes } from './routes.js'
import { applyMiddleware } from './middleware.js'
import { createServer, startServer } from './server.js'
import { configureSocketIO } from './io.js'
import { handleError, ConfigError } from './errors.js'
import { createNamespacedDebug } from './logger.js'
import { DEFAULTS, MESSAGES } from './constants.js'

const debug = createNamespacedDebug("app")
const sshRoutes = createRoutes(config)

/**
* Creates and configures the Express application
Expand Down Expand Up @@ -67,4 +68,4 @@ function initializeServer() {
}
}

module.exports = { initializeServer: initializeServer, config: config }
export { initializeServer, config }
29 changes: 17 additions & 12 deletions app/config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// server
// app/config.js

const path = require("path")
const fs = require("fs")
const readConfig = require("read-config-ng")
const { deepMerge, validateConfig } = require("./utils")
const { generateSecureSecret } = require("./crypto-utils")
const { createNamespacedDebug } = require("./logger")
const { ConfigError, handleError } = require("./errors")
const { DEFAULTS } = require("./constants")
import path from 'path'
import fs from 'fs'
import readConfig from 'read-config-ng'
import { deepMerge, validateConfig } from './utils.js'
import { generateSecureSecret } from './crypto-utils.js'
import { createNamespacedDebug } from './logger.js'
import { ConfigError, handleError } from './errors.js'
import { DEFAULTS } from './constants.js'

const debug = createNamespacedDebug("config")

Expand Down Expand Up @@ -78,9 +78,14 @@ const defaultConfig = {
}
}

import { fileURLToPath } from 'url'
import { dirname } from 'path'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

function getConfigPath() {
const nodeRoot = path.dirname(require.main.filename)
return path.join(nodeRoot, "config.json")
return path.join(__dirname, "..", "config.json")
}

function loadConfig() {
Expand Down Expand Up @@ -165,7 +170,7 @@ function getCorsConfig() {
}
}

// Extend the config object with the getCorsConfig function
// Add getCorsConfig to the config object
config.getCorsConfig = getCorsConfig

module.exports = config
export default config
3 changes: 2 additions & 1 deletion app/configSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,5 @@ const configSchema = {
},
required: ["listen", "http", "user", "ssh", "header", "options"]
}
module.exports = configSchema

export default configSchema
16 changes: 10 additions & 6 deletions app/connectionHandler.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// server
// app/connectionHandler.js

const fs = require("fs")
const path = require("path")
const { createNamespacedDebug } = require("./logger")
const { HTTP, MESSAGES, DEFAULTS } = require("./constants")
const { modifyHtml } = require("./utils")
import { fileURLToPath } from 'url'
import { dirname } from 'path'
import fs from "fs"
import path from "path"
import { createNamespacedDebug } from "./logger.js"
import { HTTP, MESSAGES, DEFAULTS } from "./constants.js"
import { modifyHtml } from "./utils.js"

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const debug = createNamespacedDebug("connectionHandler")

/**
Expand Down Expand Up @@ -58,4 +62,4 @@ function handleConnection(req, res) {
handleFileRead(filePath, tempConfig, res)
}

module.exports = handleConnection
export default handleConnection
28 changes: 13 additions & 15 deletions app/constants.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// server
// app/constants.js

const path = require("path")
import { fileURLToPath } from 'url'
import { dirname } from 'path'
import path from 'path'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

/**
* Error messages
*/
const MESSAGES = {
export const MESSAGES = {
INVALID_CREDENTIALS: "Invalid credentials format",
SSH_CONNECTION_ERROR: "SSH CONNECTION ERROR",
SSH_CONNECTION_ERROR: "SSH CONNECTION ERROR",
SHELL_ERROR: "SHELL ERROR",
CONFIG_ERROR: "CONFIG_ERROR",
UNEXPECTED_ERROR: "An unexpected error occurred",
Expand All @@ -21,29 +26,28 @@ const MESSAGES = {
/**
* Default values
*/
const DEFAULTS = {
export const DEFAULTS = {
SSH_PORT: 22,
LISTEN_PORT: 2222,
SSH_TERM: "xterm-color",
IO_PING_TIMEOUT: 60000, // 1 minute
IO_PING_INTERVAL: 25000, // 25 seconds
IO_PING_TIMEOUT: 60000,
IO_PING_INTERVAL: 25000,
IO_PATH: "/ssh/socket.io",
WEBSSH2_CLIENT_PATH: path.resolve(
__dirname,
"..",
"node_modules",
"webssh2_client",
"client",
"client",
"public"
),
CLIENT_FILE: "client.htm",
MAX_AUTH_ATTEMPTS: 2
}

/**
* HTTP Related
*/
const HTTP = {
export const HTTP = {
OK: 200,
UNAUTHORIZED: 401,
INTERNAL_SERVER_ERROR: 500,
Expand All @@ -56,9 +60,3 @@ const HTTP = {
SESSION_SID: "webssh2_sid",
CREDS_CLEARED: "Credentials cleared."
}

module.exports = {
MESSAGES,
DEFAULTS,
HTTP
}
9 changes: 2 additions & 7 deletions app/crypto-utils.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
// server
// app/crypto-utils.js

const crypto = require("crypto")

import crypto from "crypto"
/**
* Generates a secure random session secret
* @returns {string} A random 32-byte hex string
*/
function generateSecureSecret() {
export function generateSecureSecret() {
return crypto.randomBytes(32).toString("hex")
}

module.exports = {
generateSecureSecret
}
41 changes: 17 additions & 24 deletions app/errors.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// server
// app/errors.js

const util = require("util")
const { logError, createNamespacedDebug } = require("./logger")
const { HTTP, MESSAGES } = require("./constants")
import { logError, createNamespacedDebug } from './logger.js'
import { HTTP, MESSAGES } from './constants.js'

const debug = createNamespacedDebug("errors")

Expand All @@ -12,35 +11,34 @@ const debug = createNamespacedDebug("errors")
* @param {string} message - The error message
* @param {string} code - The error code
*/
function WebSSH2Error(message, code) {
Error.captureStackTrace(this, this.constructor)
this.name = this.constructor.name
this.message = message
this.code = code
class WebSSH2Error extends Error {
constructor(message, code) {
super(message)
this.name = this.constructor.name
this.code = code
}
}

util.inherits(WebSSH2Error, Error)

/**
* Custom error for configuration issues
* @param {string} message - The error message
*/
function ConfigError(message) {
WebSSH2Error.call(this, message, MESSAGES.CONFIG_ERROR)
class ConfigError extends WebSSH2Error {
constructor(message) {
super(message, MESSAGES.CONFIG_ERROR)
}
}

util.inherits(ConfigError, WebSSH2Error)

/**
* Custom error for SSH connection issues
* @param {string} message - The error message
*/
function SSHConnectionError(message) {
WebSSH2Error.call(this, message, MESSAGES.SSH_CONNECTION_ERROR)
class SSHConnectionError extends WebSSH2Error {
constructor(message) {
super(message, MESSAGES.SSH_CONNECTION_ERROR)
}
}

util.inherits(SSHConnectionError, WebSSH2Error)

/**
* Handles an error by logging it and optionally sending a response
* @param {Error} err - The error to handle
Expand All @@ -66,9 +64,4 @@ function handleError(err, res) {
}
}

module.exports = {
WebSSH2Error: WebSSH2Error,
ConfigError: ConfigError,
SSHConnectionError: SSHConnectionError,
handleError: handleError
}
export { WebSSH2Error, ConfigError, SSHConnectionError, handleError }
Loading

0 comments on commit 56a6ce1

Please sign in to comment.