feat: 允许客户端在HTTPS下使用非安全ws协议的服务端

This commit is contained in:
王立帮
2025-05-12 13:32:51 +08:00
parent 5e44e77814
commit 5bd5865892
4 changed files with 49 additions and 29 deletions

View File

@@ -21,8 +21,16 @@
"cli": "python3" "cli": "python3"
} }
}, },
"tempPath": "./temp", "client": {
"clientPath": "./mixly", "path": "./mixly",
"mode": "web-socket", "port": 4000
"port": 4000 },
"server": {
"path": {
"temp": "./temp"
},
"mode": "all",
"protocol": "wss",
"port": 4000
}
} }

View File

@@ -5,11 +5,14 @@ import fsExtra from 'fs-extra';
function processConfig(data) { function processConfig(data) {
for (let i in data.path) { for (let i in data.path) {
if (data.path[i] instanceof String) { if (!data.path[i]) {
continue;
}
if (typeof data.path[i] === 'string') {
data.path[i] = path.resolve(process.cwd(), data.path[i]); data.path[i] = path.resolve(process.cwd(), data.path[i]);
} else if (data.path[i] instanceof Array) { } else if (typeof data.path[i] === 'object') {
for (let j in data.path[i]) { for (let j in data.path[i]) {
if (!(data.path[i][j] instanceof String)) { if (typeof data.path[i][j] !== 'string') {
continue; continue;
} }
data.path[i][j] = path.resolve(process.cwd(), data.path[i][j]); data.path[i][j] = path.resolve(process.cwd(), data.path[i][j]);
@@ -25,8 +28,10 @@ export const ARDUINO = processConfig(CONFIG.arduino);
export const MICROPYTHON = processConfig(CONFIG.micropython); export const MICROPYTHON = processConfig(CONFIG.micropython);
export const PYTHON = processConfig(CONFIG.python); export const PYTHON = processConfig(CONFIG.python);
export const CURRENT_PLANTFORM = os.platform(); export const CURRENT_PLANTFORM = os.platform();
export const TEMP_PATH = path.resolve(process.cwd(), CONFIG.tempPath); export const TEMP_PATH = path.resolve(process.cwd(), CONFIG.server.path.temp);
export const CLIENT_PATH = path.resolve(process.cwd(), CONFIG.clientPath); export const CLIENT_PATH = path.resolve(process.cwd(), CONFIG.client.path);
export const CERTS_PATH = path.resolve(process.cwd(), 'certs'); export const CERTS_PATH = path.resolve(process.cwd(), 'certs');
export const PORT = CONFIG.port; export const CLIENT_PORT = CONFIG.client.port;
export const MODE = CONFIG.mode; export const SERVER_PORT = CONFIG.server.protocol === 'wss' ? CLIENT_PORT : CONFIG.server.port;
export const SERVER_MODE = CONFIG.server.mode;
export const SERVER_PROTOCOL = CONFIG.server.protocol;

View File

@@ -23,8 +23,8 @@ export default class Socket {
#shellArduino_ = new Registry(); #shellArduino_ = new Registry();
#shellAmpy_ = new Registry(); #shellAmpy_ = new Registry();
constructor(httpsServer, options) { constructor(server, options) {
this.#io_ = new Server(httpsServer, options); this.#io_ = new Server(server, options);
this.#namespaceAll_ = this.#io_.of('/all'); this.#namespaceAll_ = this.#io_.of('/all');
this.#namespaceCompile_ = this.#io_.of('/compile'); this.#namespaceCompile_ = this.#io_.of('/compile');
this.#namespaceCompile_.on('connection', (socket) => { this.#namespaceCompile_.on('connection', (socket) => {

View File

@@ -1,10 +1,11 @@
import { createServer } from 'node:https'; import https from 'node:https';
import http from 'node:http';
import path from 'node:path'; import path from 'node:path';
import fsExtra from 'fs-extra'; import fsExtra from 'fs-extra';
import express from 'express'; import express from 'express';
import Socket from './common/socket.js'; import Socket from './common/socket.js';
import { getCertificate } from './common/utils.js'; import { getCertificate } from './common/utils.js';
import { CLIENT_PATH, PORT, MODE } from './common/config.js'; import { CLIENT_PATH, CLIENT_PORT, SERVER_PORT, SERVER_MODE, SERVER_PROTOCOL } from './common/config.js';
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
@@ -12,17 +13,20 @@ const app = express();
if (CLIENT_PATH) { if (CLIENT_PATH) {
app.use(express.static(CLIENT_PATH)); app.use(express.static(CLIENT_PATH));
} }
const httpsServer = createServer(getCertificate(), app); const httpsServer = https.createServer(getCertificate(), app);
let httpServer = null;
if (MODE !== 'static') { if (CLIENT_PORT !== SERVER_PORT) {
const socket = new Socket(httpsServer, { httpServer = http.createServer()
}
if (SERVER_MODE !== 'static') {
const socket = new Socket(httpServer ? httpServer : httpsServer, {
path: '/mixly-socket/', path: '/mixly-socket/',
maxHttpBufferSize: 1e8, maxHttpBufferSize: 1e8,
cors: { cors: {
origin: '*', origin: '*',
methods: ['GET', 'POST'], methods: ['GET', 'POST'],
transports: ['websocket', 'polling', 'flashsocket'], transports: ['websocket', 'polling', 'flashsocket'],
credentials: true credentials: false
} }
}); });
@@ -46,13 +50,13 @@ if (MODE !== 'static') {
const mixlyConfigPath = path.resolve(CLIENT_PATH, 'sw-config.json'); const mixlyConfigPath = path.resolve(CLIENT_PATH, 'sw-config.json');
const mixlyConfig = fsExtra.readJSONSync(mixlyConfigPath); const mixlyConfig = fsExtra.readJSONSync(mixlyConfigPath);
if (MODE === 'web-compiler') { if (SERVER_MODE === 'compiler') {
mixlyConfig['webCompiler']['enabled'] = true; mixlyConfig['webCompiler']['enabled'] = true;
mixlyConfig['webCompiler']['url'] = `wss://127.0.0.1:${PORT}`; mixlyConfig['webCompiler']['url'] = `${SERVER_PROTOCOL}://default:${SERVER_PORT}`;
mixlyConfig['webSocket']['enabled'] = false; mixlyConfig['webSocket']['enabled'] = false;
} else if (MODE === 'web-socket') { } else if (SERVER_MODE === 'all') {
mixlyConfig['webCompiler']['enabled'] = false; mixlyConfig['webCompiler']['enabled'] = false;
mixlyConfig['webSocket']['url'] = `wss://127.0.0.1:${PORT}`; mixlyConfig['webSocket']['url'] = `${SERVER_PROTOCOL}://default:${SERVER_PORT}`;
mixlyConfig['webSocket']['enabled'] = true; mixlyConfig['webSocket']['enabled'] = true;
} else { } else {
mixlyConfig['webCompiler']['enabled'] = false; mixlyConfig['webCompiler']['enabled'] = false;
@@ -63,10 +67,13 @@ fsExtra.writeJSONSync(mixlyConfigPath, mixlyConfig, {
spaces: ' ' spaces: ' '
}); });
httpsServer.listen(PORT); httpsServer.listen(CLIENT_PORT);
if (httpServer) {
httpServer.listen(SERVER_PORT);
}
if (CLIENT_PATH) { if (CLIENT_PATH) {
console.log(`Static服务器正在运行: https://127.0.0.1:${PORT}`); console.log(`Static服务器正在运行: https://127.0.0.1:${CLIENT_PORT}`);
} }
if (MODE !== 'static') { if (SERVER_MODE !== 'static') {
console.log(`Socket.io服务器正在运行: wss://127.0.0.1:${PORT}/mixly-socket`); console.log(`Socket.io服务器正在运行: ${SERVER_PROTOCOL}://127.0.0.1:${SERVER_PORT}`);
} }