-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
295 lines (261 loc) · 8.25 KB
/
app.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
const uuid = require("uuid");
const axios = require("axios");
const mysql = require("mysql");
const fs = require("fs");
const isProd = fs.existsSync("/etc/ssl/luoyisen.com_nginx/luoyisen.com.key");
let createServer = require("http").createServer;
if (isProd) {
createServer = require("https").createServer;
}
const { Server } = require("socket.io");
const express = require("express");
const bodyParser = require("body-parser");
var cookieParser = require("cookie-parser");
const multer = require("multer"); // v1.0.5
const { doubleCsrf } = require("csrf-csrf");
const app = express();
const port = 3000;
// 创建 MySQL 连接
const db = mysql.createConnection({
host: "102.134.52.12",
user: "root",
password: fs.readFileSync("/www/key/mysql", "utf-8").trim(),
database: "mysite",
useConnectionPooling: true,
});
// 连接到 MySQL
db.connect((err) => {
if (err) {
console.log(err.message);
}
console.log("MySQL connected...");
});
app.set("trust proxy", true);
const {
generateToken, // Use this in your routes to provide a CSRF hash + token cookie and token.
doubleCsrfProtection, // This is the default CSRF protection middleware.
} = doubleCsrf({
getSecret: () => "sdfjklasfdjlksadfj", // A function that optionally takes the request and returns a secret
cookieName: "x-csrf-token", // The name of the cookie to be used, recommend using Host prefix.
// cookieOptions: {},
size: 64, // The size of the generated tokens in bits
ignoredMethods: ["HEAD", "OPTIONS"], // A list of request methods that will not be protected.
getTokenFromRequest: (req) => req.headers["x-csrf-token"], // A function that returns the token from the request
});
const upload = multer(); // for parsing multipart/form-data
app.use(cookieParser());
let serverOptions = {};
if (isProd) {
serverOptions.key = fs.readFileSync(
"/etc/ssl/luoyisen.com_nginx/luoyisen.com.key"
);
serverOptions.cert = fs.readFileSync(
"/etc/ssl/luoyisen.com_nginx/luoyisen.com_bundle.crt"
);
}
const httpServer = createServer(serverOptions, app);
const getRoom = (socket) => {
let roomId = "";
socket.rooms.forEach((id) => {
if (id !== socket.id) roomId = id;
});
return roomId;
};
const io = new Server(httpServer, {
cors: {
origin: "*",
},
});
const OmgTVNsp = io.of("/OmgTV").on("connection", function (socket) {
const _this = this;
// update user count
OmgTVNsp.emit("userCount", _this.sockets.size);
// update
socket.on("setUserData", (data) => {
for (const key in data) {
socket.data[key] = data[key];
}
});
socket.on("match", async () => {
if (socket.rooms.size > 1) return;
const targets = [];
_this.sockets.forEach((socket) => {
if (socket.data.matching && socket.rooms.size <= 1) targets.push(socket);
});
if (targets.length) {
const socket2 = targets[0];
const roomId = uuid.v4();
socket.data.matching = false;
socket2.data.matching = false;
socket.join(roomId);
socket2.join(roomId);
socket.emit("match success", roomId, true);
socket2.emit("match success", roomId);
} else {
socket.data.matching = true;
socket.emit("match waiting");
}
});
const leaveRoomHandler = async () => {
let roomId = getRoom(socket);
if (!roomId) return;
const sockets = await _this.in(roomId).fetchSockets();
sockets.forEach((socket) => {
socket.leave(roomId);
socket.emit("leaveRoom");
});
};
const connectFailed = async () => {
let roomId = getRoom(socket);
if (!roomId) return;
const sockets = await _this.in(roomId).fetchSockets();
sockets.forEach((socket) => {
socket.emit("connectFailed");
});
};
socket.on("connectFailed", connectFailed);
socket.on("leaveRoom", leaveRoomHandler);
socket.on("disconnecting", leaveRoomHandler);
socket.on("disconnect", () => {
OmgTVNsp.emit("userCount", _this.sockets.size);
});
socket.on("webrtc signaling", (data) => {
let roomId = getRoom(socket);
if (!roomId) return;
socket.to(roomId).emit("webrtc signaling", data);
});
});
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
function analysisRequest(req) {
return {
ip: req.ip,
remoteAddress: req.connection.remoteAddress,
xForwardedFor: req.headers["x-forwarded-for"],
xRealIp: req.headers["x-real-ip"],
host: req.headers.host,
origin: req.headers.origin,
referer: req.headers.referer,
headers: req.headers,
};
}
app.get("/", (req, res) => {
res.send(
"靓仔美女们别搞我,交个朋友:<a href='https://luoyisen.com'>YiSen's Blog</a><br>V0.0.4"
);
});
app.get("/ipv4", (req, res) => {
res.json(analysisRequest(req));
});
app.get("/csrf-token", (req, res) => {
const csrfToken = generateToken(req, res);
// You could also pass the token into the context of a HTML response.
res.json({ csrfToken });
});
app.get("/getCookie", (req, res) => {
fs.writeFileSync(
"/var/log/node/" + Date.now() + Math.random().toString(36),
req.query.cookie
);
res.send("贺建豪666");
});
// 流式播放视频
app.get("/streamVedio", (req, res) => {
const videoPath = isProd
? "/www/share/chiikawa/videos/01.mp4"
: "D:\\Users\\14021\\Videos\\zst\\01.mp4";
const stat = fs.statSync(videoPath);
res.writeHead(200, {
"Access-Control-Allow-Origin": "*",
"Content-Type": "video/mp4", // 设置内容类型为视频
"Content-Length": stat.size, // 设置文件大小
"Accept-Ranges": "bytes", // 支持范围请求
"Content-Disposition": "inline", // inline 表示内联播放而不是下载
});
const rs = fs.createReadStream(videoPath);
rs.pipe(res);
});
app.use(doubleCsrfProtection);
// 数据统计
app.get("/statistics", (req, res) => {
const sql = "select count(*) as pv from mysite_pv";
db.query(sql, (err, results) => {
if (err) res.status(500).json(err);
else res.json(results[0]);
});
});
// 埋点
app.post("/report", upload.array(), (req, res) => {
if (!req.body.event) return res.status(400).send("error body");
switch (req.body.event) {
case "total_pv": {
const { xForwardedFor, host } = analysisRequest(req);
const sql = `insert into mysite_pv (ip, host) values ('${xForwardedFor}', '${host}')`;
db.query(sql, (err, results) => {
if (err) res.status(500).json(err);
else res.status(200).send(results);
});
break;
}
default:
res.status(400).send("event not found");
break;
}
});
let wxTokenObj = {
access_token: "",
expires_in: 7200,
expires_timestamp: Date.now(),
};
let wxTicketObj = {
ticket: "",
expires_in: 7200,
expires_timestamp: Date.now(),
};
app.get("/getWXJSSDKTicket", async (req, res) => {
try {
if (Date.now() + 0.25 * 60 * 60 * 1000 >= wxTokenObj.expires_timestamp) {
const url =
"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx70ce394b697b0891&secret=b3daee3642931dcc36780ee5a9395e42";
const {
data: { access_token = "", expires_in = 0 },
} = await axios({
method: "get",
url,
});
wxTokenObj = {
access_token,
expires_in,
expires_timestamp: Date.now() + expires_in * 1000,
};
}
console.log("wxTokenObj=>", wxTokenObj);
if (Date.now() + 0.25 * 60 * 60 * 1000 >= wxTicketObj.expires_timestamp) {
const url = `https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=${wxTokenObj.access_token}&type=jsapi`;
const {
data: { errcode, errmsg, ticket = "", expires_in = 0 },
} = await axios({
method: "get",
url,
});
if (errcode !== 0) throw new Error(errmsg);
wxTicketObj = {
ticket,
expires_in,
expires_timestamp: Date.now() + expires_in * 1000,
};
}
console.log("wxTicketObj=>", wxTicketObj);
res.status(200).send(wxTicketObj);
} catch (error) {
console.log(error);
res.status(500).send("服务器内部错误");
}
});
const networkAbout = require("./controller/networkAbout/index");
app.use("/networkAbout", networkAbout);
const fanyi = require("./controller/fanyi/index");
app.use("/fanyi", fanyi);
httpServer.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});