-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
166 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const url = require('url') | ||
const { reporter } = require('@dhis2/cli-helpers-engine') | ||
const httpProxy = require('http-proxy') | ||
|
||
const stripCookieSecure = cookie => { | ||
return cookie | ||
.split(';') | ||
.filter(v => v.trim().toLowerCase() !== 'secure') | ||
.join('; ') | ||
} | ||
|
||
const rewriteLocation = ({ location, target, baseUrl }) => { | ||
const parsedLocation = url.parse(location) | ||
const parsedTarget = url.parse(target) | ||
const parsedBaseUrl = url.parse(baseUrl) | ||
|
||
if ( | ||
parsedLocation.host === parsedTarget.host && | ||
parsedLocation.pathname.startsWith(parsedTarget.pathname) | ||
) { | ||
return url.format({ | ||
...parsedBaseUrl, | ||
pathname: parsedLocation.pathname.replace( | ||
parsedTarget.pathname, | ||
'' | ||
), | ||
search: parsedLocation.search, | ||
}) | ||
} | ||
return location | ||
} | ||
|
||
exports = module.exports = ({ target, baseUrl, port, shellPort }) => { | ||
const proxyServer = httpProxy.createProxyServer({ | ||
target, | ||
changeOrigin: true, | ||
secure: false, | ||
protocolRewrite: 'http', | ||
cookieDomainRewrite: '', | ||
cookiePathRewrite: '/', | ||
}) | ||
|
||
proxyServer.on('proxyRes', (proxyRes, req, res) => { | ||
if (proxyRes.headers['access-control-allow-origin']) { | ||
res.setHeader( | ||
'access-control-allow-origin', | ||
`http://localhost:${shellPort}` | ||
) | ||
} | ||
|
||
if (proxyRes.headers.location) { | ||
proxyRes.headers.location = rewriteLocation({ | ||
location: proxyRes.headers.location, | ||
target, | ||
baseUrl, | ||
}) | ||
} | ||
|
||
const sc = proxyRes.headers['set-cookie'] | ||
if (Array.isArray(sc)) { | ||
proxyRes.headers['set-cookie'] = sc.map(stripCookieSecure) | ||
} | ||
}) | ||
|
||
proxyServer.on('error', error => { | ||
reporter.warn(error) | ||
}) | ||
|
||
proxyServer.listen(port) | ||
} | ||
|
||
exports.rewriteLocation = rewriteLocation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const { rewriteLocation } = require('./proxy') | ||
|
||
describe('rewriteLocation', () => { | ||
it('rewrites locations if they match the proxy target', () => { | ||
const baseUrl = 'http://localhost:8080' | ||
|
||
expect( | ||
rewriteLocation({ | ||
location: 'https://play.dhis2.org/dev/login.action', | ||
target: 'https://play.dhis2.org/dev', | ||
baseUrl, | ||
}) | ||
).toBe(`${baseUrl}/login.action`) | ||
|
||
expect( | ||
rewriteLocation({ | ||
location: 'https://play.dhis2.org/dev/page?param=value', | ||
target: 'https://play.dhis2.org/dev', | ||
baseUrl, | ||
}) | ||
).toBe(`${baseUrl}/page?param=value`) | ||
}) | ||
|
||
it('does not rewrite locations if they do not match the proxy target', () => { | ||
;[ | ||
{ | ||
location: 'https://example.com/path', | ||
target: 'https://play.dhis2.org/dev', | ||
baseUrl: 'http://localhost:8080', | ||
}, | ||
{ | ||
location: 'https://play.dhis2.org/2.35dev', | ||
target: 'https://play.dhis2.org/dev', | ||
baseUrl: 'http://localhost:8080', | ||
}, | ||
{ | ||
location: 'http://server.com:1234', | ||
target: 'http://server.com:5678', | ||
baseUrl: 'http://localhost:8080', | ||
}, | ||
].forEach(args => { | ||
expect(rewriteLocation(args)).toBe(args.location) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters