server.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. require('dotenv').config();
  2. const express = require('express');
  3. const mongoose = require('mongoose');
  4. const cors = require('cors');
  5. const crypto = require('crypto');
  6. const moment = require('moment');
  7. const License = require('./models/License');
  8. const app = express();
  9. // Middleware
  10. app.use(cors());
  11. app.use(express.json());
  12. // Encryption functions
  13. function encryptLicenseKey(text) {
  14. const cipher = crypto.createCipher('aes-256-cbc', process.env.ENCRYPTION_KEY);
  15. let encrypted = cipher.update(text, 'utf8', 'hex');
  16. encrypted += cipher.final('hex');
  17. return encrypted;
  18. }
  19. function decryptLicenseKey(encrypted) {
  20. const decipher = crypto.createDecipher('aes-256-cbc', process.env.ENCRYPTION_KEY);
  21. let decrypted = decipher.update(encrypted, 'hex', 'utf8');
  22. decrypted += decipher.final('utf8');
  23. return decrypted;
  24. }
  25. function generateLicenseKey() {
  26. const randomBytes = crypto.randomBytes(16);
  27. const timestamp = Date.now().toString();
  28. const combined = randomBytes.toString('hex') + timestamp;
  29. return encryptLicenseKey(combined).substring(0, 32); // 生成32位的许可证密钥
  30. }
  31. // Connect to MongoDB
  32. mongoose.connect(process.env.MONGODB_URI)
  33. .then(() => console.log('Connected to MongoDB'))
  34. .catch(err => console.error('MongoDB connection error:', err));
  35. // Generate license key endpoint
  36. app.post('/generate', async (req, res) => {
  37. try {
  38. const licenseKey = generateLicenseKey();
  39. return res.json({
  40. success: true,
  41. license_key: licenseKey
  42. });
  43. } catch (error) {
  44. console.error('生成许可证错误:', error);
  45. return res.status(500).json({
  46. success: false,
  47. message: '服务器错误'
  48. });
  49. }
  50. });
  51. // Activation endpoint
  52. app.post('/activate', async (req, res) => {
  53. try {
  54. const { license_key, machine_code, activation_date } = req.body;
  55. // Validate input
  56. if (!license_key || !machine_code) {
  57. return res.status(400).json({
  58. success: false,
  59. message: '许可证密钥和机器码是必需的'
  60. });
  61. }
  62. // Validate license key format
  63. try {
  64. decryptLicenseKey(license_key);
  65. } catch (error) {
  66. return res.status(400).json({
  67. success: false,
  68. message: '无效的许可证密钥'
  69. });
  70. }
  71. // Check if license already exists
  72. const existingLicense = await License.findOne({ licenseKey: license_key });
  73. if (existingLicense) {
  74. if (existingLicense.machineCode !== machine_code) {
  75. return res.status(400).json({
  76. success: false,
  77. message: '许可证已在其他设备上激活'
  78. });
  79. }
  80. if (!existingLicense.isActive) {
  81. return res.status(400).json({
  82. success: false,
  83. message: '许可证已被禁用'
  84. });
  85. }
  86. }
  87. // Create new license or update existing one
  88. const expiryDate = moment().add(1, 'year').toDate(); // 设置一年有效期
  89. const licenseData = {
  90. licenseKey: license_key,
  91. machineCode: machine_code,
  92. activationDate: activation_date || new Date(),
  93. expiryDate: expiryDate,
  94. isActive: true
  95. };
  96. if (existingLicense) {
  97. await License.updateOne({ licenseKey: license_key }, licenseData);
  98. } else {
  99. await License.create(licenseData);
  100. }
  101. return res.json({
  102. success: true,
  103. message: '激活成功',
  104. expiry_date: expiryDate.toISOString().split('T')[0]
  105. });
  106. } catch (error) {
  107. console.error('激活错误:', error);
  108. return res.status(500).json({
  109. success: false,
  110. message: '服务器错误'
  111. });
  112. }
  113. });
  114. // Verification endpoint
  115. app.post('/verify', async (req, res) => {
  116. try {
  117. const { license_key, machine_code } = req.body;
  118. // Validate input
  119. if (!license_key || !machine_code) {
  120. return res.status(400).json({
  121. success: false,
  122. message: '许可证密钥和机器码是必需的'
  123. });
  124. }
  125. // Validate license key format
  126. try {
  127. decryptLicenseKey(license_key);
  128. } catch (error) {
  129. return res.status(400).json({
  130. success: false,
  131. message: '无效的许可证密钥'
  132. });
  133. }
  134. // Find license
  135. const license = await License.findOne({ licenseKey: license_key });
  136. if (!license) {
  137. return res.status(404).json({
  138. success: false,
  139. message: '许可证不存在'
  140. });
  141. }
  142. // Check machine code
  143. if (license.machineCode !== machine_code) {
  144. return res.status(400).json({
  145. success: false,
  146. message: '硬件信息不匹配'
  147. });
  148. }
  149. // Check if license is active
  150. if (!license.isActive) {
  151. return res.status(400).json({
  152. success: false,
  153. message: '许可证已被禁用'
  154. });
  155. }
  156. // Check expiry
  157. if (moment().isAfter(license.expiryDate)) {
  158. return res.status(400).json({
  159. success: false,
  160. message: '许可证已过期'
  161. });
  162. }
  163. return res.json({
  164. success: true,
  165. message: '许可证有效',
  166. expiry_date: license.expiryDate.toISOString().split('T')[0]
  167. });
  168. } catch (error) {
  169. console.error('验证错误:', error);
  170. return res.status(500).json({
  171. success: false,
  172. message: '服务器错误'
  173. });
  174. }
  175. });
  176. const PORT = process.env.PORT || 3000;
  177. app.listen(PORT, () => {
  178. console.log(`Server is running on port ${PORT}`);
  179. });