start_server.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env node
  2. /**
  3. * Socket服务器启动脚本
  4. * 自动检测SSL证书并选择合适的启动模式
  5. */
  6. const fs = require('fs');
  7. const path = require('path');
  8. // 检查配置文件
  9. const configPath = path.join(__dirname, 'config.js');
  10. if (!fs.existsSync(configPath)) {
  11. console.error('错误: 配置文件 config.js 不存在');
  12. process.exit(1);
  13. }
  14. const config = require('./config.js');
  15. // 检查必要的配置项
  16. const requiredConfigs = ['socket_port', 'REDISHOST', 'REDISPORT', 'REDISPASS'];
  17. for (const key of requiredConfigs) {
  18. if (!config[key]) {
  19. console.error(`错误: 配置项 ${key} 未设置`);
  20. process.exit(1);
  21. }
  22. }
  23. console.log('配置检查通过');
  24. console.log('Redis配置:', config.REDISHOST + ':' + config.REDISPORT);
  25. console.log('Socket端口:', config.socket_port);
  26. // SSL证书路径配置
  27. const sslPaths = [
  28. // 默认路径
  29. {
  30. key: '/www/server/panel/vhost/cert/yjzb.yunchao2u.com/privkey.key',
  31. cert: '/www/server/panel/vhost/cert/yjzb.yunchao2u.com/fullchain.pem'
  32. },
  33. // 备用路径1
  34. {
  35. key: './ssl/privkey.key',
  36. cert: './ssl/fullchain.pem'
  37. },
  38. // 备用路径2
  39. {
  40. key: './cert/privkey.key',
  41. cert: './cert/fullchain.pem'
  42. }
  43. ];
  44. // 检查SSL证书
  45. let sslConfig = null;
  46. for (const paths of sslPaths) {
  47. if (fs.existsSync(paths.key) && fs.existsSync(paths.cert)) {
  48. try {
  49. // 尝试读取证书文件
  50. const keyContent = fs.readFileSync(paths.key);
  51. const certContent = fs.readFileSync(paths.cert);
  52. sslConfig = {
  53. key: keyContent,
  54. cert: certContent
  55. };
  56. console.log('找到SSL证书:', paths.key);
  57. break;
  58. } catch (error) {
  59. console.log('SSL证书读取失败:', paths.key, error.message);
  60. }
  61. }
  62. }
  63. // 启动服务器
  64. if (sslConfig) {
  65. console.log('使用HTTPS模式启动服务器...');
  66. process.env.SSL_MODE = 'true';
  67. } else {
  68. console.log('未找到有效SSL证书,使用HTTP模式启动服务器...');
  69. process.env.SSL_MODE = 'false';
  70. }
  71. // 启动主服务器文件
  72. require('./s1.js');