pass_water_wall.go 3.8 KB

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