Skip to content

Commit

Permalink
chore: update config, routes, middleware, and other files to es style…
Browse files Browse the repository at this point in the history
… modules #383
  • Loading branch information
billchurch committed Dec 14, 2024
1 parent 56a6ce1 commit db891ec
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 379 deletions.
12 changes: 5 additions & 7 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { handleError, ConfigError } from './errors.js'
import { createNamespacedDebug } from './logger.js'
import { DEFAULTS, MESSAGES } from './constants.js'

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

/**
Expand All @@ -30,16 +30,14 @@ function createApp() {
const { sessionMiddleware } = applyMiddleware(app, config)

// Serve static files from the webssh2_client module with a custom prefix
app.use("/ssh/assets", express.static(clientPath))
app.use('/ssh/assets', express.static(clientPath))

// Use the SSH routes
app.use("/ssh", sshRoutes)
app.use('/ssh', sshRoutes)

return { app: app, sessionMiddleware: sessionMiddleware }
} catch (err) {
throw new ConfigError(
`${MESSAGES.EXPRESS_APP_CONFIG_ERROR}: ${err.message}`
)
throw new ConfigError(`${MESSAGES.EXPRESS_APP_CONFIG_ERROR}: ${err.message}`)
}
}

Expand All @@ -59,7 +57,7 @@ function initializeServer() {
// Start the server
startServer(server, config)

debug("Server initialized")
debug('Server initialized')

return { server: server, io: io, app: app }
} catch (err) {
Expand Down
81 changes: 38 additions & 43 deletions app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import { createNamespacedDebug } from './logger.js'
import { ConfigError, handleError } from './errors.js'
import { DEFAULTS } from './constants.js'

const debug = createNamespacedDebug("config")
const debug = createNamespacedDebug('config')

const defaultConfig = {
listen: {
ip: "0.0.0.0",
port: DEFAULTS.LISTEN_PORT
ip: '0.0.0.0',
port: DEFAULTS.LISTEN_PORT,
},
http: {
origins: ["*:*"]
origins: ['*:*'],
},
user: {
name: null,
password: null
password: null,
},
ssh: {
host: null,
Expand All @@ -35,47 +35,47 @@ const defaultConfig = {
disableInteractiveAuth: false,
algorithms: {
cipher: [
"aes128-ctr",
"aes192-ctr",
"aes256-ctr",
"aes128-gcm",
"[email protected]",
"aes256-gcm",
"[email protected]",
"aes256-cbc"
'aes128-ctr',
'aes192-ctr',
'aes256-ctr',
'aes128-gcm',
'[email protected]',
'aes256-gcm',
'[email protected]',
'aes256-cbc',
],
compress: ["none", "[email protected]", "zlib"],
hmac: ["hmac-sha2-256", "hmac-sha2-512", "hmac-sha1"],
compress: ['none', '[email protected]', 'zlib'],
hmac: ['hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1'],
kex: [
"ecdh-sha2-nistp256",
"ecdh-sha2-nistp384",
"ecdh-sha2-nistp521",
"diffie-hellman-group-exchange-sha256",
"diffie-hellman-group14-sha1"
'ecdh-sha2-nistp256',
'ecdh-sha2-nistp384',
'ecdh-sha2-nistp521',
'diffie-hellman-group-exchange-sha256',
'diffie-hellman-group14-sha1',
],
serverHostKey: [
"ecdsa-sha2-nistp256",
"ecdsa-sha2-nistp384",
"ecdsa-sha2-nistp521",
"ssh-rsa"
]
}
'ecdsa-sha2-nistp256',
'ecdsa-sha2-nistp384',
'ecdsa-sha2-nistp521',
'ssh-rsa',
],
},
},
header: {
text: null,
background: "green"
background: 'green',
},
options: {
challengeButton: true,
autoLog: false,
allowReauth: true,
allowReconnect: true,
allowReplay: true
allowReplay: true,
},
session: {
secret: process.env.WEBSSH_SESSION_SECRET || generateSecureSecret(),
name: "webssh2.sid"
}
name: 'webssh2.sid',
},
}

import { fileURLToPath } from 'url'
Expand All @@ -85,7 +85,7 @@ const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

function getConfigPath() {
return path.join(__dirname, "..", "config.json")
return path.join(__dirname, '..', 'config.json')
}

function loadConfig() {
Expand All @@ -94,26 +94,21 @@ function loadConfig() {
try {
if (fs.existsSync(configPath)) {
const providedConfig = readConfig.sync(configPath)
const mergedConfig = deepMerge(
JSON.parse(JSON.stringify(defaultConfig)),
providedConfig
)
const mergedConfig = deepMerge(JSON.parse(JSON.stringify(defaultConfig)), providedConfig)

if (process.env.PORT) {
mergedConfig.listen.port = parseInt(process.env.PORT, 10)
debug("Using PORT from environment: %s", mergedConfig.listen.port)
debug('Using PORT from environment: %s', mergedConfig.listen.port)
}

const validatedConfig = validateConfig(mergedConfig)
debug("Merged and validated configuration")
debug('Merged and validated configuration')
return validatedConfig
}
debug("Missing config.json for webssh. Using default config")
debug('Missing config.json for webssh. Using default config')
return defaultConfig
} catch (err) {
const error = new ConfigError(
`Problem loading config.json for webssh: ${err.message}`
)
const error = new ConfigError(`Problem loading config.json for webssh: ${err.message}`)
handleError(error)
return defaultConfig
}
Expand Down Expand Up @@ -165,8 +160,8 @@ const config = loadConfig()
function getCorsConfig() {
return {
origin: config.http.origins,
methods: ["GET", "POST"],
credentials: true
methods: ['GET', 'POST'],
credentials: true,
}
}

Expand Down
115 changes: 54 additions & 61 deletions app/configSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,109 +2,102 @@
* Schema for validating the config
*/
const configSchema = {
type: "object",
type: 'object',
properties: {
listen: {
type: "object",
type: 'object',
properties: {
ip: { type: "string", format: "ipv4" },
port: { type: "integer", minimum: 1, maximum: 65535 }
ip: { type: 'string', format: 'ipv4' },
port: { type: 'integer', minimum: 1, maximum: 65535 },
},
required: ["ip", "port"]
required: ['ip', 'port'],
},
http: {
type: "object",
type: 'object',
properties: {
origins: {
type: "array",
items: { type: "string" }
}
type: 'array',
items: { type: 'string' },
},
},
required: ["origins"]
required: ['origins'],
},
user: {
type: "object",
type: 'object',
properties: {
name: { type: ["string", "null"] },
password: { type: ["string", "null"] },
privateKey: { type: ["string", "null"] }
name: { type: ['string', 'null'] },
password: { type: ['string', 'null'] },
privateKey: { type: ['string', 'null'] },
},
required: ["name", "password"]
required: ['name', 'password'],
},
ssh: {
type: "object",
type: 'object',
properties: {
host: { type: ["string", "null"] },
port: { type: "integer", minimum: 1, maximum: 65535 },
term: { type: "string" },
readyTimeout: { type: "integer" },
keepaliveInterval: { type: "integer" },
keepaliveCountMax: { type: "integer" },
host: { type: ['string', 'null'] },
port: { type: 'integer', minimum: 1, maximum: 65535 },
term: { type: 'string' },
readyTimeout: { type: 'integer' },
keepaliveInterval: { type: 'integer' },
keepaliveCountMax: { type: 'integer' },
algorithms: {
type: "object",
type: 'object',
properties: {
kex: {
type: "array",
items: { type: "string" }
type: 'array',
items: { type: 'string' },
},
cipher: {
type: "array",
items: { type: "string" }
type: 'array',
items: { type: 'string' },
},
hmac: {
type: "array",
items: { type: "string" }
type: 'array',
items: { type: 'string' },
},
serverHostKey: {
type: "array",
items: { type: "string" }
type: 'array',
items: { type: 'string' },
},
compress: {
type: "array",
items: { type: "string" }
}
type: 'array',
items: { type: 'string' },
},
},
required: ["kex", "cipher", "hmac", "serverHostKey", "compress"]
}
required: ['kex', 'cipher', 'hmac', 'serverHostKey', 'compress'],
},
},
required: [
"host",
"port",
"term",
"readyTimeout",
"keepaliveInterval",
"keepaliveCountMax"
]
required: ['host', 'port', 'term', 'readyTimeout', 'keepaliveInterval', 'keepaliveCountMax'],
},
header: {
type: "object",
type: 'object',
properties: {
text: { type: ["string", "null"] },
background: { type: "string" }
text: { type: ['string', 'null'] },
background: { type: 'string' },
},
required: ["text", "background"]
required: ['text', 'background'],
},
options: {
type: "object",
type: 'object',
properties: {
challengeButton: { type: "boolean" },
autoLog: { type: "boolean" },
allowReauth: { type: "boolean" },
allowReconnect: { type: "boolean" },
allowReplay: { type: "boolean" }
challengeButton: { type: 'boolean' },
autoLog: { type: 'boolean' },
allowReauth: { type: 'boolean' },
allowReconnect: { type: 'boolean' },
allowReplay: { type: 'boolean' },
},
required: ["challengeButton", "allowReauth", "allowReplay"]
required: ['challengeButton', 'allowReauth', 'allowReplay'],
},
session: {
type: "object",
type: 'object',
properties: {
secret: { type: "string" },
name: { type: "string" }
secret: { type: 'string' },
name: { type: 'string' },
},
required: ["secret", "name"]
}
required: ['secret', 'name'],
},
},
required: ["listen", "http", "user", "ssh", "header", "options"]
required: ['listen', 'http', 'user', 'ssh', 'header', 'options'],
}

export default configSchema
Loading

0 comments on commit db891ec

Please sign in to comment.