pass_water_wall.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package pass_water_wall
  2. import (
  3. "bytes"
  4. "fmt"
  5. "image/jpeg"
  6. "math"
  7. "strings"
  8. "time"
  9. "github.com/allanpk716/ChineseSubFinder/pkg/log_helper"
  10. "github.com/allanpk716/ChineseSubFinder/pkg/my_util"
  11. "github.com/allanpk716/ChineseSubFinder/pkg/rod_helper"
  12. "github.com/nfnt/resize"
  13. )
  14. // SimulationTest 模拟滑动过防水墙
  15. func SimulationTest() {
  16. // 具体的应用见 subhd 的解析器
  17. // 感谢 https://www.bigs3.com/article/gorod-crack-slider-captcha/
  18. browser, err := rod_helper.NewBrowser(log_helper.GetLogger4Tester(), "", "", false)
  19. if err != nil {
  20. println(err.Error())
  21. return
  22. }
  23. defer func() {
  24. _ = browser.Close()
  25. }()
  26. page, err := rod_helper.NewPageNavigate(browser, "https://007.qq.com/online.html", 10*time.Second, 5)
  27. if err != nil {
  28. println(err.Error())
  29. return
  30. }
  31. defer func() {
  32. _ = page.Close()
  33. }()
  34. // 切换到可疑用户
  35. page.MustElement("#app > section.wp-on-online > div > div > div > div.wp-on-box.col-md-5.col-md-offset-1 > div.wp-onb-tit > a:nth-child(2)").MustClick()
  36. //模擬Click點擊 "體驗驗證碼" 按鈕
  37. page.MustElement("#code").MustClick()
  38. //等待驗證碼窗體載入
  39. page.MustElement("#tcaptcha_iframe").MustWaitLoad()
  40. //進入到iframe
  41. iframe := page.MustElement("#tcaptcha_iframe").MustFrame()
  42. //等待拖動條加載, 延遲500秒檢測變化, 以確認加載完畢
  43. iframe.MustElement("#tcaptcha_drag_button").WaitStable(500 * time.Millisecond)
  44. //等待缺口圖像載入
  45. iframe.MustElement("#slideBg").MustWaitLoad()
  46. //取得帶缺口圖像
  47. shadowbg := iframe.MustElement("#slideBg").MustResource()
  48. //取得原始圖像
  49. src := iframe.MustElement("#slideBg").MustProperty("src")
  50. fullbg, fileName, err := my_util.DownFile(log_helper.GetLogger4Tester(), strings.Replace(src.String(), "img_index=1", "img_index=0", 1))
  51. if err != nil {
  52. return
  53. }
  54. println(fileName)
  55. //取得img展示的真實尺寸
  56. bgbox := iframe.MustElement("#slideBg").MustShape().Box()
  57. height, width := uint(math.Round(bgbox.Height)), uint(math.Round(bgbox.Width))
  58. //裁剪圖像
  59. shadowbg_img, _ := jpeg.Decode(bytes.NewReader(shadowbg))
  60. shadowbg_img = resize.Resize(width, height, shadowbg_img, resize.Lanczos3)
  61. fullbg_img, _ := jpeg.Decode(bytes.NewReader(fullbg))
  62. fullbg_img = resize.Resize(width, height, fullbg_img, resize.Lanczos3)
  63. //啓始left,排除干擾部份,所以右移10個像素
  64. left := fullbg_img.Bounds().Min.X + 10
  65. //啓始top, 排除干擾部份, 所以下移10個像素
  66. top := fullbg_img.Bounds().Min.Y + 10
  67. //最大left, 排除干擾部份, 所以左移10個像素
  68. maxleft := fullbg_img.Bounds().Max.X - 10
  69. //最大top, 排除干擾部份, 所以上移10個像素
  70. maxtop := fullbg_img.Bounds().Max.Y - 10
  71. //rgb比较阈值, 超出此阈值及代表找到缺口位置
  72. threshold := 20
  73. //缺口偏移, 拖動按鈕初始會偏移27.5
  74. distance := -27.5
  75. //取絕對值方法
  76. abs := func(n int) int {
  77. if n < 0 {
  78. return -n
  79. }
  80. return n
  81. }
  82. search:
  83. for i := left; i <= maxleft; i++ {
  84. for j := top; j <= maxtop; j++ {
  85. color_a_R, color_a_G, color_a_B, _ := fullbg_img.At(i, j).RGBA()
  86. color_b_R, color_b_G, color_b_B, _ := shadowbg_img.At(i, j).RGBA()
  87. color_a_R, color_a_G, color_a_B = color_a_R>>8, color_a_G>>8, color_a_B>>8
  88. color_b_R, color_b_G, color_b_B = color_b_R>>8, color_b_G>>8, color_b_B>>8
  89. if abs(int(color_a_R)-int(color_b_R)) > threshold ||
  90. abs(int(color_a_G)-int(color_b_G)) > threshold ||
  91. abs(int(color_a_B)-int(color_b_B)) > threshold {
  92. distance += float64(i)
  93. fmt.Printf("info: 對比完畢, 偏移量: %v\n", distance)
  94. break search
  95. }
  96. }
  97. }
  98. //獲取拖動按鈕形狀
  99. dragbtnbox := iframe.MustElement("#tcaptcha_drag_thumb").MustShape().Box()
  100. //启用滑鼠功能
  101. mouse := page.Mouse
  102. //模擬滑鼠移動至拖動按鈕處, 右移3的原因: 拖動按鈕比滑塊圖大3個像素
  103. mouse.MustMove(dragbtnbox.X+3, dragbtnbox.Y+(dragbtnbox.Height/2))
  104. //按下滑鼠左鍵
  105. mouse.MustDown("left")
  106. //開始拖動
  107. mouse.Move(dragbtnbox.X+distance, dragbtnbox.Y+(dragbtnbox.Height/2), 20)
  108. //鬆開滑鼠左鍵, 拖动完毕
  109. mouse.MustUp("left")
  110. //截圖保存
  111. page.MustScreenshot("result.png")
  112. }