123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- const WebSocket = require('ws');
- const https = require('https');
- const fs = require('fs');
- const path = require('path');
- require('dotenv').config();
- console.log('🚀 SERVER-FUNCTION-HUB STARTING UP...');
- console.log('📁 Current working directory:', process.cwd());
- console.log('🔧 Node version:', process.version);
- console.log('🔐 Attempting to import security helpers...');
- try {
- const { processAllEncryptedData } = require('./security-helpers');
- console.log('✅ Security helpers imported successfully');
- console.log('🔍 processAllEncryptedData function:', typeof processAllEncryptedData);
- } catch (error) {
- console.log('❌ FAILED to import security helpers:', error.message);
- console.log('📍 Error stack:', error.stack);
- process.exit(1);
- }
- const WS_PORT = process.env.WS_PORT || 8080;
- const SSL_PREFIX = process.env.SSL_PREFIX || '/etc/letsencrypt/live/';
- const DOMAIN = process.env.DOMAIN || 'tuvme.xyz';
- console.log('⚙️ Configuration loaded:');
- console.log(' WS_PORT:', WS_PORT);
- console.log(' DOMAIN:', DOMAIN);
- console.log(' SSL_PREFIX:', SSL_PREFIX);
- const SSL_KEY_PATH = process.env.SSL_KEY_PATH || path.join(SSL_PREFIX, DOMAIN, 'privkey.pem');
- const SSL_CERT_PATH = process.env.SSL_CERT_PATH || path.join(SSL_PREFIX, DOMAIN, 'fullchain.pem');
- console.log('🔑 SSL Certificate paths:');
- console.log(' KEY:', SSL_KEY_PATH);
- console.log(' CERT:', SSL_CERT_PATH);
- console.log('🔍 Checking SSL files...');
- try {
- fs.accessSync(SSL_KEY_PATH);
- console.log('✅ SSL Key file exists');
- } catch (error) {
- console.log('❌ SSL Key file missing:', SSL_KEY_PATH);
- }
- try {
- fs.accessSync(SSL_CERT_PATH);
- console.log('✅ SSL Cert file exists');
- } catch (error) {
- console.log('❌ SSL Cert file missing:', SSL_CERT_PATH);
- }
- function log(label, data = null) {
- const timestamp = new Date().toISOString();
- console.log(`[${timestamp}] [SERVER-FUNCTION-HUB] ${label}`);
- if (data) {
- console.log(JSON.stringify(data, null, 2));
- }
- }
- const functionStore = new Map();
- console.log('📦 Function store initialized');
- const stats = {
- totalConnections: 0,
- activeConnections: 0,
- registrations: 0,
- executions: 0,
- errors: 0
- };
- console.log('📊 Stats initialized:', stats);
- function registerFunction(functionId, functionBody, paramNames = []) {
- console.log('🔧 REGISTERING FUNCTION:', functionId);
- console.log('📝 Function body length:', functionBody.length);
- console.log('📋 Parameter names:', paramNames);
-
- stats.registrations++;
-
-
- log(`======= REGISTERING FUNCTION: ${functionId} =======`);
- console.log("FUNCTION BODY BEGIN:");
- console.log(functionBody);
- console.log("FUNCTION BODY END");
- log(`========================================`);
-
- functionStore.set(functionId, {
- body: functionBody,
- params: paramNames
- });
- console.log('✅ Function stored in functionStore');
- console.log('📊 Total functions in store:', functionStore.size);
- console.log('🔍 Function store keys:', Array.from(functionStore.keys()));
- log(`Code successfully registered: ${functionId}`);
- return true;
- }
- function isPromiseLike(value) {
- const result = value && typeof value.then === 'function';
- console.log('🔍 isPromiseLike check:', typeof value, '→', result);
- return result;
- }
- async function executeFunction(functionId, args = []) {
- console.log('🚀 EXECUTE FUNCTION CALLED');
- console.log('🔍 Function ID:', functionId);
- console.log('📊 Arguments count:', args.length);
- console.log('📋 Arguments:', args);
-
- stats.executions++;
-
- log(`======= EXECUTING FUNCTION: ${functionId} =======`);
- log(`Raw Arguments:`, args);
- console.log('🔍 Checking if function exists in store...');
- console.log('📦 Current function store size:', functionStore.size);
- console.log('🔑 Available function IDs:', Array.from(functionStore.keys()));
- console.log('🎯 Looking for:', functionId);
- console.log('✅ Function exists?', functionStore.has(functionId));
- if (!functionStore.has(functionId)) {
- console.log('❌ FUNCTION NOT FOUND!');
- log(`Code not found: ${functionId}`);
- const error = new Error(`Code not found: ${functionId}`);
- console.log('💥 Error object created:', error.message);
- throw error;
- }
- console.log('✅ Function found in store, proceeding with execution...');
- try {
- const funcInfo = functionStore.get(functionId);
- console.log('📝 Retrieved function info:', {
- bodyLength: funcInfo.body.length,
- params: funcInfo.params
- });
-
- console.log('🔐 Starting encryption processing...');
-
-
- const { processAllEncryptedData } = require('./security-helpers');
- console.log('🔍 processAllEncryptedData function type:', typeof processAllEncryptedData);
-
- console.log('🔄 Processing encrypted data...');
- const { processedBody, processedArgs } = processAllEncryptedData(funcInfo.body, args);
- console.log('✅ Encryption processing complete');
-
-
- console.log('🔐 PROCESSED ARGS COMPARISON:');
- console.log(' Original args:', args);
- console.log(' Processed args:', processedArgs);
- log(`PROCESSED ARGS:`, processedArgs);
-
- log(`EXECUTING THIS EXACT CODE:`);
- console.log(processedBody);
- log(`========================================`);
- console.log('🏗️ Creating execution context...');
-
- const executeCode = (code, processedArgs) => {
- console.log('🔧 executeCode function called');
- console.log('📝 Code length:', code.length);
- console.log('📋 Args:', processedArgs);
-
-
- const vm = require('vm');
- console.log('📦 VM module loaded');
-
- const sandbox = {
- require,
- process,
- console,
- Buffer,
- setTimeout,
- clearTimeout,
- setInterval,
- clearInterval,
- setImmediate,
- clearImmediate,
- args: processedArgs,
- Promise
- };
- console.log('🏗️ Sandbox created with keys:', Object.keys(sandbox));
- console.log('📋 Sandbox args:', sandbox.args);
-
-
- const context = vm.createContext(sandbox);
- console.log('🌍 VM context created');
-
-
-
-
- let codeToRun = code;
-
- console.log('🔍 Analyzing code structure...');
-
-
- const isAsyncIIFE = /^\s*\(?\s*async\s+function/.test(code) ||
- code.includes('new Promise');
-
- console.log('🔍 Code analysis results:');
- console.log(' isAsyncIIFE:', isAsyncIIFE);
- console.log(' contains async function:', /^\s*\(?\s*async\s+function/.test(code));
- console.log(' contains Promise:', code.includes('new Promise'));
-
-
- if (!isAsyncIIFE) {
- console.log('🔧 Code is not an async IIFE, wrapping in function...');
- log(`Code is not an async IIFE, wrapping in function...`);
- codeToRun = `(function() { ${code} })()`;
- } else {
- console.log('✅ Code appears to be async/Promise-based, executing as-is');
- log(`Code appears to be async/Promise-based, executing as-is`);
- }
-
- log(`EXECUTING FINAL CODE:`);
- console.log('🚀 FINAL CODE TO EXECUTE:');
- console.log(codeToRun);
-
- console.log('⚡ Running code in VM context...');
-
-
- const result = vm.runInContext(codeToRun, context);
-
- console.log('✅ Code execution completed');
- console.log('🔍 Raw result type:', typeof result);
- console.log('📊 Raw result:', result);
-
- log(`Raw execution result type: ${typeof result}`);
-
- return result;
- };
- console.log('🎯 Calling executeCode...');
-
-
- let result = executeCode(processedBody, processedArgs);
-
- console.log('📊 Execution result received:', typeof result);
-
-
- log(`EXECUTION RESULT TYPE: ${typeof result}`);
-
-
- if (isPromiseLike(result)) {
- console.log('⏳ Result is Promise-like, awaiting...');
- log(`DETECTED PROMISE RESULT - AWAITING...`);
- try {
- console.log('⏳ Starting Promise resolution...');
- result = await result;
- console.log('✅ Promise resolved successfully');
- console.log('📊 Final resolved result:', result);
- log(`PROMISE RESOLVED TO:`, result);
- } catch (promiseError) {
- console.log('💥 Promise rejected with error:', promiseError);
- log(`PROMISE REJECTION:`, {
- error: promiseError.message,
- stack: promiseError.stack
- });
- throw promiseError;
- }
- } else {
- console.log('✅ Result is not a Promise, using directly');
- log(`EXECUTION RESULT (NOT A PROMISE):`, result);
- }
-
- console.log('🎉 Function execution completed successfully');
- console.log('📊 Final result:', result);
-
- return result;
-
- } catch (error) {
- console.log('💥 ERROR in executeFunction:', error);
- console.log('📍 Error message:', error.message);
- console.log('📍 Error stack:', error.stack);
-
- stats.errors++;
- log(`ERROR EXECUTING CODE: ${functionId}`, {
- errorType: error.constructor.name,
- errorMessage: error.message,
- errorStack: error.stack
- });
- throw error;
- }
- }
- function startServer() {
- console.log('🌐 Starting WebSocket server...');
-
- console.log('🔑 Creating HTTPS server...');
- const server = https.createServer({
- key: fs.readFileSync(SSL_KEY_PATH),
- cert: fs.readFileSync(SSL_CERT_PATH)
- });
- console.log('✅ HTTPS server created');
- console.log('🔌 Creating WebSocket server...');
- const wss = new WebSocket.Server({
- server,
- path: '/ws'
- });
- console.log('✅ WebSocket server created');
- wss.on('connection', (ws, req) => {
- console.log('🔗 New WebSocket connection received');
-
- stats.totalConnections++;
- stats.activeConnections++;
- const connectionId = `conn-${Date.now()}-${stats.totalConnections}`;
- console.log('🆔 Connection ID:', connectionId);
- console.log('🌍 Client IP:', req.socket.remoteAddress);
- console.log('📊 Connection stats:', {
- total: stats.totalConnections,
- active: stats.activeConnections
- });
-
- log(`New connection: ${connectionId}`, { ip: req.socket.remoteAddress });
- ws.on('message', async (message) => {
- console.log('📨 Message received from client');
- console.log('📝 Message length:', message.length);
-
- try {
- console.log('🔍 Parsing JSON message...');
- const data = JSON.parse(message);
- console.log('✅ JSON parsed successfully');
- console.log('📊 Message data:', data);
-
- log(`Received message:`, data);
- const requestId = data.requestId || 'unknown';
- console.log('🆔 Request ID:', requestId);
-
- if (data.type === 'register') {
- console.log('📝 REGISTRATION REQUEST received');
-
- const { functionId, functionBody, paramNames = [] } = data;
- console.log('📋 Registration details:');
- console.log(' Function ID:', functionId);
- console.log(' Body length:', functionBody?.length);
- console.log(' Param names:', paramNames);
-
- log(`Processing code registration: ${functionId}`);
- console.log('🔧 Calling registerFunction...');
- const success = registerFunction(functionId, functionBody, paramNames);
- console.log('📊 Registration result:', success);
-
- console.log('📤 Sending registration response...');
- const response = {
- requestId,
- status: 'ok',
- result: { registered: success }
- };
- console.log('📋 Response:', response);
-
- ws.send(JSON.stringify(response));
- console.log('✅ Registration response sent');
- }
-
- else if (data.type === 'execute') {
- console.log('🚀 EXECUTION REQUEST received');
-
- const { functionId, args = [] } = data;
- console.log('📋 Execution details:');
- console.log(' Function ID:', functionId);
- console.log(' Args count:', args.length);
- console.log(' Args:', args);
-
- log(`Processing code execution: ${functionId}`);
- try {
- console.log('⚡ Starting function execution...');
-
-
-
- const result = await executeFunction(functionId, args);
-
- console.log('✅ Function execution completed');
- console.log('📊 Execution result:', result);
-
- log(`Final result to send for ${requestId}:`, result);
-
- console.log('📤 Sending execution response...');
- const response = {
- requestId,
- result,
- markerId: data.markerId
- };
- console.log('📋 Response:', response);
-
- ws.send(JSON.stringify(response));
- console.log('✅ Execution response sent');
-
- } catch (execError) {
- console.log('💥 EXECUTION ERROR:', execError);
- console.log('📍 Error message:', execError.message);
- console.log('📍 Error stack:', execError.stack);
-
- log(`Error executing code:`, { error: execError.message, stack: execError.stack });
-
- console.log('📤 Sending error response...');
- const errorResponse = { requestId, error: execError.message };
- console.log('📋 Error response:', errorResponse);
-
- ws.send(JSON.stringify(errorResponse));
- console.log('✅ Error response sent');
- }
- }
-
- else {
- console.log('❓ Unknown message type:', data.type);
- log(`Unknown message type received`, { type: data.type });
-
- const errorResponse = { requestId, error: `Unknown message type: ${data.type}` };
- ws.send(JSON.stringify(errorResponse));
- }
-
- } catch (error) {
- console.log('💥 MESSAGE PROCESSING ERROR:', error);
- console.log('📍 Error message:', error.message);
- console.log('📍 Raw message:', message.toString().substring(0, 200));
-
- log(`Error processing message:`, { error: error.message, message: message.toString().substring(0, 200) });
- stats.errors++;
- try {
- console.log('📤 Sending generic error response...');
- ws.send(JSON.stringify({ error: error.message }));
- console.log('✅ Generic error response sent');
- } catch (sendError) {
- console.log('💥 Error sending error response:', sendError);
- log(`Error sending error response:`, { error: sendError.message });
- }
- }
- });
- ws.on('close', () => {
- console.log('🔌 WebSocket connection closed:', connectionId);
- stats.activeConnections--;
- console.log('📊 Updated connection stats:', {
- total: stats.totalConnections,
- active: stats.activeConnections
- });
- log(`Connection closed: ${connectionId}`);
- });
- console.log('📤 Sending welcome message...');
- const welcomeMessage = {
- type: 'system',
- message: 'Secure WebSocket server connected',
- connectionId,
- timestamp: new Date().toISOString()
- };
- console.log('📋 Welcome message:', welcomeMessage);
-
- ws.send(JSON.stringify(welcomeMessage));
- console.log('✅ Welcome message sent');
- });
- console.log('🎧 Starting server listener...');
- server.listen(WS_PORT, () => {
- console.log('🎉 SERVER IS LIVE!');
- console.log('🌐 Listening on port:', WS_PORT);
- console.log('🔗 WebSocket path: /ws');
- console.log('🔒 SSL enabled: YES');
-
- log(`Server listening on port ${WS_PORT}`);
- });
- console.log('✅ Server startup complete');
- return wss;
- }
- console.log('🧪 Registering test function...');
- registerFunction('test', 'return "Hello, World!";', []);
- console.log('✅ Test function registered');
- if (require.main === module) {
- console.log('🚀 Starting server in standalone mode...');
- log('Starting server...');
- startServer();
- console.log('🎉 Server startup initiated!');
- }
- console.log('📤 Exporting module functions...');
- module.exports = {
- startServer,
- registerFunction,
- executeFunction
- };
- console.log('✅ Module exports ready');
- console.log('🎉 SERVER-FUNCTION-HUB INITIALIZATION COMPLETE!');
|