2023-05-29 07:09:00 +00:00
|
|
|
|
import os
|
|
|
|
|
import tempfile
|
2022-12-20 08:48:18 +00:00
|
|
|
|
import time
|
|
|
|
|
from enum import Enum
|
|
|
|
|
from subprocess import CREATE_NO_WINDOW
|
|
|
|
|
|
|
|
|
|
from selenium import webdriver
|
|
|
|
|
from selenium.webdriver.chrome.service import Service
|
2023-01-16 11:02:09 +00:00
|
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
|
from selenium.webdriver.remote.webelement import WebElement
|
2022-12-20 08:48:18 +00:00
|
|
|
|
|
2023-07-28 06:33:09 +00:00
|
|
|
|
from code_dialog import CodeDialog, wrapper_progress_bar
|
2023-01-16 11:02:09 +00:00
|
|
|
|
from common import (Asset, User, Account, Platform, Step)
|
2022-12-20 08:48:18 +00:00
|
|
|
|
from common import (BaseApplication)
|
2023-01-16 11:02:09 +00:00
|
|
|
|
from common import (notify_err_message, block_input, unblock_input)
|
2022-12-20 08:48:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Command(Enum):
|
|
|
|
|
TYPE = 'type'
|
|
|
|
|
CLICK = 'click'
|
|
|
|
|
OPEN = 'open'
|
2023-02-03 06:28:35 +00:00
|
|
|
|
CODE = 'code'
|
2023-03-20 09:26:47 +00:00
|
|
|
|
SELECT_FRAME = 'select_frame'
|
2023-06-02 08:12:16 +00:00
|
|
|
|
SLEEP = 'sleep'
|
2022-12-20 08:48:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _execute_type(ele: WebElement, value: str):
|
|
|
|
|
ele.send_keys(value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _execute_click(ele: WebElement, value: str):
|
|
|
|
|
ele.click()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
commands_func_maps = {
|
|
|
|
|
Command.CLICK: _execute_click,
|
|
|
|
|
Command.TYPE: _execute_type,
|
|
|
|
|
Command.OPEN: _execute_type,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class StepAction:
|
|
|
|
|
methods_map = {
|
|
|
|
|
"NAME": By.NAME,
|
|
|
|
|
"ID": By.ID,
|
|
|
|
|
"CLASS_NAME": By.CLASS_NAME,
|
|
|
|
|
"CSS_SELECTOR": By.CSS_SELECTOR,
|
|
|
|
|
"CSS": By.CSS_SELECTOR,
|
|
|
|
|
"XPATH": By.XPATH
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def __init__(self, target='', value='', command=Command.TYPE, **kwargs):
|
|
|
|
|
self.target = target
|
|
|
|
|
self.value = value
|
|
|
|
|
self.command = command
|
|
|
|
|
|
|
|
|
|
def execute(self, driver: webdriver.Chrome) -> bool:
|
|
|
|
|
if not self.target:
|
|
|
|
|
return True
|
2023-03-20 09:26:47 +00:00
|
|
|
|
if self.command == 'select_frame':
|
|
|
|
|
self._switch_iframe(driver, self.target)
|
|
|
|
|
return True
|
2023-06-02 08:12:16 +00:00
|
|
|
|
elif self.command == 'sleep':
|
|
|
|
|
self._sleep(driver, self.target)
|
|
|
|
|
return True
|
2022-12-20 08:48:18 +00:00
|
|
|
|
target_name, target_value = self.target.split("=", 1)
|
|
|
|
|
by_name = self.methods_map.get(target_name.upper(), By.NAME)
|
|
|
|
|
ele = driver.find_element(by=by_name, value=target_value)
|
|
|
|
|
if not ele:
|
|
|
|
|
return False
|
|
|
|
|
if self.command == 'type':
|
|
|
|
|
ele.send_keys(self.value)
|
|
|
|
|
elif self.command in ['click', 'button']:
|
|
|
|
|
ele.click()
|
|
|
|
|
elif self.command in ['open']:
|
|
|
|
|
driver.get(self.value)
|
2023-02-03 06:28:35 +00:00
|
|
|
|
elif self.command == 'code':
|
|
|
|
|
unblock_input()
|
|
|
|
|
code_string = CodeDialog(title="Code Dialog", label="Code").wait_string()
|
|
|
|
|
block_input()
|
|
|
|
|
ele.send_keys(code_string)
|
2022-12-20 08:48:18 +00:00
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def _execute_command_type(self, ele, value):
|
|
|
|
|
ele.send_keys(value)
|
|
|
|
|
|
2023-03-20 09:26:47 +00:00
|
|
|
|
def _switch_iframe(self, driver: webdriver.Chrome, target: str):
|
|
|
|
|
"""
|
|
|
|
|
driver: webdriver.Chrome
|
|
|
|
|
target: str
|
|
|
|
|
target support three format str below:
|
|
|
|
|
index=1: switch to frame by index, if index < 0, switch to default frame
|
|
|
|
|
id=xxx: switch to frame by id
|
|
|
|
|
name=xxx: switch to frame by name
|
|
|
|
|
"""
|
|
|
|
|
target_name, target_value = target.split("=", 1)
|
|
|
|
|
if target_name == 'id':
|
|
|
|
|
driver.switch_to.frame(target_value)
|
|
|
|
|
elif target_name == 'index':
|
|
|
|
|
index = int(target_value)
|
|
|
|
|
if index < 0:
|
|
|
|
|
driver.switch_to.default_content()
|
|
|
|
|
else:
|
|
|
|
|
driver.switch_to.frame(index)
|
|
|
|
|
elif target_name == 'name':
|
|
|
|
|
driver.switch_to.frame(target_value)
|
|
|
|
|
else:
|
|
|
|
|
driver.switch_to.frame(target)
|
2022-12-20 08:48:18 +00:00
|
|
|
|
|
2023-06-02 08:12:16 +00:00
|
|
|
|
def _sleep(self, driver: webdriver.Chrome, target: str):
|
|
|
|
|
try:
|
|
|
|
|
sleep_time = int(target)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
# at least sleep 1 second
|
|
|
|
|
sleep_time = 1
|
|
|
|
|
time.sleep(sleep_time)
|
|
|
|
|
|
2023-05-19 03:30:50 +00:00
|
|
|
|
|
2022-12-20 08:48:18 +00:00
|
|
|
|
def execute_action(driver: webdriver.Chrome, step: StepAction) -> bool:
|
|
|
|
|
try:
|
|
|
|
|
return step.execute(driver)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(e)
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WebAPP(object):
|
|
|
|
|
|
|
|
|
|
def __init__(self, app_name: str = '', user: User = None, asset: Asset = None,
|
|
|
|
|
account: Account = None, platform: Platform = None, **kwargs):
|
|
|
|
|
self.app_name = app_name
|
|
|
|
|
self.user = user
|
|
|
|
|
self.asset = asset
|
|
|
|
|
self.account = account
|
|
|
|
|
self.platform = platform
|
|
|
|
|
self._steps = list()
|
2023-07-06 03:47:25 +00:00
|
|
|
|
# 确保 account_username 和 account_secret 不为 None
|
|
|
|
|
self._account_username = account.username if account.username else ''
|
|
|
|
|
self._account_secret = account.secret if account.secret else ''
|
|
|
|
|
|
|
|
|
|
# 如果是匿名账号,account_username 和 account_secret 为空
|
|
|
|
|
if account.username == "@ANON":
|
|
|
|
|
self._account_username = ''
|
|
|
|
|
self._account_secret = ''
|
2023-02-09 06:11:54 +00:00
|
|
|
|
|
|
|
|
|
extra_data = self.asset.spec_info
|
|
|
|
|
autofill_type = extra_data.autofill
|
|
|
|
|
if not autofill_type:
|
|
|
|
|
protocol_setting = self.platform.get_protocol_setting("http")
|
|
|
|
|
if not protocol_setting:
|
|
|
|
|
print("No protocol setting found")
|
|
|
|
|
return
|
|
|
|
|
extra_data = protocol_setting
|
|
|
|
|
autofill_type = extra_data.autofill
|
2022-12-20 08:48:18 +00:00
|
|
|
|
if autofill_type == "basic":
|
2023-02-09 06:11:54 +00:00
|
|
|
|
self._steps = self._default_custom_steps(extra_data)
|
2022-12-20 08:48:18 +00:00
|
|
|
|
elif autofill_type == "script":
|
2023-02-09 06:11:54 +00:00
|
|
|
|
script_list = extra_data.script
|
2023-01-16 11:02:09 +00:00
|
|
|
|
steps = sorted(script_list, key=lambda step_item: step_item.step)
|
2022-12-20 08:48:18 +00:00
|
|
|
|
for item in steps:
|
2023-01-16 11:02:09 +00:00
|
|
|
|
val = item.value
|
2022-12-20 08:48:18 +00:00
|
|
|
|
if val:
|
2023-07-06 03:47:25 +00:00
|
|
|
|
val = val.replace("{USERNAME}", self._account_username)
|
|
|
|
|
val = val.replace("{SECRET}", self._account_secret)
|
2023-01-16 11:02:09 +00:00
|
|
|
|
item.value = val
|
2022-12-20 08:48:18 +00:00
|
|
|
|
self._steps.append(item)
|
|
|
|
|
|
2023-02-09 06:11:54 +00:00
|
|
|
|
def _default_custom_steps(self, spec_info) -> list:
|
2023-07-06 03:47:25 +00:00
|
|
|
|
return [
|
2023-01-16 11:02:09 +00:00
|
|
|
|
Step({
|
2022-12-20 08:48:18 +00:00
|
|
|
|
"step": 1,
|
2023-07-06 03:47:25 +00:00
|
|
|
|
"value": self._account_username,
|
2023-01-31 10:06:44 +00:00
|
|
|
|
"target": spec_info.username_selector,
|
2022-12-20 08:48:18 +00:00
|
|
|
|
"command": "type"
|
2023-01-16 11:02:09 +00:00
|
|
|
|
}),
|
|
|
|
|
Step({
|
2022-12-20 08:48:18 +00:00
|
|
|
|
"step": 2,
|
2023-07-06 03:47:25 +00:00
|
|
|
|
"value": self._account_secret,
|
2023-01-31 10:06:44 +00:00
|
|
|
|
"target": spec_info.password_selector,
|
2022-12-20 08:48:18 +00:00
|
|
|
|
"command": "type"
|
2023-01-16 11:02:09 +00:00
|
|
|
|
}),
|
|
|
|
|
Step({
|
2022-12-20 08:48:18 +00:00
|
|
|
|
"step": 3,
|
|
|
|
|
"value": "",
|
2023-01-31 10:06:44 +00:00
|
|
|
|
"target": spec_info.submit_selector,
|
2022-12-20 08:48:18 +00:00
|
|
|
|
"command": "click"
|
2023-01-16 11:02:09 +00:00
|
|
|
|
})
|
2022-12-20 08:48:18 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
def execute(self, driver: webdriver.Chrome) -> bool:
|
|
|
|
|
if not self.asset.address:
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
for step in self._steps:
|
2023-01-16 11:02:09 +00:00
|
|
|
|
action = StepAction(target=step.target, value=step.value,
|
|
|
|
|
command=step.command)
|
2022-12-20 08:48:18 +00:00
|
|
|
|
ret = execute_action(driver, action)
|
|
|
|
|
if not ret:
|
|
|
|
|
unblock_input()
|
|
|
|
|
notify_err_message(f"执行失败: target: {action.target} command: {action.command}")
|
|
|
|
|
block_input()
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
2023-05-29 07:09:00 +00:00
|
|
|
|
def load_extensions():
|
|
|
|
|
extensions_root = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'extensions')
|
|
|
|
|
extension_names = os.listdir(extensions_root)
|
|
|
|
|
extension_paths = [os.path.join(extensions_root, name) for name in extension_names]
|
|
|
|
|
return extension_paths
|
|
|
|
|
|
|
|
|
|
|
2022-12-20 08:48:18 +00:00
|
|
|
|
def default_chrome_driver_options():
|
|
|
|
|
options = webdriver.ChromeOptions()
|
2023-05-23 03:24:44 +00:00
|
|
|
|
options.add_argument("--start-maximized")
|
|
|
|
|
|
2023-02-13 07:32:31 +00:00
|
|
|
|
# 忽略证书错误相关
|
|
|
|
|
options.add_argument('--ignore-ssl-errors')
|
|
|
|
|
options.add_argument('--ignore-certificate-errors')
|
|
|
|
|
options.add_argument('--ignore-certificate-errors-spki-list')
|
|
|
|
|
options.add_argument('--allow-running-insecure-content')
|
|
|
|
|
|
2022-12-20 08:48:18 +00:00
|
|
|
|
# 禁用开发者工具
|
|
|
|
|
options.add_argument("--disable-dev-tools")
|
|
|
|
|
# 禁用 密码管理器弹窗
|
2023-05-19 03:30:50 +00:00
|
|
|
|
prefs = {
|
|
|
|
|
"credentials_enable_service": False,
|
|
|
|
|
"profile.password_manager_enabled": False
|
|
|
|
|
}
|
2022-12-20 08:48:18 +00:00
|
|
|
|
options.add_experimental_option("prefs", prefs)
|
|
|
|
|
options.add_experimental_option("excludeSwitches", ['enable-automation'])
|
|
|
|
|
return options
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AppletApplication(BaseApplication):
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
self.driver = None
|
|
|
|
|
self.app = WebAPP(app_name=self.app_name, user=self.user,
|
|
|
|
|
account=self.account, asset=self.asset, platform=self.platform)
|
2023-05-29 07:09:00 +00:00
|
|
|
|
self._tmp_user_dir = tempfile.TemporaryDirectory()
|
2022-12-20 08:48:18 +00:00
|
|
|
|
self._chrome_options = default_chrome_driver_options()
|
2023-05-23 03:24:44 +00:00
|
|
|
|
self._chrome_options.add_argument("--app={}".format(self.asset.address))
|
2023-05-29 07:09:00 +00:00
|
|
|
|
self._chrome_options.add_argument("--user-data-dir={}".format(self._tmp_user_dir.name))
|
2023-08-04 08:53:18 +00:00
|
|
|
|
protocol_setting = self.platform.get_protocol_setting(self.protocol)
|
|
|
|
|
if protocol_setting and protocol_setting.safe_mode:
|
|
|
|
|
# 加载 extensions
|
|
|
|
|
extension_paths = load_extensions()
|
|
|
|
|
for extension_path in extension_paths:
|
|
|
|
|
self._chrome_options.add_argument('--load-extension={}'.format(extension_path))
|
2022-12-20 08:48:18 +00:00
|
|
|
|
|
2023-07-28 06:33:09 +00:00
|
|
|
|
@wrapper_progress_bar
|
2022-12-20 08:48:18 +00:00
|
|
|
|
def run(self):
|
|
|
|
|
service = Service()
|
|
|
|
|
# driver 的 console 终端框不显示
|
|
|
|
|
service.creationflags = CREATE_NO_WINDOW
|
|
|
|
|
self.driver = webdriver.Chrome(options=self._chrome_options, service=service)
|
|
|
|
|
self.driver.implicitly_wait(10)
|
|
|
|
|
if self.app.asset.address != "":
|
|
|
|
|
ok = self.app.execute(self.driver)
|
|
|
|
|
if not ok:
|
|
|
|
|
print("执行失败")
|
|
|
|
|
self.driver.maximize_window()
|
2023-09-18 03:14:20 +00:00
|
|
|
|
|
2022-12-20 08:48:18 +00:00
|
|
|
|
def wait(self):
|
2023-01-16 11:02:09 +00:00
|
|
|
|
disconnected_msg = "Unable to evaluate script: disconnected: not connected to DevTools\n"
|
|
|
|
|
closed_msg = "Unable to evaluate script: no such window: target window already closed"
|
|
|
|
|
|
2022-12-20 08:48:18 +00:00
|
|
|
|
while True:
|
|
|
|
|
time.sleep(5)
|
|
|
|
|
logs = self.driver.get_log('driver')
|
|
|
|
|
if len(logs) == 0:
|
|
|
|
|
continue
|
|
|
|
|
ret = logs[-1]
|
|
|
|
|
if isinstance(ret, dict):
|
2023-01-16 11:02:09 +00:00
|
|
|
|
message = ret.get('message', '')
|
|
|
|
|
if disconnected_msg in message or closed_msg in message:
|
2022-12-20 08:48:18 +00:00
|
|
|
|
break
|
2023-01-16 11:02:09 +00:00
|
|
|
|
print("ret: ", ret)
|
2022-12-20 08:48:18 +00:00
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
|
if self.driver:
|
|
|
|
|
try:
|
2023-01-16 11:02:09 +00:00
|
|
|
|
# quit 退出全部打开的窗口
|
|
|
|
|
self.driver.quit()
|
2022-12-20 08:48:18 +00:00
|
|
|
|
except Exception as e:
|
|
|
|
|
print(e)
|
2023-05-29 07:09:00 +00:00
|
|
|
|
try:
|
|
|
|
|
self._tmp_user_dir.cleanup()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(e)
|