|
1 | | -console.log("hello world"); |
| 1 | +/* |
| 2 | + * GNU AGPL-3.0 License |
| 3 | + * |
| 4 | + * Copyright (c) 2021 - present core.ai . All rights reserved. |
| 5 | + * Original work Copyright (c) 2014 - 2021 Adobe Systems Incorporated. All rights reserved. |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify it |
| 8 | + * under the terms of the GNU Affero General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 13 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License |
| 15 | + * for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License |
| 18 | + * along with this program. If not, see https://opensource.org/licenses/AGPL-3.0. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +const readline = require('readline'); |
| 23 | +const http = require('http'); |
| 24 | +const net = require('net'); |
| 25 | +const PhoenixFS = require('@phcode/fs/dist/phoenix-fs'); |
| 26 | + |
| 27 | +function randomNonce(byteLength) { |
| 28 | + const randomBuffer = new Uint8Array(byteLength); |
| 29 | + crypto.getRandomValues(randomBuffer); |
| 30 | + |
| 31 | + // Define the character set for the random string |
| 32 | + const charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; |
| 33 | + |
| 34 | + // Convert the ArrayBuffer to a case-sensitive random string with numbers |
| 35 | + let randomId = ''; |
| 36 | + Array.from(randomBuffer).forEach(byte => { |
| 37 | + randomId += charset[byte % charset.length]; |
| 38 | + }); |
| 39 | + |
| 40 | + return randomId; |
| 41 | +} |
| 42 | + |
| 43 | +const COMMAND_RESPONSE_PREFIX = 'phnodeResp:'; |
| 44 | +// Generate a random 64-bit url. This should take 100 million+ of years to crack with current http connection speed. |
| 45 | +const PHOENIX_FS_URL = `/PhoenixFS${randomNonce(8)}`; |
| 46 | +const PHOENIX_NODE_URL = `/PhoenixNode${randomNonce(8)}`; |
| 47 | + |
| 48 | +const savedConsoleLog = console.log; |
| 49 | + |
| 50 | +const rl = readline.createInterface({ |
| 51 | + input: process.stdin, |
| 52 | + output: process.stdout |
| 53 | +}); |
| 54 | + |
| 55 | +let serverPortResolve; |
| 56 | +const serverPortPromise = new Promise((resolve) => { serverPortResolve = resolve; }); |
| 57 | + |
| 58 | +let orphanExitTimer = setTimeout(()=>{ |
| 59 | + process.exit(1); |
| 60 | +}, 60000); |
| 61 | + |
| 62 | +function _sendResponse(responseMessage, commandID) { |
| 63 | + savedConsoleLog(COMMAND_RESPONSE_PREFIX + JSON.stringify({ |
| 64 | + message: responseMessage, |
| 65 | + commandID |
| 66 | + }) + "\n"); |
| 67 | +} |
| 68 | + |
| 69 | +function processCommand(line) { |
| 70 | + try{ |
| 71 | + let jsonCmd = JSON.parse(line); |
| 72 | + switch (jsonCmd.commandCode) { |
| 73 | + case "terminate": process.exit(0); return; |
| 74 | + case "heartBeat": |
| 75 | + clearTimeout(orphanExitTimer); |
| 76 | + orphanExitTimer = setTimeout(()=>{ |
| 77 | + process.exit(1); |
| 78 | + }, 60000); |
| 79 | + return; |
| 80 | + case "ping": _sendResponse("pong", jsonCmd.commandID); return; |
| 81 | + case "setDebugMode": |
| 82 | + if(jsonCmd.commandData) { |
| 83 | + console.log = savedConsoleLog; |
| 84 | + } else { |
| 85 | + console.log = function () {}; // swallow logs |
| 86 | + } |
| 87 | + _sendResponse("done", jsonCmd.commandID); return; |
| 88 | + case "setPhoenixFSDebugMode": |
| 89 | + PhoenixFS.setDebugMode(jsonCmd.commandData); |
| 90 | + _sendResponse("done", jsonCmd.commandID); return; |
| 91 | + case "getEndpoints": |
| 92 | + serverPortPromise.then(port =>{ |
| 93 | + _sendResponse({ |
| 94 | + port, |
| 95 | + phoenixFSURL: `ws://localhost:${port}${PHOENIX_FS_URL}`, |
| 96 | + phoenixNodeURL: `ws://localhost:${port}${PHOENIX_NODE_URL}` |
| 97 | + }, jsonCmd.commandID); |
| 98 | + }); |
| 99 | + return; |
| 100 | + default: console.error("unknown command: "+ line); |
| 101 | + } |
| 102 | + } catch (e) { |
| 103 | + console.error(e); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +rl.on('line', (line) => { |
| 108 | + processCommand(line); |
| 109 | +}); |
| 110 | + |
| 111 | +function getFreePort() { |
| 112 | + return new Promise((resolve)=>{ |
| 113 | + const server = net.createServer(); |
| 114 | + |
| 115 | + server.listen(0, () => { |
| 116 | + const port = server.address().port; |
| 117 | + server.close(() => { |
| 118 | + resolve(port); |
| 119 | + }); |
| 120 | + }); |
| 121 | + }); |
| 122 | +} |
| 123 | + |
| 124 | +// Create an HTTP server |
| 125 | +const server = http.createServer((req, res) => { |
| 126 | + res.writeHead(200, { 'Content-Type': 'text/plain' }); |
| 127 | + res.end('WebSocket server running'); |
| 128 | +}); |
| 129 | + |
| 130 | +getFreePort().then((port) => { |
| 131 | + console.log('Server Opened on port: ', port); |
| 132 | + |
| 133 | + PhoenixFS.CreatePhoenixFsServer(server, PHOENIX_FS_URL); |
| 134 | + // Start the HTTP server on port 3000 |
| 135 | + server.listen(port, () => { |
| 136 | + serverPortResolve(port); |
| 137 | + console.log(`Server running on http://localhost:${port}`); |
| 138 | + console.log(`Phoenix node tauri FS url is ws://localhost:${port}${PHOENIX_FS_URL}`); |
| 139 | + }); |
| 140 | + |
| 141 | +}); |
0 commit comments