-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
86 lines (69 loc) · 1.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const config = require('./config.json')
const express = require('express');
const morgan = require("morgan");
const { createProxyMiddleware } = require('http-proxy-middleware');
const slowDown = require("express-slow-down");
const rateLimit = require("express-rate-limit");
const process = require('process');
// Configuration
const {
port, // port to start the proxy on
host, // hostname to start the proxy on
battlelog_url, // battlelog url to proxy
keeper_url, // keeper url to proxy
ratelimit, // rate limit before either enforcing a hard limit or dripping requests
rate_window, // the limit window in ms, so every x rate_window limit to x ratelimit
log_requests,
hard_limit // if the limit should be hard enforced rather than drip fed
} = config;
const app = express();
if(log_requests) {
app.use(morgan('short'));
}
const speedLimiter = slowDown({
windowMs: rate_window,
delayAfter: ratelimit,
// begin adding 500ms of delay per request above 100:
// request # 101 is delayed by 500ms
// request # 102 is delayed by 1000ms
// request # 103 is delayed by 1500ms
// etc.
delayMs: 500
});
if(hard_limit) {
const limiter = rateLimit({
windowMs: rate_window,
max: ratelimit
});
app.use(limiter);
}
app.use(speedLimiter);
app.use('/battlelog', createProxyMiddleware({
target: battlelog_url,
changeOrigin: true,
headers: {
'User-Agent': 'Battlelog Proxy',
},
pathRewrite: {
[`^/battlelog`]: '',
},
}));
app.use('/keeper', createProxyMiddleware({
target: keeper_url,
changeOrigin: true,
headers: {
'User-Agent': 'Battlelog Proxy',
},
pathRewrite: {
[`^/keeper`]: '',
},
}));
let server = app.listen(port, host, () => {
console.log(`Starting Proxy at ${host}:${port}`);
});
// Handle ^C
process.on('SIGINT', () => {
server.close(() => {
process.exit(0)
})
});