-
Notifications
You must be signed in to change notification settings - Fork 18
/
siege_attack.js
351 lines (295 loc) · 9.62 KB
/
siege_attack.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
var http = require('http')
, https = require('https')
, querystring = require('querystring')
, util = require('util')
, cookiejar = require('cookiejar')
, CookieJar = cookiejar.CookieJar
, out = process.stdout
;
// http://bluesock.org/~willg/dev/ansi.html
var HIDE_CURSOR = '\033[?25l'
var SHOW_CURSOR = '\033[?25h'
var CLEAR_SCREEN = '\033[2J'
var ERASE_LINE = '\033[K'
var RESET_STYLE = '\033[0m'
function clearScreen() {
out.write(CLEAR_SCREEN)
}
function upLine(line) {
out.write('\033[' + (line || 1) + 'A')
}
function gotoLine(line, col) {
out.write('\033[' + (line | 0) + ';' + (col | 0) + 'H')
}
// 0-5
function background(r, g, b) {
return '\033[48;5;' + rgb5(r, g, b) + 'm'
}
// 0-5
function forground(r, g, b) {
var code = rgb5(r, g, b);
return '\033[38;5;' + code + 'm'
}
/**
* Translates a 255 RGB value to a 0-5 ANSI RGV value,
* then returns the single ANSI color code to use.
*/
function rgb (r, g, b) {
var red = r / 51 // /255 * 5
, green = g / 51 // /255 * 5
, blue = b / 51 // /255 * 5
return rgb5(red, green, blue)
}
/**
* Turns rgb 0-5 values into a single ANSI color code to use.
*/
function rgb5 (r, g, b) {
var red = Math.round(r)
, green = Math.round(g)
, blue = Math.round(b)
return 16 + (red*36) + (green*6) + blue
}
// red to green
gradeColors = [ 196, 202, 166, 172, 136, 142, 106, 112, 76, 46 ].map(function(color){
return '\033[38;5;' + color + 'm'
})
// return grade color, big is better
function gradeColor(value, worst, best) {
var score = (value - worst) / (best - worst)
score = Math.min(Math.max(score, 0), 1)
var index = Math.round(score * 9)
return gradeColors[index]
}
module.exports = function(options, callback) {
var taskIndex = 0
, globalJar
;
out.write('\n')
if(options.enableCookie) {
globalJar = new CookieJar()
out.write('\033[38;5;46mEnable cookie\033[0m\n')
}
function nextTask() {
var task = options.tasks[taskIndex ++]
if(!task) {
if(callback) callback()
process.exit()
}
var enableCookie = task.enableCookie || (task.enableCookie === undefined && options.enableCookie)
var jar = enableCookie && (globalJar || new CookieJar())
var startTime = Date.now();
var intervalStart = startTime;
var intervalDone = 0;
var running = 0;
var done = 0;
var concurrent = options.concurrent || 15;
var repeat = task.repeat || options.repeat;
var duration = task.duration || options.duration;
if(!duration && !repeat) {
duration = 10000
}
if (options.sslProtocol && options.sslProtocol === true) {
concurrent = https.globalAgent.maxSockets = task.concurrent || options.concurrent || 15;
} else {
concurrent = http.globalAgent.maxSockets = task.concurrent || options.concurrent || 15;
}
var left = typeof repeat == 'undefined' ? Number.MAX_VALUE : repeat;
var min = Number.MAX_VALUE
var max = 0;
var avg = 0;
var rps = 0;
var errorsCount = 0;
var errors = {};
var status = {};
var sumTime = 0;
var headers = options.headers || {}
var requestOptions = {
path: task.path
, method: task.method
, headers: headers
}
if(options.sockpath) {
requestOptions.socketPath = options.sockpath
} else {
requestOptions.port = options.port
if (options.host) {
requestOptions.host = options.host
}
if (options.hostname) {
requestOptions.hostname = options.hostname
}
}
if (options.rejectUnauthorized) {
requestOptions.rejectUnauthorized = options.rejectUnauthorized;
}
if (options.requestCert) {
requestOptions.requestCert = options.requestCert;
}
if (options.agent) {
requestOptions.agent = options.agent;
}
var cookieAccessInfo = cookiejar.CookieAccessInfo(requestOptions.host, requestOptions.path)
function sendRequest() {
if(running > concurrent || left <=0) return;
if(running ++ < concurrent) process.nextTick(sendRequest);
if(enableCookie) {
headers['Cookie'] = jar.getCookies(cookieAccessInfo).map(function(cookie) {return cookie.toValueString()}).join(';')
}
// Add POST Headers for POST Requests
if(requestOptions.method === 'POST' && task.body) {
headers['Content-Type'] = 'application/x-www-form-urlencoded'
headers['Content-Length'] = querystring.stringify(task.body).length
}
var reqStartTime = Date.now();
var req;
// Add QueryString to URL for GET Requests with Parameters
if(requestOptions.method === 'GET' && task.query) {
// Reset query string if present
var hasQuery = requestOptions.path.indexOf('?')
if(hasQuery > 0) {
requestOptions.path = requestOptions.path.substring(0,hasQuery)
}
requestOptions.path = requestOptions.path + "?" + querystring.stringify(task.query)
}
if (options.sslProtocol && options.sslProtocol === true) {
req = https.request(requestOptions,handleRequest);
} else {
req = http.request(requestOptions,handleRequest);
}
function handleRequest(res) {
if(enableCookie) {
var cookies = res.headers['set-cookie']
if(cookies) jar.setCookies(cookies)
}
res.on('end', function() {
var resEndTime = Date.now();
var elapsed = resEndTime - reqStartTime;
if(elapsed < min) min = elapsed;
if(elapsed > max) max = elapsed;
sumTime += elapsed;
intervalDone ++;
done ++;
status[res.statusCode] = (status[res.statusCode] || 0) + 1
endRequest();
})
res.resume()
}
req.on('error', function(err){
var data = errors[err.message];
if(!data) {
data = errors[err.message] = {
message: err.message
, stack: err.stack
, count: 1
}
if(!firstTime) {
upLine(5)
out.write('\033[K\n\033[K\n\033[K\n\033[K\n\033[K\n')
upLine(5)
}
console.log(err.stack)
firstTime = true
}
data.count ++
errorsCount ++
endRequest()
})
// Add POST Body for POST requests
if(requestOptions.method === 'POST' && task.body) {
req.write(querystring.stringify(task.body));
}
req.end();
function endRequest() {
if(--left == 0) {
endTask();
}
running --;
process.nextTick(sendRequest);
}
}
function updateTaskData() {
var now = Date.now()
avg = sumTime / done;
rps = done * 1000 / (now - startTime)
realtime_rps = intervalDone * 1000 / (now - intervalStart)
intervalStart = now
intervalDone = 0
reportData()
}
var firstTime = true;
function reportData(type) {
switch(type) {
case 'csv':
out.write(util.format('\r%s:%s\t%s(done)\t%s(rps)\t%s(curr rps)\t%sms(min)\t%sms(max)\t%sms(avg)'
, task.method
, task.path
, done
, gradeColor(rps, 1000, 5000) + parseInt(rps) //+ RESET_STYLE
, gradeColor(realtime_rps, 1000, 5000) + parseInt(realtime_rps)// + RESET_STYLE
, gradeColor(min, 50, 10) + parseInt(min) //+ RESET_STYLE
, gradeColor(max, 50, 10) + parseInt(max) //+ RESET_STYLE
, gradeColor(avg, 50, 10) + parseInt(avg) //+ RESET_STYLE
));
break
default:
if(!firstTime) {
upLine(5)
}
out.write('\n\033[K' + task.method + ':' + task.path)
if(task.enableCookie !== undefined) {
out.write(enableCookie ? (forground(0, 5, 0) + ' with cookie') : (forground(5, 5, 0) + ' without cookie'))
out.write(RESET_STYLE)
}
out.write('\n\033[K\tdone:' + done + (errorsCount ? ('\terrors:' + forground(5,0,0) + errorsCount + RESET_STYLE) : '' ))
out.write('\n\033[K')
Object.keys(status).forEach(function(code){
var score
if(code >= 500) {
score = 0
} else if(code >= 400) {
score = 3
} else if (code >= 300) {
score = 8
} else if (code >= 200) {
score = 10
} else {
score = 7
}
if (options.sslProtocol && options.sslProtocol === true) {
out.write('\t' + gradeColor(score, 0, 10) + code + RESET_STYLE + ' ' + https.STATUS_CODES[code] + ': ' + status[code])
} else {
out.write('\t' + gradeColor(score, 0, 10) + code + RESET_STYLE + ' ' + http.STATUS_CODES[code] + ': ' + status[code])
}
})
out.write(
util.format('\n\033[K\trps: %s\n\033[K\tresponse: %sms(min)\t%sms(max)\t%sms(avg)\033[K'
, gradeColor(rps, 2000, 7000) + parseInt(rps) + RESET_STYLE
, gradeColor(min, 50, 10) + parseInt(min) + RESET_STYLE
, gradeColor(max, 50, 10) + parseInt(max) + RESET_STYLE
, gradeColor(avg, 50, 10) + parseInt(avg) + RESET_STYLE
))
out.write(HIDE_CURSOR)
}
firstTime = false
}
function endTask () {
var ending = Date.now();
left = 0;
updateTaskData()
console.log('')
clearInterval(timer)
if(timeout) clearTimeout(timeout)
process.nextTick(nextTask)
}
var timer = setInterval(updateTaskData, 100);
var timeout = duration && setTimeout(endTask, duration)
sendRequest();
}
nextTask()
function halt() {
out.write(SHOW_CURSOR)
}
return {
halt: halt
}
}