-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.js
218 lines (173 loc) · 6.12 KB
/
main.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
214
215
216
217
218
const { createCanvas, registerFont } = require("canvas");
const { Canvas } = require("./canvas.js");
const http = require('http');
const escape = require('escape-html');
const process = require('process');
const fs = require('fs');
const crypto = require('crypto');
var server_config;
try {
server_config = require('./server_config.json');
} catch (e) {
server_config = {
hostname: null || process.env.GSAPI_HOSTNAME,
ssl: false || (process.env.GSAPI_SSL=='1'?true:false)
}
}
const APP_VER = '2.0';
let CLUSTER_ID = 1;
let currentTasks = 0;
// webp-converter対策
if (!fs.existsSync('node_modules/webp-converter/temp')) {
fs.mkdirSync('node_modules/webp-converter/temp');
}
/* globals Canvas */
var _canvas = null;
registerFont('./notobk-subset.otf', {family: 'notobk'});
registerFont('./notoserifbk-subset.otf', {family: 'notoserifbk'});
function toArrayBuffer(buffer) {
var ab = new ArrayBuffer(buffer.length);
var view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) {
view[i] = buffer[i];
}
return ab;
}
if (process.argv.length > 2) {
if (!isNaN(process.argv[2])) {
CLUSTER_ID = Number(process.argv[2]);
}
}
if (process.env.CLID) CLUSTER_ID = Number(process.env.CLID);
const PORT = process.env.PORT || 8080;
console.log(`
*** 5000choyen API Server v${APP_VER} ***
(C)2021 CyberRex
Hostname: ${server_config.hostname}
Listening on 127.0.0.1 port ${PORT}
URL: ${server_config.ssl?'https':'http'}://${server_config.hostname?server_config.hostname:'127.0.0.1'}${PORT==80?'':':'+PORT}/
`);
http.createServer(function (req, resp) {
var hostname = server_config.hostname || req.host;
if (req.url == '/' || req.url == '/index.html') {
resp.writeHead(200, {'Content-type': 'text/html'});
resp.end('<!DOCTYPE html><head><meta charset="utf-8"><title>5000兆円ほしい!</title></head><body><h1>5000兆円ほしい!</h1><h2>使い方</h2><p>例: ' + (server_config.ssl ? 'https':'http') + '://' + hostname + '/image?top=上部文字列&bottom=下部文字列</p><br><p>(C)2021 CyberRex<br>Under GPL3.0 License</p><br><small>Runtime v'+APP_VER+'<br>Cluster#'+CLUSTER_ID+'</small></body></html>');
return;
}
if (req.url.match(/^\/image\?/)) {
if (currentTasks > 16) {
resp.writeHead(429, {'Content-type': 'text/html;charset=utf-8'});
resp.end('<h1>Too many requests</h1>');
return;
}
var queryString = req.url.split('?')[1];
var args = [...new URLSearchParams(queryString).entries()].reduce((obj, e) => ({...obj, [e[0]]: e[1]}), {});
var hoshii = false;
var noalpha = false;
var rainbow = false;
var imgtype = 'png';
var single = false;
var debug = false;
if (args.hoshii) {
hoshii = args.hoshii=='true' ? true : false;
}
if (args.noalpha) {
noalpha = args.noalpha=='true' ? true : false;
}
if (args.rainbow) {
rainbow = args.rainbow=='true' ? true : false;
}
if (args.single) {
single = args.single=='true' ? true : false;
}
if (args.debug) {
debug = args.debug=='true' ? true : false;
}
if (!args.top && (!single || (single && !args.bottom))) {
resp.writeHead(400, {'Content-type': 'text/html;charset=utf-8'});
resp.end('<h1>Bad Request</h1>パラメータtopが不足しています。');
return;
}
if (!args.bottom && !hoshii && (!single || (single && !args.top))) {
resp.writeHead(400, {'Content-type': 'text/html;charset=utf-8'});
resp.end('<h1>Bad Request</h1>パラメータbottomが不足しています。');
return;
}
if (args.type) {
if (args.type === 'jpg') {
args.type = 'jpeg';
}
if (args.type != 'png' && args.type != 'jpeg' && args.type != 'webp') {
resp.writeHead(400, {'Content-type': 'text/html;charset=utf-8'});
resp.end('<h1>Bad Request</h1>パラメータtypeの値が異常です。');
return;
}
imgtype = args.type;
}
if (single) {
if (args.top && args.bottom) {
resp.writeHead(400, {'Content-type': 'text/html;charset=utf-8'});
resp.end('<h1>Bad Request</h1>パラメータtopとbottomは同時に指定できません');
return;
}
single = true;
}
const sha1sum = crypto.createHash('sha1');
sha1sum.update(JSON.stringify(args));
const cachefname = sha1sum.digest('hex');
const cachename = '/tmp/5000_'+cachefname+'.'+imgtype;
let fileExist = false;
try {
const cachefile = fs.statSync(cachename);
fileExist = true;
} catch(e) {
fileExist = false;
}
if (fileExist) {
const data = fs.readFileSync(cachename);
resp.writeHead(200, {'Content-type': 'image/'+imgtype});
resp.write(data);
resp.end();
return;
}
if (!args.top) args.top = '';
if (!args.bottom) args.bottom = '';
// vulnerability countermeasures
if (args.top.length > 50 || args.bottom.length > 50) {
resp.writeHead(400, {'Content-type': 'text/html;charset=UTF-8'});
resp.write('<h1>Bad Request</h1>');
resp.end();
return;
}
currentTasks++;
const canvas = new Canvas(createCanvas(3840,1080), {hoshii: hoshii, noalpha: noalpha, single: single, debug: debug});
if (!single) {
canvas.redrawTop(args.top, rainbow);
if (! hoshii) {
canvas.redrawBottom(args.bottom, null, rainbow);
} else {
canvas.redrawImage();
}
}else{
if (args.top) {
canvas.redrawTop(args.top, rainbow);
}else{
canvas.redrawBottom(args.bottom, null, rainbow);
}
}
resp.writeHead(200, {'Content-type': 'image/'+imgtype});
canvas.createBuffer(imgtype, function (data) {
resp.write(data);
resp.end();
fs.writeFileSync(cachename, data); // save cache
currentTasks--;
}, args.q);
return;
} else if (req.url=='/image') {
resp.writeHead(200, {'Content-type': 'text/html;charset=utf-8'});
resp.end('<h1>Invalid Request</h1>');
return;
}
resp.writeHead(404, {'Content-type': 'text/html'});
resp.end('<h1>Not Found</h1><p>The requested URL '+escape(req.url)+' was not found on this server.</p>');
}).listen(PORT);