-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
201 lines (180 loc) · 5.98 KB
/
index.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
process.chdir(__dirname);
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const fs = require("fs");
const db = require("./modules/db.js");
const mcping = require("./modules/mcping.js");
const cronjob = require("./modules/cronjob.js");
const app = express();
app.use(cors());
app.use(bodyParser.json());
function sendJson(res,obj){
res.status(200);
res.type("application/json");
res.send(JSON.stringify(Object.assign(obj,{error:false})));
res.end();
}
function sendError(res,err){
res.status(400);
res.type("application/json");
res.send(JSON.stringify({"error":true,"message":err.message}));
res.end();
}
function _saveOrigin(serverId,origin){
if(origin)
db.server.checkUpdateOrigin(serverId,origin);
}
function init(){
app.use(express.static("frontend/"));
app.get("/embed/:serverId.js",(req,res)=>{
if(db.server.getServer(req.params.serverId)){
fs.readFile("./client-script.js",(err,file)=>{
if(err){
res.status(500).type("text/javascript").send("\"FATAL ERROR GENERATING SCRIPT!\";").end();
}
else{
res.status(200);
res.type("text/javascript");
res.send(file.toString().replace("%@SERVERID@%",req.params.serverId));
res.end();
}
});
}
else{
res.status(500).type("text/javascript").send("\"INVALID SERVER ID!\";").end();
}
});
app.get("/api/apiStatus",(req,res)=>{
sendJson(res,db.apiStatus());
});
app.get("/api/:serverId/serverInfo",(req,res)=>{
var info = db.server.getServerInfo(req.params.serverId);
if(info){
db.server.increaseAccessCount(req.params.serverId);
sendJson(res,info);
_saveOrigin(req.params.serverId,req.get("origin"));
}
else{
sendError(res,new Error("Invalid Server ID!"));
}
});
app.get("/api/:serverId/queryResults",(req,res)=>{
sendError(res,new Error("ACTION DUE TO HIGH TRAFFIC DISABLED!"));return;
var info = db.request.getQueries(req.params.serverId);
if(info){
db.server.increaseAccessCount(req.params.serverId);
sendJson(res,{results:info});
_saveOrigin(req.params.serverId,req.get("origin"));
}
else{
sendError(res,new Error("Invalid Server ID!"));
}
});
app.get("/api/:serverId/onlineHistory/:timespan?",(req,res)=>{
if(db.server.getServer(req.params.serverId)){
db.server.increaseAccessCount(req.params.serverId);
if(req.params.timespan){
if(req.params.timespan=="today"){
sendJson(res,{result:db.request.getPlayerOnlineHistory(req.params.serverId,1)});
}
else if(req.params.timespan=="week"){
sendJson(res,{result:db.request.getPlayerOnlineHistory(req.params.serverId,2)});
}
else if(req.params.timespan=="month"){
sendJson(res,{result:db.request.getPlayerOnlineHistory(req.params.serverId,3)});
}
else{
sendError(res,new Error("Invalid timespan!"));
}
_saveOrigin(req.params.serverId,req.get("origin"));
}
else{
//TIMESPAN=0;
var data = db.request.getPlayerOnlineHistory(req.params.serverId,0);
sendJson(res,{result:data});
_saveOrigin(req.params.serverId,req.get("origin"));
}
}
else{
sendError(res,new Error("Invalid Server ID!"));
}
});
app.get("/api/:serverId/playerGameTime/:timespan?",(req,res)=>{
if(db.server.getServer(req.params.serverId)){
db.server.increaseAccessCount(req.params.serverId);
if(req.params.timespan){
if(req.params.timespan=="today"){
sendJson(res,{result:db.request.getPlayerGameTime(req.params.serverId,1)});
}
else if(req.params.timespan=="week"){
sendJson(res,{result:db.request.getPlayerGameTime(req.params.serverId,2)});
}
else if(req.params.timespan=="month"){
sendJson(res,{result:db.request.getPlayerGameTime(req.params.serverId,3)});
}
else{
sendError(res,new Error("Invalid timespan!"));
}
_saveOrigin(req.params.serverId,req.get("origin"));
}
else{
//TIMESPAN=0; -> TODAY
sendJson(res,{result:db.request.getPlayerGameTime(req.params.serverId,1)});
_saveOrigin(req.params.serverId,req.get("origin"));
}
}
else{
sendError(res,new Error("Invalid Server ID!"));
}
});
app.get("/api/:serverId/playerOnline",(req,res)=>{
if(db.server.getServer(req.params.serverId)){
db.server.increaseAccessCount(req.params.serverId);
sendJson(res,{onlineList:db.request.getPlayerGameTime(req.params.serverId,0)});
_saveOrigin(req.params.serverId,req.get("origin"));
}
else{
sendError(res,new Error("Invalid Server ID!"));
}
});
app.get("/api/:serverId*",(req,res)=>{
sendError(res,new Error("Please specify an action!"));
});
app.get("/api",(req,res)=>{
sendError(res,new Error("Please specify a Server ID!"));
});
app.post("/api/insertServer",(req,res)=>{
console.log(req.body);
if(req.body.ip&&req.body.port){
var exi=db.server.checkServerExist(req.body.ip,req.body.port);
if(exi){
if(exi.active){
sendError(res,new Error("This server already exists! ID: "+exi.id));
}
else{
reEnableServer(exi.id);
sendJson(res,{status:"ENABLED",serverId:exi.id,info:"Server enabled again!"});
}
}
else{
mcping.testQuery(req.body.ip,req.body.port,(err,status)=>{
if(err){
sendError(res,new Error("Can't reach Server! Please make sure your server is online!"));
}
else{
var id=db.server.addServer(req.body.ip,req.body.port);
sendJson(res,{status:"OK",serverId:id});
}
});
}
}
else{
sendError(res,new Error("Please specify server IP and PORT"));
}
});
cronjob.startCronjob();
app.listen(8010);
console.log(db.server.getActiveServer().length+" Active Servers loaded");
}
db.init(init);