Преглед изворни кода

feat(device): can click and swipe in mumu,ld and nox

runhey пре 3 година
родитељ
комит
ecbde97888
3 измењених фајлова са 310 додато и 2 уклоњено
  1. 160 0
      module/base/cBezier.py
  2. 20 1
      module/device/handle.py
  3. 130 1
      module/device/method/window.py

+ 160 - 0
module/base/cBezier.py

@@ -0,0 +1,160 @@
+'''
+author: cbb
+这个库基于贝塞尔曲线实现模拟人手动滑动的轨迹
+可以用于selenium轨迹的模拟,或者生成轨迹数组用于js加密通过网站服务器后端分控检测
+QQ群 134064772
+里面的人说话好听,个个都是人才
+copy from https://github.com/2833844911/gurs
+'''
+
+import numpy as np
+import math
+import random
+
+class BezierTrajectory:
+
+    @classmethod
+    def _bztsg(cls, dataTrajectory):
+        lengthOfdata = len(dataTrajectory)
+
+        def staer(x):
+            t = ((x - dataTrajectory[0][0]) / (dataTrajectory[-1][0] - dataTrajectory[0][0]))
+            y = np.array([0, 0], dtype=np.float64)
+            for s in range(len(dataTrajectory)):
+                y += dataTrajectory[s] * ((math.factorial(lengthOfdata - 1) / (
+                            math.factorial(s) * math.factorial(lengthOfdata - 1 - s))) * math.pow(t, s) * math.pow(
+                    (1 - t), lengthOfdata - 1 - s))
+            return y[1]
+
+        return staer
+
+    @classmethod
+    def _type(cls, type, x, numberList):
+        numberListre = []
+        pin = (x[1] - x[0]) / numberList
+        if type == 0:
+            for i in range(numberList):
+                numberListre.append(i * pin)
+            if pin >= 0:
+                numberListre = numberListre[::-1]
+        elif type == 1:
+            for i in range(numberList):
+                numberListre.append(1 * ((i * pin) ** 2))
+            numberListre = numberListre[::-1]
+        elif type == 2:
+            for i in range(numberList):
+                numberListre.append(1 * ((i * pin - x[1]) ** 2))
+
+        elif type == 3:
+            dataTrajectory = [np.array([0,0]), np.array([(x[1]-x[0])*0.8, (x[1]-x[0])*0.6]), np.array([x[1]-x[0], 0])]
+            fun = cls._bztsg(dataTrajectory)
+            numberListre = [0]
+            for i in range(1,numberList):
+                numberListre.append(fun(i * pin) + numberListre[-1])
+            if pin >= 0:
+                numberListre = numberListre[::-1]
+        numberListre = np.abs(np.array(numberListre) - max(numberListre))
+        biaoNumberList = ((numberListre - numberListre[numberListre.argmin()]) / (
+                    numberListre[numberListre.argmax()] - numberListre[numberListre.argmin()])) * (x[1] - x[0]) + x[0]
+        biaoNumberList[0] = x[0]
+        biaoNumberList[-1] = x[1]
+        return biaoNumberList
+
+    @classmethod
+    def getFun(cls, s):
+        '''
+
+        :param s: 传入P点
+        :return: 返回公式
+        '''
+        dataTrajectory = []
+        for i in s:
+            dataTrajectory.append(np.array(i))
+        return cls._bztsg(dataTrajectory)
+
+    @classmethod
+    def simulation(cls, start, end, le=1, deviation=0, bias=0.5):
+        '''
+
+        :param start:开始点的坐标 如 start = [0, 0]
+        :param end:结束点的坐标 如 end = [100, 100]
+        :param le:几阶贝塞尔曲线,越大越复杂 如 le = 4
+        :param deviation:轨迹上下波动的范围 如 deviation = 10
+        :param bias:波动范围的分布位置 如 bias = 0.5
+        :return:返回一个字典equation对应该曲线的方程,P对应贝塞尔曲线的影响点
+        '''
+        start = np.array(start)
+        end = np.array(end)
+        cbb = []
+        if le != 1:
+            e = (1 - bias) / (le - 1)
+            cbb = [[bias + e * i, bias + e * (i + 1)] for i in range(le - 1)]
+
+        dataTrajectoryList = [start]
+
+        t = random.choice([-1, 1])
+        w = 0
+        for i in cbb:
+            px1 = start[0] + (end[0] - start[0]) * (random.random() * (i[1] - i[0]) + (i[0]))
+            p = np.array([px1, cls._bztsg([start, end])(px1) + t * deviation])
+            dataTrajectoryList.append(p)
+            w += 1
+            if w >= 2:
+                w = 0
+                t = -1 * t
+
+        dataTrajectoryList.append(end)
+        return {"equation": cls._bztsg(dataTrajectoryList), "P": np.array(dataTrajectoryList)}
+
+    @classmethod
+    def trackArray(cls, start, end, numberList, le=1, deviation=0, bias=0.5, type=0, cbb=0, yhh=10):
+        '''
+
+        :param start:开始点的坐标 如 start = [0, 0]
+        :param end:结束点的坐标 如 end = [100, 100]
+        :param numberList:返回的数组的轨迹点的数量 numberList = 150
+        :param le:几阶贝塞尔曲线,越大越复杂 如 le = 4
+        :param deviation:轨迹上下波动的范围 如 deviation = 10
+        :param bias:波动范围的分布位置 如 bias = 0.5
+        :param type:0表示均速滑动,1表示先慢后快,2表示先快后慢,3表示先慢中间快后慢 如 type = 1
+        :param cbb:在终点来回摆动的次数
+        :param yhh:在终点来回摆动的范围
+        :return:返回一个字典trackArray对应轨迹数组,P对应贝塞尔曲线的影响点
+        '''
+        s = []
+        fun = cls.simulation(start, end, le, deviation, bias)
+        w = fun['P']
+        fun = fun["equation"]
+        if cbb != 0:
+            numberListOfcbb = round(numberList*0.2/(cbb+1))
+            numberList -= (numberListOfcbb*(cbb+1))
+
+            xTrackArray = cls._type(type, [start[0], end[0]], numberList)
+            for i in xTrackArray:
+                s.append([i, fun(i)])
+            dq = yhh/cbb
+            kg = 0
+            ends = np.copy(end)
+            for i in range(cbb):
+                if kg == 0:
+                    d = np.array([end[0] + (yhh-dq*i), ((end[1]-start[1])/(end[0]-start[0]))*(end[0]+(yhh-dq*i)) +(end[1]-((end[1]-start[1])/(end[0]-start[0]))*end[0])   ])
+                    kg = 1
+                else:
+                    d = np.array([end[0] - (yhh - dq * i), ((end[1]-start[1])/(end[0]-start[0]))*(end[0]-(yhh-dq*i)) +(end[1]-((end[1]-start[1])/(end[0]-start[0]))*end[0])  ])
+                    kg = 0
+                print(d)
+                y = cls.trackArray(ends, d, numberListOfcbb, le=2, deviation=0, bias=0.5, type=0, cbb=0, yhh=10)
+                s += list(y['trackArray'])
+                ends = d
+            y = cls.trackArray(ends, end, numberListOfcbb, le=2, deviation=0, bias=0.5, type=0, cbb=0, yhh=10)
+            s += list(y['trackArray'])
+
+        else:
+            xTrackArray = cls._type(type, [start[0], end[0]], numberList)
+            for i in xTrackArray:
+                s.append([i, fun(i)])
+        # return {"trackArray": np.array(s), "P": w}
+        return [[int(s[0]), int(s[1])] for s in s]
+
+# print(BezierTrajectory.trackArray(start=[0, 0], end=[100, 100], numberList=50, le=2,
+#                     deviation=0, bias=0.5, type=0, cbb=0, yhh=10))

