From 3d0fe991e40bad6d8494b96c4e02365534c4f6df Mon Sep 17 00:00:00 2001 From: Imuli Date: Sun, 12 Jun 2016 10:35:23 -0400 Subject: [PATCH] add a host option to listen on a specific ip --- index.js | 6 ++++++ src/server/index.ts | 5 +++-- src/server/interfaces.ts | 6 ++++++ src/server/server.ts | 9 ++++----- src/server/wetty.ts | 7 +++---- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index fcbb72d..5ded0c4 100644 --- a/index.js +++ b/index.js @@ -74,6 +74,12 @@ if (require.main === module) { type: 'number', default: parseInt(process.env.PORT, 10) || 3000, }, + host: { + demand: false, + description: 'wetty listen host', + default: '0.0.0.0', + type: 'string', + }, command: { demand: false, alias: 'c', diff --git a/src/server/index.ts b/src/server/index.ts index c65f381..4dfbe69 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -13,6 +13,7 @@ export interface Options { sslkey?: string; sslcert?: string; base: string; + host: string; port: number; command?: string; } @@ -30,6 +31,7 @@ export default class Server { sshkey, sshpass, base, + host, port, command, sslkey, @@ -55,8 +57,7 @@ export default class Server { pass: sshpass, key: sshkey, }, - base, - port, + { base, host, port }, command, { key: sslkey, cert: sslcert } ); diff --git a/src/server/interfaces.ts b/src/server/interfaces.ts index 8f5cb7b..4674ecf 100644 --- a/src/server/interfaces.ts +++ b/src/server/interfaces.ts @@ -16,3 +16,9 @@ export interface SSLBuffer { key?: Buffer; cert?: Buffer; } + +export interface Server { + port: number; + host: string; + base: string; +} diff --git a/src/server/server.ts b/src/server/server.ts index bf1a34f..775db10 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -10,15 +10,14 @@ import { isUndefined } from 'lodash'; import * as morgan from 'morgan'; import logger from './logger'; import events from './emitter'; -import { SSLBuffer } from './interfaces'; +import { SSLBuffer, Server } from './interfaces'; const distDir = path.join('./', 'dist', 'client'); const trim = (str: string): string => str.replace(/\/*$/, ''); export default function createServer( - base: string, - port: number, + { base, port, host }: Server, { key, cert }: SSLBuffer ): SocketIO.Server { const basePath = trim(base); @@ -73,10 +72,10 @@ export default function createServer( return socket( !isUndefined(key) && !isUndefined(cert) - ? https.createServer({ key, cert }, app).listen(port, () => { + ? https.createServer({ key, cert }, app).listen(port, host, () => { events.server(port, 'https'); }) - : http.createServer(app).listen(port, () => { + : http.createServer(app).listen(port, host, () => { events.server(port, 'http'); }), { path: `${basePath}/socket.io` } diff --git a/src/server/wetty.ts b/src/server/wetty.ts index 4b2197c..7e18f6b 100644 --- a/src/server/wetty.ts +++ b/src/server/wetty.ts @@ -7,7 +7,7 @@ import server from './server'; import getCommand from './command'; import term from './term'; import loadSSL from './ssl'; -import { SSL, SSH, SSLBuffer } from './interfaces'; +import { SSL, SSH, SSLBuffer, Server } from './interfaces'; export default class WeTTy extends EventEmitter { /** @@ -16,8 +16,7 @@ export default class WeTTy extends EventEmitter { */ public start( ssh: SSH = { user: '', host: 'localhost', auth: 'password', port: 22 }, - basePath: string = '/wetty/', - serverPort: number = 3000, + serverConf: Server = { base: '/wetty/', port: 3000, host: '0.0.0.0' }, command: string = '', ssl?: SSL ): Promise { @@ -33,7 +32,7 @@ export default class WeTTy extends EventEmitter { ); } - const io = server(basePath, serverPort, sslBuffer); + const io = server(serverConf, sslBuffer); /** * Wetty server connected too * @fires WeTTy#connnection