forked from rahulsmehta/node-replay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replay.js
executable file
·166 lines (139 loc) · 4.35 KB
/
replay.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var server = require('./lib/proxy.js'),
url = require('url'),
fs = require('fs'),
https = require('https'),
crypto = require('crypto');
var scriptArgs = process.argv.slice(2),
flag = scriptArgs[0];
var isCap;
if (flag) {
if (flag.charAt(0) === '-') {
cmd = flag.substring(1);
if (cmd === 'c') isCap = true;
else if (cmd === 'r') isCap = false;
}
}
if (typeof isCap === "undefined") isCap = true;
console.log(isCap ? "Running in capture mode..." : "Running in replay mode...");
var proxy_config = {
'proxy-port': 8080,
'verbose': false
},
excludes = ["/GOST/"];
var utils = {
'contains': function (orig, substr) {
return orig.indexOf(substr) !== -1;
},
'hasExclude': function (urlStr) {
var i;
for (i = 0; i < excludes.length; ++i) {
if (this.contains(urlStr, excludes[i])) {
return true;
}
}
return false;
},
'hash': function (data, method, path) {
var md5 = crypto.createHash('md5');
md5.update((data + method + path), 'utf8'); // add command line flag for file format
return md5.digest('hex');
}
}
var cacheDir = "data/";
var capture = function (proxy) {
var _url;
var hashData, hashMethod, hashPath, _status;
var resData, resHead;
proxy.on('request', function (req) {
_url = req.url;
hashPath = _url;
hashMethod = req.method;
});
proxy.on('request_data', function (data) {
hashData = data.toString('utf8', 0, data.length);
});
proxy.on('response', function (response) {
var _data = response.socket._httpMessage;
_status = response.statusCode;
resHead = response.headers;
});
proxy.on('response_data', function (data) {
resData = data.toString('utf8', 0, data.length);
});
proxy.on('response_end', function () {
//move the file-writing logic into here
if (!utils.hasExclude(_url)) {
var fn = utils.hash(hashData, hashMethod, hashPath),
fd = fs.openSync(cacheDir + fn, 'w+');
try {
resData = JSON.parse(resData);
} catch (err) {
resData = {};
}
console.log(_url + ' -> ' + fn);
var fileStore = {
'url':hashPath,
'method':hashMethod,
'body':hashData,
'code': _status,
'headers': resHead,
'data': resData
},
toWrite = JSON.stringify(fileStore);
fs.writeSync(fd, toWrite);
} else {
console.log("Excluding " + _url);
}
});
};
var replay = function (proxy) {
this.url_rewrite = function (req_url) {
var path = url.format(req_url.path);
if (!utils.hasExclude(path)) {
req_url.hostname = 'localhost';
req_url.port = 8888;
} else {
console.log("Excluding " + path);
}
};
}
new server(proxy_config, isCap ? capture : replay);
if (!isCap) {
var _key = fs.readFileSync("lib/certs/agent2-key.pem", 'utf8'),
_cert = fs.readFileSync("lib/certs/agent2-cert.pem", 'utf8'),
replay_cred = {
key: _key,
cert: _cert
};
replayServer = https.createServer(replay_cred, function (req, res) {
var _data;
function respond(data, method, req_url) {
var fn = utils.hash(data, method, req_url);
console.log(req_url + " -> " + fn);
var fileData,
err = false;
try {
fileData = JSON.parse(fs.readFileSync(cacheDir + fn, 'utf8'));
} catch (errorThrown) {
err = true;
}
if (err) {
res.write('{}');
res.end();
} else {
res.writeHead(fileData.code, fileData.headers);
_resData = JSON.stringify(fileData.data);
res.write(_resData);
res.end();
}
};
req.on('data', function (data) {
_data = data.toString('utf8', 0, data.length);;
});
req.on('end', function () {
respond(_data, req.method, req.url);
});
});
replayServer.listen(8888);
console.log("Replay server started at port " + replayServer.address().port);
}