forked from JCMais/node-libcurl
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbrute-force-leak-test.ts
More file actions
207 lines (182 loc) · 4.93 KB
/
brute-force-leak-test.ts
File metadata and controls
207 lines (182 loc) · 4.93 KB
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
/* eslint-disable no-inner-declarations */
/**
* Copyright (c) Jonathan Cardoso Machado. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { Curl, CurlHsts, Easy } from '../lib'
import readline from 'readline'
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
const MAX_REQUESTS_AT_ONCE = 50
const AMOUNT_ITERATIONS_PER_RUN = 1e4
let instances: Easy[] | Curl[] = []
let iteration = 0
function createAndCloseCurlHandlesTest() {
let i = 0
const shouldClose = iteration++ % 2
const postData = [
{
name: 'file',
file: 'test.img',
type: 'image/png',
},
]
return () => {
if (shouldClose) {
console.log('Closing handles.')
} else {
console.log('Opening handles.')
}
while (i++ < AMOUNT_ITERATIONS_PER_RUN) {
if (shouldClose) {
const instance = instances.splice(i, 1)
if (instance) {
instances[i].close()
}
} else {
const handle = new Easy()
handle.setOpt('HTTPPOST', postData)
handle.setOpt(Curl.option.XFERINFOFUNCTION, () => 0)
instances[i] = handle
}
}
if (global.gc && shouldClose) {
console.log('Calling garbage collector.')
global.gc()
}
}
}
function createAndPerformAndCloseCurlHandlesMultiTest() {
let i = AMOUNT_ITERATIONS_PER_RUN
let toClose: Curl[] = []
let toPerform: Curl[] = []
let currentlyPerforming: Curl[] = []
const postData = [
{
name: 'field',
contents: 'value',
},
]
const getHstsCache = () => [
{
host: 'owasp.org',
expire: null, // infinite
},
{
host: 'github.com',
expire: '20350101 00:00:00', // infinite
},
{
host: 'donotcall.gov',
includeSubdomain: true,
},
]
return function run() {
return new Promise<void>((resolve) => {
while (i--) {
console.log('creating handler')
const handle = new Curl()
handle.setOpt('HTTPPOST', postData)
handle.setOpt('URL', 'http://127.0.0.1:8080/index.html')
handle.setOpt('HSTS_CTRL', CurlHsts.Enable)
handle.setOpt('HSTSREADFUNCTION', () => getHstsCache())
instances[i] = handle
toPerform.push(handle)
function doCleanupIfLastOne() {
const totalFinished = toClose.length
const total = instances.length
if (totalFinished === total) {
for (const handle of toClose) {
try {
handle.close()
} catch (error) {
console.error('error while closing handle', error)
}
}
toClose = []
toPerform = []
currentlyPerforming = []
instances = []
console.log('toClose.length', toClose.length)
console.log('instances.length', instances.length)
console.log('toPerform.length', toPerform.length)
console.log(
'currentlyPerforming.length',
currentlyPerforming.length,
)
console.log('Calling garbage collector.')
global.gc()
resolve()
}
}
handle.on('end', (status, data, headers, handle) => {
toClose.push(handle)
console.log('end', status)
currentlyPerforming.splice(currentlyPerforming.indexOf(handle), 1)
doPerformIfNeeded()
doCleanupIfLastOne()
})
handle.on('error', (error, code, handle) => {
toClose.push(handle)
console.log('error', error?.message || code)
currentlyPerforming.splice(currentlyPerforming.indexOf(handle), 1)
doPerformIfNeeded()
doCleanupIfLastOne()
})
}
function doPerformIfNeeded() {
const instancesToPerform = toPerform.splice(
0,
MAX_REQUESTS_AT_ONCE - currentlyPerforming.length,
)
for (const instance of instancesToPerform) {
instance.perform()
}
}
doPerformIfNeeded()
})
}
}
function clearInstances() {
return async () => {
const instance = instances.shift()
while (instance) {
try {
instance.close()
iteration = 0
} catch (error) {
/* noop */
}
}
}
}
function loop() {
rl.question(
[
`Type number to proceed, where number is:`,
`[1]: createAndCloseCurlHandlesTest`,
`[2]:createAndPerformAndCloseCurlHandlesTest`,
`[3]:clearInstances`,
``,
].join('\n'),
async function (answer) {
switch (answer) {
case '1':
await createAndCloseCurlHandlesTest()()
break
case '2':
await createAndPerformAndCloseCurlHandlesMultiTest()()
break
case '3':
default:
await clearInstances()()
}
process.nextTick(loop)
},
)
}
loop()