Parcourir la source

feat: 实现许可证激活和验证功能,增加许可证管理服务器

chengchongzhen il y a 1 an
Parent
commit
dbf8690753
6 fichiers modifiés avec 333 ajouts et 30 suppressions
  1. 12 5
      cursor_pro_keep_alive.py
  2. 125 25
      license_manager.py
  3. 3 0
      server/.env
  4. 28 0
      server/models/License.js
  5. 21 0
      server/package.json
  6. 144 0
      server/server.js

+ 12 - 5
cursor_pro_keep_alive.py

@@ -348,13 +348,20 @@ def cleanup_temp_files():
 
 if __name__ == "__main__":
     browser_manager = None
-    license_manager = None
     try:
-        # 检查许可证
         license_manager = LicenseManager()
-        if not license_manager.check_license():
-            print("免费使用次数已用完")
-            sys.exit(1)
+
+        # 验证许可证
+        is_valid, message = license_manager.verify_license()
+        if not is_valid:
+            print(f"许可证验证失败: {message}")
+            # 提示用户激活
+            license_key = input("请输入激活码: ")
+            success, activate_message = license_manager.activate_license(license_key)
+            if not success:
+                print(f"激活失败: {activate_message}")
+                sys.exit(1)
+            print("激活成功!")
 
         # 加载配置
         config = load_config()

+ 125 - 25
license_manager.py

@@ -1,5 +1,12 @@
+import requests
 import json
 import os
+import platform
+import uuid
+import hashlib
+from datetime import datetime
+from cryptography.fernet import Fernet
+import base64
 
 
 class LicenseManager:
@@ -7,39 +14,132 @@ class LicenseManager:
         self.license_file = os.path.join(
             os.getenv("APPDATA"), "CursorPro", "license.json"
         )
-        self.max_uses = 10  # 最大使用次数
+        self.activation_url = (
+            "https://your-activation-server.com/activate"  # 替换为您的激活服务器地址
+        )
+        self.verify_url = (
+            "https://your-activation-server.com/verify"  # 替换为您的验证服务器地址
+        )
+        self.key = b"Kj8nP9x2Qs5mY7vR4wL1hC3fA6tD0iB8"
+        self.fernet = Fernet(base64.b64encode(self.key))
+
+    def get_hardware_info(self):
+        """获取硬件信息作为机器码"""
+        system_info = {
+            "platform": platform.platform(),
+            "machine": platform.machine(),
+            "processor": platform.processor(),
+            "hostname": platform.node(),
+        }
+
+        # 获取MAC地址
+        mac = ":".join(
+            [
+                "{:02x}".format((uuid.getnode() >> elements) & 0xFF)
+                for elements in range(0, 2 * 6, 2)
+            ][::-1]
+        )
+        system_info["mac"] = mac
+
+        # 生成机器码
+        machine_code = hashlib.md5(json.dumps(system_info).encode()).hexdigest()
+        return machine_code
 
-    def check_license(self):
+    def activate_license(self, license_key):
+        """在线激活许可证"""
         try:
-            # 确保目录存在
-            os.makedirs(os.path.dirname(self.license_file), exist_ok=True)
+            machine_code = self.get_hardware_info()
+
+            # 准备激活数据
+            activation_data = {
+                "license_key": license_key,
+                "machine_code": machine_code,
+                "activation_date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
+            }
+
+            # 发送激活请求
+            response = requests.post(
+                self.activation_url, json=activation_data, timeout=10
+            )
 
+            if response.status_code == 200:
+                result = response.json()
+                if result.get("success"):
+                    # 保存许可证信息
+                    license_data = {
+                        "license_key": license_key,
+                        "machine_code": machine_code,
+                        "activation_date": activation_data["activation_date"],
+                        "expiry_date": result.get("expiry_date"),
+                        "is_activated": True,
+                    }
+
+                    self._save_license(license_data)
+                    return True, "激活成功!"
+                else:
+                    return False, result.get("message", "激活失败")
+            else:
+                return False, "服务器连接失败"
+
+        except Exception as e:
+            return False, f"激活过程出错: {str(e)}"
+
+    def verify_license(self):
+        """验证许可证"""
+        try:
             if not os.path.exists(self.license_file):
