-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
212 lines (195 loc) · 6.51 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const express = require('express');
const fs = require('fs');
const path = require('path');
var config = require(path.resolve(__dirname,'config.js'));
const request = require('request');
const mysql = require('mysql2');
const app = express();
app.use('/js/control/monaco',express.static('./node_modules/monaco-editor'));
app.use("/",express.static('./public'));
var update = false;
var has_cache = false;
var cache = null;
app.get("/list",async (req,res)=>{
res.json({result:true,data:[]});
return;
if(!update && has_cache) {
console.log("cache");
res.send(cache);
return;
}
has_cache = false;
var connection = null;
try {
connection = mysql.createConnection(config.connection);
var data = await new Promise((resolve,reject)=>{
connection.query(`
SELECT * FROM comments;
`,function(err,results,fields) {
//console.log(results);
connection.close();
resolve(results);
});
});
has_cache = true;
cache = JSON.stringify({result:true,data:data});
res.send(cache);
} catch(e) {
if(connection) connection.close();
console.log(e);
res.json({result:false});
}
});
if(config.ping) {
app.post("/ping",(req,res)=>{
res.json({result:true});
});
}
app.post("/send",(req,res)=>{
function convert_date(dt) {
return dt.toISOString();
}
function genId(schema,size) {
if(!schema) {
schema = "";
}
var code = 'xxxxxxxxxxxxxxxx';
if(size) {
var sb = [];
for(var x = 0; x < size;x++) {
sb.push("x");
}
code = sb.join("");
}
return schema+(code).replace(/[x]/g, function(c) {
var r = Math.random() * 16 | 0;
if( c == 'x' ) {
return r.toString(16);
} else {
return c;
}
});
}
function validString(str) {
if(Object.prototype.toString.apply(str)!="[object String]" || str.length == 0 || str===null || str===undefined) {
if(str===null || str ===undefined || str.length ==0) return `''`;
throw new Error("invalid string");
}
var close = false;
if(str.charAt(0) != '\'') close = true;
else {
if(str.charAt(str.length-1) == '\'') str = str.substring(1,str.length-1);
else str = str.substring(1,str.length);
}
str = str.split("'").join("\\'");
return '\'' + str + '\'';
}
var body = [];
var total = 0;
var error = false;
console.log("/SEND",req.query);
req.on("data",(chunk)=>{
body.push(chunk);
total += chunk.length;
if(total > 500000000) { // ~ 1min 30s de gravação
req.connection.destroy();
error = true;
}
});
req.on("end",async ()=>{
var connection = null;
if(error) {
return;
}
if(body.length>0) {
try {
var size = 0;
for(var x = 0; x < body.length;x++) {
size += body[x].length;
}
var c = new Int8Array(size);
var offset = 0;
for(var x = 0; x < body.length;x++) {
if(x==0)
c.set(body[x])
else
c.set(body[x],offset);
offset += body[x].length;
}
var buffer = Buffer.from(c);
var id = genId("msg");
var filename = id;
while(fs.existsSync("./public/messages/user1/"+id+".ncp")) {
id = genId("msg");
}
console.log("STORING DATA");
connection = mysql.createConnection(config.connection);
var maxid = await new Promise((resolve,reject)=>{
connection.query(
`
SELECT MAX(id) FROM comments;
`,function(err, results, fields) {
if(err) {
//console.log(err);
reject( 0 );
}
resolve( results[0]['MAX(id)'] );
});
});
if(maxid == null) maxid = 0;
//console.log("MAXID",maxid);
num = {
id : maxid+1,
user : 1,
state : 1,
mode : 1
};
text = {
date : convert_date(new Date()),
comment : "",
file : id
};
for(var key in text) {
text[key] = validString(text[key]);
}
var data = await new Promise((resolve,reject)=>{
connection.query(`
INSERT INTO \`comments\`(\`id\`,\`user\`,\`mode\`,\`state\`,\`file\`,\`date\`,\`comment\`)
VALUES
( ${num.id}, ${num.user}, ${num.mode}, ${num.state}, ${text.file}, ${text.date}, ${text.comment} )
`,function(err,results,fields) {
//console.log(results);
connection.close();
resolve(results);
});
});
fs.writeFileSync("./public/messages/user1/"+id+".ncp",buffer,"binary");
// ncp stands for number cooler product
console.log("DATA STORED");
res.json({result:true});
update = true;
return;
} catch(e) {
if(connection) connection.close();
console.log(e);
res.json({result:false});
return;
}
}
res.json({result:false});
});
});
const https = require('https');
const privateKey = fs.readFileSync('server.key', 'utf8');
const certificate = fs.readFileSync('server.cert', 'utf8');
const credentials = {key: privateKey, cert: certificate};
var httpsServer = https.createServer(credentials, app);
var port = 83;
/*
httpsServer.listen(port,"0.0.0.0",()=>{
console.log("listening " + port);
});
*/
httpsServer.listen(port,"::",()=>{
console.log("listening " + port);
});