-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
155 lines (136 loc) · 3.25 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const http = require('http');
const fs = require('fs');
const url = require("url");
const open = require('open');
const file = require("./module/file_management")
const git = require("./module/git")
const {
parse
} = require('querystring');
function retFail(res) {
res.writeHead(404)
res.end()
}
function retJson(res, data) {
try {
res.writeHead(200, {
'Content-Type': 'application/json'
});
res.write(JSON.stringify(data));
res.end();
} catch (error) {
retFail(res)
}
}
function retText(res, data) {
try {
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.write(data);
res.end();
} catch (error) {
retFail(res)
}
}
let router = []
function handle(req, res) {
if (req.url == "/") {
fs.readFile('static/index.html', function (err, data) {
retText(res, data)
});
return
}
for (r of router) {
if (r.test(req.url)) {
r.func(req, res, url.parse(req.url, true).query)
return
}
}
// handle static resource
fs.readFile("static" + req.url, function (err, data) {
let file_type = /.*\.(\w+$)/.exec(req.url)[1]
let content_type = "text/html"
if (file_type == "js") {
content_type = "text/javascript"
} else if (file_type == "css") {
content_type = "text/css"
} else if (file_type == "ico") {
content_type = "image/vnd.microsoft.icon"
}
try {
res.writeHead(200, {
'Content-Type': content_type
});
res.write(data);
res.end();
} catch (error) {
retFail(res)
}
});
}
function regPath() {
function register(path_func) {
router.push({
test: (url) => /.*\/([^?]*)/.exec(url)[1] == path_func[0],
func: path_func[1]
})
}
let pairs = [
["listfile", (req, res, q) => retJson(res, file.allFiles())],
["decryptT", (req, res, q) => retText(res, file.decryptFile(q.name))],
["decryptJ", (req, res, q) => retJson(res, file.decryptFile(q.name))],
["keyexist", (req, res, q) => retJson(res, file.keyExist())],
["recoverkey", (req, res, q) => {
try {
file.recoverKey(q.pass)
return retJson(res, true);
} catch (error) {
return retJson(res, false);
}
}],
["genkey", (req, res, q) => {
file.genKey(q.pass)
//remove all encrypted files with other key
file.allFiles().forEach((x) => {
file.deleteFile(x)
})
return retJson(res, true);
}],
["keyrecovered", (req, res, q) => retJson(res, file.keyRecovered())],
["savefile", (req, res, q) => {
let body = "";
req.on('data', chunk => {
body += chunk.toString();
});
req.on("end", () => {
d = JSON.parse(body);
file.encryptFile(d.name, d.content)
retJson(res, true);
})
}],
["delete", (req, res, q) => {
file.deleteFile(q.name);
return retJson(res, true);
}],
["push", (req, res, q) => {
git.push()
return retJson(res, true);
}],
["pull",
(req, res, q) => {
git.pull()
return retJson(res, true);
}
],
]
for (pf of pairs) {
register(pf)
}
}
open('http://localhost:4321');
http.createServer(function (req, res) {
file.mkDirIfNeeded()
regPath()
handle(req, res)
}).listen(4321);