-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrequests.go
155 lines (125 loc) · 3.18 KB
/
requests.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
/*
http/https connections
@sha0coder
*/
package main
import "fmt"
import "os"
import "net/url"
import "strings"
import "net/http"
import "io/ioutil"
import "crypto/tls"
type Requests struct {
TlsCfg *tls.Config
Transport *http.Transport
UserAgent string
ProxyUrl string
User string
Passw string
}
func NewRequests() *Requests {
h := new(Requests)
h.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:5.0.1) Gecko/20100101 Firefox/5.0.1"
h.ProxyUrl = ""
h.User = ""
h.Passw = ""
h.TlsCfg = &tls.Config{InsecureSkipVerify: true}
h.Transport = &http.Transport{
TLSClientConfig: h.TlsCfg,
}
return h
}
func (h *Requests) SetProxy(proxyurl string) {
proxy, err := url.Parse(proxyurl)
if err != nil {
fmt.Println("bad proxy url")
return
}
h.Transport.Proxy = http.ProxyURL(proxy)
h.ProxyUrl = proxyurl
}
func (h *Requests) Get(url string) (string, int, *http.Response) {
return h.Launch("GET", url, "")
}
func (h *Requests) Post(url string, post string) (string, int, *http.Response) {
return h.Launch("POST", url, post)
}
func (h *Requests) GetOrPost(url string, post string) (string, int, *http.Response) {
if post == "" {
return h.Launch("GET", url, "")
} else {
return h.Launch("POST", url, post)
}
}
func (h *Requests) Options(url string) (string, int, *http.Response) {
return h.Launch("OPTIONS", url, "")
}
func (h *Requests) QuitOnFail(code int, msg string) {
if code == 0 {
fmt.Println(msg)
os.Exit(1)
}
}
func (r *Requests) SetBasicAuth(user string, passw string) {
// This call is not thread-safe
r.User = user
r.Passw = passw
}
func (h *Requests) LaunchNoRead(method string, url string, post string) *http.Response {
req, err := http.NewRequest(method, url, strings.NewReader(post)) // hace la resolucion dns aqui?
if err != nil {
fmt.Println("Server is not responding :/")
return nil
}
if h.User != "" {
req.SetBasicAuth(h.User, h.Passw)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
req.Header.Set("User-Agent", h.UserAgent)
req.Header.Set("Accept-Encoding", "*")
client := &http.Client{Transport: h.Transport}
resp, err := client.Do(req)
if err != nil || resp == nil {
return nil
}
if resp.Body == nil {
return nil
}
return resp
}
func (h *Requests) Launch(method string, url string, post string) (string, int, *http.Response) {
req, err := http.NewRequest(method, url, strings.NewReader(post)) // hace la resolucion dns aqui?
if err != nil {
fmt.Println("Server is not responding :/")
return "", 0, nil
}
if h.User != "" {
req.SetBasicAuth(h.User, h.Passw)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
req.Header.Set("User-Agent", h.UserAgent)
req.Header.Set("Accept-Encoding", "*")
client := &http.Client{Transport: h.Transport}
resp, err := client.Do(req)
if err != nil || resp == nil {
return "", 0, nil
}
code := resp.StatusCode
if resp.Body == nil {
return "", code, resp
}
html, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", code, resp
}
resp.Body.Close()
return string(html), code, resp
}
/*
func test() {
h := NewHTTP()
html, code := h.Get("https://google.com")
fmt.Println(code)
fmt.Println(html)
}*/