-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
166 lines (122 loc) · 5.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
var fs = require('fs/promises');
var { readFileSync } = require('fs');
var http = require('http');
var path = require('path');
var mime = require('mime');
var { v5: uuid } = require('uuid');
var __PKG_META = [];
var sessions = [];
function createDownloadSession(id, time) {
var _id = uuid(time.toString() + id, uuid.URL);
sessions.push([id, _id]);
return _id;
}
function validateSession(id, session) {
if (sessions.find(e => e[0] == id && e[1] == session)) return true;
return false;
}
var server = http.createServer();
function returnInvalidRequest(res, msg, err) {
return res.writeHead(500, { 'x-xen-error': err || '', 'Access-Control-Allow-Origin': '*' }).end(msg);
}
async function loadMeta() {
var data = (await fs.readFile('./meta', 'utf-8')).split('\n').map(e => e.split('='));
return data;
}
async function createDownloadStream(id, response) {
try {
__PKG_META = await loadMeta();
console.log(id)
if (!id) return returnInvalidRequest(response, 'ID Must be provided in Request body. Download Aborted.');
var _path = (__PKG_META.find(e => e[0] == id) || [null, null])[1];
if (!_path) return returnInvalidRequest(response, 'Path Invalid or Not Found. Make Sure This Repository Exists. Download Aborted.');
var dir = await fs.readdir(path.join(__dirname, _path));
if (!dir.includes('manifest.json')) return returnInvalidRequest(response, 'Package Contains Invalid or Unreachable "manifest.json" File. Download Aborted.');
var meta = await fs.readFile(path.join(__dirname, _path, 'manifest.json'), 'utf-8');
try {
meta = JSON.parse(meta);
} catch (e) {
return returnInvalidRequest(response, 'Package Meta is Invalid or Blank. Download Aborted', e);
}
meta.date = new Date().getTime();
meta.id = id;
meta.session = createDownloadSession(id, meta.date);
return response.writeHead(200, { 'Access-Control-Allow-Origin': '*', 'content-type': 'application/json' }).end(JSON.stringify(meta));
} catch(e) {
return returnInvalidRequest(response, e.toString());
}
}
async function sendDownloadChunk(id, asset, response) {
try {
if (!id || !asset) return returnInvalidRequest(response, 'ID and Asset Must be provided in Request body. Download Aborted.');
var _path = (__PKG_META.find(e => e[0] == id) || [null, null])[1];
if (!_path) return returnInvalidRequest(response, 'Path Invalid or Not Found. Make Sure This Repository Exists. Download Aborted.');
var data = await fs.readFile(path.join(__dirname, _path, asset));
return response.writeHead(201, { 'Access-Control-Allow-Origin': '*', 'Content-Type': mime.getType(asset) }).end(data);
} catch(e) {
return returnInvalidRequest(response, e.toString());
}
}
async function clearDownloadSession(session, response) {
try {
if (!session) return returnInvalidRequest(response, 'Session Must be provided in Request body. Download Aborted.');
var index = sessions.findIndex(e => e[1] == session);
if (index == -1) return returnInvalidReponse(response, 'Session not found. Could be previously terminated.');
sessions.splice(index, 1);
return response.writeHead(200, { 'Access-Control-Allow-Origin': '*' }).end('true');
} catch(e) {
return returnInvalidRequest(response, e.toString());
}
}
async function sendPackageVersion(id, response) {
try {
__PKG_META = await loadMeta();
if (!id) return returnInvalidRequest(response, 'ID Must be provided in Request body. Download Aborted.');
var _path = (__PKG_META.find(e => e[0] == id) || [null, null])[1];
if (!_path) return returnInvalidRequest(response, 'Path Invalid or Not Found. Make Sure This Repository Exists. Download Aborted.');
var dir = await fs.readdir(path.join(__dirname, _path));
if (!dir.includes('manifest.json')) return returnInvalidRequest(response, 'Package Contains Invalid or Unreachable "manifest.json" File. Download Aborted.');
var meta = await fs.readFile(path.join(__dirname, _path, 'manifest.json'), 'utf-8');
try {
meta = JSON.parse(meta);
} catch (e) {
return returnInvalidRequest(response, 'Package Meta is Invalid or Blank. Download Aborted', e);
}
return response.writeHead(200, { 'Access-Control-Allow-Origin': '*', 'content-type': 'text/plain' }).end(meta.version);
} catch(e) {
return returnInvalidRequest(response, e.toString());
}
}
loadMeta().then(async (e) => {
__PKG_META = e;
server.on('request', function(req, res) {
var chunks = [];
var final = '';
req.on('data', (e) => chunks.push(e)).on('end', async () => {
final = Buffer.concat(chunks).toString(); if (final) try { final = JSON.parse(final); } catch (e) { console.log(e) };
if (req.url == '/ip') {
return res.end(req.headers['x-forwarded-for']?.split(',').shift() || req.socket?.remoteAddress);
}
if (req.url == '/') {
loadMeta().then(e => {
var obj = {};
e.forEach(a=>obj[a[0]]=readFileSync(path.join(__dirname, a[1], 'manifest.json')).toString());
return res.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify(Object.values(obj)));
});
}
if (req.method == 'POST' && req.url == '/stream') {
return await createDownloadStream(final.id || false, res)
}
if (req.method == 'POST' && req.url == '/version') {
return await sendPackageVersion(final.pkg || false, res)
}
if (req.method == 'POST' && req.url == '/download' && validateSession(final.id, final.session)) {
return await sendDownloadChunk(final.id || false, final.asset || false, res);
}
if (req.method == 'POST' && req.url == '/clear') {
return await clearDownloadSession(final.session || false, res);
}
})
});
server.listen(8080);
}).catch(e => console.log(e));