forked from linuxserver/emulatorjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.js
145 lines (141 loc) · 4.89 KB
/
profile.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
// NPM modules
var crypto = require('crypto');
var home = require('os').homedir();
var express = require('express');
var app = express();
var fs = require('fs');
var fsw = require('fs').promises;
var path = require('path');
var JSZip = require('jszip');
// Default vars
var error = {status: 'error'};
app.use(express.json({ limit: '500MB' }));
// Catch all to detect endpoint
app.get('/*', function(req, res) {
res.send('pong');
});
// Catch all for any post
app.post('/*', async function(req, res) {
try {
let type = req.body.type;
// Send default profile unauthenticated
if (type == 'default') {
try {
let profilePath = home + '/profile/default/';
let zip = new JSZip();
let items = await fs.readdirSync(profilePath);
async function addToZip(item) {
if (fs.lstatSync(item).isDirectory()) {
let items = await fs.readdirSync(item);
if (items.length > 0) {
for await (let subPath of items) {
await addToZip(item + '/' + subPath);
}
}
} else {
let data = fs.readFileSync(item);
let zipPath = item.replace(profilePath,'');
zip.file(zipPath, data);
}
return;
}
for await (let item of items) {
await addToZip(profilePath + item);
}
zip.generateAsync({type:"base64"}).then(function callback(base64) {
res.json({status: 'success',data: base64});
});
} catch (e) {
console.log(e);
res.json(error);
}
} else {
let auth = req.body.user + req.body.pass;
// Simple hash auth
let hash = crypto.createHash('sha256').update(auth).digest('hex');
let profileJson = await fsw.readFile(home + '/profile/profile.json', 'utf8');
let profile = JSON.parse(profileJson);
if (profile.hasOwnProperty(hash)) {
// Return username if found
if (type == 'login') {
res.json({status: 'success', user: profile[hash].username});
// Take client data and write it to profile
} else if (type == 'push') {
let profilePath = home + '/profile/' + profile[hash].username + '/';
let baseData = req.body.data;
// Purge current storage
let items = await fsw.readdir(profilePath);
if (items.length > 0) {
for await (let item of items) {
var filePath = profilePath + item;
if (fs.statSync(filePath).isFile()) {
await fsw.rm(filePath);
} else {
await fsw.rm(filePath, { recursive: true, force: true });
}
}
}
// Load zip from data
let zip = new JSZip();
zip.loadAsync(baseData, {base64: true}).then(async function(contents) {
// Unzip the files to the FS by name
for await (let fileName of Object.keys(contents.files)) {
if (fileName.endsWith('/')) {
if (! fs.existsSync(profilePath + fileName)) {
await fsw.mkdir(profilePath + fileName);
}
}
}
for await (let fileName of Object.keys(contents.files)) {
if (! fileName.endsWith('/')) {
zip.file(fileName).async('arraybuffer').then(async function(content) {
await fsw.writeFile(profilePath + fileName, Buffer.from(content));
});
}
}
});
res.json({status: 'success',user: profile[hash].username});
// Send client data to write to indexedDB
} else if (type == 'pull') {
try {
let profilePath = home + '/profile/' + profile[hash].username + '/';
let zip = new JSZip();
let items = await fs.readdirSync(profilePath);
async function addToZip(item) {
if (fs.lstatSync(item).isDirectory()) {
let items = await fs.readdirSync(item);
if (items.length > 0) {
for await (let subPath of items) {
await addToZip(item + '/' + subPath);
}
}
} else {
let data = fs.readFileSync(item);
let zipPath = item.replace(profilePath,'');
zip.file(zipPath, data);
}
return;
}
for await (let item of items) {
await addToZip(profilePath + item);
}
zip.generateAsync({type:"base64"}).then(function callback(base64) {
res.json({status: 'success',data: base64});
});
} catch (e) {
console.log(e);
res.json(error);
}
} else {
res.json(error);
}
} else {
res.json(error);
}
}
} catch (e) {
console.log(e)
res.json(error);
}
});
app.listen(3001);