-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
254 lines (241 loc) · 6.16 KB
/
client.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
package browsermobproxy
import (
"bytes"
"encoding/json"
"fmt"
"github.com/bitly/go-simplejson"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
)
var HTTPClient = http.DefaultClient
type Params map[string]string
var ports = make(portMap,100)
type portMap map[int]bool
// jsonContentType is JSON content type.
const jsonContentType = "application/json"
type Client struct {
Host string `json:"host"`
Port int `json:"port"`
Proxy string `json:"proxy"`
}
func init(){
for i:=1;i<=100; i++{
ports[8080+i]=false
}
}
func NewClient(urlStr string,param Params,options Params) *Client{
host :="http://"+urlStr
var port int
q := new(url.URL).Query()
if param!=nil {
for key, val := range param {
q.Add(key, val)
}
}
if v,ok :=options["existing_proxy_port_to_use"]; ok{
port,_=strconv.Atoi(v)
}else{
for k,v :=range ports{
if v==false{
port=k
ports[k]=true
q.Add("port",strconv.Itoa(port))
break
}
}
resp,err :=http.PostForm(fmt.Sprintf("%s/proxy",host),q)
if err != nil {
fmt.Printf("Fail to connect, %s\n", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
res := make(map[string]interface{})
_ =json.Unmarshal(body,&res)
port = int(res["port"].(float64))
}
client :=&Client{Host:host,Port:port}
urlParts :=strings.Split(host,":")
client.Proxy=urlParts[1][2:]+":"+strconv.Itoa(port)
return client
}
//关闭客户端
func (c *Client)Close() int{
req,_ :=http.NewRequest("DELETE",fmt.Sprintf("%s/proxy/%d",c.Host,c.Port),nil)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Fail to connect, %s\n", err)
}
//释放port
ports[c.Port]=false
return resp.StatusCode
}
//get the ports
func (c *Client)proxyPorts() *simplejson.Json{
r,err :=http.Get(fmt.Sprintf("%s/proxy",c.Host))
if err!=nil{
fmt.Printf("Fail to get proxy, %s\n", err)
}
body, err := ioutil.ReadAll(r.Body)
dataJson, _ := simplejson.NewJson(body)
return dataJson
}
func(c *Client)Har() *simplejson.Json{
res,err :=http.Get(fmt.Sprintf("%s/proxy/%d/har",c.Host,c.Port))
if err != nil {
fmt.Printf("Fail to connect, %s\n", err)
}
body, err := ioutil.ReadAll(res.Body)
dataJson, _ := simplejson.NewJson(body)
return dataJson
}
func (c *Client)NewHar(ref string,options map[string]string){
options["initialPageRef"] = ref
req,_ :=http.NewRequest("PUT",fmt.Sprintf("%s/proxy/%d/har",c.Host,c.Port),nil)
q := req.URL.Query()
if options != nil {
for key, val := range options {
q.Add(key, val)
}
req.URL.RawQuery = q.Encode()
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Fail to set new har, %s\n", err)
}
body, err := ioutil.ReadAll(resp.Body)
res := make(map[string]interface{})
_ =json.Unmarshal(body,&res)
}
//加入黑名单
func(c *Client)Blacklist(regexp string, statusCode int){
req,_ :=http.NewRequest("PUT",fmt.Sprintf("%s/proxy/%d/blacklist",c.Host,c.Port),nil)
q := req.URL.Query()
q.Add("regex", regexp)
q.Add("status", strconv.Itoa(statusCode))
req.URL.RawQuery = q.Encode()
client := &http.Client{}
_, err := client.Do(req)
if err != nil {
fmt.Printf("Fail to connect, %s\n", err)
}
}
//白名单
func(c *Client)Whitelist(regexp string, statusCode int){
req,_ :=http.NewRequest("PUT",fmt.Sprintf("%s/proxy/%d/whitelist",c.Host,c.Port),nil)
q := req.URL.Query()
q.Add("regex", regexp)
q.Add("status", strconv.Itoa(statusCode))
req.URL.RawQuery = q.Encode()
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Fail to connect, %s\n", err)
}
status := resp.StatusCode
fmt.Printf("blacklist statusCode, %d\n", status)
}
//拦截请求内容
func (c *Client) ResponseInterceptor(js string) int{
resp,err :=http.Post(fmt.Sprintf("%s/proxy/%d/filter/response",c.Host,c.Port),"text/plain",strings.NewReader(js))
if err!=nil{
fmt.Println(err)
}
defer resp.Body.Close()
return resp.StatusCode
}
//请求拦截
func (c *Client) RequestInterceptor(js string) int{
resp,err :=http.Post(fmt.Sprintf("%s/proxy/%d/filter/request",c.Host,c.Port),"text/plain",strings.NewReader(js))
if err!=nil{
fmt.Println(err)
}
defer resp.Body.Close()
return resp.StatusCode
}
//new page
func (c *Client) NewPage(ref,title string) int{
params := map[string]string{
"pageRef": ref,
"pageTitle": title,
}
data, err := json.Marshal(params)
if err != nil {
fmt.Sprintln(err)
}
request,err :=http.NewRequest("PUT",fmt.Sprintf("%s/proxy/%d/har/pageRef",c.Host,c.Port),bytes.NewReader(data))
if err!=nil{
fmt.Sprintln(err)
}
request.Header.Add("Accept", jsonContentType)
response, err := HTTPClient.Do(request)
if err != nil {
fmt.Sprintln(err)
}
return response.StatusCode
}
func (c *Client)Headers(headers map[string]string) int{
data, err := json.Marshal(headers)
if err != nil {
fmt.Sprintln(err)
}
resp,err :=http.Post(fmt.Sprintf("%s/proxy/%d/headers",c.Host,c.Port),"text/plain",bytes.NewReader(data))
if err!=nil{
fmt.Sprintln(err)
}
return resp.StatusCode
}
func (c *Client) RewriteUrl(match,replace string) int{
params := map[string]string{
"matchRegex": match,
"replace": replace,
}
data, err := json.Marshal(params)
if err != nil {
fmt.Sprintln(err)
}
request,err :=http.NewRequest("PUT",fmt.Sprintf("%s/proxy/%d/rewrite",c.Host,c.Port),bytes.NewReader(data))
if err!=nil{
fmt.Sprintln(err)
}
request.Header.Add("Accept", jsonContentType)
response, err := HTTPClient.Do(request)
if err != nil {
fmt.Sprintln(err)
}
return response.StatusCode
}
func (c *Client) ClearAllRewriteUrlRules() int{
request,err :=http.NewRequest("DELETE",fmt.Sprintf("%s/proxy/%d/rewrite",c.Host,c.Port),nil)
if err!=nil{
fmt.Sprintln(err)
}
response, err := HTTPClient.Do(request)
if err != nil {
fmt.Sprintln(err)
}
return response.StatusCode
}
func (c *Client)Retry(count int) int{
params := map[string]int{
"retrycount": count,
}
data, err := json.Marshal(params)
if err != nil {
fmt.Sprintln(err)
}
request,err :=http.NewRequest("PUT",fmt.Sprintf("%s/proxy/%d/rewrite",c.Host,c.Port),bytes.NewReader(data))
if err!=nil{
fmt.Sprintln(err)
}
request.Header.Add("Accept", jsonContentType)
response, err := HTTPClient.Do(request)
if err != nil {
fmt.Sprintln(err)
}
return response.StatusCode
}