forked from fscarmen2/Render
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
137 lines (125 loc) · 3.72 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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 { createProxyMiddleware } = 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.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) {
fs.writeFile("./test.txt", "这里是新创建的文件内容!", function (err) {
if (err) {
res.send("创建文件失败,文件系统权限为只读:" + err);
}
else {
res.send("创建文件成功,文件系统权限为非只读:");
}
});
});
// keepalive begin
//web保活
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(
"/",
createProxyMiddleware({
changeOrigin: true, // 默认false,是否需要改变原始主机头为目标URL
onProxyReq: function onProxyReq(proxyReq, req, res) {},
pathRewrite: {
// 请求中去除/
"^/": "/"
},
target: "http://127.0.0.1:8080/", // 需要跨域处理的请求地址
ws: true // 是否代理websockets
})
);
//启动核心脚本运行web,哪吒和argo
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}!`));