-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·87 lines (73 loc) · 2.06 KB
/
server.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
87
/**
* server.js: Web应用防火墙的服务器入口
* 基于koa的http代理程序
*/
const koa = require('koa');
const request = require('koa-request');
const bodyParser = require('koa-bodyparser');
const yaml = require('js-yaml');
const fs = require('fs');
const waf = require('./src/index.js');
const config = yaml.safeLoad(fs.readFileSync(`${__dirname}/server.yml`, 'utf-8'));
const app = new koa();
const firewall = new waf(config.waf);
app.use(bodyParser());
/**
* waf中间件:修改请求和响应
* 调用waf的process方法,在请求前processRequest,请求后
* processResponse
* meta提供给waf使用
*/
app.use(async (ctx, next) => {
const meta = {};
if (firewall.processRequest(
ctx.request.method,
ctx.request.url,
ctx.request.headers,
ctx.request.body,
meta
)) {
await next();
}
const res = firewall.processResponse(
ctx.response.status,
ctx.response.headers,
ctx.response.body,
meta
);
ctx.status = res.status;
ctx.set(res.headers);
ctx.body = res.body;
})
/**
* 日志中间件:打印日志,输出代理延时
*/
app.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
console.log(`${ctx.status} ${ctx.method} ${ctx.url} - ${ms}`);
});
/**
* 代理中间件:将请求转发至目标服务器
* 请求包括url,method,headers,body
* 响应包括headers,body
*/
app.use(function *() {
const { scheme, host, port } = config.proxy;
const headers = this.request.headers;
headers['host'] = host;
headers['Accept-Encoding'] = 'chunked';
const url = `${scheme}://${host}:${port}${this.request.url}`
const response = yield request({
url: url,
headers: headers,
method: this.request.method,
body: this.request.body,
});
this.status = response.statusCode;
this.set(response.headers);
this.body = response.body;
});
const { host, port } = config.server;
app.listen(port, host);