forked from flyingpot/funcaptcha
-
Notifications
You must be signed in to change notification settings - Fork 18
/
util.go
75 lines (66 loc) · 1.69 KB
/
util.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
package funcaptcha
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/url"
"os"
"strings"
"time"
http "github.com/bogdanfinn/fhttp"
)
func toJSON(data interface{}) string {
str, _ := json.Marshal(data)
return string(str)
}
func jsonToForm(data string) string {
// Unmarshal into map
var form_data map[string]interface{}
json.Unmarshal([]byte(data), &form_data)
// Use reflection to convert to form data
var form url.Values = url.Values{}
for k, v := range form_data {
form.Add(k, fmt.Sprintf("%v", v))
}
return form.Encode()
}
func (s *Session) DownloadChallenge(urls []string, b64 bool) ([]string, error) {
var b64_imgs []string = make([]string, len(urls))
for i, url := range urls {
req, _ := http.NewRequest(http.MethodGet, url, nil)
req.Header = headers
resp, err := (*s.Client).Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("status code %d", resp.StatusCode)
}
body, _ := io.ReadAll(resp.Body)
// Figure out filename from URL
url_paths := strings.Split(url, "/")
if !b64 {
filename := strings.Split(url_paths[len(url_paths)-1], "?")[0]
if filename == "image" {
filename = fmt.Sprintf("image_%s.png", getTimeStamp())
}
err = os.WriteFile(filename, body, 0644)
if err != nil {
return nil, err
}
} else {
// base64 encode body
b64_imgs[i] = base64.StdEncoding.EncodeToString(body)
}
}
return b64_imgs, nil
}
func getTimeStamp() string {
return fmt.Sprintf("%d", time.Now().UnixNano()/int64(time.Millisecond))
}
func getRequestId(sessionId string) string {
pwd := fmt.Sprintf("REQUESTED%sID", sessionId)
return Encrypt(`{"sc":[147,307]}`, pwd)
}