1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
| import sys
if sys.version_info[0] < 3: print("错误: 此脚本需要 Python 3") sys.exit(1)
import json import os from pathlib import Path import argparse import hashlib import uuid import random import string import subprocess
def get_config_path(): """根据不同操作系统返回配置文件路径""" if sys.platform == "darwin": base_path = Path("~/Library/Application Support/Cursor/User/globalStorage") elif sys.platform == "win32": base_path = Path(os.environ.get("APPDATA", "")) / "Cursor/User/globalStorage" else: base_path = Path("~/.config/Cursor/User/globalStorage") return Path(os.path.expanduser(str(base_path))) / "storage.json"
CONFIG_PATH = get_config_path()
def is_cursor_running(): """检查 Cursor 是否正在运行(不依赖第三方库)""" try: if sys.platform == "win32": output = subprocess.check_output('tasklist', shell=True).decode() return 'cursor' in output.lower() else: output = subprocess.check_output(['ps', 'aux']).decode() return 'cursor' in output.lower() except: return False
def check_cursor_process(func): """装饰器:检查 Cursor 进程""" def wrapper(*args, **kwargs): if is_cursor_running(): print("警告: 检测到 Cursor 正在运行!") print("请先关闭 Cursor 再执行操作,否则修改可能会被覆盖。") choice = input("是否继续?(y/N): ") if choice.lower() != 'y': return return func(*args, **kwargs) return wrapper
def show_config(): """显示当前配置文件的内容""" try: if not CONFIG_PATH.exists(): print(f"配置文件不存在: {CONFIG_PATH}") return with open(CONFIG_PATH, 'r', encoding='utf-8') as f: data = json.load(f) print(json.dumps(data, indent=2, ensure_ascii=False)) except Exception as e: print(f"读取配置文件时出错: {str(e)}")
def get_value(key): """获取指定键的值""" try: if not CONFIG_PATH.exists(): print(f"配置文件不存在: {CONFIG_PATH}") return with open(CONFIG_PATH, 'r', encoding='utf-8') as f: data = json.load(f) value = data.get(key) if value is None: print(f"未找到键: {key}") else: print(json.dumps(value, indent=2, ensure_ascii=False)) except Exception as e: print(f"读取配置文件时出错: {str(e)}")
def get_machine_ids(): """获取机器 ID 信息""" try: if not CONFIG_PATH.exists(): print(f"配置文件不存在: {CONFIG_PATH}") return with open(CONFIG_PATH, 'r', encoding='utf-8') as f: data = json.load(f) mac_id = data.get("telemetry.macMachineId", "未设置") machine_id = data.get("telemetry.machineId", "未设置") print(f"Mac机器ID: {mac_id}") print(f"机器ID: {machine_id}") except Exception as e: print(f"读取配置文件时出错: {str(e)}")
@check_cursor_process def set_value(key, value): """设置指定键的值""" try: data = {} if CONFIG_PATH.exists(): with open(CONFIG_PATH, 'r', encoding='utf-8') as f: data = json.load(f) try: value = json.loads(value) except json.JSONDecodeError: pass data[key] = value CONFIG_PATH.parent.mkdir(parents=True, exist_ok=True) with open(CONFIG_PATH, 'w', encoding='utf-8') as f: json.dump(data, f, indent=2, ensure_ascii=False) print(f"已设置 {key} = {value}") except Exception as e: print(f"设置值时出错: {str(e)}")
@check_cursor_process def reset_machine_ids(): """重置机器 ID""" try: if not CONFIG_PATH.exists(): print(f"配置文件不存在: {CONFIG_PATH}") return with open(CONFIG_PATH, 'r', encoding='utf-8') as f: data = json.load(f) data.pop("telemetry.macMachineId", None) data.pop("telemetry.machineId", None) with open(CONFIG_PATH, 'w', encoding='utf-8') as f: json.dump(data, f, indent=2, ensure_ascii=False) print("已成功重置机器 ID") except Exception as e: print(f"重置机器 ID 时出错: {str(e)}")
@check_cursor_process def generate_random_machine_ids(): """生成随机的机器 ID""" try: if not CONFIG_PATH.exists(): print(f"配置文件不存在: {CONFIG_PATH}") return def generate_random_hash(): random_str = ''.join(random.choices(string.ascii_letters + string.digits, k=32)) random_str += str(uuid.uuid4()) return hashlib.sha256(random_str.encode()).hexdigest() with open(CONFIG_PATH, 'r', encoding='utf-8') as f: data = json.load(f) data["telemetry.macMachineId"] = generate_random_hash() data["telemetry.machineId"] = generate_random_hash() with open(CONFIG_PATH, 'w', encoding='utf-8') as f: json.dump(data, f, indent=2, ensure_ascii=False) print("已生成新的机器 ID:") print(f"Mac机器ID: {data['telemetry.macMachineId']}") print(f"机器ID: {data['telemetry.machineId']}") except Exception as e: print(f"生成机器 ID 时出错: {str(e)}")
def main(): parser = argparse.ArgumentParser(description='管理 Cursor 配置文件的命令行工具') subparsers = parser.add_subparsers(dest='command', help='可用命令')
subparsers.add_parser('show', help='显示当前配置文件的内容')
get_parser = subparsers.add_parser('get', help='获取指定键的值') get_parser.add_argument('key', help='键名')
set_parser = subparsers.add_parser('set', help='设置指定键的值') set_parser.add_argument('key', help='键名') set_parser.add_argument('value', help='值')
subparsers.add_parser('ids', help='显示机器 ID 信息') subparsers.add_parser('reset-ids', help='重置机器 ID') subparsers.add_parser('random-ids', help='生成随机机器 ID')
args = parser.parse_args()
if args.command == 'show': show_config() elif args.command == 'get': get_value(args.key) elif args.command == 'set': set_value(args.key, args.value) elif args.command == 'ids': get_machine_ids() elif args.command == 'reset-ids': reset_machine_ids() elif args.command == 'random-ids': generate_random_machine_ids() else: parser.print_help()
if __name__ == '__main__': main()
|