-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
195 lines (179 loc) · 5.28 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
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
import captureWebsite from "capture-website";
import express from "express";
import fs from "fs";
import request from "request";
import path from "path";
import {createRequire} from "module";
const require = createRequire(import.meta.url);
const config = require("./config.json");
const imageTypes = require("./image-types.json");
const privateServer = express();
function dir() {
return path.resolve(path.dirname(""));
}
const template = fs.readFileSync(dir() + "/template.html", "utf8");
var uuidCache = new Object;
const imageEndings = ["png", "jpg", "jpeg"]
async function getUuidFromUsername(username) {
if (!config.enableCaching) return username;
username = username.toLowerCase();
if (Object.keys(uuidCache).includes(username)) return uuidCache[username].uuid;
try {
var data = await doRequest("http://api.cosmetica.cc/get/userdata?user=" + username);
data = JSON.parse(data);
if (data.uuid) {
uuidCache[username] = new Object({
uuid: data.uuid,
timeout: setTimeout(function() {
delete uuidCache[username];
}, config.uuidCachePeriod * 60 * 1000)
});
return data.uuid;
} else {
uuidCache[username] = new Object({
uuid: false,
timeout: setTimeout(function() {
delete uuidCache[username];
}, config.uuidCachePeriod * 60 * 1000)
});
return false;
}
} catch (e) {
console.log(e);
uuidCache[username] = new Object({
uuid: false,
timeout: setTimeout(function() {
delete uuidCache[username];
}, config.uuidFailCachePeriod * 60 * 1000)
});
return false;
}
}
function doRequest(url) {
return new Promise(function (resolve, reject) {
request(url, function (error, res, body) {
if (!error && res.statusCode == 200) {
resolve(body);
} else {
reject(error);
}
});
});
}
privateServer.use("/static", express.static("static"));
privateServer.use("/access-control/*", async function(req, res) {
try {
request(req.params[0]).pipe(res);
} catch (e) {
res.status(404).send("Invalid url");
}
});
privateServer.get("/:subject/:imageType/:user", async function (req, res) {
var subject = req.params.subject;
var id = req.params.user;
var imageType = req.params.imageType;
if (!Object.keys(imageTypes).includes(subject) || !Object.keys(imageTypes[subject]).includes(imageType)) {
res.status(404).send("Invalid image type");
return;
}
var html = template;
html = html.split("IMAGE_TYPE").join(imageType);
html = html.split("IMAGE_SUBJECT").join(subject);
html = html.split("IMAGE_ID").join(id);
res.send(html);
});
const app = express();
app.get("/:subject/:imageType/:user", async function (req, res) {
var subject = req.params.subject;
var id = req.params.user.toLowerCase();
var imageType = req.params.imageType;
if (!Object.keys(imageTypes).includes(subject) || !Object.keys(imageTypes[subject]).includes(imageType)) {
res.status(404).send("Invalid image type");
return;
}
for (let i = 0; i < imageEndings.length; i++) {
if (id.endsWith("." + imageEndings[i])) {
id = id.substring(0, id.length - imageEndings[i].length - 1);
break;
}
}
if (subject == "player") {
id = await getUuidFromUsername(id);
if (!id) {
res.status(404).send("Invalid username or UUID");
return;
}
}
var fileType = imageTypes[subject][imageType].imageType;
if (config.enableCaching) {
var fileName = dir() + "/images/" + subject + "-" + imageType + "-" + id + "." + fileType;
try {
var stats = fs.statSync(fileName);
if (new Date().getTime() - stats.mtime < 1440 * 60 * 1000) {
res.sendFile(fileName);
return;
}
} catch {}
var didScreenshot = await screenshot(subject, id, imageType, fileType);
if (didScreenshot) {
res.sendFile(fileName);
return;
}
} else {
var didScreenshot = await screenshot(subject, id, imageType, fileType);
if (didScreenshot && didScreenshot.length) {
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': didScreenshot.length
});
res.end(didScreenshot);
return;
}
}
res.status(404).end();
});
app.get('/', function (req, res) {
res.send("Hello World");
});
app.listen(config.port, function() {
console.log("Started server on port " + config.port);
});
privateServer.listen(config.internalPort, function() {
console.log("Started internal server on port " + config.internalPort);
});
const puppeteerArgs = {
args: [
'--no-sandbox',
'--disable-setuid-sandbox'
]
};
async function screenshot(subject, id, imageType, fileType) {
var properties = imageTypes[subject][imageType];
var options = {
waitForElement: "#hider",
width: properties.width,
height: properties.height,
type: properties.imageType,
scaleFactor: 1,
launchOptions: puppeteerArgs,
defaultBackground: false,
timeout: 15
};
if (options.type == "jpeg") options.quality = properties.quality;
try {
if (config.enableCaching) {
var fileName = dir() + "/images/" + subject + "-" + imageType + "-" + id + "." + properties.imageType;
try {
fs.unlinkSync(fileName);
} catch {}
await captureWebsite.file("http://127.0.0.1:" + config.internalPort + "/" + subject + "/" + imageType + "/" + id, fileName, options);
return true;
} else {
var shot = await captureWebsite.buffer("http://127.0.0.1:" + config.internalPort + "/" + subject + "/" + imageType + "/" + id, options);
return shot;
}
} catch (e) {
console.log(e);
return false;
}
}