-                # 首次运行,创建许可文件
-                license_data = {"use_count": 0, "is_activated": False}
-                with open(self.license_file, "w") as f:
-                    json.dump(license_data, f)
-                return True
+                return False, "未找到许可证文件"
+
+            license_data = self._load_license()
+            if not license_data:
+                return False, "许可证文件无效"
 
-            # 读取许可信息
-            with open(self.license_file, "r") as f:
-                license_data = json.load(f)
+            # 验证机器码
+            current_machine = self.get_hardware_info()
+            if current_machine != license_data.get("machine_code"):
+                return False, "硬件信息不匹配"
 
-            if license_data.get("is_activated"):
-                return True
+            # 在线验证
+            verify_data = {
+                "license_key": license_data.get("license_key"),
+                "machine_code": current_machine,
+            }
 
-            # 检查使用次数
-            use_count = license_data.get("use_count", 0)
-            if use_count >= self.max_uses:
-                return False
+            response = requests.post(self.verify_url, json=verify_data, timeout=10)
 
-            # 增加使用次数并保存
-            license_data["use_count"] = use_count + 1
-            with open(self.license_file, "w") as f:
-                json.dump(license_data, f)
+            if response.status_code == 200:
+                result = response.json()
+                if result.get("success"):
+                    return True, "许可证有效"
+                return False, result.get("message", "许可证无效")
 
-            return True
+            # 如果在线验证失败,使用本地数据
+            expiry_date = datetime.strptime(license_data["expiry_date"], "%Y-%m-%d")
+            if datetime.now() > expiry_date:
+                return False, "许可证已过期"
+
+            return True, "许可证有效"
+
+        except Exception as e:
+            return False, f"验证过程出错: {str(e)}"
 
+    def _save_license(self, license_data):
+        """加密保存许可证数据"""
+        try:
+            os.makedirs(os.path.dirname(self.license_file), exist_ok=True)
+            encrypted_data = self.fernet.encrypt(json.dumps(license_data).encode())
+            with open(self.license_file, "wb") as f:
+                f.write(encrypted_data)
+        except Exception as e:
+            print(f"保存许可证出错: {e}")
+
+    def _load_license(self):
+        """加密读取许可证数据"""
+        try:
+            with open(self.license_file, "rb") as f:
+                encrypted_data = f.read()
+            decrypted_data = self.fernet.decrypt(encrypted_data)
+            return json.loads(decrypted_data)
         except Exception as e:
-            print(f"License check error: {e}")
-            return False
+            print(f"读取许可证出错: {e}")
+            return None

+ 3 - 0
server/.env

@@ -0,0 +1,3 @@
+MONGODB_URI=mongodb://49.234.181.38:27017/license-manager
+PORT=3000
+ENCRYPTION_KEY=Kj8nP9x2Qs5mY7vR4wL1hC3fA6tD0iB8 

+ 28 - 0
server/models/License.js

@@ -0,0 +1,28 @@
+const mongoose = require('mongoose');
+
+const licenseSchema = new mongoose.Schema({
+    licenseKey: {
+        type: String,
+        required: true,
+        unique: true
+    },
+    machineCode: {
+        type: String,
+        required: true
+    },
+    activationDate: {
+        type: Date,
+        required: true,
+        default: Date.now
+    },
+    expiryDate: {
+        type: Date,
+        required: true
+    },
+    isActive: {
+        type: Boolean,
+        default: true
+    }
+});
+
+module.exports = mongoose.model('License', licenseSchema); 

+ 21 - 0
server/package.json

@@ -0,0 +1,21 @@
+{
+  "name": "license-server",
+  "version": "1.0.0",
+  "description": "License activation server for Cursor Pro",
+  "main": "server.js",
+  "scripts": {
+    "start": "node server.js",
+    "dev": "nodemon server.js"
+  },
+  "dependencies": {
+    "express": "^4.18.2",
+    "mongoose": "^8.0.3",
+    "dotenv": "^16.3.1",
+    "cors": "^2.8.5",
+    "crypto": "^1.0.1",
+    "moment": "^2.29.4"
+  },
+  "devDependencies": {
+    "nodemon": "^3.0.2"
+  }
+} 

