-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactored and improved API, better certbot support
- Loading branch information
1 parent
a3bb218
commit 271818d
Showing
5 changed files
with
5,374 additions
and
3,201 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,62 @@ | ||
const http = require('http'); | ||
const url = require('url'); | ||
const util = require('util'); | ||
|
||
const proxy = http.createServer((req, res) => { | ||
res.writeHead(301, { | ||
Location: url.parse(`https://${req.headers.host}${req.url}`).href | ||
}); | ||
const Router = require('router'); | ||
const _ = require('lodash'); | ||
const finalhandler = require('finalhandler'); | ||
const parse = require('url-parse'); | ||
|
||
res.end(); | ||
}); | ||
class ProxyServer { | ||
constructor(config) { | ||
this.config = { | ||
logger: console, | ||
port: process.env.PROXY_PORT || null, | ||
certbot: { | ||
name: process.env.CERTBOT_WELL_KNOWN_NAME || null, | ||
contents: process.env.CERTBOT_WELL_KNOWN_CONTENTS || null | ||
}, | ||
...config | ||
}; | ||
|
||
if (!module.parent) proxy.listen(80); | ||
const router = new Router(); | ||
|
||
module.exports = proxy; | ||
// support for lets encrypt verification | ||
if ( | ||
_.isObject(this.config.certbot) && | ||
_.isString(this.config.certbot.name) && | ||
_.isString(this.config.certbot.contents) | ||
) | ||
router.get( | ||
`/.well-known/acme-challenge/${this.config.certbot.name}`, | ||
(req, res) => { | ||
res.setHeader('Content-Type', 'text/plain; charset=utf-8'); | ||
res.end(this.config.certbot.contents); | ||
} | ||
); | ||
|
||
router.use((req, res) => { | ||
res.writeHead(301, { | ||
Location: parse(`https://${req.headers.host}${req.url}`).href | ||
}); | ||
res.end(); | ||
}); | ||
|
||
this.server = http.createServer((req, res) => { | ||
router(req, res, finalhandler(req, res)); | ||
}); | ||
|
||
// bind listen/close to this | ||
this.listen = this.listen.bind(this); | ||
this.close = this.close.bind(this); | ||
} | ||
|
||
async listen(port) { | ||
await util.promisify(this.server.listen).bind(this.server)(port); | ||
} | ||
|
||
async close() { | ||
await util.promisify(this.server.close).bind(this.server); | ||
} | ||
} | ||
|
||
module.exports = ProxyServer; |
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 |
---|---|---|
@@ -1,3 +1,29 @@ | ||
const test = require('ava'); | ||
const request = require('supertest'); | ||
|
||
test.todo('write tests'); | ||
const ProxyServer = require('..'); | ||
|
||
test('redirects http to https', async t => { | ||
const proxy = new ProxyServer(); | ||
await proxy.listen(); | ||
const res = await request(proxy.server).get('/foobar'); | ||
const { port } = proxy.server.address(); | ||
t.is(res.status, 301); | ||
t.is(res.headers.location, `https://127.0.0.1:${port}/foobar`); | ||
}); | ||
|
||
test('serves acme challenge', async t => { | ||
const config = { | ||
certbot: { | ||
name: 'name', | ||
contents: 'contents' | ||
} | ||
}; | ||
const proxy = new ProxyServer(config); | ||
const res = await request(proxy.server).get( | ||
`/.well-known/acme-challenge/${config.certbot.name}` | ||
); | ||
t.is(res.status, 200); | ||
t.is(res.headers['content-type'], 'text/plain; charset=utf-8'); | ||
t.is(res.text, config.certbot.contents); | ||
}); |
Oops, something went wrong.