+ 20 - 1
module/device/handle.py

@@ -252,7 +252,10 @@ class Handle:
                         return node.num
                         return node.num
         # 夜神
         # 夜神
         elif self.emulator_family == 'nox_player_family':
         elif self.emulator_family == 'nox_player_family':
-            return self.root_node.children[1].children[1].num
+            try:
+                return self.root_node.children[1].children[1].num
+            except:
+                return self.root_node.children[2].children[1].num
 
 
         elif self.emulator_family == 'ld_player_family':
         elif self.emulator_family == 'ld_player_family':
             for node in PreOrderIter(self.root_node):
             for node in PreOrderIter(self.root_node):
@@ -281,6 +284,22 @@ class Handle:
         height: int = winRect[3] - winRect[1]  # 下y - 上y 计算高度
         height: int = winRect[3] - winRect[1]  # 下y - 上y 计算高度
         return width, height
         return width, height
 
 
+    @cached_property
+    def window_scale_rate(self) -> float:
+        """
+        获取window的系统缩放 一般是1
+        :return:
+        """
+        hDC = GetDC(0)
+        # 物理上(真实的)的 横纵向分辨率
+        wReal = GetDeviceCaps(hDC, DESKTOPHORZRES)
+        hReal = GetDeviceCaps(hDC, DESKTOPVERTRES)
+        # 缩放后的 分辨率
+        wAfter = GetSystemMetrics(0)
+        hAfter = GetSystemMetrics(1)
+        # print(wReal, wAfter)
+        return round(wReal / wAfter, 2)
+
 if __name__ == '__main__':
 if __name__ == '__main__':
     h = Handle(config='oas1')
     h = Handle(config='oas1')
     logger.info(h.auto_handle_title(h.all_windows()))
     logger.info(h.auto_handle_title(h.all_windows()))

