This repository has been archived by the owner on Oct 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
213 lines (198 loc) · 7.64 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
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
const https = require('https');
const fs = require('fs');
const requestIp = require('request-ip');
const url = require('url');
const html_pages = require('./src/modules/html-pages');
const validator = require('validator');
const path = require('path');
const axios = require('axios');
const busboy = require('busboy');
const sharp = require("sharp");
const options = {
key: fs.readFileSync('ssl/apache2.key'),
cert: fs.readFileSync('ssl/apache2.cert')
};
const sitePath = './site'
https.createServer(options, function (req, res) {
const req_URL = new URL('https://hikk0o.dev' + req.url);
let req_url = req_URL.pathname
consoleLog(req, 'URL: ' + req_url)
let redirects
let req_path = req_URL.pathname.split('/')
req_path.shift()
// start REDIRECT
// const parts = url.parse(req.url,true);
// end REDIRECT
// start HTML
if (req_path[0] === '') {
const parts = url.parse(req.url,true);
try { redirects = JSON.parse(fs.readFileSync('./src/redirects.json', 'utf8')); } catch (e) {/* pass */}
if (parts.query['file'] !== undefined) {
fs.readFile('./download/' + parts.query['file'], null, function (error, data) {
if (error) {
html_pages.openHtml404(res)
} else {
res.end(data);
}
});
} else {
html_pages.openHtmlFile(res, req_url + 'index.html')
}
}
else if (req_path[0] === 'upload' && req.method === 'POST') {
try {
// console.log(req)
let body = ''
req.on('data', (data) => {
body += data
});
req.on('end', async () => {
// console.log(body)
if (!body.startsWith("data:image")) return res.end("WrongData")
const config = { headers: { 'Content-Type': 'application/json' } };
let data = body.replace(/^data:image\/png;base64,/, '');
let img = Buffer.from(data, 'base64');
await sharp(img)
.png({ quality: 85, compressionLevel: 3, progressive: true })
.toBuffer()
.then(async function(outputBuffer) {
let outputBody = await Buffer.from(outputBuffer).toString('base64')
await axios.post("https://hikk0o.dev:2096/api/redirect/img", outputBody, config).then(function (response) {
// console.log(response.data);
res.end(response.data)
}).catch(function (e) {
console.error("Error: ", e)
res.end("ServerError")
})
}).catch(function (e) {
console.error("Error: ", e)
res.end("ServerError")
});
// console.log(body)
});
} catch (e) {
consoleLog(req, e)
console.error(e)
res.end('Server error!')
}
}
else if (req_url.endsWith('favicon.ico')) {
res.end()
} else if (req_path[0] === 'short-url' && req.method === 'POST') {
try {
// console.log(req)
let body = ''
req.on('data', (data) => {
body += data
});
req.on('end', () => {
// console.log(body)
const config = { headers: { 'Content-Type': 'application/json' } };
axios.post("https://hikk0o.dev:2096/api/redirect/", body, config).then(function (response) {
// console.log(response.data);
res.end(response.data)
}).catch(function (e) {
consoleLog(req, e)
// console.error(e)
})
});
} catch (e) {
consoleLog(req, e)
console.error(e)
res.end('Server error!')
} } else if (req_url.endsWith('.html')) {
html_pages.openHtmlFile(res, req_url)
} else if (req_url.endsWith('.js') || req_url.endsWith('.css') || req_url.endsWith('.png')) {
let contentType = ''
if (req_url.endsWith('.js')) {
contentType = 'text/javascript'
}
if (req_url.endsWith('.css')) {
contentType = 'text/css'
}
if (req_url.endsWith('.png')) {
contentType = 'image/png'
}
fs.readFile(sitePath + req_url, null, function (error, html) {
if (error) {
html_pages.openHtml404(res)
} else {
res.writeHead(200, {
'Content-Type': contentType,
'Cache-Control': 'max-age=31536000'
});
res.write(html);
res.end();
}
});
} else if (req_path[0] === 's' && req.method === 'GET') {
console.log("Check redirect")
axios.get('https://hikk0o.dev:2096/api/redirect/' + req_path[1])
.then(response => {
console.log(response.data);
let pathUrl = response.data;
if (pathUrl === "NotFound") {
html_pages.openHtml404(res)
} else {
res.writeHead(301, {
Location: pathUrl
});
consoleLog(req, 'Redirect to ' + pathUrl)
res.end()
}
})
.catch(error => {
console.log(error);
})
} else if (req_path[0] === 'i' && req.method === 'GET') {
console.log("Check redirect")
axios.get('https://hikk0o.dev:2096/api/redirect/img/' + req_path[1])
.then(response => {
// console.log(response.data);
let imgData = response.data;
if (imgData === "NotFound") {
html_pages.openHtml404(res)
} else {
consoleLog(req, 'Open img')
let data = imgData.replace(/^data:image\/png;base64,/, '');
let img = Buffer.from(data, 'base64');
res.setHeader("Pragma", 'public');
res.setHeader("Cache-Control", 'max-age=25920000');
res.setHeader("Content-Type", 'image/png');
res.setHeader("Expires", new Date(Date.now() + 25920000000).toUTCString());
res.writeHead(200);
res.end(img)
// sharp(img)
// .png({ quality: 85, progressive: true, force: false })
// .toBuffer()
// .then(function(outputBuffer) {
// res.end(outputBuffer)
// });
}
})
.catch(error => {
console.log(error);
})
} else {
html_pages.openHtmlFile(res, req_path[0] + '.html')
}
// end HTML
}).listen(443, '0.0.0.0');
function consoleLog(req, msg) {
let now = new Date();
let time = '[' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds() + ']'
console.log(time + ' ' + requestIp.getClientIp(req) + ' ' + msg)
}
function generateUrl(redirects) {
let length = 6,
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
retVal = "";
for (let i = 0, n = charset.length; i < length; ++i) {
retVal += charset.charAt(Math.floor(Math.random() * n));
}
if (redirects[retVal] === undefined) {
return retVal;
} else {
return generateUrl(redirects)
}
}