feat: refact certMgr and httpsServerMgr into TS

This commit is contained in:
砚然 2018-08-28 15:47:04 +08:00
parent 201447a19a
commit 5bf6a55323
8 changed files with 123 additions and 72 deletions

View File

@ -9,7 +9,7 @@ const program = require('commander'),
util = require('../dist/util').default, util = require('../dist/util').default,
rootCACheck = require('./rootCaCheck'), rootCACheck = require('./rootCaCheck'),
startServer = require('./startServer'), startServer = require('./startServer'),
certMgr = require('../dist/certMgr'), certMgr = require('../dist/certMgr').default,
logUtil = require('../dist/log'); logUtil = require('../dist/log');
program program

View File

@ -1,12 +1,20 @@
'use strict' 'use strict';
const EasyCert = require('node-easy-cert'); import * as EasyCert from 'node-easy-cert';
const co = require('co'); import * as co from 'co';
const os = require('os'); import * as os from 'os';
const inquirer = require('inquirer'); import * as inquirer from 'inquirer';
import util from './util';
import logUtil from './log';
const util = require('./util').default; declare interface ICertMgr {
const logUtil = require('./log'); ifRootCAFileExists?: boolean;
generateRootCA?: ( cb: (error: boolean, keyPath: string, crtPath: string) => void ) => void;
getCAStatus?: () => Generator;
trustRootCA?: () => Generator;
getRootCAFilePath?: () => string;
getCertificate?: (serverName: string, cb: (err: Error, key: string, crt: string) => void) => void;
}
const options = { const options = {
rootDirPath: util.getAnyProxyPath('certificates'), rootDirPath: util.getAnyProxyPath('certificates'),
@ -15,24 +23,24 @@ const options = {
{ name: 'countryName', value: 'CN' }, { name: 'countryName', value: 'CN' },
{ name: 'organizationName', value: 'AnyProxy' }, { name: 'organizationName', value: 'AnyProxy' },
{ shortName: 'ST', value: 'SH' }, { shortName: 'ST', value: 'SH' },
{ shortName: 'OU', value: 'AnyProxy SSL Proxy' } { shortName: 'OU', value: 'AnyProxy SSL Proxy' },
] ],
}; };
const easyCert = new EasyCert(options); const easyCert = new EasyCert(options);
const crtMgr = util.merge({}, easyCert); const crtMgr: ICertMgr = util.merge({}, easyCert);
// rename function // rename function
crtMgr.ifRootCAFileExists = easyCert.isRootCAFileExists; crtMgr.ifRootCAFileExists = easyCert.isRootCAFileExists;
crtMgr.generateRootCA = function (cb) { crtMgr.generateRootCA = function(cb: (error: boolean, keyPath: string, crtPath: string) => void): void {
doGenerate(false); doGenerate(false);
// set default common name of the cert // set default common name of the cert
function doGenerate(overwrite) { function doGenerate(overwrite: boolean): void {
const rootOptions = { const rootOptions = {
commonName: 'AnyProxy', commonName: 'AnyProxy',
overwrite: !!overwrite overwrite: !!overwrite,
}; };
easyCert.generateRootCA(rootOptions, (error, keyPath, crtPath) => { easyCert.generateRootCA(rootOptions, (error, keyPath, crtPath) => {
@ -41,10 +49,11 @@ crtMgr.generateRootCA = function (cb) {
} }
}; };
crtMgr.getCAStatus = function *() { crtMgr.getCAStatus = function *(): Generator {
return co(function *() { return co(function *(): Generator {
const result = { const result = {
exist: false, exist: false,
trusted: undefined,
}; };
const ifExist = easyCert.isRootCAFileExists(); const ifExist = easyCert.isRootCAFileExists();
if (!ifExist) { if (!ifExist) {
@ -57,12 +66,12 @@ crtMgr.getCAStatus = function *() {
return result; return result;
} }
}); });
} };
/** /**
* trust the root ca by command * trust the root ca by command
*/ */
crtMgr.trustRootCA = function *() { crtMgr.trustRootCA = function *(): Generator {
const platform = os.platform(); const platform = os.platform();
const rootCAPath = crtMgr.getRootCAFilePath(); const rootCAPath = crtMgr.getRootCAFilePath();
const trustInquiry = [ const trustInquiry = [
@ -70,8 +79,8 @@ crtMgr.trustRootCA = function *() {
type: 'list', type: 'list',
name: 'trustCA', name: 'trustCA',
message: 'The rootCA is not trusted yet, install it to the trust store now?', message: 'The rootCA is not trusted yet, install it to the trust store now?',
choices: ['Yes', "No, I'll do it myself"] choices: ['Yes', 'No, I\'ll do it myself'],
} },
]; ];
if (platform === 'darwin') { if (platform === 'darwin') {
@ -79,7 +88,8 @@ crtMgr.trustRootCA = function *() {
if (answer.trustCA === 'Yes') { if (answer.trustCA === 'Yes') {
logUtil.info('About to trust the root CA, this may requires your password'); logUtil.info('About to trust the root CA, this may requires your password');
// https://ss64.com/osx/security-cert.html // https://ss64.com/osx/security-cert.html
const result = util.execScriptSync(`sudo security add-trusted-cert -d -k /Library/Keychains/System.keychain ${rootCAPath}`); const result =
(util.execScriptSync(`sudo security add-trusted-cert -d -k /Library/Keychains/System.keychain ${rootCAPath}`) as IExecScriptResult);
if (result.status === 0) { if (result.status === 0) {
logUtil.info('Root CA install, you are ready to intercept the https now'); logUtil.info('Root CA install, you are ready to intercept the https now');
} else { } else {
@ -98,6 +108,6 @@ crtMgr.trustRootCA = function *() {
logUtil.info('You can install the root CA manually.'); logUtil.info('You can install the root CA manually.');
} }
logUtil.info('The root CA file path is: ' + crtMgr.getRootCAFilePath()); logUtil.info('The root CA file path is: ' + crtMgr.getRootCAFilePath());
} };
module.exports = crtMgr; export default crtMgr;

View File

@ -1,25 +1,46 @@
'use strict' 'use strict';
//manage https servers import * as async from 'async';
const async = require('async'), import * as https from 'https';
https = require('https'), import * as http from 'http';
tls = require('tls'), import * as tls from 'tls';
crypto = require('crypto'), import * as crypto from 'crypto';
color = require('colorful'), import * as color from 'colorful';
certMgr = require('./certMgr'), import * as WebSocket from 'ws';
logUtil = require('./log'), import * as constants from 'constants';
util = require('./util').default, import * as AsyncTask from 'async-task-mgr';
wsServerMgr = require('./wsServerMgr'), import Recorder from './recorder';
co = require('co'), import certMgr from './certMgr';
constants = require('constants'), import logUtil from './log';
asyncTask = require('async-task-mgr'); import util from './util';
import * as wsServerMgr from './wsServerMgr';
import * as co from 'co';
const createSecureContext = tls.createSecureContext || crypto.createSecureContext; // // manage https servers
//using sni to avoid multiple ports // const async = require('async'),
function SNIPrepareCert(serverName, SNICallback) { // https = require('https'),
let keyContent, // tls = require('tls'),
crtContent, // crypto = require('crypto'),
ctx; // color = require('colorful'),
// certMgr = require('./certMgr'),
// logUtil = require('./log'),
// util = require('./util').default,
// wsServerMgr = require('./wsServerMgr'),
// co = require('co'),
// constants = require('constants'),
// asyncTask = require('async-task-mgr');
declare type THttpsRequestHanlder = (req: http.IncomingMessage, userRes: http.ServerResponse) => void;
declare type TWsRequestHandler =
(userRule: AnyProxyRule, recorder: Recorder, wsClient: WebSocket, wsReq: http.IncomingMessage) => void;
const createSecureContext = tls.createSecureContext || (crypto as any).createSecureContext;
// using sni to avoid multiple ports
function SNIPrepareCert(
serverName: string, SNICallback: (error: Error, ctx: tls.SecureContext) => void): void {
let keyContent;
let crtContent;
let ctx;
async.series([ async.series([
(callback) => { (callback) => {
@ -37,13 +58,13 @@ function SNIPrepareCert(serverName, SNICallback) {
try { try {
ctx = createSecureContext({ ctx = createSecureContext({
key: keyContent, key: keyContent,
cert: crtContent cert: crtContent,
}); });
callback(); callback();
} catch (e) { } catch (e) {
callback(e); callback(e);
} }
} },
], (err) => { ], (err) => {
if (!err) { if (!err) {
const tipText = 'proxy server for __NAME established'.replace('__NAME', serverName); const tipText = 'proxy server for __NAME established'.replace('__NAME', serverName);
@ -56,18 +77,17 @@ function SNIPrepareCert(serverName, SNICallback) {
}); });
} }
//config.port - port to start https server
//config.handler - request handler
/** /**
* Create an https server * Create an https server
* *
* @param {object} config * @param {object} config
* @param {number} config.port * @param {number} config.port port to start https server
* @param {function} config.handler * @param {function} config.handler request handler
*/ */
function createHttpsServer(config) { function createHttpsServer(config: {
port: number;
handler: THttpsRequestHanlder;
}): Promise<https.Server> {
if (!config || !config.port || !config.handler) { if (!config || !config.port || !config.handler) {
throw (new Error('please assign a port')); throw (new Error('please assign a port'));
} }
@ -78,7 +98,7 @@ function createHttpsServer(config) {
secureOptions: constants.SSL_OP_NO_SSLv3 || constants.SSL_OP_NO_TLSv1, secureOptions: constants.SSL_OP_NO_SSLv3 || constants.SSL_OP_NO_TLSv1,
SNICallback: SNIPrepareCert, SNICallback: SNIPrepareCert,
key: keyContent, key: keyContent,
cert: crtContent cert: crtContent,
}, config.handler).listen(config.port); }, config.handler).listen(config.port);
resolve(server); resolve(server);
}); });
@ -92,7 +112,11 @@ function createHttpsServer(config) {
* @param @required {number} config.port the port to listen on * @param @required {number} config.port the port to listen on
* @param @required {function} handler the handler of each connect * @param @required {function} handler the handler of each connect
*/ */
function createIPHttpsServer(config) { function createIPHttpsServer(config: {
port: number;
ip: string;
handler: THttpsRequestHanlder;
}): Promise<https.Server> {
if (!config || !config.port || !config.handler) { if (!config || !config.port || !config.handler) {
throw (new Error('please assign a port')); throw (new Error('please assign a port'));
} }
@ -106,7 +130,7 @@ function createIPHttpsServer(config) {
const server = https.createServer({ const server = https.createServer({
secureOptions: constants.SSL_OP_NO_SSLv3 || constants.SSL_OP_NO_TLSv1, secureOptions: constants.SSL_OP_NO_SSLv3 || constants.SSL_OP_NO_TLSv1,
key: keyContent, key: keyContent,
cert: crtContent cert: crtContent,
}, config.handler).listen(config.port); }, config.handler).listen(config.port);
resolve(server); resolve(server);
@ -122,26 +146,39 @@ function createIPHttpsServer(config) {
* @param {function} config.handler handler to deal https request * @param {function} config.handler handler to deal https request
* *
*/ */
class httpsServerMgr { class HttpsServerMgr {
constructor(config) { private instanceDefaultHost: string;
private httpsAsyncTask: AsyncTask;
private handler: THttpsRequestHanlder;
private wsHandler: TWsRequestHandler;
constructor(config: {
handler: THttpsRequestHanlder;
wsHandler: TWsRequestHandler;
}) {
if (!config || !config.handler) { if (!config || !config.handler) {
throw new Error('handler is required'); throw new Error('handler is required');
} }
this.instanceDefaultHost = '127.0.0.1'; this.instanceDefaultHost = '127.0.0.1';
this.httpsAsyncTask = new asyncTask(); this.httpsAsyncTask = new AsyncTask();
this.handler = config.handler; this.handler = config.handler;
this.wsHandler = config.wsHandler this.wsHandler = config.wsHandler;
} }
getSharedHttpsServer(hostname) { public getSharedHttpsServer(hostname: string): Promise<{
host: string;
port: number;
}> {
// ip address will have a unique name // ip address will have a unique name
const finalHost = util.isIpDomain(hostname) ? hostname : this.instanceDefaultHost; const finalHost = util.isIpDomain(hostname) ? hostname : this.instanceDefaultHost;
const self = this; const self = this;
function prepareServer(callback) { function prepareServer(callback: (e: Error, result?: {
host: string;
port: number;
}) => void): void {
let instancePort; let instancePort;
co(util.getFreePort) co(util.getFreePort)
.then(co.wrap(function *(port) { .then(co.wrap(function *(port: number): Generator {
instancePort = port; instancePort = port;
let httpsServer = null; let httpsServer = null;
@ -150,18 +187,18 @@ class httpsServerMgr {
httpsServer = yield createIPHttpsServer({ httpsServer = yield createIPHttpsServer({
ip: hostname, ip: hostname,
port, port,
handler: self.handler handler: self.handler,
}); });
} else { } else {
httpsServer = yield createHttpsServer({ httpsServer = yield createHttpsServer({
port, port,
handler: self.handler handler: self.handler,
}); });
} }
wsServerMgr.getWsServer({ wsServerMgr.getWsServer({
server: httpsServer, server: httpsServer,
connHandler: self.wsHandler connHandler: self.wsHandler,
}); });
httpsServer.on('upgrade', (req, cltSocket, head) => { httpsServer.on('upgrade', (req, cltSocket, head) => {
@ -175,7 +212,7 @@ class httpsServerMgr {
callback(null, result); callback(null, result);
return result; return result;
})) }))
.catch(e => { .catch((e) => {
callback(e); callback(e);
}); });
} }
@ -194,4 +231,4 @@ class httpsServerMgr {
} }
} }
export default httpsServerMgr; export default HttpsServerMgr;

View File

@ -4,7 +4,7 @@ const http = require('http'),
https = require('https'), https = require('https'),
async = require('async'), async = require('async'),
color = require('colorful'), color = require('colorful'),
certMgr = require('./certMgr'), certMgr = require('./certMgr').default,
Recorder = require('./recorder'), Recorder = require('./recorder'),
logUtil = require('./log'), logUtil = require('./log'),
util = require('./util').default, util = require('./util').default,

View File

@ -1,6 +1,6 @@
/// <reference path="../../typings/index.d.ts" /> /// <reference path="../../typings/index.d.ts" />
declare interface ErrorResponse { declare interface IErrorResponse {
statusCode: number; statusCode: number;
header: OneLevelObjectType; header: OneLevelObjectType;
body: string; body: string;
@ -201,7 +201,7 @@ function fetchRemoteResponse(
/* /*
* get error response for exception scenarios * get error response for exception scenarios
*/ */
function getErrorResponse(error: NodeJS.ErrnoException, fullUrl: string): ErrorResponse { function getErrorResponse(error: NodeJS.ErrnoException, fullUrl: string): IErrorResponse {
// default error response // default error response
const errorResponse = { const errorResponse = {
statusCode: 500, statusCode: 500,

View File

@ -10,7 +10,7 @@ const express = require('express'),
events = require('events'), events = require('events'),
qrCode = require('qrcode-npm'), qrCode = require('qrcode-npm'),
util = require('./util').default, util = require('./util').default,
certMgr = require('./certMgr'), certMgr = require('./certMgr').default,
wsServer = require('./wsServer'), wsServer = require('./wsServer'),
juicer = require('juicer'), juicer = require('juicer'),
ip = require('ip'), ip = require('ip'),

View File

@ -4,7 +4,7 @@ const koaBody = require('koa-body');
const send = require('koa-send'); const send = require('koa-send');
const path = require('path'); const path = require('path');
const https = require('https'); const https = require('https');
const certMgr = require('../../lib/certMgr'); const certMgr = require('../../lib/certMgr').default;
const fs = require('fs'); const fs = require('fs');
const nurl = require('url'); const nurl = require('url');
const color = require('colorful'); const color = require('colorful');

4
typings/index.d.ts vendored
View File

@ -78,4 +78,8 @@ declare interface AnyProxyReponseDetail {
declare interface OneLevelObjectType { declare interface OneLevelObjectType {
[key: string]: string | boolean | number [key: string]: string | boolean | number
}
declare interface IExecScriptResult {
status: number;
} }