-
Notifications
You must be signed in to change notification settings - Fork 4
/
amazoncaptcha_test.go
398 lines (355 loc) · 9.97 KB
/
amazoncaptcha_test.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package amazoncaptcha
import (
"bytes"
"encoding/json"
"fmt"
"image"
"image/png"
"os"
"path"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"testing"
"github.com/PuerkitoBio/goquery"
"github.com/go-resty/resty/v2"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)
const (
dirName = "./captchas"
failedDir = "./failed_image"
trainingDataDir = "./training_data"
maxWorkers = 50
)
func initDir(t *testing.T) {
dirs := []string{
dirName, failedDir, trainingDataDir,
}
for _, dir := range dirs {
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.Mkdir(dir, 0777); err != nil {
t.Fatalf("Failed to create directory %s: %v\n", dir, err)
}
}
}
// Loop through each letter of the alphabet
for c := 'A'; c <= 'Z'; c++ {
// Create the full path to the letter directory
letterDir := path.Join(trainingDataDir, string(c))
// Check if the letter directory already exists
if _, err := os.Stat(letterDir); os.IsNotExist(err) {
// Create the letter directory if it doesn't exist
err := os.Mkdir(letterDir, 0777)
if err != nil {
fmt.Println(err)
}
} else if err != nil {
// Handle any other errors that occurred
fmt.Println(err)
}
}
}
func TestSolveBatch(t *testing.T) {
initDir(t)
files, err := os.ReadDir(dirName)
if err != nil {
t.Fatalf("Error reading directory %s: %v\n", dirName, err)
}
var testFiles []string
for _, file := range files {
if file.IsDir() || !strings.HasSuffix(file.Name(), ".jpg") {
continue
}
testFiles = append(testFiles, file.Name())
}
var successes, total int32
var failedFiles []string
t.Logf("Processing %d files with %d workers...\n", len(testFiles), maxWorkers)
fileChan := make(chan string, len(testFiles))
for _, file := range testFiles {
fileChan <- file
}
close(fileChan)
var wg sync.WaitGroup
for i := 0; i < maxWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for file := range fileChan {
imagePath := filepath.Join(dirName, file)
imageFile, err := os.ReadFile(imagePath)
if err != nil {
t.Logf("Failed to open image file %s: %v", imagePath, err)
continue
}
result, err := Solve(bytes.NewReader(imageFile))
if err != nil {
t.Logf("Failed to solve captcha in image file %s: %v", imagePath, err)
continue
}
atomic.AddInt32(&total, 1)
if result == file[:len(file)-4] {
atomic.AddInt32(&successes, 1)
} else {
failedFiles = append(failedFiles, fmt.Sprintf("%s -> %s", path.Join(dirName, file), result))
err = os.Rename(imagePath, filepath.Join(failedDir, file))
if err != nil {
t.Logf("Failed to move failed image file %s: %v", imagePath, err)
}
}
curSuccesses := atomic.LoadInt32(&successes)
curTotal := atomic.LoadInt32(&total)
successRate := float32(curSuccesses) / float32(curTotal) * 100
progress := float32(curTotal) / float32(len(testFiles)) * 100
t.Logf("Processing file %s... success rate: %.2f%%, progress: %.2f%%", file, successRate, progress)
}
}()
}
wg.Wait()
t.Logf("Processed %d files with success rate: %d/%d (%.2f%%)", total, successes, total, float32(successes)/float32(total)*100)
if len(failedFiles) > 0 {
t.Logf("Failed to solve captcha in the following files:")
for _, file := range failedFiles {
t.Logf("%s", file)
}
}
}
func TestSplitAndSaveCaptchaByLetter(t *testing.T) {
initDir(t)
files, err := os.ReadDir(dirName)
if err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
jobs := make(chan string, len(files))
for i := 0; i < maxWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for job := range jobs {
if !strings.HasSuffix(job, ".jpg") {
continue
}
imgPath := filepath.Join(dirName, job)
readFile, err := os.ReadFile(imgPath)
if err != nil {
t.Logf("Error reading image file %s: %v", imgPath, err)
continue
}
letters, err := FindLetters(bytes.NewReader(readFile))
if err != nil {
t.Logf("Failed to split captcha in image file %s: %v", imgPath, err)
continue
}
arr := strings.TrimSuffix(job, ".jpg")
if len(arr) == 6 && len(letters) > 5 && len(letters) < 8 {
for x, v := range arr {
letterPath := filepath.Join(trainingDataDir, string(v))
filename := uuid.New().String() + ".png"
filename = filepath.Join(letterPath, filename)
file, err := os.Create(filename)
if err != nil {
t.Logf("Failed to save letter %s for captcha %s: %v", string(v), imgPath, err)
continue
}
defer file.Close()
err = png.Encode(file, letters[x])
if err != nil {
t.Logf("Failed to save letter %s for captcha %s: %v", string(v), imgPath, err)
continue
}
}
} else {
_ = os.Remove(imgPath)
t.Logf("Invalid captcha file name: %s", job)
}
}
}()
}
for _, file := range files {
jobs <- file.Name()
}
close(jobs)
wg.Wait()
t.Logf("Successfully split and saved captcha letters from %d files", len(files))
}
func TestExtractFeatures(t *testing.T) {
initDir(t)
tmpMap := make(map[string][]string)
files, err := os.ReadDir(trainingDataDir)
if err != nil {
panic(err)
}
for _, file := range files {
if file.IsDir() && len(file.Name()) == 1 && file.Name()[0] >= 'A' && file.Name()[0] <= 'Z' {
featureList := make([]string, 0)
subDirPath := filepath.Join(trainingDataDir, file.Name())
subDirFiles, err := os.ReadDir(subDirPath)
if err != nil {
panic(err)
}
var wg sync.WaitGroup
for _, subFile := range subDirFiles {
if !subFile.IsDir() && strings.HasSuffix(strings.ToLower(subFile.Name()), ".png") {
imgPath := filepath.Join(subDirPath, subFile.Name())
wg.Add(1)
go func(imgPath string) {
defer wg.Done()
imgFile, err := os.Open(imgPath)
if err != nil {
t.Errorf("open image imgFile error: %s, %v\n", imgPath, err)
return
}
defer imgFile.Close()
img, err := png.Decode(imgFile)
if err != nil {
t.Errorf("png.Decode error: %s, %v\n", imgPath, err)
_ = os.Remove(imgPath)
return
}
features, err := ExtractFeatures(img.(*image.Gray))
if err != nil {
t.Errorf("ExtractFeatures error: %s, %v\n", imgPath, err)
return
}
featureList = append(featureList, features)
}(imgPath)
}
}
wg.Wait()
tmpMap[file.Name()] = removeDuplicates(featureList)
}
}
featureMap2 := make(map[string]string)
for k, arr := range tmpMap {
for _, v := range arr {
featureMap2[v] = k
}
}
jsonBytes, err := json.MarshalIndent(featureMap2, "", " ")
if err != nil {
t.Errorf("Failed to marshal feature map to json: %v\n", err)
return
}
jsonPath := "./training_data.json"
if err := os.WriteFile(jsonPath, jsonBytes, 0644); err != nil {
t.Errorf("Failed to write feature map json file: %v\n", err)
return
}
t.Logf("Extracted features for %d training images and saved to %s", len(files), jsonPath)
}
func TestDownloadCaptchaImages(t *testing.T) {
var NotFeatures = make(map[string]string)
client := resty.New()
client.SetHeaders(map[string]string{
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
"Referer": "https://www.amazon.com/errors/validateCaptcha",
"Accept-Language": "en-US,en;q=0.9",
})
var wg sync.WaitGroup
urls := make(chan string, 5000)
// Start workers to download images
for i := 0; i < maxWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for url := range urls {
resp, err := client.R().Get(url)
if err != nil {
fmt.Println(err)
continue
}
letters, err := FindLetters(bytes.NewReader(resp.Body()))
if err != nil {
fmt.Println(err)
continue
}
if len(letters) < 6 {
continue
}
for _, v := range letters {
feature, err := ExtractFeatures(v)
if err != nil {
panic(err)
}
if _, ok := featureMap[feature]; !ok {
if _, ok := NotFeatures[feature]; ok {
continue
}
NotFeatures[feature] = ""
filename := fmt.Sprintf("./captchas1/%s", path.Base(url))
err = os.WriteFile(filename, resp.Body(), os.ModePerm)
if err != nil {
panic(err)
}
break
}
}
}
}()
}
// Start workers to get image URLs
for i := 0; i < maxWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 5000/maxWorkers; j++ {
resp, err := client.R().Get("https://www.amazon.com/errors/validateCaptcha")
if err != nil {
fmt.Println(err)
continue
}
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(resp.Body()))
if err != nil {
fmt.Println(err)
continue
}
captchaImgURL, exists := doc.Find("div.a-row.a-text-center > img").Attr("src")
if !exists {
fmt.Println("failed to find captcha image URL")
continue
}
fmt.Println("enqueue -> ", captchaImgURL)
select {
case urls <- captchaImgURL:
// Send URL to the urls channel
default:
// Ignore the URL if the urls channel is closed
}
//time.Sleep(10 * time.Millisecond)
}
}()
}
// Wait for all the goroutines to exit before closing the urls channel
wg.Wait()
close(urls)
// Wait for workers to finish downloading images
wg.Wait()
}
func removeDuplicates(strSlice []string) []string {
uniqueMap := make(map[string]bool, len(strSlice))
for _, str := range strSlice {
if !uniqueMap[str] {
uniqueMap[str] = true
}
}
uniqueSlice := make([]string, 0, len(uniqueMap))
for str := range uniqueMap {
uniqueSlice = append(uniqueSlice, str)
}
return uniqueSlice
}
func TestSolveFromImageFile(t *testing.T) {
// Test the SolveFromImageFile function
result, err := SolveFromImageFile(path.Join(dirName, "AABTRE.jpg"))
assert.NoError(t, err)
assert.Equal(t, "AABTRE", result)
}
func TestSolveFromURL(t *testing.T) {
// Test the SolveFromURL function
result, err := SolveFromURL("https://images-na.ssl-images-amazon.com/captcha/sargzmyv/Captcha_kvvvwatlha.jpg")
assert.NoError(t, err)
assert.Equal(t, "MYKYAN", result)
}