|
const username = process.env.WEB_USERNAME || "admin"; |
|
const password = process.env.WEB_PASSWORD || "password"; |
|
const url = process.env.RENDER_EXTERNAL_URL; |
|
const port = process.env.PORT || 3000; |
|
const express = require("express"); |
|
const app = express(); |
|
var exec = require("child_process").exec; |
|
const os = require("os"); |
|
const { legacyCreateProxyMiddleware } = require("http-proxy-middleware"); |
|
var request = require("request"); |
|
var fs = require("fs"); |
|
var path = require("path"); |
|
const auth = require("basic-auth"); |
|
|
|
app.get("/", function (req, res) { |
|
res.status(200).send("hello world"); |
|
}); |
|
|
|
|
|
app.use((req, res, next) => { |
|
const user = auth(req); |
|
if (user && user.name === username && user.pass === password) { |
|
return next(); |
|
} |
|
res.set("WWW-Authenticate", 'Basic realm="Node"'); |
|
return res.status(401).send(); |
|
}); |
|
|
|
|
|
app.get("/status", function (req, res) { |
|
let cmdStr = "pm2 list; ps -ef"; |
|
exec(cmdStr, function (err, stdout, stderr) { |
|
if (err) { |
|
res.type("html").send("<pre>命令行执行错误:\n" + err + "</pre>"); |
|
} else { |
|
res.type("html").send("<pre>获取守护进程和系统进程表:\n" + stdout + "</pre>"); |
|
} |
|
}); |
|
}); |
|
|
|
|
|
app.get("/listen", function (req, res) { |
|
let cmdStr = "ss -nltp"; |
|
exec(cmdStr, function (err, stdout, stderr) { |
|
if (err) { |
|
res.type("html").send("<pre>命令行执行错误:\n" + err + "</pre>"); |
|
} else { |
|
res.type("html").send("<pre>获取系统监听端口:\n" + stdout + "</pre>"); |
|
} |
|
}); |
|
}); |
|
|
|
|
|
app.get("/list", function (req, res) { |
|
let cmdStr = "bash argo.sh"; |
|
exec(cmdStr, function (err, stdout, stderr) { |
|
if (err) { |
|
res.type("html").send("<pre>命令行执行错误:\n" + err + "</pre>"); |
|
} |
|
else { |
|
res.type("html").send("<pre>节点数据:\n\n" + stdout + "</pre>"); |
|
} |
|
}); |
|
}); |
|
|
|
|
|
app.get("/info", function (req, res) { |
|
let cmdStr = "cat /etc/*release | grep -E ^NAME"; |
|
exec(cmdStr, function (err, stdout, stderr) { |
|
if (err) { |
|
res.send("命令行执行错误:" + err); |
|
} |
|
else { |
|
res.send( |
|
"命令行执行结果:\n" + |
|
"Linux System:" + |
|
stdout + |
|
"\nRAM:" + |
|
os.totalmem() / 1000 / 1000 + |
|
"MB" |
|
); |
|
} |
|
}); |
|
}); |
|
|
|
|
|
app.get("/test", function (req, res) { |
|
let cmdStr = 'mount | grep " / " | grep "(ro," >/dev/null'; |
|
exec(cmdStr, function (error, stdout, stderr) { |
|
if (error !== null) { |
|
res.send("系统权限为---非只读"); |
|
} else { |
|
res.send("系统权限为---只读"); |
|
} |
|
}); |
|
}); |
|
|
|
|
|
|
|
function keep_web_alive() { |
|
|
|
exec("curl -m8 " + url, function (err, stdout, stderr) { |
|
if (err) { |
|
console.log("保活-请求主页-命令行执行错误:" + err); |
|
} |
|
else { |
|
console.log("保活-请求主页-命令行执行成功,响应报文:" + stdout); |
|
} |
|
}); |
|
} |
|
|
|
setInterval(keep_web_alive, 10 * 1000); |
|
|
|
app.use( |
|
legacyCreateProxyMiddleware({ |
|
target: 'http://127.0.0.1:8080/', |
|
ws: true, |
|
changeOrigin: true, |
|
on: { |
|
proxyRes: function proxyRes(proxyRes, req, res) { |
|
|
|
|
|
|
|
}, |
|
proxyReq: function proxyReq(proxyReq, req, res) { |
|
|
|
|
|
|
|
}, |
|
error: function error(err, req, res) { |
|
console.warn('websocket error.', err); |
|
} |
|
}, |
|
pathRewrite: { |
|
'^/': '/', |
|
}, |
|
|
|
}) |
|
); |
|
|
|
|
|
exec("bash entrypoint.sh", function (err, stdout, stderr) { |
|
if (err) { |
|
console.error(err); |
|
return; |
|
} |
|
console.log(stdout); |
|
}); |
|
|
|
app.listen(port, () => console.log(`Example app listening on port ${port}!`)); |