+ 130 - 1
module/device/method/window.py

@@ -5,14 +5,24 @@ import cv2
 from numpy import frombuffer, uint8, array, random
 from numpy import frombuffer, uint8, array, random
 import numpy as np
 import numpy as np
 import time
 import time
+
+from math import dist
+from cached_property import cached_property
 from win32gui import (GetWindowText, EnumWindows, FindWindow, FindWindowEx,
 from win32gui import (GetWindowText, EnumWindows, FindWindow, FindWindowEx,
                       IsWindow, GetWindowRect, GetWindowDC, DeleteObject,
                       IsWindow, GetWindowRect, GetWindowDC, DeleteObject,
                       SetForegroundWindow, IsWindowVisible, GetDC, GetParent,
                       SetForegroundWindow, IsWindowVisible, GetDC, GetParent,
                       EnumChildWindows)
                       EnumChildWindows)
+from win32con import (SRCCOPY, DESKTOPHORZRES, DESKTOPVERTRES, WM_LBUTTONUP,
+                      WM_LBUTTONDOWN, WM_ACTIVATE, WA_ACTIVE, MK_LBUTTON,
+                      WM_NCHITTEST, WM_SETCURSOR, HTCLIENT, WM_MOUSEMOVE)
 from win32ui import CreateDCFromHandle, CreateBitmap
 from win32ui import CreateDCFromHandle, CreateBitmap
+from win32api import GetSystemMetrics, SendMessage, MAKELONG, PostMessage
 from win32con import SRCCOPY
 from win32con import SRCCOPY
 
 
+from module.base.cBezier import BezierTrajectory
 from module.exception import RequestHumanTakeover, ScriptError
 from module.exception import RequestHumanTakeover, ScriptError
+from module.base.decorator import Config
+from module.base.timer import timer
 from module.logger import logger
 from module.logger import logger
 from module.device.handle import Handle, window_scale_rate
 from module.device.handle import Handle, window_scale_rate
 
 
@@ -61,7 +71,126 @@ class Window(Handle):
         mfcDc.DeleteDC()
         mfcDc.DeleteDC()
         return imgSrceen
         return imgSrceen
 
 
