Browse Source

add a host option to listen on a specific ip

pull/155/head
Imuli 8 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',
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',

5
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 }
);

6
src/server/interfaces.ts

@ -16,3 +16,9 @@ export interface SSLBuffer {
key?: 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 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` }

7
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<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
* @fires WeTTy#connnection

Loading…
Cancel
Save