-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
165 lines (151 loc) · 3.89 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
var http = require("http");
var url = require("url");
var fs = require("fs");
var ws = require("ws").Server;
var list = {
list: ["temp"]
};
var linkedList = {
list: [{ temp: "http://google.com" }]
};
var wsaddress = "ws://159.65.185.219:3000";
if (process.argv.length > 0) {
wsaddress = "ws://localhost:3000";
}
function scan(nameOfFile, res, q) {
fs.readFile(nameOfFile, function(err, data) {
if (err) {
res.writeHead(404, { "Content-Type": "text/html" });
return res.end("404 Not Found");
}
if (q.pathname.toString().endsWith(".js")) {
res.writeHead(200, { "Content-Type": "text/javascript" });
res.write(data);
return res.end();
}
if (q.pathname.toString().endsWith(".css")) {
res.writeHead(200, { "Content-Type": "text/css" });
res.write(data);
return res.end();
}
if (q.pathname.toString().endsWith(".png")) {
res.writeHead(200, { "Content-Type": "image/png" });
res.write(data);
return res.end();
}
res.writeHead(200, { "Content-Type": "text/html" });
//res.write(data);
return res.end(data);
});
}
http
.createServer(function(req, res) {
var q = url.parse(req.url, true);
var filename = "." + q.pathname;
if(filename == "./") {
scan("./index.html", res, q);
return;
}
if (!q.pathname.includes(".")) {
var actual = q.pathname.substr(1);
if (!doesExist(actual)) {
res.writeHead(404, { "Content-Type": "text/html" });
return res.end("404 Not Found");
}
res.writeHead(302, { Location: getLink(actual) });
return res.end("Redirecting...");
}
if(q.pathname.toString().indexOf("../..") > -1) {
res.writeHead(403, {"Content-Type": "text/html"});
return res.end("403 Forbidden");
}
scan(filename, res, q);
})
.listen(80);
var server = new ws({
port: 3000
});
server.on("connection", function(ws) {
ws.on("message", function(msg) {
if (msg.toString().startsWith("--test ")) {
var cancel = false;
const id = msg.toLowerCase().split(" ");
if (id.length > 3) {
ws.send("-error TOO MANY ARGS");
return;
}
cancel = doesExist(id[1]);
if (cancel) {
ws.send("-error TAKEN");
return;
}
ws.send("-suc avail");
}
if (msg.toString() == "-get") {
console.log("-------------------------");
for (var i = 0; i < linkedList.list.length; i++) {
console.log(linkedList.list[i]);
}
console.log("-------------------------");
}
if (msg.toString().startsWith("--add ")) {
const addmsg = msg.toLowerCase().split(" ");
if (doesExist(addmsg[1])) {
ws.send("-error TAKEN");
return;
}
addToList(addmsg[1], addmsg[2]);
let week = 604800000;
setTimeout(remove, week, addmsg[1]);
ws.send("-suc added");
}
});
});
function remove(name) {
for (var i = 0; i < linkedList.list.length; i++) {
for (key in linkedList.list[i]) {
if (key === name) {
linkedList.list.splice(i, 1);
for (var i = 0; i < list.list.length; i++) {
if (list.list[i] === name) {
list.list.splice(i, 1);
}
}
}
}
}
}
function doesExist(name) {
var cancel = false;
for (var i = 0; i < list.list.length; i++) {
if (name === list.list[i]) {
cancel = true;
break;
}
}
return cancel;
}
function addToList(name, url) {
if (doesExist(name)) {
return;
}
list.list[list.list.length] = name;
linkedList.list[linkedList.list.length] = { [name]: `${url.toString()}` };
}
function getLink(name) {
if (!doesExist(name)) {
return;
}
var link = "";
for (var i = 0; i < linkedList.list.length; i++) {
for (key in linkedList.list[i]) {
if (key === name) {
link = linkedList.list[i]["" + name];
}
}
}
if (link === "") {
link = "";
}
return link;
}