-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.go
149 lines (119 loc) · 3.1 KB
/
browser.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
package browser
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/http/httputil"
"net/url"
"time"
)
const (
DEVELOPER = "developer"
HOMOLOGATION = "homologation"
PRODUCTION = "production"
)
type BrowserCli interface {
Get(url string) (*http.Response, error)
Post(url string, payload io.Reader) (*http.Response, error)
Put(url string, payload io.Reader) (*http.Response, error)
Delete(url string) (*http.Response, error)
List(url string) (*http.Response, error)
Patch(url string, payload io.Reader) (*http.Response, error)
CopyConfig() BrowserConfig
AddHeader(key, value string)
SetHeader(key, value string)
GetHeader() http.Header
SetUserAgentName(name string)
}
type BrowserConfig struct {
BaseURL string `json:"base_url"`
SSLVerify bool `json:"ssl_verify"`
Header http.Header `json:"header"`
Timeout int64 `json:"timeout"`
TLSClientConfig *tls.Config `json:"-"`
ProxyURL string `json:"proxy_url"`
Mode string `json:"mode"`
}
type browser_cli struct {
BrowserConfig
http_client *http.Client
http_req *http.Request
}
func NewBrowser(bro_conf BrowserConfig) BrowserCli {
new_timeout := time.Duration(10) * time.Second // Default 10 segundos
if bro_conf.Mode == "" {
bro_conf.Mode = PRODUCTION
}
jar, err := cookiejar.New(nil)
if err != nil {
fmt.Println("Erro ao criar cookiejar")
fmt.Println(err.Error())
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: !bro_conf.SSLVerify},
}
if bro_conf.TLSClientConfig != nil {
tr.TLSClientConfig = bro_conf.TLSClientConfig
}
if bro_conf.ProxyURL != "" {
proxyURL, _ := url.Parse(bro_conf.ProxyURL)
tr.Proxy = http.ProxyURL(proxyURL)
}
if bro_conf.Timeout > 0 {
new_timeout = time.Duration(bro_conf.Timeout) * time.Second
}
http_client := &http.Client{
Jar: jar,
Transport: tr,
Timeout: new_timeout,
}
client := browser_cli{
http_client: http_client,
}
client.BrowserConfig = bro_conf
if bro_conf.Header == nil {
client.Header = make(http.Header)
} else {
client.Header = bro_conf.Header
}
client.Header.Set("User-Agent", "Go Browser "+VERSION)
return &client
}
func (b *browser_cli) do(method, url string, body io.Reader) (*http.Response, error) {
if b.BaseURL != "" {
url = fmt.Sprintf("%v%v", b.BaseURL, url)
}
http_req, err := http.NewRequest(method, url, body)
if err != nil {
fmt.Printf("Erro ao criar HTTP %v Request\n", method)
fmt.Println(err.Error())
}
b.http_req = http_req
b.http_req.Header = b.Header
if b.Mode != PRODUCTION {
reqDump, err := httputil.DumpRequestOut(b.http_req, true)
if err != nil {
fmt.Println(err)
}
fmt.Println("REQUEST:")
fmt.Println(string(reqDump))
}
resp, err := b.http_client.Do(http_req)
if err != nil {
fmt.Println(err.Error())
}
if b.Mode != PRODUCTION {
respDump, err := httputil.DumpResponse(resp, true)
if err != nil {
fmt.Println(err)
}
fmt.Println("RESPONSE:")
fmt.Println(string(respDump))
}
return resp, err
}
func (b *browser_cli) CopyConfig() BrowserConfig {
return b.BrowserConfig
}