Skip to content

Commit f89fbc1

Browse files
committed
feat: added support for https and redirect
1 parent b61d816 commit f89fbc1

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

index.js

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const process = require('node:process');
22
const http = require('node:http');
3+
const https = require('node:https');
34
const util = require('node:util');
45
const Router = require('router');
56
const _ = require('lodash');
@@ -8,12 +9,20 @@ const parse = require('url-parse');
89
const proxyWrap = require('findhit-proxywrap');
910
const { boolean } = require('boolean');
1011

12+
const router = new Router();
13+
14+
function middleware(request, response) {
15+
return router(request, response, finalhandler(request, response));
16+
}
17+
1118
class ProxyServer {
1219
constructor(config) {
1320
this.config = {
1421
logger: console,
1522
port: process.env.PROXY_PORT || 0,
1623
serverHost: process.env.PROXY_HOST || '::',
24+
protocol: process.env.PROXY_PROTOCOL || 'http',
25+
ssl: {},
1726
certbot: {
1827
name: process.env.CERTBOT_WELL_KNOWN_NAME || null,
1928
contents: process.env.CERTBOT_WELL_KNOWN_CONTENTS || null
@@ -22,14 +31,16 @@ class ProxyServer {
2231
removeWwwPrefix: true,
2332
// useful option if you don't need https redirect
2433
// (e.g. it's just a certbot server)
25-
redirect: true,
34+
redirect:
35+
typeof process.env.PROXY_REDIRECT === 'string'
36+
? process.env.PROXY_REDIRECT
37+
: true,
2638
// <https://github.com/cusspvz/proxywrap>
2739
proxyOptions: {},
2840
...config
2941
};
3042

3143
const proxiedHttp = proxyWrap.proxy(http, this.config.proxyOptions);
32-
const router = new Router();
3344

3445
// support for lets encrypt verification
3546
if (
@@ -47,11 +58,19 @@ class ProxyServer {
4758

4859
if (this.config.redirect)
4960
router.use((request, response) => {
50-
if (this.config.removeWwwPrefix)
51-
request.headers.host = request.headers.host.replace('www.', '');
52-
response.writeHead(301, {
53-
Location: parse(`https://${request.headers.host}${request.url}`).href
54-
});
61+
if (typeof this.config.redirect === 'string') {
62+
response.writeHead(301, {
63+
Location: this.config.redirect
64+
});
65+
} else {
66+
if (this.config.removeWwwPrefix)
67+
request.headers.host = request.headers.host.replace('www.', '');
68+
response.writeHead(301, {
69+
Location: parse(`https://${request.headers.host}${request.url}`)
70+
.href
71+
});
72+
}
73+
5574
response.end();
5675
});
5776
else
@@ -66,11 +85,14 @@ class ProxyServer {
6685

6786
const createServer = this.config.proxyProtocol
6887
? proxiedHttp.createServer
69-
: http.createServer;
88+
: this.config.protocol === 'https'
89+
? https.createServer
90+
: http.createServer;
7091

71-
this.server = createServer((request, response) => {
72-
router(request, response, finalhandler(request, response));
73-
});
92+
this.server =
93+
this.config.protocol === 'https'
94+
? createServer(this.config.ssl, middleware)
95+
: createServer(middleware);
7496

7597
// bind listen/close to this
7698
this.listen = this.listen.bind(this);

0 commit comments

Comments
 (0)