forked from Danny-Dasilva/CycleTLS
/
index.go
289 lines (234 loc) · 6.32 KB
/
index.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package cycletls
import (
"encoding/json"
"flag"
"io"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"runtime"
"strings"
"time"
"github.com/gorilla/websocket"
)
// Options sets CycleTLS client options
type Options struct {
InsecureSkipVerify bool `json:"insecureSkipVerify"`
URL string `json:"url"`
Method string `json:"method"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
Ja3 string `json:"ja3"`
UserAgent string `json:"userAgent"`
Proxy string `json:"proxy"`
Timeout int `json:"timeout"`
DisableRedirect bool `json:"disableRedirect"`
CookiesJar *cookiejar.Jar `json:"cookiesJar"`
}
type cycleTLSRequest struct {
RequestID string `json:"requestId"`
Options Options `json:"options"`
}
//rename to request+client+options
type fullRequest struct {
req *http.Request
client http.Client
options cycleTLSRequest
}
//TODO: rename this response struct
type respData struct {
Status int
BodyReader io.ReadCloser
Body string
Headers map[string]string
Url string
}
//Response contains Cycletls response data
type Response struct {
RequestID string
Response respData
}
//CycleTLS creates full request and response
type CycleTLS struct {
ReqChan chan fullRequest
RespChan chan Response
}
func getWebsocketAddr() string {
port, exists := os.LookupEnv("WS_PORT")
var addr *string
if exists {
addr = flag.String("addr", "localhost:"+port, "http service address")
} else {
addr = flag.String("addr", "localhost:9112", "http service address")
}
u := url.URL{Scheme: "ws", Host: *addr, Path: "/"}
return u.String()
}
// ready Request
func processRequest(request cycleTLSRequest) (result fullRequest) {
var browser = browser{
InsecureSkipVerify: request.Options.InsecureSkipVerify,
JA3: request.Options.Ja3,
UserAgent: request.Options.UserAgent,
CookieJar: request.Options.CookiesJar,
}
client, err := newClient(
browser,
request.Options.Timeout,
request.Options.DisableRedirect,
request.Options.UserAgent,
request.Options.Proxy,
)
if err != nil {
log.Fatal(err)
}
req, err := http.NewRequest(strings.ToUpper(request.Options.Method), request.Options.URL, strings.NewReader(request.Options.Body))
if err != nil {
log.Print(request.RequestID + "Request_Id_On_The_Left" + err.Error())
return
}
for k, v := range request.Options.Headers {
if k != "host" {
req.Header.Set(k, v)
}
}
return fullRequest{req: req, client: client, options: request}
}
func dispatcher(res fullRequest) (response Response, err error) {
resp, err := res.client.Do(res.req)
if err != nil {
parsedError := parseError(err)
headers := make(map[string]string)
respData := respData{Status: parsedError.StatusCode, Body: parsedError.ErrorMsg + "-> \n" + string(err.Error()), Headers: headers}
return Response{res.options.RequestID, respData}, nil //normally return error here
// return response, err
}
headers := make(map[string]string)
for name, values := range resp.Header {
if name == "Set-Cookie" {
headers[name] = strings.Join(values, "/,/")
} else {
for _, value := range values {
headers[name] = value
}
}
}
respData := respData{
Status: resp.StatusCode,
BodyReader: resp.Body,
Headers: headers,
Url: resp.Request.URL.String(),
}
return Response{res.options.RequestID, respData}, nil
}
// Queue queues request in worker pool
func (client CycleTLS) Queue(URL string, options Options, Method string) {
options.URL = URL
options.Method = Method
//TODO add timestamp to request
opt := cycleTLSRequest{"n", options}
response := processRequest(opt)
client.ReqChan <- response
}
// Do creates a single request
func (client CycleTLS) Do(URL string, options Options, Method string) (response Response, err error) {
options.URL = URL
options.Method = Method
opt := cycleTLSRequest{"cycleTLSRequest", options}
res := processRequest(opt)
response, err = dispatcher(res)
if err != nil {
log.Print("Request Failed: " + err.Error())
return response, err
}
return response, nil
}
//TODO rename this
// Init starts the worker pool or returns a empty cycletls struct
func Init(workers ...bool) CycleTLS {
if len(workers) > 0 && workers[0] {
reqChan := make(chan fullRequest)
respChan := make(chan Response)
go workerPool(reqChan, respChan)
log.Println("Worker Pool Started")
return CycleTLS{ReqChan: reqChan, RespChan: respChan}
}
return CycleTLS{}
}
// Close closes channels
func (client CycleTLS) Close() {
close(client.ReqChan)
close(client.RespChan)
}
// Worker Pool
func workerPool(reqChan chan fullRequest, respChan chan Response) {
//MAX
for i := 0; i < 100; i++ {
go worker(reqChan, respChan)
}
}
// Worker
func worker(reqChan chan fullRequest, respChan chan Response) {
for res := range reqChan {
response, err := dispatcher(res)
if err != nil {
log.Print("Request Failed: " + err.Error())
}
respChan <- response
}
}
func readSocket(reqChan chan fullRequest, c *websocket.Conn) {
for {
_, message, err := c.ReadMessage()
if err != nil {
log.Print("Socket Error", err)
continue
}
request := new(cycleTLSRequest)
err = json.Unmarshal(message, &request)
if err != nil {
log.Print("Unmarshal Error", err)
return
}
reply := processRequest(*request)
reqChan <- reply
}
}
func writeSocket(respChan chan Response, c *websocket.Conn) {
for {
select {
case r := <-respChan:
message, err := json.Marshal(r)
if err != nil {
log.Print("Marshal Json Failed" + err.Error())
continue
}
err = c.WriteMessage(websocket.TextMessage, message)
if err != nil {
log.Print("Socket WriteMessage Failed" + err.Error())
continue
}
}
}
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
start := time.Now()
defer func() {
log.Println("Execution Time: ", time.Since(start))
}()
websocketAddress := getWebsocketAddr()
c, _, err := websocket.DefaultDialer.Dial(websocketAddress, nil)
if err != nil {
log.Print(err)
return
}
reqChan := make(chan fullRequest)
respChan := make(chan Response)
go workerPool(reqChan, respChan)
go readSocket(reqChan, c)
//run as main thread
writeSocket(respChan, c)
}