fix error + format

This commit is contained in:
2025-04-29 00:29:00 +02:00
parent 3307f89d7d
commit 99f1890257
63 changed files with 9380 additions and 7015 deletions
+15 -20
View File
@@ -1,12 +1,9 @@
import crypto from "crypto";
import crypto from 'crypto'
let SHA256_cache = {}
function _SHA256(str) {
return crypto
.createHash("sha256")
.update(str)
.digest("base64");
return crypto.createHash('sha256').update(str).digest('base64')
}
/**
@@ -17,25 +14,23 @@ function _SHA256(str) {
* @returns {string} base64 digested hash
*/
function SHA256(str, salt, num) {
if (!num && num !== 0)
num = 1;
if (!str)
return;
let identifier = _SHA256(str+salt+num.toString())
if(SHA256_cache[identifier] != undefined) {
return SHA256_cache[identifier];
if (!num && num !== 0) num = 1
if (!str) return
let identifier = _SHA256(str + salt + num.toString())
if (SHA256_cache[identifier] != undefined) {
return SHA256_cache[identifier]
}
let ret = str;
let ret = str
for (let i = 0; i < num; i++) {
ret = _SHA256(ret + salt)
}
SHA256_cache[identifier] = ret;
setTimeout(()=>{
SHA256_cache[identifier] = ret
setTimeout(() => {
SHA256_cache[identifier] = undefined
},10000) //cache for 10s
return ret;
}, 10000) //cache for 10s
return ret
}
export { SHA256 };
export { SHA256 }
export default {
SHA256: SHA256
};
SHA256: SHA256,
}
+11 -15
View File
@@ -1,4 +1,4 @@
import {mkdir} from "fs"
import { mkdir } from 'fs'
/**
* makes sure that a given folder exists, if it doesn't it creates one for you
@@ -7,23 +7,19 @@ import {mkdir} from "fs"
* @param {Function} cb callback, gives null if the folder exists, otherwise gives the error
* @return {undefined} see: callback
*/
function ensureExists(path, mask, cb) {
if (typeof mask === 'function') { // Allow the `mask` parameter to be optional
cb = mask;
mask = 0o744;
function ensureExists(path, mask, cb) {
if (typeof mask === 'function') {
// Allow the `mask` parameter to be optional
cb = mask
mask = 0o744
}
mkdir(path, mask, function (err) {
if (err) {
if (err.code === 'EEXIST')
cb(null); // Ignore the error if the folder already exists
else
cb(err); // Something else went wrong
}
else
cb(null); // Successfully created folder
});
cb(null) // Ignore the error if the folder already exists
else cb(err) // Something else went wrong
} else cb(null) // Successfully created folder
})
}
export {
ensureExists
} ;
export { ensureExists }
+10 -7
View File
@@ -1,14 +1,17 @@
import fs from "fs";
const config = JSON.parse(fs.readFileSync("server_config.json"));
import fs from 'fs'
const config = JSON.parse(fs.readFileSync('server_config.json'))
/**
* gets ip of a request
* @param {request} req
* @returns ip of the given request, after taking preferred headers into account
*/
function getIP(req) {
let ip = req.socket.remoteAddress;
if (req.headers[config.preferred_ip_header] !== undefined && ip === config.only_prefer_when_ip)
ip = req.headers[config.preferred_ip_header];
return ip;
let ip = req.socket.remoteAddress
if (
req.headers[config.preferred_ip_header] !== undefined &&
ip === config.only_prefer_when_ip
)
ip = req.headers[config.preferred_ip_header]
return ip
}
export default getIP;
export default getIP
+14 -6
View File
@@ -1,7 +1,15 @@
<ul class="navbar noselect">
<li><a href="/">Home</a></li>
<li><a href="/user" id="hide_user">Profile</a></li>
<li><a href="/posts" id="hide_posts">Posts</a></li>
<li><a href="/dms" id="hide_dms">DMs</a></li>
<li class="right"><a href="/settings" id="hide_settings" class="less_padding"><img src="/images/settings_min.png" width=30 height=30 alt="settings"></a></li>
</ul>
<li><a href="/">Home</a></li>
<li><a href="/user" id="hide_user">Profile</a></li>
<li><a href="/posts" id="hide_posts">Posts</a></li>
<li><a href="/dms" id="hide_dms">DMs</a></li>
<li class="right">
<a href="/settings" id="hide_settings" class="less_padding"
><img
src="/images/settings_min.png"
width="30"
height="30"
alt="settings"
/></a>
</li>
</ul>
+23 -24
View File
@@ -1,7 +1,7 @@
import * as signature from "cookie-signature";
import fs from "fs";
import getIP from "./getip.js";
const cookiesecret = fs.readFileSync("cookiesecret.txt").toString();
import * as signature from 'cookie-signature'
import fs from 'fs'
import getIP from './getip.js'
const cookiesecret = fs.readFileSync('cookiesecret.txt').toString()
/**
* usignes a string
* @param {string} text text to unsign
@@ -10,14 +10,14 @@ const cookiesecret = fs.readFileSync("cookiesecret.txt").toString();
* @return {string/boolean} unsigned text, or if unsigning was unsuccessful, false
*/
function unsign(text, req, res) {
let ip = getIP(req);
let unsigned = signature.unsign(text, cookiesecret + ip);
let ip = getIP(req)
let unsigned = signature.unsign(text, cookiesecret + ip)
if (!unsigned) {
unsigned = signature.unsign(text, cookiesecret); //unsafe login?
if(!unsigned)return false;
unsigned = signature.unsign(text, cookiesecret) //unsafe login?
if (!unsigned) return false
return unsigned
}
return unsigned;
return unsigned
}
/**
* unsignes the auth cookie of a request, also sends json response if auth cookie was invalid
@@ -26,26 +26,25 @@ function unsign(text, req, res) {
* @return {string/boolean} unsigned cookie, or if unsigning was unsuccessful, false
*/
function getunsigned(req, res) {
let cookie = req.cookies.AUTH_COOKIE;
let cookie = req.cookies.AUTH_COOKIE
if (!cookie) {
res.status(403);
res.json({ "error": "you are not logged in! (no cookie)" });
return;
res.status(403)
res.json({ error: 'you are not logged in! (no cookie)' })
return
}
let unsigned = unsign(cookie, req, res);
let unsigned = unsign(cookie, req, res)
if (!unsigned) {
try {
res.status(401);
res.json({ "error": "Bad auth cookie set" });
}
catch (ignored) { } //sometimes it errors, gotta debug soon
return false;
res.status(401)
res.json({ error: 'Bad auth cookie set' })
} catch (ignored) {} //sometimes it errors, gotta debug soon
return false
}
return decodeURIComponent(unsigned);
return decodeURIComponent(unsigned)
}
export { unsign };
export { getunsigned };
export { unsign }
export { getunsigned }
export default {
unsign: unsign,
getunsigned: getunsigned
};
getunsigned: getunsigned,
}
+15 -10
View File
@@ -1,19 +1,24 @@
function XOR_hex(a, b) {
var res = "", i = a.length, j = b.length;
var res = '',
i = a.length,
j = b.length
while (i-- > 0 && j-- > 0)
res = (parseInt(a.charAt(i), 16) ^ parseInt(b.charAt(j), 16)).toString(16) + res;
return res;
res =
(parseInt(a.charAt(i), 16) ^ parseInt(b.charAt(j), 16)).toString(
16
) + res
return res
}
function hexEncode(a) {
let hex;
let result = "";
let hex
let result = ''
for (let i = 0; i < a.length; i++) {
hex = a.charCodeAt(i).toString(16);
result += ("000" + hex).slice(-4);
hex = a.charCodeAt(i).toString(16)
result += ('000' + hex).slice(-4)
}
return result;
return result
}
function xor(a, b) {
return XOR_hex(hexEncode(a), hexEncode(b)).toString("hex");
return XOR_hex(hexEncode(a), hexEncode(b)).toString('hex')
}
export default xor;
export default xor