|
|
|
@ -1,5 +1,4 @@
|
|
|
|
|
require("dotenv").config();
|
|
|
|
|
process.env.NODE_ENV = "production";
|
|
|
|
|
var colors = require("colors");
|
|
|
|
|
var express = require("express");
|
|
|
|
|
var formidable = require("formidable");
|
|
|
|
@ -14,7 +13,10 @@ if (process.env.HOSTNAMES) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var app = express();
|
|
|
|
|
app.enable('trust proxy', '127.0.0.1');
|
|
|
|
|
app.set("env", "production");
|
|
|
|
|
app.set('trust proxy', '127.0.0.1');
|
|
|
|
|
app.listen(process.env.PORT || 8568, process.env.ADDRESS);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.use((req, res, next)=>{
|
|
|
|
|
var d = new Date;
|
|
|
|
@ -25,53 +27,56 @@ app.use((req, res, next)=>{
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.post("/upload", (req, res, next) => {
|
|
|
|
|
if (req.headers.authentication != process.env.AUTH_TOKEN) return res.status(403).send("Unauthorized");
|
|
|
|
|
|
|
|
|
|
app.post("*", (req, res, next) => {
|
|
|
|
|
if (req.headers.authentication != process.env.AUTH_TOKEN) return void res.status(403).send("Unauthorized");
|
|
|
|
|
var form = new formidable.IncomingForm({
|
|
|
|
|
maxFileSize: 2**30, // 1 GiB
|
|
|
|
|
maxFields: 1,
|
|
|
|
|
uploadDir: process.env.TMP_DIR
|
|
|
|
|
});
|
|
|
|
|
form.parse(req, function(err, fields, files) {
|
|
|
|
|
if (err) return next(err);
|
|
|
|
|
if (err) return void next(err);
|
|
|
|
|
var file = files.file;
|
|
|
|
|
if (!file) return res.sendStatus(400);
|
|
|
|
|
do {
|
|
|
|
|
if (!file) return void res.sendStatus(400);
|
|
|
|
|
(function trymkdir() {
|
|
|
|
|
var filecode = Math.random().toString(36).slice(2).substring(0,4);
|
|
|
|
|
} while ( fs.existsSync(path.join(FILES_DIR, filecode)) && !console.log("oof") );
|
|
|
|
|
try {
|
|
|
|
|
fs.mkdirSync(path.join(FILES_DIR, filecode));
|
|
|
|
|
fs.renameSync(file.path, path.join(FILES_DIR, filecode, file.name));
|
|
|
|
|
} catch(e) {
|
|
|
|
|
return next(e);
|
|
|
|
|
}
|
|
|
|
|
let url = `${req.protocol}://${filecode}.${randomHostname?.() || req.hostname}`;
|
|
|
|
|
res.send(url);
|
|
|
|
|
require("./discord-preloader.js")(url);
|
|
|
|
|
var webroot = path.join(FILES_DIR, filecode);
|
|
|
|
|
fs.mkdir(webroot, function (error) {
|
|
|
|
|
if (error) if (error.code == "EEXIST") {
|
|
|
|
|
console.log("oof");
|
|
|
|
|
trymkdir();
|
|
|
|
|
} else next(error);
|
|
|
|
|
else {
|
|
|
|
|
fs.rename(file.path, path.join(webroot, file.name), function (error) {
|
|
|
|
|
if (error) return void next(error);
|
|
|
|
|
let url = `${req.protocol}://${filecode}.${randomHostname?.() || req.hostname}`;
|
|
|
|
|
res.send(url);
|
|
|
|
|
require("./discord-preloader.js")(url);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
})();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.get("*", function(req, res, next){
|
|
|
|
|
let filecode = req.hostname.split('.')[0]; // for home page just create file in directory named as the last level of base hostname
|
|
|
|
|
let webroot = path.join(FILES_DIR, filecode);
|
|
|
|
|
let webrootdirlist = fs.readdirSync(webroot);
|
|
|
|
|
if (webrootdirlist.length > 1) {
|
|
|
|
|
req.url = path.join(filecode, req.url);
|
|
|
|
|
next();
|
|
|
|
|
} else if (webrootdirlist.length == 1) {
|
|
|
|
|
res.sendFile(webrootdirlist[0], {
|
|
|
|
|
root: path.join(process.cwd(), webroot),
|
|
|
|
|
headers: {"Content-Disposition": `filename=${webrootdirlist[0]}`}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.sendStatus(204);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
var filecode = req.hostname.split('.')[0]; // for webroot of main domain i.e. qonq.gq, mkdir files/qonq
|
|
|
|
|
var webroot = path.join(FILES_DIR, filecode);
|
|
|
|
|
fs.readdir(webroot, function(error, webrootdirlist) {
|
|
|
|
|
if (error) return void next(error.code == "ENOENT" ? "route" : error);
|
|
|
|
|
if (webrootdirlist.length > 1) {
|
|
|
|
|
req.url = path.join(filecode, req.url);
|
|
|
|
|
next();
|
|
|
|
|
} else if (webrootdirlist.length == 1) {
|
|
|
|
|
res.sendFile(webrootdirlist[0], {
|
|
|
|
|
root: path.join(process.cwd(), webroot),
|
|
|
|
|
headers: {"Content-Disposition": `filename=${webrootdirlist[0]}`}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.sendStatus(204);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}, express.static(FILES_DIR), serveIndex(FILES_DIR, {icons: true}));
|
|
|
|
|
|
|
|
|
|
app.get("*", express.static(FILES_DIR), serveIndex(FILES_DIR, {icons: true}));
|
|
|
|
|
|
|
|
|
|
app.use(function (error, req, res, next) {
|
|
|
|
|
res.status(error.code == "ENOENT" ? 404 : console.error(error.stack) || 500).type("text/plain").send(error.toString());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.listen(process.env.PORT || 8568, process.env.ADDRESS);
|
|
|
|
|