From b1dc6bd3fec45f11dc05dd680e89f1633b687095 Mon Sep 17 00:00:00 2001 From: floydwch Date: Tue, 22 Mar 2016 18:13:42 +0800 Subject: [PATCH 1/4] extract ssl key and cert to conf --- src/daemon/index.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/daemon/index.js b/src/daemon/index.js index da754286..0feb6fbd 100644 --- a/src/daemon/index.js +++ b/src/daemon/index.js @@ -15,15 +15,28 @@ exitHook(() => { pidFile.remove() }) +const ssl = {} +const { + HOME, + HOMEPATH, + USERPROFILE +} = process.env +const home_path = HOME || HOMEPATH || USERPROFILE + +if (conf.key_path && conf.cert_path) { + ssl.key = fs.readFileSync(path.join(home_path, conf.key_path)) + ssl.cert = fs.readFileSync(path.join(home_path, conf.cert_path)) +} else { + ssl.key = fs.readFileSync(path.join(__dirname, 'certs/server.key')) + ssl.cert = fs.readFileSync(path.join(__dirname, 'certs/server.crt')) +} + const proxy = httpProxy.createServer({ target: { host: '127.0.0.1', port: conf.port }, - ssl: { - key: fs.readFileSync(path.join(__dirname, 'certs/server.key')), - cert: fs.readFileSync(path.join(__dirname, 'certs/server.crt')) - }, + ssl, ws: true }) From d96bb379fd84aa1de9d307ed2245c5620cae0f65 Mon Sep 17 00:00:00 2001 From: floydwch Date: Tue, 22 Mar 2016 18:20:34 +0800 Subject: [PATCH 2/4] add doc for self-signed certificate --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index da23bc89..6348348b 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,22 @@ hotel add "cmd -p %PORT%" # Windows ~/.hotel/servers/.json ``` +### Self-signed certificate + +To use self-signed certificate, please put the cert and key to `~/.hotel/`, and add `key_path` and `cert_path` to `~/.hotel.conf.json`. + +### Example +```json +{ + "port": 2000, + "host": "127.0.0.1", + "timeout": 5000, + "tld": "dev", + "key_path": ".hotel/hotel.key", + "cert_path": ".hotel/hotel.crt" +} +``` + ## Third-party tools * [Hotel Clerk](https://github.com/therealklanni/hotel-clerk) OS X menubar From 2a32bb0268a47458c39379971d3ade6f96ac682d Mon Sep 17 00:00:00 2001 From: floydwch Date: Tue, 22 Mar 2016 19:19:01 +0800 Subject: [PATCH 3/4] use path.resolve to support absolute path --- src/daemon/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/daemon/index.js b/src/daemon/index.js index 0feb6fbd..e4073d52 100644 --- a/src/daemon/index.js +++ b/src/daemon/index.js @@ -24,8 +24,8 @@ const { const home_path = HOME || HOMEPATH || USERPROFILE if (conf.key_path && conf.cert_path) { - ssl.key = fs.readFileSync(path.join(home_path, conf.key_path)) - ssl.cert = fs.readFileSync(path.join(home_path, conf.cert_path)) + ssl.key = fs.readFileSync(path.resolve(home_path, conf.key_path)) + ssl.cert = fs.readFileSync(path.resolve(home_path, conf.cert_path)) } else { ssl.key = fs.readFileSync(path.join(__dirname, 'certs/server.key')) ssl.cert = fs.readFileSync(path.join(__dirname, 'certs/server.crt')) From 32f31f82295d56c9106249b73caf0467e7539e7e Mon Sep 17 00:00:00 2001 From: floydwch Date: Fri, 1 Apr 2016 06:46:15 +0800 Subject: [PATCH 4/4] fix http-proxy's crash when refresh quickly --- src/daemon/index.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/daemon/index.js b/src/daemon/index.js index e4073d52..be867b57 100644 --- a/src/daemon/index.js +++ b/src/daemon/index.js @@ -40,6 +40,16 @@ const proxy = httpProxy.createServer({ ws: true }) +proxy.on('proxyReq', (proxyReq, req) => { + req._proxyReq = proxyReq +}) + +proxy.on('error', (err, req) => { + if (req.socket.destroyed && err.code === 'ECONNRESET') { + req._proxyReq.abort() + } +}) + proxy.listen(conf.port + 1) server.listen(conf.port, conf.host, function () {