Skip to content

Commit

Permalink
switch nodejs and add remote api
Browse files Browse the repository at this point in the history
  • Loading branch information
Seshpenguin committed Nov 8, 2023
1 parent 916363a commit 31e64a3
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 23 deletions.
2 changes: 1 addition & 1 deletion distro-files/plctl
@@ -1,2 +1,2 @@
#!/usr/bin/env sh
exec node /opt/prolinuxd/prolinux-cli/index.js $@
exec /opt/nodejs/bin/node /opt/prolinuxd/prolinux-cli/index.js $@
2 changes: 1 addition & 1 deletion distro-files/prolinuxd
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
while true
do
node /opt/prolinuxd/index.js
/opt/nodejs/bin/node /opt/prolinuxd/index.js
echo "prolinuxd exited with code $?. Respawning..." >&2
sleep 2
done
36 changes: 25 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -24,12 +24,13 @@
},
"dependencies": {
"@ltd/j-toml": "^1.37.0",
"@types/deep-extend": "^0.6.2",
"@types/lodash": "^4.14.200",
"axios": "^1.3.4",
"commander": "^10.0.0",
"deep-extend": "^0.6.0",
"dotenv": "^16.0.3",
"is-reachable": "^5.2.1",
"lodash": "^4.17.21",
"node-pty": "^0.10.1",
"uuid": "^9.0.0",
"ws": "^8.11.0",
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Expand Up @@ -16,6 +16,7 @@ export enum LocalActions {
RESULT = "result",
SET_DISABLE_KEXEC = "set-disable-kexec",
DESCRIBE_API = "describe-api",
SET_REMOTE_API = "set-remote-api",
}
export interface ProLinuxInfo {
buildnum: string,
Expand Down
44 changes: 35 additions & 9 deletions src/index.ts
Expand Up @@ -5,7 +5,7 @@ import { WebSocket } from "ws";
import isReachable from "is-reachable";
import * as TOML from '@ltd/j-toml';
import axios from "axios";
import * as _ from "lodash";
import deepExtend from "deep-extend";

import { log, logger } from "./logging";
import { OCS2Connection } from "./modules/ocs2/cloudapi";
Expand Down Expand Up @@ -47,15 +47,16 @@ export let config = {
selected_root: "a",
locked_root: true,
hostname: "",
disable_kexec: false
disable_kexec: false,
remote_api: true
}
}

async function main() {
// Read configuration file
try {
const tomlConfig = TOML.parse(fs.readFileSync(process.env.CONFIG_FILE ?? path.join(__dirname, "prolinux.toml"), "utf-8")) as typeof config;
config = _.merge(config, tomlConfig);
config = deepExtend(config, tomlConfig);
log.info("Configuration file loaded!");
log.info(JSON.stringify(config, null, 4));
} catch(e) {
Expand All @@ -71,12 +72,9 @@ async function main() {
try{
fs.unlinkSync("/tmp/prolinuxd.sock");
} catch (err) {}
const server = http.createServer()
const wss = new WebSocket.Server({ server });
localSocket = wss;

// Local websocket server (/tmp/prolinuxd.sock)
wss.on("connection", (socket) => {
const wsConnectionHandler = (socket: WebSocket) => {
log.info("Client connected to ProLinuxD!");
const saveConfig = () => {
let tomlConfig = TOML.stringify(config, {
Expand Down Expand Up @@ -138,6 +136,11 @@ async function main() {
saveConfig();
replyResult(LocalActions.SET_DISABLE_KEXEC, true, {});
} break;
case LocalActions.SET_REMOTE_API: {
config.pl2.remote_api = msg.payload.remoteAPI;
saveConfig();
replyResult(LocalActions.SET_REMOTE_API, true, {});
} break;
case LocalActions.STATUS: {
console.log("Sending status...")
socket.send(JSON.stringify({
Expand Down Expand Up @@ -248,8 +251,13 @@ async function main() {
socket.on("close", () => {
log.info("Client disconnected from ProLinuxD!");
});
});

};

const server = http.createServer()
const wss = new WebSocket.Server({ server });
localSocket = wss;

wss.on("connection", wsConnectionHandler);
wss.on("error", (err) => {
log.error("WS Server error: " + err.message);
console.log(err)
Expand Down Expand Up @@ -285,6 +293,24 @@ async function main() {
log.info("Starting ProLinux 2 Module...");
await loadPL2Module();
}
console.log(config.pl2.remote_api)
if(config.pl2.remote_api) {
// create a new wss server on port 25567 using wsConnectionHandler
const remoteServer = http.createServer();
const remoteWss = new WebSocket.Server({ server: remoteServer });
remoteWss.on("connection", (socket) => {
// todo check for auth
wsConnectionHandler(socket);
});
remoteWss.on("error", (err) => {
log.error("Remote WS Server error: " + err.message);
console.log(err)
});
remoteServer.listen(25567);
remoteServer.on("listening", async () => {
log.info("Remote API is listening on port 25567!");
});
}
});
}
try {
Expand Down

0 comments on commit 31e64a3

Please sign in to comment.