Skip to content

Commit 9d72450

Browse files
committed
Fix #160 Can't proxy request when target server is HTTPS
1 parent f3d71e6 commit 9d72450

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## Unreleased
4+
5+
* Fix proxying to `https` target `hotel add https://example.com`
6+
* Log proxy errors
7+
38
## 0.5.13
49

510
* Fix `hotel add` CLI bug

src/daemon/group.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ class Group extends EventEmitter {
2424
super()
2525

2626
this._list = {}
27-
this._proxy = httpProxy.createProxyServer({ xfwd: true })
27+
this._proxy = httpProxy.createProxyServer({
28+
xfwd: true
29+
})
2830
this._proxy.on('error', this.handleProxyError)
2931
}
3032

@@ -281,8 +283,16 @@ class Group extends EventEmitter {
281283

282284
// Make sure to send only one response
283285
const send = once(() => {
284-
util.log(`Proxy http://${hostname} to ${item.target}`)
285-
this._proxy.web(req, res, { target: item.target })
286+
const isHTTPS = /^https:\/\//.test(item.target)
287+
util.log(
288+
`Proxy http://${hostname} to ${item.target}`,
289+
isHTTPS ? '(changeOrigin: true)' : ''
290+
)
291+
this._proxy.web(req, res, {
292+
target: item.target,
293+
// https won't work if host doesn't match certificate, so we're changing it
294+
changeOrigin: isHTTPS
295+
})
286296
})
287297

288298
if (item.start) {

test/daemon/app.js

+27-10
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ test.before(() => {
3333
// Fake server to respond to URL
3434
http.createServer((req, res) => {
3535
res.statusCode = 200
36-
res.end('ok')
36+
res.end(`ok - host: ${req.headers.host}`)
3737
}).listen(4000)
3838

3939
// Add server
@@ -72,6 +72,9 @@ test.before(() => {
7272
// Add URL
7373
servers.add('http://localhost:4000', { n: 'proxy' })
7474

75+
// Add https URL
76+
servers.add('https://jsonplaceholder.typicode.com', { n: 'proxy-with-https-target' })
77+
7578
// Add unavailable URL
7679
servers.add('http://localhost:4100', { n: 'unavailable-proxy' })
7780

@@ -101,12 +104,16 @@ test.cb('GET http://hotel.dev/index.html should serve index.html', (t) => {
101104
.expect(200, t.end)
102105
})
103106

104-
test.cb('GET http://node.dev should proxy request', (t) => {
105-
request(app)
106-
.get('/')
107-
.set('Host', 'node.dev')
108-
.expect(200, /Hello World/, t.end)
109-
})
107+
test.cb(
108+
'GET http://node.dev should proxy request and host should be node.dev',
109+
(t) => {
110+
request(app)
111+
.get('/')
112+
.set('Host', 'node.dev')
113+
.expect(/host: node.dev/)
114+
.expect(200, /Hello World/, t.end)
115+
}
116+
)
110117

111118
test.cb('GET http://subdomain.node.dev should proxy request', (t) => {
112119
request(app)
@@ -136,10 +143,20 @@ test.cb('GET http://failing.dev should return 502', (t) => {
136143
.expect(502, t.end)
137144
})
138145

139-
test.cb('GET http://proxy.dev should return 502', (t) => {
146+
test.cb(
147+
'GET http://proxy.dev should return 200 and host should be proxy.dev',
148+
(t) => {
149+
request(app)
150+
.get('/')
151+
.set('Host', 'proxy.dev')
152+
.expect(200, /host: proxy.dev/, t.end)
153+
}
154+
)
155+
156+
test.cb('GET http://proxy-with-https-target.dev should return 200', (t) => {
140157
request(app)
141158
.get('/')
142-
.set('Host', 'proxy.dev')
159+
.set('Host', 'proxy-with-https-target.dev')
143160
.expect(200, t.end)
144161
})
145162

@@ -166,7 +183,7 @@ test.cb('GET /_/servers', t => {
166183
.get('/_/servers')
167184
.expect(200, (err, res) => {
168185
if (err) return t.end(err)
169-
t.is(Object.keys(res.body).length, 8, 'got wrong number of servers')
186+
t.is(Object.keys(res.body).length, 9, 'got wrong number of servers')
170187
t.end()
171188
})
172189
})

test/fixtures/app/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ http.createServer(function (req, res) {
77
'Hello World',
88
process.env.FOO,
99
process.env.HTTP_PROXY,
10-
'x-forwarded-host: ' + req.headers['x-forwarded-host']
10+
'x-forwarded-host: ' + req.headers['x-forwarded-host'],
11+
'host: ' + req.headers.host
1112
].join(' '))
1213
}).listen(process.env.PORT, '127.0.0.1')
1314

0 commit comments

Comments
 (0)