pass_water_wall.go 4.0 KB

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