feat: 添加许可证密钥管理功能,更新许可证激活逻辑,支持环境变量配置,增加Docker支持
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -21,3 +21,5 @@ __pycache__/
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
||||||
venv/
|
venv/
|
||||||
|
|
||||||
|
node_modules/
|
||||||
|
|||||||
@@ -8,19 +8,22 @@ from datetime import datetime
|
|||||||
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
||||||
from cryptography.hazmat.backends import default_backend
|
from cryptography.hazmat.backends import default_backend
|
||||||
import base64
|
import base64
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
class LicenseManager:
|
class LicenseManager:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.license_file = os.path.join(
|
# 根据不同操作系统获取适当的配置目录
|
||||||
os.getenv("APPDATA"), "CursorPro", "license.json"
|
if platform.system() == "Windows":
|
||||||
)
|
config_dir = os.getenv("APPDATA")
|
||||||
self.activation_url = (
|
elif platform.system() == "Darwin": # macOS
|
||||||
"https://your-activation-server.com/activate" # 替换为您的激活服务器地址
|
config_dir = os.path.expanduser("~/Library/Application Support")
|
||||||
)
|
else: # Linux 和其他类 Unix 系统
|
||||||
self.verify_url = (
|
config_dir = os.path.expanduser("~/.config")
|
||||||
"https://your-activation-server.com/verify" # 替换为您的验证服务器地址
|
|
||||||
)
|
self.license_file = os.path.join(config_dir, "CursorPro", "license.json")
|
||||||
|
self.activation_url = "http://119.8.35.41:3003/activate"
|
||||||
|
self.verify_url = "http://119.8.35.41:3003/verify"
|
||||||
self.key = b"Kj8nP9x2Qs5mY7vR4wL1hC3fA6tD0iB8"
|
self.key = b"Kj8nP9x2Qs5mY7vR4wL1hC3fA6tD0iB8"
|
||||||
|
|
||||||
def encrypt(self, text):
|
def encrypt(self, text):
|
||||||
@@ -89,32 +92,55 @@ class LicenseManager:
|
|||||||
"activation_date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
"activation_date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
||||||
}
|
}
|
||||||
|
|
||||||
# 发送激活请求
|
try:
|
||||||
response = requests.post(
|
print("\n正在连接激活服务器...")
|
||||||
self.activation_url, json=activation_data, timeout=10
|
print("发送激活请求数据:", activation_data)
|
||||||
)
|
|
||||||
|
|
||||||
if response.status_code == 200:
|
response = requests.post(
|
||||||
result = response.json()
|
self.activation_url, json=activation_data, timeout=10
|
||||||
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)
|
print(f"服务器响应状态码: {response.status_code}")
|
||||||
return True, "激活成功!"
|
print(f"服务器响应内容: {response.text}")
|
||||||
|
|
||||||
|
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_active": True,
|
||||||
|
}
|
||||||
|
self._save_license(license_data)
|
||||||
|
return True, "激活成功!"
|
||||||
|
else:
|
||||||
|
error_msg = result.get("message", "激活失败")
|
||||||
|
print(f"激活失败: {error_msg}")
|
||||||
|
return False, error_msg
|
||||||
else:
|
else:
|
||||||
return False, result.get("message", "激活失败")
|
error_msg = f"服务器响应错误: HTTP {response.status_code}"
|
||||||
else:
|
print(error_msg)
|
||||||
return False, "服务器连接失败"
|
return False, error_msg
|
||||||
|
|
||||||
|
except requests.exceptions.ConnectionError as e:
|
||||||
|
error_msg = f"无法连接到激活服务器: {str(e)}"
|
||||||
|
print(error_msg)
|
||||||
|
return False, "无法连接到激活服务器,请检查网络连接"
|
||||||
|
except requests.exceptions.Timeout:
|
||||||
|
error_msg = "服务器响应超时"
|
||||||
|
print(error_msg)
|
||||||
|
return False, error_msg
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return False, f"激活过程出错: {str(e)}"
|
error_msg = f"激活过程出错: {str(e)}"
|
||||||
|
print(error_msg)
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
print(traceback.format_exc())
|
||||||
|
return False, error_msg
|
||||||
|
|
||||||
def verify_license(self):
|
def verify_license(self):
|
||||||
"""验证许可证"""
|
"""验证许可证"""
|
||||||
@@ -137,23 +163,60 @@ class LicenseManager:
|
|||||||
"machine_code": current_machine,
|
"machine_code": current_machine,
|
||||||
}
|
}
|
||||||
|
|
||||||
response = requests.post(self.verify_url, json=verify_data, timeout=10)
|
try:
|
||||||
|
print("正在验证许可证...")
|
||||||
|
print("发送验证请求数据:", verify_data)
|
||||||
|
|
||||||
if response.status_code == 200:
|
# 设置更长的超时时间
|
||||||
result = response.json()
|
response = requests.post(
|
||||||
if result.get("success"):
|
self.verify_url, json=verify_data, timeout=30 # 增加超时时间到30秒
|
||||||
return True, "许可证有效"
|
)
|
||||||
return False, result.get("message", "许可证无效")
|
|
||||||
|
|
||||||
# 如果在线验证失败,使用本地数据
|
# 等待响应
|
||||||
expiry_date = datetime.strptime(license_data["expiry_date"], "%Y-%m-%d")
|
time.sleep(2) # 添加短暂延迟
|
||||||
if datetime.now() > expiry_date:
|
|
||||||
return False, "许可证已过期"
|
|
||||||
|
|
||||||
return True, "许可证有效"
|
print(f"服务器响应状态码: {response.status_code}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
response_text = response.text
|
||||||
|
print(f"服务器响应内容: {response_text}")
|
||||||
|
result = response.json()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"解析响应失败: {str(e)}")
|
||||||
|
return False, "服务器响应格式错误"
|
||||||
|
|
||||||
|
if response.status_code == 200:
|
||||||
|
if result.get("success"):
|
||||||
|
print("许可证验证成功")
|
||||||
|
return True, "许可证有效"
|
||||||
|
error_msg = result.get("message", "许可证无效")
|
||||||
|
print(f"验证失败: {error_msg}")
|
||||||
|
return False, error_msg
|
||||||
|
|
||||||
|
error_msg = f"服务器响应错误: HTTP {response.status_code}"
|
||||||
|
print(error_msg)
|
||||||
|
return False, error_msg
|
||||||
|
|
||||||
|
except requests.exceptions.ConnectionError as e:
|
||||||
|
error_msg = f"无法连接到验证服务器: {str(e)}"
|
||||||
|
print(error_msg)
|
||||||
|
return False, "无法连接到验证服务器,请检查网络连接"
|
||||||
|
except requests.exceptions.Timeout:
|
||||||
|
error_msg = "服务器响应超时"
|
||||||
|
print(error_msg)
|
||||||
|
return False, error_msg
|
||||||
|
except Exception as e:
|
||||||
|
error_msg = f"验证请求失败: {str(e)}"
|
||||||
|
print(error_msg)
|
||||||
|
return False, error_msg
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return False, f"验证过程出错: {str(e)}"
|
error_msg = f"验证过程出错: {str(e)}"
|
||||||
|
print(error_msg)
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
print(traceback.format_exc())
|
||||||
|
return False, error_msg
|
||||||
|
|
||||||
def _save_license(self, license_data):
|
def _save_license(self, license_data):
|
||||||
"""加密保存许可证数据"""
|
"""加密保存许可证数据"""
|
||||||
|
|||||||
10
server/.dockerignore
Normal file
10
server/.dockerignore
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
node_modules
|
||||||
|
npm-debug.log
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
.git
|
||||||
|
.gitignore
|
||||||
|
README.md
|
||||||
|
.dockerignore
|
||||||
|
Dockerfile
|
||||||
|
docker-compose.yml
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
MONGODB_URI=mongodb://49.234.181.38:27017/license-manager
|
MONGODB_URI=mongodb://mongo_3y6JyM:mongo_iNySJ5@119.8.35.41:27017/license-manager?authSource=admin&retryWrites=true&w=majority
|
||||||
PORT=3000
|
PORT=3000
|
||||||
ENCRYPTION_KEY=Kj8nP9x2Qs5mY7vR4wL1hC3fA6tD0iB8
|
ENCRYPTION_KEY=f1e2d3c4b5a6978899aabbccddeeff00112233445566778899aabbccddeeff00
|
||||||
|
|||||||
20
server/Dockerfile
Normal file
20
server/Dockerfile
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# 使用 Node.js 18 作为基础镜像
|
||||||
|
FROM node:18-alpine
|
||||||
|
|
||||||
|
# 设置工作目录
|
||||||
|
WORKDIR /usr/src/app
|
||||||
|
|
||||||
|
# 复制 package.json 和 package-lock.json(如果存在)
|
||||||
|
COPY package*.json ./
|
||||||
|
|
||||||
|
# 安装依赖
|
||||||
|
RUN npm install
|
||||||
|
|
||||||
|
# 复制源代码
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
# 暴露端口
|
||||||
|
EXPOSE 3000
|
||||||
|
|
||||||
|
# 启动命令
|
||||||
|
CMD ["node", "server.js"]
|
||||||
33
server/docker-compose.yml
Normal file
33
server/docker-compose.yml
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
version: '3.8'
|
||||||
|
|
||||||
|
services:
|
||||||
|
# Node.js 应用服务
|
||||||
|
app:
|
||||||
|
build: .
|
||||||
|
ports:
|
||||||
|
- "3000:3000"
|
||||||
|
environment:
|
||||||
|
- MONGODB_URI=${MONGODB_URI}
|
||||||
|
- PORT=3000
|
||||||
|
- ENCRYPTION_KEY=f1e2d3c4b5a6978899aabbccddeeff00112233445566778899aabbccddeeff00
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
|
depends_on:
|
||||||
|
- mongo
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
|
# MongoDB 服务
|
||||||
|
mongo:
|
||||||
|
image: mongo:latest
|
||||||
|
ports:
|
||||||
|
- "27017:27017"
|
||||||
|
environment:
|
||||||
|
- MONGO_INITDB_ROOT_USERNAME=mongo_3y6JyM
|
||||||
|
- MONGO_INITDB_ROOT_PASSWORD=mongo_iNySJ5
|
||||||
|
- MONGO_INITDB_DATABASE=license-manager
|
||||||
|
volumes:
|
||||||
|
- mongodb_data:/data/db
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
mongodb_data:
|
||||||
19
server/models/LicenseKey.js
Normal file
19
server/models/LicenseKey.js
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
const mongoose = require('mongoose');
|
||||||
|
|
||||||
|
const licenseKeySchema = new mongoose.Schema({
|
||||||
|
licenseKey: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
unique: true
|
||||||
|
},
|
||||||
|
isUsed: {
|
||||||
|
type: Boolean,
|
||||||
|
default: false
|
||||||
|
},
|
||||||
|
generatedAt: {
|
||||||
|
type: Date,
|
||||||
|
default: Date.now
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = mongoose.model('LicenseKey', licenseKeySchema);
|
||||||
1484
server/package-lock.json
generated
Normal file
1484
server/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -17,5 +17,6 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"nodemon": "^3.0.2"
|
"nodemon": "^3.0.2"
|
||||||
}
|
},
|
||||||
}
|
"packageManager": "npm@10.8.3+sha512.d08425c8062f56d43bb8e84315864218af2492eb769e1f1ca40740f44e85bd148969382d651660363942e5909cb7ffcbef7ca0ae963ddc2c57a51243b4da8f56"
|
||||||
|
}
|
||||||
|
|||||||
122
server/server.js
122
server/server.js
@@ -5,6 +5,7 @@ const cors = require('cors');
|
|||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const moment = require('moment');
|
const moment = require('moment');
|
||||||
const License = require('./models/License');
|
const License = require('./models/License');
|
||||||
|
const LicenseKey = require('./models/LicenseKey');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
@@ -13,16 +14,31 @@ app.use(cors());
|
|||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
// Encryption functions
|
// Encryption functions
|
||||||
|
function getIV() {
|
||||||
|
// 如果需要固定 IV(不推荐),可以从环境变量获取
|
||||||
|
if (process.env.ENCRYPTION_IV) {
|
||||||
|
return Buffer.from(process.env.ENCRYPTION_IV, 'hex');
|
||||||
|
}
|
||||||
|
// 否则生成随机 IV(更安全,但需要存储)
|
||||||
|
return crypto.randomBytes(16);
|
||||||
|
}
|
||||||
|
|
||||||
function encryptLicenseKey(text) {
|
function encryptLicenseKey(text) {
|
||||||
const cipher = crypto.createCipher('aes-256-cbc', process.env.ENCRYPTION_KEY);
|
const key = Buffer.from(process.env.ENCRYPTION_KEY, 'hex');
|
||||||
|
const iv = getIV();
|
||||||
|
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
|
||||||
let encrypted = cipher.update(text, 'utf8', 'hex');
|
let encrypted = cipher.update(text, 'utf8', 'hex');
|
||||||
encrypted += cipher.final('hex');
|
encrypted += cipher.final('hex');
|
||||||
return encrypted;
|
// 将 IV 附加到加密文本中,以便解密时使用
|
||||||
|
return iv.toString('hex') + ':' + encrypted;
|
||||||
}
|
}
|
||||||
|
|
||||||
function decryptLicenseKey(encrypted) {
|
function decryptLicenseKey(encrypted) {
|
||||||
const decipher = crypto.createDecipher('aes-256-cbc', process.env.ENCRYPTION_KEY);
|
const [ivHex, encryptedText] = encrypted.split(':');
|
||||||
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
|
const key = Buffer.from(process.env.ENCRYPTION_KEY, 'hex');
|
||||||
|
const iv = Buffer.from(ivHex, 'hex');
|
||||||
|
const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
|
||||||
|
let decrypted = decipher.update(encryptedText, 'hex', 'utf8');
|
||||||
decrypted += decipher.final('utf8');
|
decrypted += decipher.final('utf8');
|
||||||
return decrypted;
|
return decrypted;
|
||||||
}
|
}
|
||||||
@@ -31,18 +47,51 @@ function generateLicenseKey() {
|
|||||||
const randomBytes = crypto.randomBytes(16);
|
const randomBytes = crypto.randomBytes(16);
|
||||||
const timestamp = Date.now().toString();
|
const timestamp = Date.now().toString();
|
||||||
const combined = randomBytes.toString('hex') + timestamp;
|
const combined = randomBytes.toString('hex') + timestamp;
|
||||||
return encryptLicenseKey(combined).substring(0, 32); // 生成32位的许可证密钥
|
const encrypted = encryptLicenseKey(combined);
|
||||||
|
// 由于加密后的字符串现在包含 IV,我们需要使用完整的字符串
|
||||||
|
return encrypted;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect to MongoDB
|
// Connect to MongoDB
|
||||||
mongoose.connect(process.env.MONGODB_URI)
|
mongoose.connect(process.env.MONGODB_URI, {
|
||||||
.then(() => console.log('Connected to MongoDB'))
|
useNewUrlParser: true,
|
||||||
.catch(err => console.error('MongoDB connection error:', err));
|
useUnifiedTopology: true,
|
||||||
|
serverSelectionTimeoutMS: 5000, // 超时时间
|
||||||
|
socketTimeoutMS: 45000, // Socket 超时
|
||||||
|
family: 4, // 强制使用 IPv4
|
||||||
|
})
|
||||||
|
.then(() => console.log('Connected to MongoDB'))
|
||||||
|
.catch(err => {
|
||||||
|
console.error('MongoDB connection error:', err);
|
||||||
|
process.exit(1); // 如果连接失败,终止程序
|
||||||
|
});
|
||||||
|
|
||||||
|
// 添加连接错误处理
|
||||||
|
mongoose.connection.on('error', err => {
|
||||||
|
console.error('MongoDB connection error:', err);
|
||||||
|
});
|
||||||
|
|
||||||
|
mongoose.connection.on('disconnected', () => {
|
||||||
|
console.log('MongoDB disconnected');
|
||||||
|
});
|
||||||
|
|
||||||
|
// 定义一个复杂的路径,可以放在环境变量中
|
||||||
|
const GENERATE_PATH = process.env.GENERATE_PATH || crypto.randomBytes(16).toString('hex');
|
||||||
|
|
||||||
|
// 在应用启动时输出生成路径(仅在控制台显示一次)
|
||||||
|
console.log('License generation path:', GENERATE_PATH);
|
||||||
|
|
||||||
// Generate license key endpoint
|
// Generate license key endpoint
|
||||||
app.post('/generate', async (req, res) => {
|
app.post(`/${GENERATE_PATH}`, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const licenseKey = generateLicenseKey();
|
const licenseKey = generateLicenseKey();
|
||||||
|
|
||||||
|
// 保存生成的许可证记录
|
||||||
|
await LicenseKey.create({
|
||||||
|
licenseKey: licenseKey,
|
||||||
|
isUsed: false
|
||||||
|
});
|
||||||
|
|
||||||
return res.json({
|
return res.json({
|
||||||
success: true,
|
success: true,
|
||||||
license_key: licenseKey
|
license_key: licenseKey
|
||||||
@@ -79,38 +128,39 @@ app.post('/activate', async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if license already exists
|
// 检查许可证是否存在于生成记录中
|
||||||
|
const licenseKeyRecord = await LicenseKey.findOne({ licenseKey: license_key });
|
||||||
|
if (!licenseKeyRecord) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
message: '无效的许可证密钥'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查许可证是否已被使用
|
||||||
|
if (licenseKeyRecord.isUsed) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
message: '此许可证密钥已被使用,不能重复激活'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查许可证激活状态
|
||||||
const existingLicense = await License.findOne({ licenseKey: license_key });
|
const existingLicense = await License.findOne({ licenseKey: license_key });
|
||||||
if (existingLicense) {
|
if (existingLicense) {
|
||||||
if (existingLicense.machineCode !== machine_code) {
|
return res.status(400).json({
|
||||||
return res.status(400).json({
|
success: false,
|
||||||
success: false,
|
message: '此许可证已被激活,不能重复使用'
|
||||||
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 expiryDate = moment().add(1, 'month').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);
|
licenseKeyRecord.isUsed = true;
|
||||||
} else {
|
await licenseKeyRecord.save();
|
||||||
await License.create(licenseData);
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.json({
|
return res.json({
|
||||||
success: true,
|
success: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user