Let's get this thing working
This commit is contained in:
100
server.js
Normal file
100
server.js
Normal file
@@ -0,0 +1,100 @@
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
var server = require("http").createServer(onRequest);
|
||||
|
||||
var io = require("socket.io")(server);
|
||||
var SSHClient = require("ssh2").Client;
|
||||
|
||||
var config = {};
|
||||
try {
|
||||
// try and load the config file
|
||||
const data = fs.readFileSync("/data/config.json", "utf-8");
|
||||
config = JSON.parse(data);
|
||||
console.log("Loaded config.json");
|
||||
} catch (ex) {
|
||||
console.log("Unable to load config.json, using defaults");
|
||||
console.error(ex);
|
||||
}
|
||||
|
||||
var sshConfig = {
|
||||
host: config.ssh?.host ?? "host.docker.internal",
|
||||
port: config.ssh?.port ?? 22,
|
||||
username: config.ssh?.username,
|
||||
};
|
||||
|
||||
if (config.ssh?.password) {
|
||||
sshConfig.password = config.ssh.password;
|
||||
} else if (config.ssh?.privateKey) {
|
||||
sshConfig.privateKey = fs.readFileSync(
|
||||
path.join("/data", config.ssh.privateKey)
|
||||
);
|
||||
}
|
||||
|
||||
// Load static files into memory
|
||||
var staticFiles = {};
|
||||
var basePath = path.join(require.resolve("@xterm/xterm"), "..");
|
||||
staticFiles["/xterm.css"] = fs.readFileSync(
|
||||
path.join(basePath, "../css/xterm.css")
|
||||
);
|
||||
staticFiles["/xterm.js"] = fs.readFileSync(path.join(basePath, "xterm.js"));
|
||||
basePath = path.join(require.resolve("@xterm/addon-fit"), "..");
|
||||
staticFiles["/xterm-addon-fit.js"] = fs.readFileSync(
|
||||
path.join(basePath, "addon-fit.js")
|
||||
);
|
||||
staticFiles["/"] = fs.readFileSync("index.html");
|
||||
|
||||
// Handle static file serving
|
||||
function onRequest(req, res) {
|
||||
var file;
|
||||
if (req.method === "GET" && (file = staticFiles[req.url])) {
|
||||
res.writeHead(200, {
|
||||
"Content-Type":
|
||||
"text/" +
|
||||
(/css$/.test(req.url)
|
||||
? "css"
|
||||
: /js$/.test(req.url)
|
||||
? "javascript"
|
||||
: "html"),
|
||||
});
|
||||
return res.end(file);
|
||||
}
|
||||
res.writeHead(404);
|
||||
res.end();
|
||||
}
|
||||
|
||||
io.on("connection", function (socket) {
|
||||
var conn = new SSHClient();
|
||||
conn
|
||||
.on("ready", function () {
|
||||
socket.emit("data", "\r\nConnecting...\r\n");
|
||||
conn.shell(function (err, stream) {
|
||||
if (err)
|
||||
return socket.emit(
|
||||
"data",
|
||||
`\r\nConnection Error: ${err.message}\r\n`
|
||||
);
|
||||
socket.on("data", function (data) {
|
||||
stream.write(data);
|
||||
});
|
||||
stream
|
||||
.on("data", function (d) {
|
||||
socket.emit("data", d.toString("binary"));
|
||||
})
|
||||
.on("close", function () {
|
||||
conn.end();
|
||||
});
|
||||
});
|
||||
})
|
||||
.on("close", function () {
|
||||
socket.emit("data", "\r\nConnection Closed\r\n");
|
||||
})
|
||||
.on("error", function (err) {
|
||||
socket.emit("data", `\r\nConnection Error: ${err.message}\r\n`);
|
||||
})
|
||||
.connect(sshConfig);
|
||||
});
|
||||
|
||||
let port = config.web?.port ?? 80;
|
||||
let ip = config.web?.ip ?? "0.0.0.0";
|
||||
console.log(`Listening on ${ip}:${port}`);
|
||||
server.listen(port, ip);
|
||||
Reference in New Issue
Block a user