Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

Commit

Permalink
feat(websocket): Adding ability to configure WebSocket to allow for s…
Browse files Browse the repository at this point in the history
…ecure connections

* Added configurable WebSocket Secure support.

* Update types.ts

removed additional emptyline

* Modified Configuration Structure
  • Loading branch information
alexjebens authored and rmevans9 committed Dec 13, 2018
1 parent 01ee393 commit 6c3652a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
49 changes: 41 additions & 8 deletions src/reactotron-core-server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { merge, find, propEq, without, contains, forEach, pluck, reject, equals } from "ramda"
import { createServer as createHttpsServer, ServerOptions as HttpsServerOptions } from "https"
import { Server as WebSocketServer, OPEN } from "ws"
import * as mitt from "mitt"
import validate from "./validation"
Expand All @@ -9,7 +10,10 @@ import {
ServerEvent,
CommandEvent,
WebSocketEvent,
PfxServerOptions,
WssServerOptions,
} from "./types"
import { readFileSync } from "fs"

/**
* The default server options.
Expand All @@ -27,6 +31,30 @@ function createGuid() {
return s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() + s4() + s4()
}

function isPfxServerOptions(wssOptions: WssServerOptions): wssOptions is PfxServerOptions {
return !!(wssOptions as PfxServerOptions).pathToPfx
}

function buildHttpsServerOptions(wssOptions: WssServerOptions): HttpsServerOptions {
if (!wssOptions) {
return undefined
}
if (isPfxServerOptions(wssOptions)) {
return {
pfx: readFileSync(wssOptions.pathToPfx),
passphrase: wssOptions.passphrase,
}
}
if (!wssOptions.pathToCert) {
return undefined
}
return {
cert: readFileSync(wssOptions.pathToCert),
key: wssOptions.pathToKey ? readFileSync(wssOptions.pathToKey) : undefined,
passphrase: wssOptions.passphrase,
}
}

/**
* The Reactotron server.
*/
Expand Down Expand Up @@ -106,9 +134,14 @@ export default class Server {
*/
start = () => {
const { port } = this.options

// start listening
this.wss = new WebSocketServer({ port })
const httpsServerOptions = buildHttpsServerOptions(this.options.wss)
if (!httpsServerOptions) {
this.wss = new WebSocketServer({ port })
} else {
const server = createHttpsServer(httpsServerOptions)
this.wss = new WebSocketServer({ server })
server.listen(port)
}

// register events
this.wss.on("connection", (socket, request) => {
Expand Down Expand Up @@ -292,14 +325,14 @@ export default class Server {
if (contains(path, this.subscriptions)) {
return
}

// monitor the complete state when * (star selector) is entered
if (equals(path, '*')) {
this.subscriptions.push('')
if (equals(path, "*")) {
this.subscriptions.push("")
} else {
this.subscriptions.push(path)
this.subscriptions.push(path)
}


// subscribe
this.stateValuesSendSubscriptions()
Expand Down
33 changes: 33 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,41 @@ export interface ServerOptions {
* Which port to listen to. Default: 9090.
*/
port: number

/**
* Web Socket Secure Configuration
*/
wss?: WssServerOptions
}

export interface PfxServerOptions {
/**
* Path to a PFX file.
*/
pathToPfx: string
/**
* Passphrase, if required, of the PFX file.
*/
passphrase?: string
}

export interface CertServerOptions {
/**
* Path to a signing cert file.
*/
pathToCert: string
/**
* Path to a the corresponding key of the cert file.
*/
pathToKey: string
/**
* Passphrase, if required, of the key file.
*/
passphrase?: string
}

export type WssServerOptions = PfxServerOptions | CertServerOptions

/**
* A client which is in the process of connecting.
*/
Expand Down

0 comments on commit 6c3652a

Please sign in to comment.