lybic-guiagents 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of lybic-guiagents might be problematic. Click here for more details.
- desktop_env/__init__.py +1 -0
- desktop_env/actions.py +203 -0
- desktop_env/controllers/__init__.py +0 -0
- desktop_env/controllers/python.py +471 -0
- desktop_env/controllers/setup.py +882 -0
- desktop_env/desktop_env.py +509 -0
- desktop_env/evaluators/__init__.py +5 -0
- desktop_env/evaluators/getters/__init__.py +41 -0
- desktop_env/evaluators/getters/calc.py +15 -0
- desktop_env/evaluators/getters/chrome.py +1774 -0
- desktop_env/evaluators/getters/file.py +154 -0
- desktop_env/evaluators/getters/general.py +42 -0
- desktop_env/evaluators/getters/gimp.py +38 -0
- desktop_env/evaluators/getters/impress.py +126 -0
- desktop_env/evaluators/getters/info.py +24 -0
- desktop_env/evaluators/getters/misc.py +406 -0
- desktop_env/evaluators/getters/replay.py +20 -0
- desktop_env/evaluators/getters/vlc.py +86 -0
- desktop_env/evaluators/getters/vscode.py +35 -0
- desktop_env/evaluators/metrics/__init__.py +160 -0
- desktop_env/evaluators/metrics/basic_os.py +68 -0
- desktop_env/evaluators/metrics/chrome.py +493 -0
- desktop_env/evaluators/metrics/docs.py +1011 -0
- desktop_env/evaluators/metrics/general.py +665 -0
- desktop_env/evaluators/metrics/gimp.py +637 -0
- desktop_env/evaluators/metrics/libreoffice.py +28 -0
- desktop_env/evaluators/metrics/others.py +92 -0
- desktop_env/evaluators/metrics/pdf.py +31 -0
- desktop_env/evaluators/metrics/slides.py +957 -0
- desktop_env/evaluators/metrics/table.py +585 -0
- desktop_env/evaluators/metrics/thunderbird.py +176 -0
- desktop_env/evaluators/metrics/utils.py +719 -0
- desktop_env/evaluators/metrics/vlc.py +524 -0
- desktop_env/evaluators/metrics/vscode.py +283 -0
- desktop_env/providers/__init__.py +35 -0
- desktop_env/providers/aws/__init__.py +0 -0
- desktop_env/providers/aws/manager.py +278 -0
- desktop_env/providers/aws/provider.py +186 -0
- desktop_env/providers/aws/provider_with_proxy.py +315 -0
- desktop_env/providers/aws/proxy_pool.py +193 -0
- desktop_env/providers/azure/__init__.py +0 -0
- desktop_env/providers/azure/manager.py +87 -0
- desktop_env/providers/azure/provider.py +207 -0
- desktop_env/providers/base.py +97 -0
- desktop_env/providers/gcp/__init__.py +0 -0
- desktop_env/providers/gcp/manager.py +0 -0
- desktop_env/providers/gcp/provider.py +0 -0
- desktop_env/providers/virtualbox/__init__.py +0 -0
- desktop_env/providers/virtualbox/manager.py +463 -0
- desktop_env/providers/virtualbox/provider.py +124 -0
- desktop_env/providers/vmware/__init__.py +0 -0
- desktop_env/providers/vmware/manager.py +455 -0
- desktop_env/providers/vmware/provider.py +105 -0
- gui_agents/__init__.py +0 -0
- gui_agents/agents/Action.py +209 -0
- gui_agents/agents/__init__.py +0 -0
- gui_agents/agents/agent_s.py +832 -0
- gui_agents/agents/global_state.py +610 -0
- gui_agents/agents/grounding.py +651 -0
- gui_agents/agents/hardware_interface.py +129 -0
- gui_agents/agents/manager.py +568 -0
- gui_agents/agents/translator.py +132 -0
- gui_agents/agents/worker.py +355 -0
- gui_agents/cli_app.py +560 -0
- gui_agents/core/__init__.py +0 -0
- gui_agents/core/engine.py +1496 -0
- gui_agents/core/knowledge.py +449 -0
- gui_agents/core/mllm.py +555 -0
- gui_agents/tools/__init__.py +0 -0
- gui_agents/tools/tools.py +727 -0
- gui_agents/unit_test/__init__.py +0 -0
- gui_agents/unit_test/run_tests.py +65 -0
- gui_agents/unit_test/test_manager.py +330 -0
- gui_agents/unit_test/test_worker.py +269 -0
- gui_agents/utils/__init__.py +0 -0
- gui_agents/utils/analyze_display.py +301 -0
- gui_agents/utils/common_utils.py +263 -0
- gui_agents/utils/display_viewer.py +281 -0
- gui_agents/utils/embedding_manager.py +53 -0
- gui_agents/utils/image_axis_utils.py +27 -0
- lybic_guiagents-0.1.0.dist-info/METADATA +416 -0
- lybic_guiagents-0.1.0.dist-info/RECORD +85 -0
- lybic_guiagents-0.1.0.dist-info/WHEEL +5 -0
- lybic_guiagents-0.1.0.dist-info/licenses/LICENSE +201 -0
- lybic_guiagents-0.1.0.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,882 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import logging
|
|
3
|
+
import os
|
|
4
|
+
import os.path
|
|
5
|
+
import platform
|
|
6
|
+
import shutil
|
|
7
|
+
import sqlite3
|
|
8
|
+
import tempfile
|
|
9
|
+
import time
|
|
10
|
+
import traceback
|
|
11
|
+
import uuid
|
|
12
|
+
from datetime import datetime, timedelta
|
|
13
|
+
from typing import Any, Union, Optional
|
|
14
|
+
from typing import Dict, List
|
|
15
|
+
|
|
16
|
+
import requests
|
|
17
|
+
from playwright.sync_api import sync_playwright, TimeoutError
|
|
18
|
+
from pydrive.auth import GoogleAuth
|
|
19
|
+
from pydrive.drive import GoogleDrive, GoogleDriveFile, GoogleDriveFileList
|
|
20
|
+
from requests_toolbelt.multipart.encoder import MultipartEncoder
|
|
21
|
+
|
|
22
|
+
from desktop_env.controllers.python import PythonController
|
|
23
|
+
from desktop_env.evaluators.metrics.utils import compare_urls
|
|
24
|
+
from desktop_env.providers.aws.proxy_pool import get_global_proxy_pool, init_proxy_pool, ProxyInfo
|
|
25
|
+
|
|
26
|
+
import dotenv
|
|
27
|
+
# Load environment variables from .env file
|
|
28
|
+
dotenv.load_dotenv()
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
PROXY_CONFIG_FILE = os.getenv("PROXY_CONFIG_FILE", "evaluation_examples/settings/proxy/dataimpulse.json") # Default proxy config file
|
|
32
|
+
|
|
33
|
+
logger = logging.getLogger("desktopenv.setup")
|
|
34
|
+
|
|
35
|
+
FILE_PATH = os.path.dirname(os.path.abspath(__file__))
|
|
36
|
+
|
|
37
|
+
init_proxy_pool(PROXY_CONFIG_FILE) # initialize the global proxy pool
|
|
38
|
+
|
|
39
|
+
MAX_RETRIES = 20
|
|
40
|
+
|
|
41
|
+
class SetupController:
|
|
42
|
+
def __init__(self, vm_ip: str, server_port: int = 5000, chromium_port: int = 9222, vlc_port: int = 8080, cache_dir: str = "cache", client_password: str = "", screen_width: int = 1920, screen_height: int = 1080):
|
|
43
|
+
self.vm_ip: str = vm_ip
|
|
44
|
+
self.server_port: int = server_port
|
|
45
|
+
self.chromium_port: int = chromium_port
|
|
46
|
+
self.vlc_port: int = vlc_port
|
|
47
|
+
self.http_server: str = f"http://{vm_ip}:{server_port}"
|
|
48
|
+
self.http_server_setup_root: str = f"http://{vm_ip}:{server_port}/setup"
|
|
49
|
+
self.cache_dir: str = cache_dir
|
|
50
|
+
self.use_proxy: bool = False
|
|
51
|
+
self.client_password: str = client_password
|
|
52
|
+
self.screen_width: int = screen_width
|
|
53
|
+
self.screen_height: int = screen_height
|
|
54
|
+
|
|
55
|
+
def reset_cache_dir(self, cache_dir: str):
|
|
56
|
+
self.cache_dir = cache_dir
|
|
57
|
+
|
|
58
|
+
def setup(self, config: List[Dict[str, Any]], use_proxy: bool = False)-> bool:
|
|
59
|
+
"""
|
|
60
|
+
Args:
|
|
61
|
+
config (List[Dict[str, Any]]): list of dict like {str: Any}. each
|
|
62
|
+
config dict has the structure like
|
|
63
|
+
{
|
|
64
|
+
"type": str, corresponding to the `_{:}_setup` methods of
|
|
65
|
+
this class
|
|
66
|
+
"parameters": dict like {str, Any} providing the keyword
|
|
67
|
+
parameters
|
|
68
|
+
}
|
|
69
|
+
"""
|
|
70
|
+
self.use_proxy = use_proxy
|
|
71
|
+
# make sure connection can be established
|
|
72
|
+
logger.info(f"try to connect {self.http_server}")
|
|
73
|
+
retry = 0
|
|
74
|
+
while retry < MAX_RETRIES:
|
|
75
|
+
try:
|
|
76
|
+
_ = requests.get(self.http_server + "/terminal")
|
|
77
|
+
break
|
|
78
|
+
except:
|
|
79
|
+
time.sleep(5)
|
|
80
|
+
retry += 1
|
|
81
|
+
logger.info(f"retry: {retry}/{MAX_RETRIES}")
|
|
82
|
+
|
|
83
|
+
if retry == MAX_RETRIES:
|
|
84
|
+
return False
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
for i, cfg in enumerate(config):
|
|
88
|
+
config_type: str = cfg["type"]
|
|
89
|
+
parameters: Dict[str, Any] = cfg["parameters"]
|
|
90
|
+
|
|
91
|
+
# Assumes all the setup the functions should follow this name
|
|
92
|
+
# protocol
|
|
93
|
+
setup_function: str = "_{:}_setup".format(config_type)
|
|
94
|
+
assert hasattr(self, setup_function), f'Setup controller cannot find init function {setup_function}'
|
|
95
|
+
|
|
96
|
+
try:
|
|
97
|
+
logger.info(f"Executing setup step {i+1}/{len(config)}: {setup_function}")
|
|
98
|
+
logger.debug(f"Setup parameters: {parameters}")
|
|
99
|
+
getattr(self, setup_function)(**parameters)
|
|
100
|
+
logger.info(f"SETUP COMPLETED: {setup_function}({str(parameters)})")
|
|
101
|
+
except Exception as e:
|
|
102
|
+
logger.error(f"SETUP FAILED at step {i+1}/{len(config)}: {setup_function}({str(parameters)})")
|
|
103
|
+
logger.error(f"Error details: {e}")
|
|
104
|
+
logger.error(f"Traceback: {traceback.format_exc()}")
|
|
105
|
+
raise Exception(f"Setup step {i+1} failed: {setup_function} - {e}") from e
|
|
106
|
+
|
|
107
|
+
return True
|
|
108
|
+
|
|
109
|
+
def _download_setup(self, files: List[Dict[str, str]]):
|
|
110
|
+
"""
|
|
111
|
+
Args:
|
|
112
|
+
files (List[Dict[str, str]]): files to download. lisf of dict like
|
|
113
|
+
{
|
|
114
|
+
"url": str, the url to download
|
|
115
|
+
"path": str, the path on the VM to store the downloaded file
|
|
116
|
+
}
|
|
117
|
+
"""
|
|
118
|
+
for f in files:
|
|
119
|
+
url: str = f["url"]
|
|
120
|
+
path: str = f["path"]
|
|
121
|
+
cache_path: str = os.path.join(self.cache_dir, "{:}_{:}".format(
|
|
122
|
+
uuid.uuid5(uuid.NAMESPACE_URL, url),
|
|
123
|
+
os.path.basename(path)))
|
|
124
|
+
if not url or not path:
|
|
125
|
+
raise Exception(f"Setup Download - Invalid URL ({url}) or path ({path}).")
|
|
126
|
+
|
|
127
|
+
if not os.path.exists(cache_path):
|
|
128
|
+
logger.info(f"Cache file not found, downloading from {url} to {cache_path}")
|
|
129
|
+
max_retries = 3
|
|
130
|
+
downloaded = False
|
|
131
|
+
e = None
|
|
132
|
+
for i in range(max_retries):
|
|
133
|
+
try:
|
|
134
|
+
logger.info(f"Download attempt {i+1}/{max_retries} for {url}")
|
|
135
|
+
response = requests.get(url, stream=True, timeout=300) # Add 5 minute timeout
|
|
136
|
+
response.raise_for_status()
|
|
137
|
+
|
|
138
|
+
# Get file size if available
|
|
139
|
+
total_size = int(response.headers.get('content-length', 0))
|
|
140
|
+
if total_size > 0:
|
|
141
|
+
logger.info(f"File size: {total_size / (1024*1024):.2f} MB")
|
|
142
|
+
|
|
143
|
+
downloaded_size = 0
|
|
144
|
+
with open(cache_path, 'wb') as f:
|
|
145
|
+
for chunk in response.iter_content(chunk_size=8192):
|
|
146
|
+
if chunk:
|
|
147
|
+
f.write(chunk)
|
|
148
|
+
downloaded_size += len(chunk)
|
|
149
|
+
if total_size > 0 and downloaded_size % (1024*1024) == 0: # Log every MB
|
|
150
|
+
progress = (downloaded_size / total_size) * 100
|
|
151
|
+
logger.info(f"Download progress: {progress:.1f}%")
|
|
152
|
+
|
|
153
|
+
logger.info(f"File downloaded successfully to {cache_path} ({downloaded_size / (1024*1024):.2f} MB)")
|
|
154
|
+
downloaded = True
|
|
155
|
+
break
|
|
156
|
+
|
|
157
|
+
except requests.RequestException as e:
|
|
158
|
+
logger.error(
|
|
159
|
+
f"Failed to download {url} caused by {e}. Retrying... ({max_retries - i - 1} attempts left)")
|
|
160
|
+
# Clean up partial download
|
|
161
|
+
if os.path.exists(cache_path):
|
|
162
|
+
os.remove(cache_path)
|
|
163
|
+
if not downloaded:
|
|
164
|
+
raise requests.RequestException(f"Failed to download {url}. No retries left.")
|
|
165
|
+
|
|
166
|
+
form = MultipartEncoder({
|
|
167
|
+
"file_path": path,
|
|
168
|
+
"file_data": (os.path.basename(path), open(cache_path, "rb"))
|
|
169
|
+
})
|
|
170
|
+
headers = {"Content-Type": form.content_type}
|
|
171
|
+
logger.debug(form.content_type)
|
|
172
|
+
|
|
173
|
+
# send request to server to upload file
|
|
174
|
+
try:
|
|
175
|
+
logger.info(f"Uploading {os.path.basename(path)} to VM at {path}")
|
|
176
|
+
logger.debug("REQUEST ADDRESS: %s", self.http_server + "/setup" + "/upload")
|
|
177
|
+
response = requests.post(self.http_server + "/setup" + "/upload", headers=headers, data=form, timeout=600) # 10 minute timeout for upload
|
|
178
|
+
if response.status_code == 200:
|
|
179
|
+
logger.info(f"File uploaded successfully: {path}")
|
|
180
|
+
logger.debug("Upload response: %s", response.text)
|
|
181
|
+
else:
|
|
182
|
+
logger.error(f"Failed to upload file {path}. Status code: {response.status_code}, Response: {response.text}")
|
|
183
|
+
raise requests.RequestException(f"Upload failed with status {response.status_code}")
|
|
184
|
+
except requests.exceptions.RequestException as e:
|
|
185
|
+
logger.error(f"An error occurred while trying to upload {path}: {e}")
|
|
186
|
+
raise
|
|
187
|
+
|
|
188
|
+
def _upload_file_setup(self, files: List[Dict[str, str]]):
|
|
189
|
+
"""
|
|
190
|
+
Args:
|
|
191
|
+
files (List[Dict[str, str]]): files to download. lisf of dict like
|
|
192
|
+
{
|
|
193
|
+
"local_path": str, the local path to the file to upload
|
|
194
|
+
"path": str, the path on the VM to store the downloaded file
|
|
195
|
+
}
|
|
196
|
+
"""
|
|
197
|
+
for f in files:
|
|
198
|
+
local_path: str = f["local_path"]
|
|
199
|
+
path: str = f["path"]
|
|
200
|
+
|
|
201
|
+
if not os.path.exists(local_path):
|
|
202
|
+
logger.error(f"Setup Upload - Invalid local path ({local_path}).")
|
|
203
|
+
return
|
|
204
|
+
|
|
205
|
+
form = MultipartEncoder({
|
|
206
|
+
"file_path": path,
|
|
207
|
+
"file_data": (os.path.basename(path), open(local_path, "rb"))
|
|
208
|
+
})
|
|
209
|
+
headers = {"Content-Type": form.content_type}
|
|
210
|
+
logger.debug(form.content_type)
|
|
211
|
+
|
|
212
|
+
# send request to server to upload file
|
|
213
|
+
try:
|
|
214
|
+
logger.debug("REQUEST ADDRESS: %s", self.http_server + "/setup" + "/upload")
|
|
215
|
+
response = requests.post(self.http_server + "/setup" + "/upload", headers=headers, data=form)
|
|
216
|
+
if response.status_code == 200:
|
|
217
|
+
logger.info("Command executed successfully: %s", response.text)
|
|
218
|
+
else:
|
|
219
|
+
logger.error("Failed to upload file. Status code: %s", response.text)
|
|
220
|
+
except requests.exceptions.RequestException as e:
|
|
221
|
+
logger.error("An error occurred while trying to send the request: %s", e)
|
|
222
|
+
|
|
223
|
+
def _change_wallpaper_setup(self, path: str):
|
|
224
|
+
if not path:
|
|
225
|
+
raise Exception(f"Setup Wallpaper - Invalid path ({path}).")
|
|
226
|
+
|
|
227
|
+
payload = json.dumps({"path": path})
|
|
228
|
+
headers = {
|
|
229
|
+
'Content-Type': 'application/json'
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
# send request to server to change wallpaper
|
|
233
|
+
try:
|
|
234
|
+
response = requests.post(self.http_server + "/setup" + "/change_wallpaper", headers=headers, data=payload)
|
|
235
|
+
if response.status_code == 200:
|
|
236
|
+
logger.info("Command executed successfully: %s", response.text)
|
|
237
|
+
else:
|
|
238
|
+
logger.error("Failed to change wallpaper. Status code: %s", response.text)
|
|
239
|
+
except requests.exceptions.RequestException as e:
|
|
240
|
+
logger.error("An error occurred while trying to send the request: %s", e)
|
|
241
|
+
|
|
242
|
+
def _tidy_desktop_setup(self, **config):
|
|
243
|
+
raise NotImplementedError()
|
|
244
|
+
|
|
245
|
+
def _open_setup(self, path: str):
|
|
246
|
+
if not path:
|
|
247
|
+
raise Exception(f"Setup Open - Invalid path ({path}).")
|
|
248
|
+
|
|
249
|
+
payload = json.dumps({"path": path})
|
|
250
|
+
headers = {
|
|
251
|
+
'Content-Type': 'application/json'
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
# send request to server to open file
|
|
255
|
+
try:
|
|
256
|
+
# The server-side call is now blocking and can take time.
|
|
257
|
+
# We set a timeout that is slightly longer than the server's timeout (1800s).
|
|
258
|
+
response = requests.post(self.http_server + "/setup" + "/open_file", headers=headers, data=payload, timeout=1810)
|
|
259
|
+
response.raise_for_status() # This will raise an exception for 4xx and 5xx status codes
|
|
260
|
+
logger.info("Command executed successfully: %s", response.text)
|
|
261
|
+
except requests.exceptions.RequestException as e:
|
|
262
|
+
logger.error(f"Failed to open file '{path}'. An error occurred while trying to send the request or the server responded with an error: {e}")
|
|
263
|
+
raise Exception(f"Failed to open file '{path}'. An error occurred while trying to send the request or the server responded with an error: {e}") from e
|
|
264
|
+
|
|
265
|
+
def _launch_setup(self, command: Union[str, List[str]], shell: bool = False):
|
|
266
|
+
if not command:
|
|
267
|
+
raise Exception("Empty command to launch.")
|
|
268
|
+
|
|
269
|
+
if not shell and isinstance(command, str) and len(command.split()) > 1:
|
|
270
|
+
logger.warning("Command should be a list of strings. Now it is a string. Will split it by space.")
|
|
271
|
+
command = command.split()
|
|
272
|
+
|
|
273
|
+
if command[0] == "google-chrome" and self.use_proxy:
|
|
274
|
+
command.append("--proxy-server=http://127.0.0.1:18888") # Use the proxy server set up by _proxy_setup
|
|
275
|
+
|
|
276
|
+
payload = json.dumps({"command": command, "shell": shell})
|
|
277
|
+
headers = {"Content-Type": "application/json"}
|
|
278
|
+
|
|
279
|
+
try:
|
|
280
|
+
logger.info("REQUEST ADDRESS: %s", self.http_server + "/setup" + "/launch")
|
|
281
|
+
response = requests.post(self.http_server + "/setup" + "/launch", headers=headers, data=payload)
|
|
282
|
+
if response.status_code == 200:
|
|
283
|
+
logger.info("Command executed successfully: %s", response.text)
|
|
284
|
+
else:
|
|
285
|
+
logger.error("Failed to launch application. Status code: %s", response.text)
|
|
286
|
+
except requests.exceptions.RequestException as e:
|
|
287
|
+
logger.error("An error occurred while trying to send the request: %s", e)
|
|
288
|
+
|
|
289
|
+
def _execute_setup(
|
|
290
|
+
self,
|
|
291
|
+
command: List[str],
|
|
292
|
+
stdout: str = "",
|
|
293
|
+
stderr: str = "",
|
|
294
|
+
shell: bool = False,
|
|
295
|
+
until: Optional[Dict[str, Any]] = None
|
|
296
|
+
):
|
|
297
|
+
if not command:
|
|
298
|
+
raise Exception("Empty command to launch.")
|
|
299
|
+
|
|
300
|
+
until: Dict[str, Any] = until or {}
|
|
301
|
+
terminates: bool = False
|
|
302
|
+
nb_failings = 0
|
|
303
|
+
|
|
304
|
+
def replace_screen_env_in_command(command):
|
|
305
|
+
password = self.client_password
|
|
306
|
+
width = self.screen_width
|
|
307
|
+
height = self.screen_height
|
|
308
|
+
width_half = str(width // 2)
|
|
309
|
+
height_half = str(height // 2)
|
|
310
|
+
new_command_list = []
|
|
311
|
+
new_command = ""
|
|
312
|
+
if isinstance(command, str):
|
|
313
|
+
new_command = command.replace("{CLIENT_PASSWORD}", password)
|
|
314
|
+
new_command = new_command.replace("{SCREEN_WIDTH_HALF}", width_half)
|
|
315
|
+
new_command = new_command.replace("{SCREEN_HEIGHT_HALF}", height_half)
|
|
316
|
+
new_command = new_command.replace("{SCREEN_WIDTH}", str(width))
|
|
317
|
+
new_command = new_command.replace("{SCREEN_HEIGHT}", str(height))
|
|
318
|
+
return new_command
|
|
319
|
+
else:
|
|
320
|
+
for item in command:
|
|
321
|
+
item = item.replace("{CLIENT_PASSWORD}", password)
|
|
322
|
+
item = item.replace("{SCREEN_WIDTH_HALF}", width_half)
|
|
323
|
+
item = item.replace("{SCREEN_HEIGHT_HALF}", height_half)
|
|
324
|
+
item = item.replace("{SCREEN_WIDTH}", str(width))
|
|
325
|
+
item = item.replace("{SCREEN_HEIGHT}", str(height))
|
|
326
|
+
new_command_list.append(item)
|
|
327
|
+
return new_command_list
|
|
328
|
+
command = replace_screen_env_in_command(command)
|
|
329
|
+
payload = json.dumps({"command": command, "shell": shell})
|
|
330
|
+
headers = {"Content-Type": "application/json"}
|
|
331
|
+
|
|
332
|
+
while not terminates:
|
|
333
|
+
try:
|
|
334
|
+
response = requests.post(self.http_server + "/setup" + "/execute", headers=headers, data=payload)
|
|
335
|
+
if response.status_code == 200:
|
|
336
|
+
results: Dict[str, str] = response.json()
|
|
337
|
+
if stdout:
|
|
338
|
+
with open(os.path.join(self.cache_dir, stdout), "w") as f:
|
|
339
|
+
f.write(results["output"])
|
|
340
|
+
if stderr:
|
|
341
|
+
with open(os.path.join(self.cache_dir, stderr), "w") as f:
|
|
342
|
+
f.write(results["error"])
|
|
343
|
+
logger.info("Command executed successfully: %s -> %s"
|
|
344
|
+
, " ".join(command) if isinstance(command, list) else command
|
|
345
|
+
, response.text
|
|
346
|
+
)
|
|
347
|
+
else:
|
|
348
|
+
logger.error("Failed to launch application. Status code: %s", response.text)
|
|
349
|
+
results = None
|
|
350
|
+
nb_failings += 1
|
|
351
|
+
except requests.exceptions.RequestException as e:
|
|
352
|
+
logger.error("An error occurred while trying to send the request: %s", e)
|
|
353
|
+
traceback.print_exc()
|
|
354
|
+
|
|
355
|
+
results = None
|
|
356
|
+
nb_failings += 1
|
|
357
|
+
|
|
358
|
+
if len(until) == 0:
|
|
359
|
+
terminates = True
|
|
360
|
+
elif results is not None:
|
|
361
|
+
terminates = "returncode" in until and results["returncode"] == until["returncode"] \
|
|
362
|
+
or "stdout" in until and until["stdout"] in results["output"] \
|
|
363
|
+
or "stderr" in until and until["stderr"] in results["error"]
|
|
364
|
+
terminates = terminates or nb_failings >= 5
|
|
365
|
+
if not terminates:
|
|
366
|
+
time.sleep(0.3)
|
|
367
|
+
|
|
368
|
+
def _execute_with_verification_setup(
|
|
369
|
+
self,
|
|
370
|
+
command: List[str],
|
|
371
|
+
verification: Dict[str, Any] = None,
|
|
372
|
+
max_wait_time: int = 10,
|
|
373
|
+
check_interval: float = 1.0,
|
|
374
|
+
shell: bool = False
|
|
375
|
+
):
|
|
376
|
+
"""Execute command with verification of results
|
|
377
|
+
|
|
378
|
+
Args:
|
|
379
|
+
command: Command to execute
|
|
380
|
+
verification: Dict with verification criteria:
|
|
381
|
+
- window_exists: Check if window with this name exists
|
|
382
|
+
- command_success: Execute this command and check if it succeeds
|
|
383
|
+
max_wait_time: Maximum time to wait for verification
|
|
384
|
+
check_interval: Time between verification checks
|
|
385
|
+
shell: Whether to use shell
|
|
386
|
+
"""
|
|
387
|
+
if not command:
|
|
388
|
+
raise Exception("Empty command to launch.")
|
|
389
|
+
|
|
390
|
+
verification = verification or {}
|
|
391
|
+
|
|
392
|
+
payload = json.dumps({
|
|
393
|
+
"command": command,
|
|
394
|
+
"shell": shell,
|
|
395
|
+
"verification": verification,
|
|
396
|
+
"max_wait_time": max_wait_time,
|
|
397
|
+
"check_interval": check_interval
|
|
398
|
+
})
|
|
399
|
+
headers = {"Content-Type": "application/json"}
|
|
400
|
+
|
|
401
|
+
try:
|
|
402
|
+
response = requests.post(self.http_server + "/setup" + "/execute_with_verification",
|
|
403
|
+
headers=headers, data=payload, timeout=max_wait_time + 10)
|
|
404
|
+
if response.status_code == 200:
|
|
405
|
+
result = response.json()
|
|
406
|
+
logger.info("Command executed and verified successfully: %s -> %s"
|
|
407
|
+
, " ".join(command) if isinstance(command, list) else command
|
|
408
|
+
, response.text
|
|
409
|
+
)
|
|
410
|
+
return result
|
|
411
|
+
else:
|
|
412
|
+
logger.error("Failed to execute with verification. Status code: %s", response.text)
|
|
413
|
+
raise Exception(f"Command verification failed: {response.text}")
|
|
414
|
+
except requests.exceptions.RequestException as e:
|
|
415
|
+
logger.error("An error occurred while trying to send the request: %s", e)
|
|
416
|
+
traceback.print_exc()
|
|
417
|
+
raise Exception(f"Request failed: {e}")
|
|
418
|
+
|
|
419
|
+
def _command_setup(self, command: List[str], **kwargs):
|
|
420
|
+
self._execute_setup(command, **kwargs)
|
|
421
|
+
|
|
422
|
+
def _sleep_setup(self, seconds: float):
|
|
423
|
+
time.sleep(seconds)
|
|
424
|
+
|
|
425
|
+
def _act_setup(self, action_seq: List[Union[Dict[str, Any], str]]):
|
|
426
|
+
# TODO
|
|
427
|
+
raise NotImplementedError()
|
|
428
|
+
|
|
429
|
+
def _replay_setup(self, trajectory: str):
|
|
430
|
+
"""
|
|
431
|
+
Args:
|
|
432
|
+
trajectory (str): path to the replay trajectory file
|
|
433
|
+
"""
|
|
434
|
+
|
|
435
|
+
# TODO
|
|
436
|
+
raise NotImplementedError()
|
|
437
|
+
|
|
438
|
+
def _activate_window_setup(self, window_name: str, strict: bool = False, by_class: bool = False):
|
|
439
|
+
if not window_name:
|
|
440
|
+
raise Exception(f"Setup Open - Invalid path ({window_name}).")
|
|
441
|
+
|
|
442
|
+
payload = json.dumps({"window_name": window_name, "strict": strict, "by_class": by_class})
|
|
443
|
+
headers = {
|
|
444
|
+
'Content-Type': 'application/json'
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
# send request to server to open file
|
|
448
|
+
try:
|
|
449
|
+
response = requests.post(self.http_server + "/setup" + "/activate_window", headers=headers, data=payload)
|
|
450
|
+
if response.status_code == 200:
|
|
451
|
+
logger.info("Command executed successfully: %s", response.text)
|
|
452
|
+
else:
|
|
453
|
+
logger.error(f"Failed to activate window {window_name}. Status code: %s", response.text)
|
|
454
|
+
except requests.exceptions.RequestException as e:
|
|
455
|
+
logger.error("An error occurred while trying to send the request: %s", e)
|
|
456
|
+
|
|
457
|
+
def _close_window_setup(self, window_name: str, strict: bool = False, by_class: bool = False):
|
|
458
|
+
if not window_name:
|
|
459
|
+
raise Exception(f"Setup Open - Invalid path ({window_name}).")
|
|
460
|
+
|
|
461
|
+
payload = json.dumps({"window_name": window_name, "strict": strict, "by_class": by_class})
|
|
462
|
+
headers = {
|
|
463
|
+
'Content-Type': 'application/json'
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
# send request to server to open file
|
|
467
|
+
try:
|
|
468
|
+
response = requests.post(self.http_server + "/setup" + "/close_window", headers=headers, data=payload)
|
|
469
|
+
if response.status_code == 200:
|
|
470
|
+
logger.info("Command executed successfully: %s", response.text)
|
|
471
|
+
else:
|
|
472
|
+
logger.error(f"Failed to close window {window_name}. Status code: %s", response.text)
|
|
473
|
+
except requests.exceptions.RequestException as e:
|
|
474
|
+
logger.error("An error occurred while trying to send the request: %s", e)
|
|
475
|
+
|
|
476
|
+
def _proxy_setup(self, client_password: str = ""):
|
|
477
|
+
"""Setup system-wide proxy configuration using proxy pool
|
|
478
|
+
|
|
479
|
+
Args:
|
|
480
|
+
client_password (str): Password for sudo operations, defaults to "password"
|
|
481
|
+
"""
|
|
482
|
+
retry = 0
|
|
483
|
+
while retry < MAX_RETRIES:
|
|
484
|
+
try:
|
|
485
|
+
_ = requests.get(self.http_server + "/terminal")
|
|
486
|
+
break
|
|
487
|
+
except:
|
|
488
|
+
time.sleep(5)
|
|
489
|
+
retry += 1
|
|
490
|
+
logger.info(f"retry: {retry}/{MAX_RETRIES}")
|
|
491
|
+
|
|
492
|
+
if retry == MAX_RETRIES:
|
|
493
|
+
return False
|
|
494
|
+
|
|
495
|
+
# Get proxy from global proxy pool
|
|
496
|
+
proxy_pool = get_global_proxy_pool()
|
|
497
|
+
current_proxy = proxy_pool.get_next_proxy()
|
|
498
|
+
|
|
499
|
+
if not current_proxy:
|
|
500
|
+
logger.error("No proxy available from proxy pool")
|
|
501
|
+
raise Exception("No proxy available from proxy pool")
|
|
502
|
+
|
|
503
|
+
# Format proxy URL
|
|
504
|
+
proxy_url = proxy_pool._format_proxy_url(current_proxy)
|
|
505
|
+
logger.info(f"Setting up proxy: {current_proxy.host}:{current_proxy.port}")
|
|
506
|
+
|
|
507
|
+
# Configure system proxy environment variables
|
|
508
|
+
proxy_commands = [
|
|
509
|
+
f"echo '{client_password}' | sudo -S bash -c \"apt-get update\"", ## TODO: remove this line if ami is already updated
|
|
510
|
+
f"echo '{client_password}' | sudo -S bash -c \"apt-get install -y tinyproxy\"", ## TODO: remove this line if tinyproxy is already installed
|
|
511
|
+
f"echo '{client_password}' | sudo -S bash -c \"echo 'Port 18888' > /tmp/tinyproxy.conf\"",
|
|
512
|
+
f"echo '{client_password}' | sudo -S bash -c \"echo 'Allow 127.0.0.1' >> /tmp/tinyproxy.conf\"",
|
|
513
|
+
f"echo '{client_password}' | sudo -S bash -c \"echo 'Upstream http {current_proxy.username}:{current_proxy.password}@{current_proxy.host}:{current_proxy.port}' >> /tmp/tinyproxy.conf\"",
|
|
514
|
+
|
|
515
|
+
# CML commands to set environment variables for proxy
|
|
516
|
+
f"echo 'export http_proxy={proxy_url}' >> ~/.bashrc",
|
|
517
|
+
f"echo 'export https_proxy={proxy_url}' >> ~/.bashrc",
|
|
518
|
+
f"echo 'export HTTP_PROXY={proxy_url}' >> ~/.bashrc",
|
|
519
|
+
f"echo 'export HTTPS_PROXY={proxy_url}' >> ~/.bashrc",
|
|
520
|
+
]
|
|
521
|
+
|
|
522
|
+
# Execute all proxy configuration commands
|
|
523
|
+
for cmd in proxy_commands:
|
|
524
|
+
try:
|
|
525
|
+
self._execute_setup([cmd], shell=True)
|
|
526
|
+
except Exception as e:
|
|
527
|
+
logger.error(f"Failed to execute proxy setup command: {e}")
|
|
528
|
+
proxy_pool.mark_proxy_failed(current_proxy)
|
|
529
|
+
raise
|
|
530
|
+
|
|
531
|
+
self._launch_setup(["tinyproxy -c /tmp/tinyproxy.conf -d"], shell=True)
|
|
532
|
+
|
|
533
|
+
# Reload environment variables
|
|
534
|
+
reload_cmd = "source /etc/environment"
|
|
535
|
+
try:
|
|
536
|
+
logger.info(f"Proxy setup completed successfully for {current_proxy.host}:{current_proxy.port}")
|
|
537
|
+
proxy_pool.mark_proxy_success(current_proxy)
|
|
538
|
+
except Exception as e:
|
|
539
|
+
logger.error(f"Failed to reload environment variables: {e}")
|
|
540
|
+
proxy_pool.mark_proxy_failed(current_proxy)
|
|
541
|
+
raise
|
|
542
|
+
|
|
543
|
+
# Chrome setup
|
|
544
|
+
def _chrome_open_tabs_setup(self, urls_to_open: List[str]):
|
|
545
|
+
host = self.vm_ip
|
|
546
|
+
port = self.chromium_port # fixme: this port is hard-coded, need to be changed from config file
|
|
547
|
+
|
|
548
|
+
remote_debugging_url = f"http://{host}:{port}"
|
|
549
|
+
logger.info("Connect to Chrome @: %s", remote_debugging_url)
|
|
550
|
+
logger.debug("PLAYWRIGHT ENV: %s", repr(os.environ))
|
|
551
|
+
for attempt in range(15):
|
|
552
|
+
if attempt > 0:
|
|
553
|
+
time.sleep(5)
|
|
554
|
+
|
|
555
|
+
browser = None
|
|
556
|
+
with sync_playwright() as p:
|
|
557
|
+
try:
|
|
558
|
+
browser = p.chromium.connect_over_cdp(remote_debugging_url)
|
|
559
|
+
# break
|
|
560
|
+
except Exception as e:
|
|
561
|
+
if attempt < 14:
|
|
562
|
+
logger.error(f"Attempt {attempt + 1}: Failed to connect, retrying. Error: {e}")
|
|
563
|
+
# time.sleep(10)
|
|
564
|
+
continue
|
|
565
|
+
else:
|
|
566
|
+
logger.error(f"Failed to connect after multiple attempts: {e}")
|
|
567
|
+
raise e
|
|
568
|
+
|
|
569
|
+
if not browser:
|
|
570
|
+
return
|
|
571
|
+
|
|
572
|
+
logger.info("Opening %s...", urls_to_open)
|
|
573
|
+
for i, url in enumerate(urls_to_open):
|
|
574
|
+
# Use the first context (which should be the only one if using default profile)
|
|
575
|
+
if i == 0:
|
|
576
|
+
context = browser.contexts[0]
|
|
577
|
+
|
|
578
|
+
page = context.new_page() # Create a new page (tab) within the existing context
|
|
579
|
+
try:
|
|
580
|
+
page.goto(url, timeout=60000)
|
|
581
|
+
except:
|
|
582
|
+
logger.warning("Opening %s exceeds time limit", url) # only for human test
|
|
583
|
+
logger.info(f"Opened tab {i + 1}: {url}")
|
|
584
|
+
|
|
585
|
+
if i == 0:
|
|
586
|
+
# clear the default tab
|
|
587
|
+
default_page = context.pages[0]
|
|
588
|
+
default_page.close()
|
|
589
|
+
|
|
590
|
+
# Do not close the context or browser; they will remain open after script ends
|
|
591
|
+
return browser, context
|
|
592
|
+
|
|
593
|
+
def _chrome_close_tabs_setup(self, urls_to_close: List[str]):
|
|
594
|
+
time.sleep(5) # Wait for Chrome to finish launching
|
|
595
|
+
|
|
596
|
+
host = self.vm_ip
|
|
597
|
+
port = self.chromium_port # fixme: this port is hard-coded, need to be changed from config file
|
|
598
|
+
|
|
599
|
+
remote_debugging_url = f"http://{host}:{port}"
|
|
600
|
+
with sync_playwright() as p:
|
|
601
|
+
browser = None
|
|
602
|
+
for attempt in range(15):
|
|
603
|
+
try:
|
|
604
|
+
browser = p.chromium.connect_over_cdp(remote_debugging_url)
|
|
605
|
+
break
|
|
606
|
+
except Exception as e:
|
|
607
|
+
if attempt < 14:
|
|
608
|
+
logger.error(f"Attempt {attempt + 1}: Failed to connect, retrying. Error: {e}")
|
|
609
|
+
time.sleep(5)
|
|
610
|
+
else:
|
|
611
|
+
logger.error(f"Failed to connect after multiple attempts: {e}")
|
|
612
|
+
raise e
|
|
613
|
+
|
|
614
|
+
if not browser:
|
|
615
|
+
return
|
|
616
|
+
|
|
617
|
+
for i, url in enumerate(urls_to_close):
|
|
618
|
+
# Use the first context (which should be the only one if using default profile)
|
|
619
|
+
if i == 0:
|
|
620
|
+
context = browser.contexts[0]
|
|
621
|
+
|
|
622
|
+
for page in context.pages:
|
|
623
|
+
|
|
624
|
+
# if two urls are the same, close the tab
|
|
625
|
+
if compare_urls(page.url, url):
|
|
626
|
+
context.pages.pop(context.pages.index(page))
|
|
627
|
+
page.close()
|
|
628
|
+
logger.info(f"Closed tab {i + 1}: {url}")
|
|
629
|
+
break
|
|
630
|
+
|
|
631
|
+
# Do not close the context or browser; they will remain open after script ends
|
|
632
|
+
return browser, context
|
|
633
|
+
|
|
634
|
+
# google drive setup
|
|
635
|
+
def _googledrive_setup(self, **config):
|
|
636
|
+
""" Clean google drive space (eliminate the impact of previous experiments to reset the environment)
|
|
637
|
+
@args:
|
|
638
|
+
config(Dict[str, Any]): contain keys
|
|
639
|
+
settings_file(str): path to google drive settings file, which will be loaded by pydrive.auth.GoogleAuth()
|
|
640
|
+
operation(List[str]): each operation is chosen from ['delete', 'upload']
|
|
641
|
+
args(List[Dict[str, Any]]): parameters for each operation
|
|
642
|
+
different args dict for different operations:
|
|
643
|
+
for delete:
|
|
644
|
+
query(str): query pattern string to search files or folder in google drive to delete, please refer to
|
|
645
|
+
https://developers.google.com/drive/api/guides/search-files?hl=en about how to write query string.
|
|
646
|
+
trash(bool): whether to delete files permanently or move to trash. By default, trash=false, completely delete it.
|
|
647
|
+
for mkdirs:
|
|
648
|
+
path(List[str]): the path in the google drive to create folder
|
|
649
|
+
for upload:
|
|
650
|
+
path(str): remote url to download file
|
|
651
|
+
dest(List[str]): the path in the google drive to store the downloaded file
|
|
652
|
+
"""
|
|
653
|
+
settings_file = config.get('settings_file', 'evaluation_examples/settings/googledrive/settings.yml')
|
|
654
|
+
gauth = GoogleAuth(settings_file=settings_file)
|
|
655
|
+
drive = GoogleDrive(gauth)
|
|
656
|
+
|
|
657
|
+
def mkdir_in_googledrive(paths: List[str]):
|
|
658
|
+
paths = [paths] if type(paths) != list else paths
|
|
659
|
+
parent_id = 'root'
|
|
660
|
+
for p in paths:
|
|
661
|
+
q = f'"{parent_id}" in parents and title = "{p}" and mimeType = "application/vnd.google-apps.folder" and trashed = false'
|
|
662
|
+
folder = drive.ListFile({'q': q}).GetList()
|
|
663
|
+
if len(folder) == 0: # not exists, create it
|
|
664
|
+
parents = {} if parent_id == 'root' else {'parents': [{'id': parent_id}]}
|
|
665
|
+
file = drive.CreateFile({'title': p, 'mimeType': 'application/vnd.google-apps.folder', **parents})
|
|
666
|
+
file.Upload()
|
|
667
|
+
parent_id = file['id']
|
|
668
|
+
else:
|
|
669
|
+
parent_id = folder[0]['id']
|
|
670
|
+
return parent_id
|
|
671
|
+
|
|
672
|
+
for oid, operation in enumerate(config['operation']):
|
|
673
|
+
if operation == 'delete': # delete a specific file
|
|
674
|
+
# query pattern string, by default, remove all files/folders not in the trash to the trash
|
|
675
|
+
params = config['args'][oid]
|
|
676
|
+
q = params.get('query', '')
|
|
677
|
+
trash = params.get('trash', False)
|
|
678
|
+
q_file = f"( {q} ) and mimeType != 'application/vnd.google-apps.folder'" if q.strip() else "mimeType != 'application/vnd.google-apps.folder'"
|
|
679
|
+
filelist: GoogleDriveFileList = drive.ListFile({'q': q_file}).GetList()
|
|
680
|
+
q_folder = f"( {q} ) and mimeType = 'application/vnd.google-apps.folder'" if q.strip() else "mimeType = 'application/vnd.google-apps.folder'"
|
|
681
|
+
folderlist: GoogleDriveFileList = drive.ListFile({'q': q_folder}).GetList()
|
|
682
|
+
for file in filelist: # first delete file, then folder
|
|
683
|
+
file: GoogleDriveFile
|
|
684
|
+
if trash:
|
|
685
|
+
file.Trash()
|
|
686
|
+
else:
|
|
687
|
+
file.Delete()
|
|
688
|
+
for folder in folderlist:
|
|
689
|
+
folder: GoogleDriveFile
|
|
690
|
+
# note that, if a folder is trashed/deleted, all files and folders in it will be trashed/deleted
|
|
691
|
+
if trash:
|
|
692
|
+
folder.Trash()
|
|
693
|
+
else:
|
|
694
|
+
folder.Delete()
|
|
695
|
+
elif operation == 'mkdirs':
|
|
696
|
+
params = config['args'][oid]
|
|
697
|
+
mkdir_in_googledrive(params['path'])
|
|
698
|
+
elif operation == 'upload':
|
|
699
|
+
params = config['args'][oid]
|
|
700
|
+
url = params['url']
|
|
701
|
+
with tempfile.NamedTemporaryFile(mode='wb', delete=False) as tmpf:
|
|
702
|
+
response = requests.get(url, stream=True)
|
|
703
|
+
response.raise_for_status()
|
|
704
|
+
for chunk in response.iter_content(chunk_size=8192):
|
|
705
|
+
if chunk:
|
|
706
|
+
tmpf.write(chunk)
|
|
707
|
+
tmpf.close()
|
|
708
|
+
paths = [params['path']] if params['path'] != list else params['path']
|
|
709
|
+
parent_id = mkdir_in_googledrive(paths[:-1])
|
|
710
|
+
parents = {} if parent_id == 'root' else {'parents': [{'id': parent_id}]}
|
|
711
|
+
file = drive.CreateFile({'title': paths[-1], **parents})
|
|
712
|
+
file.SetContentFile(tmpf.name)
|
|
713
|
+
file.Upload()
|
|
714
|
+
return
|
|
715
|
+
else:
|
|
716
|
+
raise ValueError('[ERROR]: not implemented clean type!')
|
|
717
|
+
|
|
718
|
+
def _login_setup(self, **config):
|
|
719
|
+
""" Login to a website with account and password information.
|
|
720
|
+
@args:
|
|
721
|
+
config(Dict[str, Any]): contain keys
|
|
722
|
+
settings_file(str): path to the settings file
|
|
723
|
+
platform(str): platform to login, implemented platforms include:
|
|
724
|
+
googledrive: https://drive.google.com/drive/my-drive
|
|
725
|
+
|
|
726
|
+
"""
|
|
727
|
+
host = self.vm_ip
|
|
728
|
+
port = self.chromium_port
|
|
729
|
+
|
|
730
|
+
remote_debugging_url = f"http://{host}:{port}"
|
|
731
|
+
with sync_playwright() as p:
|
|
732
|
+
browser = None
|
|
733
|
+
for attempt in range(15):
|
|
734
|
+
try:
|
|
735
|
+
browser = p.chromium.connect_over_cdp(remote_debugging_url)
|
|
736
|
+
break
|
|
737
|
+
except Exception as e:
|
|
738
|
+
if attempt < 14:
|
|
739
|
+
logger.error(f"Attempt {attempt + 1}: Failed to connect, retrying. Error: {e}")
|
|
740
|
+
time.sleep(5)
|
|
741
|
+
else:
|
|
742
|
+
logger.error(f"Failed to connect after multiple attempts: {e}")
|
|
743
|
+
raise e
|
|
744
|
+
if not browser:
|
|
745
|
+
return
|
|
746
|
+
|
|
747
|
+
context = browser.contexts[0]
|
|
748
|
+
platform = config['platform']
|
|
749
|
+
|
|
750
|
+
if platform == 'googledrive':
|
|
751
|
+
url = 'https://drive.google.com/drive/my-drive'
|
|
752
|
+
page = context.new_page() # Create a new page (tab) within the existing context
|
|
753
|
+
try:
|
|
754
|
+
page.goto(url, timeout=60000)
|
|
755
|
+
except:
|
|
756
|
+
logger.warning("Opening %s exceeds time limit", url) # only for human test
|
|
757
|
+
logger.info(f"Opened new page: {url}")
|
|
758
|
+
settings = json.load(open(config['settings_file']))
|
|
759
|
+
email, password = settings['email'], settings['password']
|
|
760
|
+
|
|
761
|
+
try:
|
|
762
|
+
page.wait_for_selector('input[type="email"]', state="visible", timeout=3000)
|
|
763
|
+
page.fill('input[type="email"]', email)
|
|
764
|
+
page.click('#identifierNext > div > button')
|
|
765
|
+
page.wait_for_selector('input[type="password"]', state="visible", timeout=5000)
|
|
766
|
+
page.fill('input[type="password"]', password)
|
|
767
|
+
page.click('#passwordNext > div > button')
|
|
768
|
+
page.wait_for_load_state('load', timeout=5000)
|
|
769
|
+
except TimeoutError:
|
|
770
|
+
logger.info('[ERROR]: timeout when waiting for google drive login page to load!')
|
|
771
|
+
return
|
|
772
|
+
|
|
773
|
+
else:
|
|
774
|
+
raise NotImplementedError
|
|
775
|
+
|
|
776
|
+
return browser, context
|
|
777
|
+
|
|
778
|
+
def _update_browse_history_setup(self, **config):
|
|
779
|
+
cache_path = os.path.join(self.cache_dir, "history_new.sqlite")
|
|
780
|
+
db_url = "https://drive.usercontent.google.com/u/0/uc?id=1Lv74QkJYDWVX0RIgg0Co-DUcoYpVL0oX&export=download" # google drive
|
|
781
|
+
if not os.path.exists(cache_path):
|
|
782
|
+
max_retries = 3
|
|
783
|
+
downloaded = False
|
|
784
|
+
e = None
|
|
785
|
+
for i in range(max_retries):
|
|
786
|
+
try:
|
|
787
|
+
response = requests.get(db_url, stream=True)
|
|
788
|
+
response.raise_for_status()
|
|
789
|
+
|
|
790
|
+
with open(cache_path, 'wb') as f:
|
|
791
|
+
for chunk in response.iter_content(chunk_size=8192):
|
|
792
|
+
if chunk:
|
|
793
|
+
f.write(chunk)
|
|
794
|
+
logger.info("File downloaded successfully")
|
|
795
|
+
downloaded = True
|
|
796
|
+
break
|
|
797
|
+
|
|
798
|
+
except requests.RequestException as e:
|
|
799
|
+
logger.error(
|
|
800
|
+
f"Failed to download {db_url} caused by {e}. Retrying... ({max_retries - i - 1} attempts left)")
|
|
801
|
+
if not downloaded:
|
|
802
|
+
raise requests.RequestException(f"Failed to download {db_url}. No retries left. Error: {e}")
|
|
803
|
+
else:
|
|
804
|
+
logger.info("File already exists in cache directory")
|
|
805
|
+
# copy a new history file in the tmp folder
|
|
806
|
+
db_path = cache_path
|
|
807
|
+
|
|
808
|
+
history = config['history']
|
|
809
|
+
|
|
810
|
+
for history_item in history:
|
|
811
|
+
url = history_item['url']
|
|
812
|
+
title = history_item['title']
|
|
813
|
+
visit_time = datetime.now() - timedelta(seconds=history_item['visit_time_from_now_in_seconds'])
|
|
814
|
+
|
|
815
|
+
# Chrome use ms from 1601-01-01 as timestamp
|
|
816
|
+
epoch_start = datetime(1601, 1, 1)
|
|
817
|
+
chrome_timestamp = int((visit_time - epoch_start).total_seconds() * 1000000)
|
|
818
|
+
|
|
819
|
+
conn = sqlite3.connect(db_path)
|
|
820
|
+
cursor = conn.cursor()
|
|
821
|
+
|
|
822
|
+
cursor.execute('''
|
|
823
|
+
INSERT INTO urls (url, title, visit_count, typed_count, last_visit_time, hidden)
|
|
824
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
825
|
+
''', (url, title, 1, 0, chrome_timestamp, 0))
|
|
826
|
+
|
|
827
|
+
url_id = cursor.lastrowid
|
|
828
|
+
|
|
829
|
+
cursor.execute('''
|
|
830
|
+
INSERT INTO visits (url, visit_time, from_visit, transition, segment_id, visit_duration)
|
|
831
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
832
|
+
''', (url_id, chrome_timestamp, 0, 805306368, 0, 0))
|
|
833
|
+
|
|
834
|
+
conn.commit()
|
|
835
|
+
conn.close()
|
|
836
|
+
|
|
837
|
+
logger.info('Fake browsing history added successfully.')
|
|
838
|
+
|
|
839
|
+
controller = PythonController(self.vm_ip, self.server_port)
|
|
840
|
+
|
|
841
|
+
# get the path of the history file according to the platform
|
|
842
|
+
os_type = controller.get_vm_platform()
|
|
843
|
+
|
|
844
|
+
if os_type == 'Windows':
|
|
845
|
+
chrome_history_path = controller.execute_python_command(
|
|
846
|
+
"""import os; print(os.path.join(os.getenv('USERPROFILE'), "AppData", "Local", "Google", "Chrome", "User Data", "Default", "History"))""")[
|
|
847
|
+
'output'].strip()
|
|
848
|
+
elif os_type == 'Darwin':
|
|
849
|
+
chrome_history_path = controller.execute_python_command(
|
|
850
|
+
"""import os; print(os.path.join(os.getenv('HOME'), "Library", "Application Support", "Google", "Chrome", "Default", "History"))""")[
|
|
851
|
+
'output'].strip()
|
|
852
|
+
elif os_type == 'Linux':
|
|
853
|
+
if "arm" in platform.machine():
|
|
854
|
+
chrome_history_path = controller.execute_python_command(
|
|
855
|
+
"import os; print(os.path.join(os.getenv('HOME'), 'snap', 'chromium', 'common', 'chromium', 'Default', 'History'))")[
|
|
856
|
+
'output'].strip()
|
|
857
|
+
else:
|
|
858
|
+
chrome_history_path = controller.execute_python_command(
|
|
859
|
+
"import os; print(os.path.join(os.getenv('HOME'), '.config', 'google-chrome', 'Default', 'History'))")[
|
|
860
|
+
'output'].strip()
|
|
861
|
+
else:
|
|
862
|
+
raise Exception('Unsupported operating system')
|
|
863
|
+
|
|
864
|
+
form = MultipartEncoder({
|
|
865
|
+
"file_path": chrome_history_path,
|
|
866
|
+
"file_data": (os.path.basename(chrome_history_path), open(db_path, "rb"))
|
|
867
|
+
})
|
|
868
|
+
headers = {"Content-Type": form.content_type}
|
|
869
|
+
logger.debug(form.content_type)
|
|
870
|
+
|
|
871
|
+
# send request to server to upload file
|
|
872
|
+
try:
|
|
873
|
+
logger.debug("REQUEST ADDRESS: %s", self.http_server + "/setup" + "/upload")
|
|
874
|
+
response = requests.post(self.http_server + "/setup" + "/upload", headers=headers, data=form)
|
|
875
|
+
if response.status_code == 200:
|
|
876
|
+
logger.info("Command executed successfully: %s", response.text)
|
|
877
|
+
else:
|
|
878
|
+
logger.error("Failed to upload file. Status code: %s", response.text)
|
|
879
|
+
except requests.exceptions.RequestException as e:
|
|
880
|
+
logger.error("An error occurred while trying to send the request: %s", e)
|
|
881
|
+
|
|
882
|
+
self._execute_setup(["sudo chown -R user:user /home/user/.config/google-chrome/Default/History"], shell=True)
|