-
Notifications
You must be signed in to change notification settings - Fork 46
/
utils.go
84 lines (79 loc) · 2.01 KB
/
utils.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
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
)
func serializeDB(fname string) error {
dbdata, err := ioutil.ReadFile(fname)
if err != nil {
return err
}
if err := json.Unmarshal(dbdata, &dbData); err != nil {
return err
}
return nil
}
func validateStatusCode(code int, codes []int) bool {
for _, x := range codes {
if x == code {
return true
}
}
return false
}
func serializeRegexDB(fname string) bool {
dbdata, err := ioutil.ReadFile(fname)
if err != nil {
log.Println(err.Error())
return false
}
if err := json.Unmarshal(dbdata, ®exData); err != nil {
log.Println(err.Error())
return false
}
return true
}
func makeRequest(url string) (string, *[]byte, error) {
hostheader := strings.Split(url, "://")[1]
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: !verifySSL,
ServerName: hostheader,
},
},
Timeout: time.Duration(httpTimeout) * time.Second,
/*
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
*/
}
req, _ := http.NewRequest("GET", url, nil)
req.Host = hostheader
req.Header.Add("User-Agent", `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36`)
req.Header.Add("Accept", `text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9`)
req.Header.Add("Accept-Language", `en-GB,en-US;q=0.9,en;q=0.8`)
req.Header.Add("Accept-Encoding", `identity`)
req.Header.Add("DNT", `1`)
conn, err := client.Do(req)
if err != nil {
// log.Printf("Error making request to %s: %s", url, err.Error())
return "", nil, err
}
var respHeaders string
for key, val := range conn.Header {
respHeaders += fmt.Sprintf("%s: %s", key, strings.Join(val, ""))
}
body, err := ioutil.ReadAll(conn.Body)
if err != nil {
return "", nil, err
}
return respHeaders, &body, nil
}