+    @cached_property
+    def control_handle_list(self) -> list:
+        """
+        不同的模拟器需要的子句柄不同
+        mumu模拟器的控制需要 根窗口和第一个子窗口
+        雷电模拟器只需要TheRender窗口
+        夜神模拟器
+        :return:
+        """
+        result = []
+        if self.emulator_family == "mumu_player_family":
+            result.append(self.root_node.num)
+            result.append(self.root_node.children[0].num)
+            return result
+        elif self.emulator_family == 'nox_player_family':
+            result.append(self.root_node.num)
+            try:
+                result.append(self.root_node.children[1].num)
+                result.append(self.root_node.children[1].children[1].num)
+                result.append(self.root_node.children[1].children[1].children[0].num)
+            except:
+                result.append(self.root_node.children[2].num)
+                result.append(self.root_node.children[2].children[1].num)
+                result.append(self.root_node.children[2].children[1].children[0].num)
+            return result
+        elif self.emulator_family == 'ld_player_family':
+            result.append(self.root_node.children[0].num)
+            return result
+        elif self.emulator_family == 'memu_player_family':
+            pass
+        elif self.emulator_family == 'bluestacks_family':
+            pass
+
+    def click_window_message(self, x: int, y: int):
+        """
+
+        :param x:
+        :param y:
+        :return:
+        """
+        # if x >= 1280:
+        #     x = 1280
+        # if x <= 0:
+        #     x = 0
+        # if y >= 720:
+        #     y = 720
+        # if y <= 0:
+        #     y = 0
+        # 我不知道为什么的使用的pywin32==306的版本会导致获取的图片的是(1024, 576)
+        # 所有我在点击的时候会除以这个缩放比例
+        x = int(x / self.window_scale_rate)
+        y = int(y / self.window_scale_rate)
+        logger.info(self.control_handle_list)
+        emulator_type = len(self.control_handle_list)
+        if emulator_type == 2:  # mumu模拟器
+            SendMessage(self.control_handle_list[0], WM_LBUTTONDOWN, 0, MAKELONG(x, y))  # 模拟鼠标按下 先是父窗口 上面的框高度是57
+            time.sleep((random.randint(100, 200)) / 1000.0)  # 点击弹起改为随机,时间100ms-200ms
+            SendMessage(self.control_handle_list[1], WM_LBUTTONUP, 0, MAKELONG(x, y))  # 模拟鼠标弹起 后是子窗口
+        elif emulator_type > 2:  # 夜神模拟器
+            SendMessage(self.control_handle_list[0], WM_LBUTTONDOWN, 0, MAKELONG(x, y))  # 模拟鼠标按下 先是父窗口 上面的框高度是57
+            SendMessage(self.control_handle_list[1], WM_LBUTTONDOWN, 0, MAKELONG(x, y))
+            SendMessage(self.control_handle_list[2], WM_LBUTTONDOWN, 0, MAKELONG(x, y))
+            SendMessage(self.control_handle_list[3], WM_LBUTTONDOWN, 0, MAKELONG(x, y))
+            time.sleep((random.randint(100, 200)) / 1000.0)  # 点击弹起改为随机,时间100ms-200ms
+            SendMessage(self.control_handle_list[3], WM_LBUTTONUP, 0, MAKELONG(x, y))  # 模拟鼠标弹起 后是子窗口
+        elif emulator_type == 1:  # 雷电模拟器
+            clickPos = MAKELONG(x, y)
+            SendMessage(self.control_handle_list[0], WM_LBUTTONDOWN, 0, clickPos)  # 模拟鼠标按下
+            time.sleep((random.randint(100, 200)) / 1000.0)  # 点击弹起改为随机,时间100ms-200ms
+            SendMessage(self.control_handle_list[0], WM_LBUTTONUP, 0, clickPos)  # 模拟鼠标弹起
+
+    # @timer
+    def swipe_window_message(self, startPos :list, endPos :list) -> None:
+        """
+        后台滑动
+        :param startPos:
+        :param endPos:
+        :return:
+        """
+        # 生成的坐标点列表
+        interval: int = 8  # 每次移动的间隔时间
+        numberList :int = int(dist(startPos, endPos)/(1.5*interval))  # 表示每毫秒移动1.5个像素点, 总的时间除以每个点10ms就得到总的点的个数
+        le = random.randint(2, 4)  #
+        deviation = random.randint(20, 40)  # 幅度
+        _type: int = 3
+        obbsType = random.random()   # 0.8的概率是先快中间慢后面快, 0.1概率是先快后慢, 0.1概率先慢后快
+        if 0 < obbsType <= 0.8:
+            _type = 3
+        elif obbsType<0.9:
+            _type = 2
+        else:
+            _type = 1
+        trace :list = BezierTrajectory.trackArray(start=startPos, end=endPos, numberList=numberList, le=4,
+                     deviation=30, bias=0.5, type=_type, cbb=0, yhh=20)
+
+        # 使用生成的点列表进行拖拽
+        emulator_type = len(self.control_handle_list)
+        if emulator_type == 1:  # 雷电模拟器
+            handleNum = self.control_handle_list[0]
+        elif emulator_type == 2:  # mumu模拟器
+            handleNum = self.control_handle_list[1]
+        elif emulator_type > 2:  # 夜神模拟器
+            handleNum = self.control_handle_list[3]
+        PostMessage(handleNum, WM_ACTIVATE, WA_ACTIVE, 0)  # 激活窗口
+        # 先移动到第一个点
+        tmpPos = MAKELONG(trace[0][0], trace[0][1])
+        SendMessage(handleNum, WM_NCHITTEST, 0, tmpPos)
+        SendMessage(handleNum, WM_SETCURSOR, handleNum, MAKELONG(HTCLIENT, WM_LBUTTONDOWN))
+        PostMessage(handleNum, WM_LBUTTONDOWN, MK_LBUTTON, tmpPos)
+        # 一点一点移动鼠标
+        for pos in trace:
+            tmpPos = MAKELONG(pos[0], pos[1])
+            PostMessage(handleNum, WM_MOUSEMOVE, MK_LBUTTON, tmpPos)
+            time.sleep((interval+random.randint(-2, 2))/1000.0)
+        # 最后释放鼠标
+        tmpPos = MAKELONG(trace[-1][0], trace[-1][1])
+        PostMessage(handleNum, WM_LBUTTONUP, 0, tmpPos)
+
 
 
 if __name__ == "__main__":
 if __name__ == "__main__":
     w = Window(config='oas1')
     w = Window(config='oas1')
-    w.screenshot_window_background()
+    # w.click_window_message(x=350, y=220)
+    w.swipe_window_message(startPos=[200, 5], endPos=[300, 300])