| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- #!/usr/bin/env node
- /**
- * Socket服务器启动脚本
- * 自动检测SSL证书并选择合适的启动模式
- */
- const fs = require('fs');
- const path = require('path');
- // 检查配置文件
- const configPath = path.join(__dirname, 'config.js');
- if (!fs.existsSync(configPath)) {
- console.error('错误: 配置文件 config.js 不存在');
- process.exit(1);
- }
- const config = require('./config.js');
- // 检查必要的配置项
- const requiredConfigs = ['socket_port', 'REDISHOST', 'REDISPORT', 'REDISPASS'];
- for (const key of requiredConfigs) {
- if (!config[key]) {
- console.error(`错误: 配置项 ${key} 未设置`);
- process.exit(1);
- }
- }
- console.log('配置检查通过');
- console.log('Redis配置:', config.REDISHOST + ':' + config.REDISPORT);
- console.log('Socket端口:', config.socket_port);
- // SSL证书路径配置
- const sslPaths = [
- // 默认路径
- {
- key: '/www/server/panel/vhost/cert/yjzb.yunchao2u.com/privkey.key',
- cert: '/www/server/panel/vhost/cert/yjzb.yunchao2u.com/fullchain.pem'
- },
- // 备用路径1
- {
- key: './ssl/privkey.key',
- cert: './ssl/fullchain.pem'
- },
- // 备用路径2
- {
- key: './cert/privkey.key',
- cert: './cert/fullchain.pem'
- }
- ];
- // 检查SSL证书
- let sslConfig = null;
- for (const paths of sslPaths) {
- if (fs.existsSync(paths.key) && fs.existsSync(paths.cert)) {
- try {
- // 尝试读取证书文件
- const keyContent = fs.readFileSync(paths.key);
- const certContent = fs.readFileSync(paths.cert);
-
- sslConfig = {
- key: keyContent,
- cert: certContent
- };
-
- console.log('找到SSL证书:', paths.key);
- break;
- } catch (error) {
- console.log('SSL证书读取失败:', paths.key, error.message);
- }
- }
- }
- // 启动服务器
- if (sslConfig) {
- console.log('使用HTTPS模式启动服务器...');
- process.env.SSL_MODE = 'true';
- } else {
- console.log('未找到有效SSL证书,使用HTTP模式启动服务器...');
- process.env.SSL_MODE = 'false';
- }
- // 启动主服务器文件
- require('./s1.js');
|