Skip to content

Commit

Permalink
added protocol function to properly write http/https
Browse files Browse the repository at this point in the history
  • Loading branch information
TannerReynolds committed Mar 20, 2019
1 parent 5fe1e52 commit 8aff72d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 34 deletions.
8 changes: 8 additions & 0 deletions src/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ class ShareXAPI {
let currentYear = date.getFullYear()
return `${currentMonth}/${currentYear}`
}

protocol() {
if(this.c.secure) {
return "https"
} else {
return "http"
}
}
}

module.exports = ShareXAPI;
31 changes: 12 additions & 19 deletions src/server/routes/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ async function files(req, res) {
res.setHeader("Content-Type", "text/text")
let fileName = this.randomToken(6) // 56,800,235,584 possible file names
let form = new formidable.IncomingForm()
let protocol = this.protocol()
form.parse(req, (err, fields, files) => {
let userIP = req.headers["x-forwarded-for"] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress
let usingUploader = false
Expand Down Expand Up @@ -51,7 +52,7 @@ async function files(req, res) {
res.redirect("/upload?error=File_Too_Big")
return res.end()
} else {
res.write(`http://${req.headers.host}/ERR_FILE_TOO_BIG`)
res.write(`${protocol}://${req.headers.host}/ERR_FILE_TOO_BIG`)
return res.end()
}
} else {
Expand All @@ -73,18 +74,14 @@ async function files(req, res) {
})
})
}
if (this.monitorChannel !== null) this.bot.createMessage(this.monitorChannel, `\`\`\`MARKDOWN\n[NEW UPLOAD][ADMIN]\n[SIZE](${Math.round(files.fdata.size/1024)}KB)\n[TYPE](${files.fdata.type})\n[IP](${userIP})\`\`\`\nhttp://${req.headers.host}/${returnedFileName}`)
if (this.monitorChannel !== null) this.bot.createMessage(this.monitorChannel, `\`\`\`MARKDOWN\n[NEW UPLOAD][ADMIN]\n[SIZE](${Math.round(files.fdata.size/1024)}KB)\n[TYPE](${files.fdata.type})\n[IP](${userIP})\`\`\`\n${protocol}://${req.headers.host}/${returnedFileName}`)
if (err) return res.write(err)
this.log.verbose(`New File Upload: http://${req.headers.host}/${returnedFileName} | IP: ${userIP}`)
this.log.verbose(`New File Upload: ${protocol}://${req.headers.host}/${returnedFileName} | IP: ${userIP}`)
if(usingUploader === true) {
let insecure = `/upload?success=http://${req.headers.host}/${returnedFileName}`
let secure = `/upload?success=https://${req.headers.host}/${returnedFileName}`
res.redirect(this.c.secure ? secure : insecure)
res.redirect(`/upload?success=${protocol}://${req.headers.host}/${returnedFileName}`)
return res.end()
} else {
let insecure = `http://${req.headers.host}/${returnedFileName}`
let secure = `https://${req.headers.host}/${returnedFileName}`
res.write(this.c.secure ? secure : insecure)
res.write(`${protocol}://${req.headers.host}/${returnedFileName}`)
return res.end()
}
})
Expand All @@ -97,7 +94,7 @@ async function files(req, res) {
res.redirect("/upload?error=File_Too_Big")
return res.end()
} else {
res.write(`http://${req.headers.host}/ERR_FILE_TOO_BIG`)
res.write(`${protocol}://${req.headers.host}/ERR_FILE_TOO_BIG`)
return res.end()
}
} else {
Expand All @@ -108,7 +105,7 @@ async function files(req, res) {
res.redirect("/upload?error=Illegal_File_Type")
return res.end()
} else {
res.write(`http://${req.headers.host}/ERR_ILLEGAL_FILE_TYPE`)
res.write(`${protocol}://${req.headers.host}/ERR_ILLEGAL_FILE_TYPE`)
return res.end()
}
} else {
Expand All @@ -130,18 +127,14 @@ async function files(req, res) {
})
})
}
if (this.monitorChannel !== null) this.bot.createMessage(this.monitorChannel, `\`\`\`MARKDOWN\n[NEW UPLOAD][USER]\n[SIZE](${Math.round(files.fdata.size/1024)}KB)\n[TYPE](${files.fdata.type})\n[IP](${userIP})\n\`\`\`\nhttp://${req.headers.host}/${returnedFileName}`)
if (this.monitorChannel !== null) this.bot.createMessage(this.monitorChannel, `\`\`\`MARKDOWN\n[NEW UPLOAD][USER]\n[SIZE](${Math.round(files.fdata.size/1024)}KB)\n[TYPE](${files.fdata.type})\n[IP](${userIP})\n\`\`\`\n${protocol}://${req.headers.host}/${returnedFileName}`)
if (err) return res.write(err)
this.log.verbose(`New File Upload: http://${req.headers.host}/${returnedFileName} | IP: ${userIP}`)
this.log.verbose(`New File Upload: ${protocol}://${req.headers.host}/${returnedFileName} | IP: ${userIP}`)
if(usingUploader === true) {
let insecure = `/upload?success=http://${req.headers.host}/${returnedFileName}`
let secure = `/upload?success=https://${req.headers.host}/${returnedFileName}`
res.redirect(this.c.secure ? secure : insecure)
res.redirect(`/upload?success=${protocol}://${req.headers.host}/${returnedFileName}`)
return res.end()
} else {
let insecure = `http://${req.headers.host}/${returnedFileName}`
let secure = `https://${req.headers.host}/${returnedFileName}`
res.write(this.c.secure ? secure : insecure)
res.write(`${protocol}://${req.headers.host}/${returnedFileName}`)
return res.end()
}
})
Expand Down
3 changes: 2 additions & 1 deletion src/server/routes/gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ async function get(req, res) {
async function post(req, res) {
let userIP = req.headers["x-forwarded-for"] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress
res.setHeader("Content-Type", "text/html");
let protocol = this.protocol()
let password = this.c.admin.key
if (!this.auth(password, req.body.password, {public: false, admin: {key: this.c.admin.key}})) {
res.statusCode = 401
Expand All @@ -19,7 +20,7 @@ async function post(req, res) {
fs.readdir(`${__dirname}/../uploads`, (err, files) => {
files.forEach((file, idx, array) => {
if (file.toString().includes(".jpg") || file.toString().includes(".png") || file.toString().includes(".gif")) {
pics.push(`http://${req.headers.host}/${file.toString()}`);
pics.push(`${protocol}://${req.headers.host}/${file.toString()}`);
if (idx === array.length - 1) {
res.render("gallery", {
pictures: pics
Expand Down
7 changes: 3 additions & 4 deletions src/server/routes/paste.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ async function paste(req, res) {
res.setHeader("Content-Type", "text/text")
let fileName = this.randomToken(5) // 916,132,832 possible file names
let form = new formidable.IncomingForm()
let protocol = this.protocol()
form.parse(req, (err, fields, files) => {
let userIP = req.headers["x-forwarded-for"] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress
if (!this.auth(this.c.key, fields.key, this.c)) {
Expand All @@ -26,7 +27,7 @@ async function paste(req, res) {
if (Math.round((files.fdata.size / 1024) / 1000) > this.c.paste.max_upload_size) {
if (this.monitorChannel !== null) this.bot.createMessage(this.monitorChannel, `\`\`\`MARKDOWN\n[FAILED PASTE][USER]\n[FILE](${files.fdata.name})\n[SIZE](${Math.round(files.fdata.size/1024)}KB)\n[TYPE](${files.fdata.type})\n[IP](${userIP})\n\n[ERROR](ERR_FILE_TOO_BIG)\`\`\``)
res.statusCode = 413
res.write(`http://${req.headers.host}/ERR_FILE_TOO_BIG`)
res.write(`${protocol}://${req.headers.host}/ERR_FILE_TOO_BIG`)
return res.end()
} else {
fs.move(oldpath, newpath, err => {
Expand All @@ -45,9 +46,7 @@ async function paste(req, res) {
fs.unlink(newpath, err => {
if (err) return
});
let insecure = `http://${req.headers.host}/${fileName}`
let secure = `https://${req.headers.host}/${fileName}`
res.write(this.c.secure ? secure : insecure)
res.write(`${protocol}://${req.headers.host}/${fileName}`)
return res.end()
})
})
Expand Down
9 changes: 4 additions & 5 deletions src/server/routes/short.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ async function get(req, res) {
async function post(req, res) {
let userIP = req.headers["x-forwarded-for"] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress
res.setHeader("Content-Type", "text/text");
let protocol = this.protocol()
let fileName = this.randomToken(4)
if (req.body.URL == undefined || req.body.URL == "" || req.body.URL == null) {
res.redirect("/short?error=No URL Input");
return res.end();
}
let stream = fs.createWriteStream(`${__dirname}/../uploads/${fileName}.html`)
stream.once("open", fd => {
stream.write(`<meta http-equiv="refresh" content="0;URL='${req.body.URL}'" />`);
stream.write(`<meta ${protocol}-equiv="refresh" content="0;URL='${req.body.URL}'" />`);
stream.end();
if (this.monitorChannel !== null) this.bot.createMessage(this.monitorChannel, `\`\`\`MARKDOWN\n[NEW][SHORT URL]\n[URL](${req.body.URL})\n[NEW](${req.headers.host}/${fileName})\n[IP](${userIP})\n\`\`\``)
this.log.verbose(`New Short URL: http://${req.headers.host}/${fileName} | IP: ${userIP}`)
let insecure = `/short?success=http://${req.headers.host}/${fileName}`
let secure = `/short?success=https://${req.headers.host}/${fileName}`
res.redirect(req.secure ? secure : insecure)
this.log.verbose(`New Short URL: ${protocol}://${req.headers.host}/${fileName} | IP: ${userIP}`)
res.redirect(`/short?success=${protocol}://${req.headers.host}/${fileName}`)
this.db.get("files")
.push({path: `/${fileName}`, ip: userIP, views: 0})
.write();
Expand Down
9 changes: 4 additions & 5 deletions src/server/routes/shortener.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ async function shortener(req, res) {
let form = new formidable.IncomingForm()
form.parse(req, (err, fields, files) => {
let userIP = req.headers["x-forwarded-for"] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress
let protocol = this.protocol()
if (!this.auth(this.c.key, fields.key, this.c)) {
res.statusCode = 401
res.write("Unauthorized");
Expand All @@ -23,13 +24,11 @@ async function shortener(req, res) {
} else {
let stream = fs.createWriteStream(`${__dirname}/../uploads/${fileName}.html`)
stream.once("open", fd => {
stream.write(`<meta http-equiv="refresh" content="0; url=${url}" />`)
stream.write(`<meta ${protocol}-equiv="refresh" content="0; url=${url}" />`)
stream.end()
if (this.monitorChannel !== null) this.bot.createMessage(this.monitorChannel, `\`\`\`MARKDOWN\n[NEW][SHORT URL]\n[URL](${url})\n[NEW](${req.headers.host}/${fileName})\n[IP](${userIP})\n\`\`\``)
this.log.verbose(`New Short URL: http://${req.headers.host}/${fileName} | IP: ${userIP}`)
let insecure = `http://${req.headers.host}/${fileName}`
let secure = `https://${req.headers.host}/${fileName}`
res.write(this.c.secure ? secure : insecure)
this.log.verbose(`New Short URL: ${protocol}://${req.headers.host}/${fileName} | IP: ${userIP}`)
res.write(`${protocol}://${req.headers.host}/${fileName}`)
this.db.get("files")
.push({path: `/${fileName}`, ip: userIP, views: 0})
.write();
Expand Down

0 comments on commit 8aff72d

Please sign in to comment.