Skip to content

Commit

Permalink
🐞 fix: fix some bugs/#9
Browse files Browse the repository at this point in the history
  • Loading branch information
GalacticDevOps committed Jan 16, 2025
1 parent 67d3075 commit 2e61619
Show file tree
Hide file tree
Showing 16 changed files with 414 additions and 278 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.0] - 2024-01-16

### Added
- Auto Restart Cursor Process
- Auto Reset Machine Ids

### Fixed
- Fix some known issues

## [1.0.9] - 2024-01-15

### Fixed
Expand Down
2 changes: 2 additions & 0 deletions electron-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ nsis:
allowElevation: true
# 是否允许用户更改安装目录
allowToChangeInstallationDirectory: true
portable:
artifactName: ${name}-${version}-portable.${ext}
mac:
executableName: ez-cursor-free
icon: build/icons/512x512.png
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ez-cursor-free",
"version": "1.0.9",
"version": "1.1.0",
"description": "Ez2 Use Cursor Free Tools",
"main": "./out/main/index.js",
"author": "Soleil",
Expand Down
88 changes: 88 additions & 0 deletions resources/workspace/_machine_ids_reset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import os
import sys
import json
import uuid
import hashlib
from logger import logging


class ResetterMachineIDs:
def __init__(self):
if sys.platform == "win32":
appdata = os.getenv("APPDATA")
if appdata is None:
raise EnvironmentError("not.set.environment.variable.APPDATA")
self.db_path = os.path.join(
appdata, "Cursor", "User", "globalStorage", "storage.json"
)
elif sys.platform == "darwin":
self.db_path = os.path.abspath(
os.path.expanduser(
"~/Library/Application Support/Cursor/User/globalStorage/storage.json"
)
)
elif sys.platform == "linux":
self.db_path = os.path.abspath(
os.path.expanduser("~/.config/Cursor/User/globalStorage/storage.json")
)
else:
raise NotImplementedError('os.not.supported: {sys.platform}')

def generate_new_ids(self):
dev_device_id = str(uuid.uuid4())
machine_id = hashlib.sha256(os.urandom(32)).hexdigest()
mac_machine_id = hashlib.sha512(os.urandom(64)).hexdigest()
sqm_id = "{" + str(uuid.uuid4()).upper() + "}"

return {
"telemetry.devDeviceId": dev_device_id,
"telemetry.macMachineId": mac_machine_id,
"telemetry.machineId": machine_id,
"telemetry.sqmId": sqm_id,
}

def reset_machine_ids(self):
try:
logging.info('resetting.machine.ids')

if not os.path.exists(self.db_path):
logging.error('config.not.exists')
return False

if not os.access(self.db_path, os.R_OK | os.W_OK):
logging.error('config.not.accessible')
logging.warning('read.write.permission.required')
return False

logging.info('reading.config')
with open(self.db_path, "r", encoding="utf-8") as f:
config = json.load(f)

logging.info('generating.new.ids')
new_ids = self.generate_new_ids()

config.update(new_ids)

logging.info('saving.new.config')
with open(self.db_path, "w", encoding="utf-8") as f:
json.dump(config, f, indent=4)

logging.info('machine.ids.reset.success')
logging.info('machine.ids.reset.info:')
for key, value in new_ids.items():
logging.info('key: %s, value: %s' % (key, value))

return True

except PermissionError as e:
logging.error('admin.permission.required')
return False
except Exception as e:
logging.error(f'error.resetting.machine.ids: {str(e)}')

return False


if __name__ == "__main__":
resetter = ResetterMachineIDs()
resetter.reset_machine_ids()
20 changes: 5 additions & 15 deletions resources/workspace/browser_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,25 @@ def __init__(self, extension_path=None):
self.extension_path = extension_path

def init_browser(self):
"""初始化浏览器"""
co = self._get_browser_options()
self.browser = Chromium(co)
return self.browser

