forked from kwar0715/mockableExpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
355 lines (327 loc) · 11.8 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
const Server = require("./framework/server");
const express = require("express");
const _ = require("lodash");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const logger = require("./framework/logger");
const domainRouter = require("./routes/domainRouter");
const pathRouter = require("./routes/pathRoutes");
const schedulers = require("./routes/shedulers");
const variables = require("./routes/variables");
const socketRouter = require("./routes/socket");
const uuidv1 = require("uuid/v1");
const db = require("./framework/db");
const { getPublicIP } = require("./framework/utils");
const { send } = require("./framework/emailsender");
const { HOST, API_PORT, FROM_EMAIL, ADMIN_PREFIX } = require("./config");
const port = API_PORT;
const systemApp = express();
systemApp.set("view engine", "ejs");
systemApp.use(express.static("public"));
systemApp.use(cookieParser());
systemApp.use(
session({
key: "userId",
secret: "aaaa",
resave: false,
saveUninitialized: false,
cookie: {}
})
);
systemApp.use(bodyParser.json({limit: '50mb'}));
systemApp.use(bodyParser.urlencoded({
limit: '50mb',
extended: true,
parameterLimit:50000
}));
// create Admin User Default
try {
db.getAllUsers();
} catch (error) {
db.setUser("user", "12345678", "", 556677);
}
// save api url
let apiUrl;
(async() => {
apiUrl = `http://${HOST ? HOST : await getPublicIP()}:${port}`;
db.saveApiUrl(apiUrl);
})();
systemApp.use((req, res, next) => {
if (req.cookies.userId && !req.session.user) {
res.clearCookie("userId");
}
next();
});
const sessionChecker = (req, res, next) => {
return next();
if (req.session.user && req.cookies.userId) {
next();
} else {
res.redirect(`${ADMIN_PREFIX}`);
}
};
systemApp.get("/", function(req, res) {
res.render("login/login", { message: "" });
});
systemApp.get("/requestReset", function(req, res) {
res.render("login/requestReset");
});
systemApp.post("/requestReset", async function(req, res) {
try {
req.session.user = null;
if (!req.body.username) {
logger.error("Required Params not found");
res.redirect(`${ADMIN_PREFIX}`);
}
const user = await db.getUserFromUsername(req.body.username);
const userEmail = user.userEmail;
if (!userEmail) {
logger.error(`User email not found ${req.body.username}`);
res.redirect(`${ADMIN_PREFIX}`);
}
const uuid = uuidv1();
const resetLink = `http://${
HOST ? HOST : await getPublicIP()
}/${ADMIN_PREFIX}/resetPassword/${req.body.username}/${uuid}`;
db.saveResetToken(uuid);
logger.info(`Email Is Sending to ${userEmail}`);
const subject = "Reset Your Password (MockableExpress)";
const text = `
<h3> Mockable Express Reset Password </h3>
<br/>
<p>
Reset Password : ${resetLink}<br/>
user name: ${user.username}<br/>
password: 12345678<br/>
</p>
`;
send({ from: FROM_EMAIL, to: userEmail, subject, text });
res.render("login/login", {
message: `Password Reset Link sent to ${userEmail}`
});
} catch (error) {
res.redirect(`${ADMIN_PREFIX}/`);
}
});
systemApp.get("/resetPassword/:username/:token", async function(req, res) {
try {
const token = await db.getResetToken();
const user = await db.getUserFromUsername(req.params.username);
if (!user) {
req.session.user = null;
res.redirect(`${ADMIN_PREFIX}/`);
}
if (req.params.token === token) {
logger.info("Token matched");
await db.deleteUsers(user.counter);
logger.info("Need To Reset Default Password Through Token");
db.setUser(user.username, "12345678", user.userEmail, 556677);
db.deleteResetToken();
}
} catch (error) {
logger.error(error);
}
res.redirect(`${ADMIN_PREFIX}/`);
});
systemApp.get("/status", sessionChecker, async function(req, res) {
res.redirect(`${HOST ? HOST : await getPublicIP()}/status`);
});
systemApp.post("/saveToken", function(req, res) {
db.saveToken(`Bearer ${req.body.id}`);
res.end();
});
systemApp.get("/logout", function(req, res) {
req.session.user = null;
logger.error(`Logging out`);
res.redirect(`${ADMIN_PREFIX}/`);
});
systemApp.post("/passChange", sessionChecker, async function(req, res) {
try {
db.setUser(req.body.username, req.body.password, req.body.userEmail);
res.redirect(`${ADMIN_PREFIX}/domain`);
} catch (error) {
logger.error(`Update Password Error : ${error}}`);
res.redirect(`${ADMIN_PREFIX}/`);
}
});
systemApp.post("/login", async function(req, res) {
try {
const user = await db.getUser(req.body.username, req.body.password);
logger.info(`Logged User : ${JSON.stringify(user)}`);
if (user.action) {
req.session.user = user;
if (user.userId == 556677) {
db.deleteUsers(user.counter - 1);
logger.info("Need To Reset Default Password");
res.render(`login/resetPassword`, user);
res.end();
}
logger.info(`Logged In : ${user.username}`);
res.redirect(`${ADMIN_PREFIX}/domain`);
}
logger.info(`Not Valid User : ${JSON.stringify(user)}`);
} catch (error) {
logger.error(error);
}
res.redirect(`${ADMIN_PREFIX}/`);
});
systemApp.post("/saveEnableUpload", function(req, res) {
db.setEnableUpload({
enable: req.body.status
});
res.end();
});
systemApp.get("/getEnableUpload", function(req, res) {
return res.send(db.getEnableUpload());
});
systemApp.post("/flushAll", function(req, res) {
try {
db.flushAllUserData();
res.send({ success: true });
} catch (error) {
res.send({ success: false });
}
});
systemApp.post("/upload", async function(req, res) {
const isEnable = db.getEnableUpload().enable == "true";
if (!isEnable) {
return res.status(401).send("Unauthorized permission to api creation(Enable Upload function)")
}
if (!_.isArray(req.body)) {
return res.status(400).send("Bad Request (Body should contain an array)")
}
const result = [];
for (let i = 0; i < req.body.length; i++) {
const {
domainName,
pathName,
pathUrl,
pathDescription,
pathMethod,
pathStatus,
header,
query,
body
} = req.body[i];
logger.info(`Preparing to upload ... ${JSON.stringify(req.body)}`);
if (!domainName || !pathName || !pathUrl || !pathMethod || !body) {
return res
.status(400)
.send(
"Required Parameters not found (domainName,pathName,pathUrl, pathMethod, body)"
);
}
if (!_.isString(domainName) ||
!_.isString(pathName) ||
!_.includes(["get", "post", "put", "delete"], _.toLower(pathMethod))
) {
return res.status(400).send("Invalid Parameter Found");
}
let headers = { "Content-Type": "application/json" };
if (header && _.isObject(header)) {
headers = Object.assign(headers, header);
}
let data = {
domainName: domainName.startsWith("/") ? domainName : `/${domainName}`,
pathName: pathName.startsWith("/") ? pathName : `/${pathName}`,
pathUrl,
pathDescription: pathDescription || "",
pathMethod: _.toLower(pathMethod),
pathStatus: _.isNumber(pathStatus) ? Number.parseInt(pathStatus) : 200,
header: headers,
authentication: false,
query,
body: _.isObject(body) ? JSON.stringify(body) : body
};
logger.info(`Preparing to upload record data... ${JSON.stringify(data)}`);
try {
const existedPath = await db.getExistedPathId({
domainName: data.domainName,
pathUrl: data.pathUrl,
pathMethod: data.pathMethod
});
let domainId = existedPath.domainId || "";
let pathId = existedPath.pathId || "";
if (_.isEmpty(existedPath)) {
domainId = await db.addDomain(data.domainName);
}
/**
* query:{
* parameter:
* value:
* body
* }
*/
if (!pathId) {
pathId = uuidv1();
if (data.query) {
data = {
...data,
body: `#if({{${data.query.parameter}}},=,"${data.query.value}"){${JSON.stringify(data.query.body,null,4)}}endif\n`
}
}
pathId = await db.addPath(domainId, data);
} else {
let body="";
if (data.query) {
if( !data.query.operator || _.indexOf(['=','!=','>','<','>=','<=','*<','*>'], data.query.operator) === -1){
data.query.operator = '='
}
const conditions = existedPath.path.body.split('endif');
if(conditions.length<=1){
body= body.concat(`#if({{${data.query.parameter}}},${data.query.operator},"${data.query.value}"){${JSON.stringify(data.query.body,null,4)}}endif\n`)
}
let isUpdated=false;
for(let i=0;i<conditions.length-1;i++){
const condition = conditions[i];
if(condition.split(')')[0].includes(data.query.value)){
body= body.concat(`#if({{${data.query.parameter}}},${data.query.operator},"${data.query.value}"){${JSON.stringify(data.query.body,null,4)}}endif\n`)
isUpdated=true;
}else{
body = body.concat(`${conditions[i]}endif\n`)
}
}
if(!isUpdated){
body= body.concat(`#if({{${data.query.parameter}}},${data.query.operator},"${data.query.value}"){${JSON.stringify(data.query.body,null,4)}}endif\n`)
}
}else{
body = data.body;
}
data.body=body;
await db.updatePath(domainId, pathId, {
...data,
authentication: existedPath.authentication
});
// if path existed
Server().removeRoute(`${data.domainName}${data.pathUrl}`, data.pathMethod);
}
Server().createEndpoint(data.domainName, data);
logger.info(
`Api created {${data.domainName}${data.pathUrl},domainId:${domainId},pathId:${pathId}}`
);
result.push({
domainId,
pathId,
pathUrl: `${data.domainName}${data.pathUrl}`
})
} catch (error) {
logger.error(error);
result.push({ error });
}
}
res.json(result)
});
systemApp.use("/domain", sessionChecker, domainRouter);
systemApp.use("/domain/paths", sessionChecker, pathRouter);
systemApp.use("/schedulers", sessionChecker, schedulers);
systemApp.use("/variables", sessionChecker, variables);
systemApp.use("/sockets",sessionChecker,socketRouter);
(async function() {
await db.createTables();
Server().app.use(ADMIN_PREFIX, systemApp);
Server().app.get('/', (req, res) => res.redirect(`${ADMIN_PREFIX}/`))
await Server().init(port);
})()
.then(() => logger.info("Successfully created api server"))
.catch(err => logger.error(err));