-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwebUtils.go
70 lines (54 loc) · 1.49 KB
/
webUtils.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
package main
import (
"bytes"
"net/http"
"net"
"math/rand"
"fmt"
"bufio"
)
func BuildTcpUrl(host string, port string, path string) string {
return fmt.Sprintf("%s:%s%s", host, port, path)
}
func BuildNetworkPath(protocol string, host string, port string, path string) string {
if protocol == "" {
return fmt.Sprintf("%s:%s%s", host, port, path)
}
return fmt.Sprintf("%s://%s:%s%s", protocol, host, port, path)
}
func SendHTTPRequest(endpoint string, c chan *http.Response, method string) (*http.Response, error) {
client := &http.Client{}
request, _ := http.NewRequest(method, endpoint, bytes.NewBufferString("hello"))
response, _ := client.Do(request)
c <- response
return response, nil
}
func SendRandomHTTPRequest(endpoint string, c chan *http.Response) (*http.Response, error) {
method := getRandomRequestMethod()
return SendHTTPRequest(endpoint, c, method)
}
var MAX_JUNK_LENGTH = 100
func SendCorruptHTTPData(endpoint string, c chan string) error {
conn, err := net.Dial("tcp", endpoint)
if err != nil {
fmt.Printf("%v", err)
c <- ""
return err
}
junkLength := rand.Intn(MAX_JUNK_LENGTH)
junkStr := CreateRandomString(junkLength)
fmt.Fprintf(conn, "%s\n", junkStr)
status, err := bufio.NewReader(conn).ReadString('\n')
c <- status
return err
}
func getRandomRequestMethod() string {
diceRoll := rand.Intn(100)
if diceRoll < 25 {
return "GET"
} else if diceRoll < 50 {
return "POST"
} else {
return "HEAD"
}
}