def _get_browser_options(self):
"""获取浏览器配置"""
co = ChromiumOptions()
browser_path = os.getenv("BROWSER_PATH", None)
if browser_path and os.path.exists(browser_path):
co.set_paths(browser_path=browser_path)
# 尝试加载插件
try:
extension_path = self._get_extension_path()
if extension_path:
co.add_extension(extension_path)
logging.info("插件加载成功")
logging.info('extension.loaded')
else:
logging.warning("未找到插件目录")
logging.warning('extension.not.loaded')
except Exception as e:
logging.warning(f"加载插件时出错: {e}")
logging.warning(f'extension.load.error {str(e)}')

# 基本配置
co.set_user_agent(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36"
)
Expand All @@ -51,33 +47,27 @@ def _get_browser_options(self):
return co

def _get_extension_path(self):
"""获取插件路径"""
# 如果指定了插件路径,优先使用
if self.extension_path and os.path.exists(self.extension_path):
return self.extension_path

# 获取脚本所在目录
script_dir = os.path.dirname(os.path.abspath(__file__))
extension_path = os.path.join(script_dir, "turnstilePatch")

# 如果是打包后的环境
if hasattr(sys, "_MEIPASS"):
extension_path = os.path.join(sys._MEIPASS, "turnstilePatch")

# 检查插件文件是否完整
if os.path.exists(extension_path):
required_files = ['manifest.json', 'script.js']
if all(os.path.exists(os.path.join(extension_path, f)) for f in required_files):
return extension_path
else:
logging.warning(f"插件目录 {extension_path} 文件不完整")
logging.warning(f'not.all.required.files {required_files}')
else:
raise FileNotFoundError(f"插件不存在: {extension_path}")
raise FileNotFoundError(f'extension.not.found {extension_path}')

return None

def quit(self):
"""关闭浏览器"""
if self.browser:
try:
self.browser.quit()
Expand Down
21 changes: 10 additions & 11 deletions resources/workspace/cursor_auth_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@


class CursorAuthManager:
"""Cursor认证信息管理器"""

def __init__(self):
# 判断操作系统
if os.name == "nt": # Windows
appdata = os.getenv("APPDATA")
if appdata is None:
raise EnvironmentError("not.set.environment.variable.APPDATA")
self.db_path = os.path.join(
os.getenv("APPDATA"), "Cursor", "User", "globalStorage", "state.vscdb"
appdata, "Cursor", "User", "globalStorage", "state.vscdb"
)
elif os.name == 'posix': # Linux or macOS
# Further check for macOS
if os.uname().sysname == 'Darwin':
self.db_path = os.path.expanduser(
"~/Library/Application Support/Cursor/User/globalStorage/state.vscdb"
Expand All @@ -24,9 +23,9 @@ def __init__(self):
"~/.config/Cursor/User/globalStorage/state.vscdb"
)
else:
logging.warning("Unknown operating system.")
raise NotImplementedError('os.not.supported: {sys.platform}')

logging.info(f"Database path is: {self.db_path}")
logging.info(f"auth.db.path: {self.db_path}")


def update_auth(self, email=None, access_token=None, refresh_token=None):
Expand All @@ -51,7 +50,7 @@ def update_auth(self, email=None, access_token=None, refresh_token=None):
conn.close()

conn = sqlite3.connect(self.db_path)
logging.info('auth.connected_to_database')
logging.info('auth.connected.database')
cursor = conn.cursor()

conn.execute("PRAGMA busy_timeout = 5000")
Expand Down Expand Up @@ -81,18 +80,18 @@ def update_auth(self, email=None, access_token=None, refresh_token=None):
UPDATE ItemTable SET value = ?
WHERE key = ?
""", (value, key))
logging.info(f'Updating {key.split('/')[-1]}')
logging.info(f'auth.updating {key.split('/')[-1]}')

cursor.execute("COMMIT")
logging.info('auth.database_updated_successfully')
logging.info('auth.database.updated.successfully')
return True

except Exception as e:
cursor.execute("ROLLBACK")
raise e

except sqlite3.Error as e:
logging.error("database_error:", str(e))
logging.error("database.error:", str(e))
return False
except Exception as e:
logging.error("error:", str(e))
Expand Down
Loading

0 comments on commit 2e61619

Please sign in to comment.