Browse Source

add a host option to listen on a specific ip

pull/155/head
Imuli 9 years ago
committed by butlerx
parent
commit
3d0fe991e4
No known key found for this signature in database GPG Key ID: B37CA765BAA89170
  1. 6
      index.js
  2. 5
      src/server/index.ts
  3. 6
      src/server/interfaces.ts
  4. 9
      src/server/server.ts
  5. 7
      src/server/wetty.ts

6
index.js

@ -74,6 +74,12 @@ if (require.main === module) {
type: 'number', type: 'number',
default: parseInt(process.env.PORT, 10) || 3000, default: parseInt(process.env.PORT, 10) || 3000,
}, },
host: {
demand: false,
description: 'wetty listen host',
default: '0.0.0.0',
type: 'string',
},
command: { command: {
demand: false, demand: false,
alias: 'c', alias: 'c',

5
src/server/index.ts

@ -13,6 +13,7 @@ export interface Options {
sslkey?: string; sslkey?: string;
sslcert?: string; sslcert?: string;
base: string; base: string;
host: string;
port: number; port: number;
command?: string; command?: string;
} }
@ -30,6 +31,7 @@ export default class Server {
sshkey, sshkey,
sshpass, sshpass,
base, base,
host,
port, port,
command, command,
sslkey, sslkey,
@ -55,8 +57,7 @@ export default class Server {
pass: sshpass, pass: sshpass,
key: sshkey, key: sshkey,
}, },
base, { base, host, port },
port,
command, command,
{ key: sslkey, cert: sslcert } { key: sslkey, cert: sslcert }
); );

6
src/server/interfaces.ts

@ -16,3 +16,9 @@ export interface SSLBuffer {
key?: Buffer; key?: Buffer;
cert?: Buffer; cert?: Buffer;
} }
export interface Server {
port: number;
host: string;
base: string;
}

9
src/server/server.ts

@ -10,15 +10,14 @@ import { isUndefined } from 'lodash';
import * as morgan from 'morgan'; import * as morgan from 'morgan';
import logger from './logger'; import logger from './logger';
import events from './emitter'; import events from './emitter';
import { SSLBuffer } from './interfaces'; import { SSLBuffer, Server } from './interfaces';
const distDir = path.join('./', 'dist', 'client'); const distDir = path.join('./', 'dist', 'client');
const trim = (str: string): string => str.replace(/\/*$/, ''); const trim = (str: string): string => str.replace(/\/*$/, '');
export default function createServer( export default function createServer(
base: string, { base, port, host }: Server,
port: number,
{ key, cert }: SSLBuffer { key, cert }: SSLBuffer
): SocketIO.Server { ): SocketIO.Server {
const basePath = trim(base); const basePath = trim(base);
@ -73,10 +72,10 @@ export default function createServer(
return socket( return socket(
!isUndefined(key) && !isUndefined(cert) !isUndefined(key) && !isUndefined(cert)
? https.createServer({ key, cert }, app).listen(port, () => { ? https.createServer({ key, cert }, app).listen(port, host, () => {
events.server(port, 'https'); events.server(port, 'https');
}) })
: http.createServer(app).listen(port, () => { : http.createServer(app).listen(port, host, () => {
events.server(port, 'http'); events.server(port, 'http');
}), }),
{ path: `${basePath}/socket.io` } { path: `${basePath}/socket.io` }

7
src/server/wetty.ts

@ -7,7 +7,7 @@ import server from './server';
import getCommand from './command'; import getCommand from './command';
import term from './term'; import term from './term';
import loadSSL from './ssl'; import loadSSL from './ssl';
import { SSL, SSH, SSLBuffer } from './interfaces'; import { SSL, SSH, SSLBuffer, Server } from './interfaces';
export default class WeTTy extends EventEmitter { export default class WeTTy extends EventEmitter {
/** /**
@ -16,8 +16,7 @@ export default class WeTTy extends EventEmitter {
*/ */
public start( public start(
ssh: SSH = { user: '', host: 'localhost', auth: 'password', port: 22 }, ssh: SSH = { user: '', host: 'localhost', auth: 'password', port: 22 },
basePath: string = '/wetty/', serverConf: Server = { base: '/wetty/', port: 3000, host: '0.0.0.0' },
serverPort: number = 3000,
command: string = '', command: string = '',
ssl?: SSL ssl?: SSL
): Promise<void> { ): Promise<void> {
@ -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 * Wetty server connected too
* @fires WeTTy#connnection * @fires WeTTy#connnection

Loading…
Cancel
Save