+ 144 - 0
server/server.js

@@ -0,0 +1,144 @@
+require('dotenv').config();
+const express = require('express');
+const mongoose = require('mongoose');
+const cors = require('cors');
+const crypto = require('crypto');
+const moment = require('moment');
+const License = require('./models/License');
+
+const app = express();
+
+// Middleware
+app.use(cors());
+app.use(express.json());
+
+// Connect to MongoDB
+mongoose.connect(process.env.MONGODB_URI)
+    .then(() => console.log('Connected to MongoDB'))
+    .catch(err => console.error('MongoDB connection error:', err));
+
+// Activation endpoint
+app.post('/activate', async (req, res) => {
+    try {
+        const { license_key, machine_code, activation_date } = req.body;
+
+        // Validate input
+        if (!license_key || !machine_code) {
+            return res.status(400).json({
+                success: false,
+                message: '许可证密钥和机器码是必需的'
+            });
+        }
+
+        // Check if license already exists
+        const existingLicense = await License.findOne({ licenseKey: license_key });
+        if (existingLicense) {
+            if (existingLicense.machineCode !== machine_code) {
+                return res.status(400).json({
+                    success: false,
+                    message: '许可证已在其他设备上激活'
+                });
+            }
+            if (!existingLicense.isActive) {
+                return res.status(400).json({
+                    success: false,
+                    message: '许可证已被禁用'
+                });
+            }
+        }
+
+        // Create new license or update existing one
+        const expiryDate = moment().add(1, 'year').toDate(); // 设置一年有效期
+        const licenseData = {
+            licenseKey: license_key,
+            machineCode: machine_code,
+            activationDate: activation_date || new Date(),
+            expiryDate: expiryDate,
+            isActive: true
+        };
+
+        if (existingLicense) {
+            await License.updateOne({ licenseKey: license_key }, licenseData);
+        } else {
+            await License.create(licenseData);
+        }
+
+        return res.json({
+            success: true,
+            message: '激活成功',
+            expiry_date: expiryDate.toISOString().split('T')[0]
+        });
+    } catch (error) {
+        console.error('激活错误:', error);
+        return res.status(500).json({
+            success: false,
+            message: '服务器错误'
+        });
+    }
+});
+
+// Verification endpoint
+app.post('/verify', async (req, res) => {
+    try {
+        const { license_key, machine_code } = req.body;
+
+        // Validate input
+        if (!license_key || !machine_code) {
+            return res.status(400).json({
+                success: false,
+                message: '许可证密钥和机器码是必需的'
+            });
+        }
+
+        // Find license
+        const license = await License.findOne({ licenseKey: license_key });
+        
+        if (!license) {
+            return res.status(404).json({
+                success: false,
+                message: '许可证不存在'
+            });
+        }
+
+        // Check machine code
+        if (license.machineCode !== machine_code) {
+            return res.status(400).json({
+                success: false,
+                message: '硬件信息不匹配'
+            });
+        }
+
+        // Check if license is active
+        if (!license.isActive) {
+            return res.status(400).json({
+                success: false,
+                message: '许可证已被禁用'
+            });
+        }
+
+        // Check expiry
+        if (moment().isAfter(license.expiryDate)) {
+            return res.status(400).json({
+                success: false,
+                message: '许可证已过期'
+            });
+        }
+
+        return res.json({
+            success: true,
+            message: '许可证有效',
+            expiry_date: license.expiryDate.toISOString().split('T')[0]
+        });
+    } catch (error) {
+        console.error('验证错误:', error);
+        return res.status(500).json({
+            success: false,
+            message: '服务器错误'
+        });
+    }
+});
+
+const PORT = process.env.PORT || 3000;
+app.listen(PORT, () => {
+    console.log(`Server is running on port ${PORT}`);
+});