forked from btfak/sniper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwork.go
365 lines (329 loc) · 7.52 KB
/
work.go
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package main
import (
"bufio"
"crypto/tls"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"strconv"
"strings"
"time"
)
func Snipe() {
for i := 0; i < config.Command.concurrent; i++ {
go run()
}
}
func run() {
c := &Worker{}
//record
c.trans = &transaction{}
//receive
if maxRecvSize < 1024 {
c.data = make([]byte, maxRecvSize)
} else {
c.data = make([]byte, 1024)
}
//https tls
if config.Basic.https {
cert, err := tls.LoadX509KeyPair(config.Ssl.cert, config.Ssl.key)
if err != nil {
fmt.Println("open ssl key err:", err)
sniperUsage()
}
c.tlsConfig = &tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true}
} else {
//http
ip := net.ParseIP(config.Basic.target[0].ip)
port, _ := strconv.Atoi(config.Basic.target[0].port)
c.tcpAddr = &net.TCPAddr{ip, port, ""}
}
if config.Protocol.connection == "close" {
for {
c.fire()
}
} else {
//keep-alive
c.shoot()
}
}
type Transactions []*transaction
type transaction struct {
currentTime time.Time
totalTime time.Duration
connectionTime time.Duration
responseTime time.Duration
code int
}
func (t Transactions) Len() int { return len(t) }
func (t Transactions) Less(i, j int) bool { return t[i].currentTime.Before(t[j].currentTime) }
func (t Transactions) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
type Worker struct {
data []byte
tcpAddr *net.TCPAddr
conn net.Conn
trans *transaction
tlsConfig *tls.Config
}
func (c *Worker) fire() {
var err error
var deadline int
c.trans.currentTime = time.Now()
//http & https
if config.Basic.https {
c.conn, err = tls.Dial("tcp", config.Basic.target[0].ip+":"+config.Basic.target[0].port, c.tlsConfig)
deadline = config.Ssl.timeout
} else {
c.conn, err = net.DialTCP("tcp", nil, c.tcpAddr)
deadline = config.Process.timeout
}
if err != nil {
c.trans.totalTime = 0
record.trans <- c.trans
return
}
c.conn.SetDeadline(c.trans.currentTime.Add(time.Second * time.Duration(deadline)))
connection := time.Now()
c.trans.connectionTime = connection.Sub(c.trans.currentTime)
_, err = c.conn.Write(message.content)
if err != nil {
c.trans.totalTime = 0
record.trans <- c.trans
c.conn.Close()
return
}
sum := 0
var isFirstLine = true
for {
n, err := c.conn.Read(c.data)
if err != nil {
if err == io.EOF {
break
}
c.trans.totalTime = 0
record.trans <- c.trans
c.conn.Close()
return
}
if isFirstLine && len(c.data) > 12 {
if c.data[9] == 50 && c.data[10] == 48 && c.data[11] == 48 {
c.trans.code = 200
}
isFirstLine = false
}
sum += n
if sum == maxRecvSize {
break
}
}
response := time.Now()
c.trans.responseTime = response.Sub(connection)
c.trans.totalTime = response.Sub(c.trans.currentTime)
record.trans <- c.trans
c.conn.Close()
return
}
func (c *Worker) shoot() {
c.connect()
for {
success := c.request()
if !success {
c.conn.Close()
c.connect()
}
}
}
func (c *Worker) connect() {
var err error
var deadline int
ip := config.Basic.target[0].ip
port := config.Basic.target[0].port
begin := time.Now()
c.trans.currentTime = begin
//http & https
if config.Basic.https {
c.conn, err = tls.Dial("tcp", ip+":"+port, c.tlsConfig)
deadline = config.Ssl.timeout
} else {
c.conn, err = net.DialTCP("tcp", nil, c.tcpAddr)
deadline = config.Process.timeout
}
if err != nil {
c.trans.totalTime = 0
record.trans <- c.trans
c.connect()
}
c.conn.SetDeadline(c.trans.currentTime.Add(time.Second * time.Duration(deadline)))
connection := time.Now()
c.trans.connectionTime = connection.Sub(begin)
}
func (c *Worker) request() bool {
connection := time.Now()
c.trans.currentTime = connection
_, err := c.conn.Write(message.content)
if err != nil {
c.trans.totalTime = 0
record.trans <- c.trans
return true
}
sum := 0
var isFirstLine = true
for {
n, err := c.conn.Read(c.data)
if err != nil {
// When server close the connection or server reset the connection
// We don't reguard it as an request error, just reconnect server.
if err == io.EOF {
return false
} else if opErr, ok := err.(*net.OpError); ok {
if opErr.Err.Error() == "connection reset by peer" {
return false
}
}
// Other situation we reguard it as request error.
c.trans.totalTime = 0
record.trans <- c.trans
return true
}
if isFirstLine && len(c.data) > 12 {
if c.data[9] == 50 && c.data[10] == 48 && c.data[11] == 48 {
c.trans.code = 200
}
isFirstLine = false
}
sum += n
if sum == maxRecvSize {
break
}
}
response := time.Now()
c.trans.responseTime = response.Sub(connection)
c.trans.totalTime = response.Sub(connection)
record.trans <- c.trans
c.trans.connectionTime = 0
return true
}
func taste() error {
var conn net.Conn
var err error
ip := config.Basic.target[0].ip
port := config.Basic.target[0].port
if config.Basic.https {
cert, err := tls.LoadX509KeyPair(config.Ssl.cert, config.Ssl.key)
if err != nil {
fmt.Println("open ssl key err:", err)
return err
}
tlsConfig := &tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true}
conn, err = tls.Dial("tcp", ip+":"+port, tlsConfig)
if err != nil {
return err
}
} else {
conn, err = net.Dial("tcp", ip+":"+port)
if err != nil {
return err
}
}
defer conn.Close()
remoteAddr := conn.RemoteAddr().String()
addr := strings.Split(remoteAddr, ":")
config.Basic.target[0].ip = addr[0]
_, err = conn.Write(message.content)
if err != nil {
return err
}
if config.Protocol.connection == "close" {
b, err := ioutil.ReadAll(conn)
if err != nil {
return err
}
maxRecvSize = len(b)
manager.HttpLen.total = len(b)
} else {
var chunked bool
var content_len int
var head_len int
buffer := bufio.NewReader(conn)
var hasContentLength bool
for {
line, _ := buffer.ReadString('\n')
head_len += len(line)
//end of header
if len(line) <= 2 {
c := strings.Replace(line, "\n", "", -1)
c = strings.Replace(c, "\r", "", -1)
if c == "" {
if hasContentLength == false && chunked == false {
return errors.New("http stack err")
}
break
}
}
//Content-Length
r := strings.Split(strings.ToLower(line), ":")
if r[0] == "content-length" {
content_len, err = strconv.Atoi(eatCRLF(r[1]))
if err != nil {
return err
}
hasContentLength = true
}
//Transfer-Encoding
if r[0] == "transfer-encoding" {
c := eatCRLF(r[1])
if c == "chunked" {
chunked = true
}
}
}
if chunked {
for {
//chunk-size
line, _ := buffer.ReadString('\n')
content_len += len(line)
//chunk-ext
tempLine := strings.Split(line, ";")
line = tempLine[0]
//CRLF
line = strings.Replace(line, "\n", "", -1)
line = strings.Replace(line, "\r", "", -1)
chunk_len, err := strconv.ParseInt(line, 16, 64)
if err != nil {
return errors.New("http stack err")
}
if chunk_len == 0 {
content_len += buffer.Buffered()
break
}
var b byte
var count int64
for {
b, err = buffer.ReadByte()
if err != nil {
return errors.New("http stack err")
}
count++
content_len += 1
if count == chunk_len+1 && b == '\n' {
break
}
if count == chunk_len+2 {
break
}
}
}
}
maxRecvSize = head_len + content_len
manager.HttpLen = httpLen{maxRecvSize, head_len, content_len}
}
return nil
}
func eatCRLF(str string) string {
c := strings.Replace(str, " ", "", -1)
c = strings.Replace(c, "\n", "", -1)
c = strings.Replace(c, "\r", "", -1)
return c
}