Skip to content

Commit

Permalink
changed logging function pos
Browse files Browse the repository at this point in the history
  • Loading branch information
SorenEricMent committed Jul 17, 2022
1 parent 47bfc6a commit bc837db
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 32 deletions.
32 changes: 4 additions & 28 deletions modules/iapi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,6 @@ class IAPI {
timestamp(){
return new Date().getTime();
}
IP(req) {
if(this.siteConfig.ip_detect_method == "connection"){
return req.connection.remoteAddress;
}else if(this.siteConfig.ip_detect_method == "header"){
//Default header X-Forwarded-From.
if(req.headers.hasOwnProperty(this.siteConfig.ip_detect_header)){
return req.headers[this.siteConfig.ip_detect_header];
}else{
this.log("error", "IAPI", "Dictated IP detection method is header, but header is not found");
if(req.headers.hasOwnProperty("X-Forwarded-From")){
return req.headers["X-Forwarded-From"];
}else{
return req.connection.remoteAddress;
}
}
}else{
this.log("error", "IAPI", "Dictated IP detection method is not found.");
return req.connection.remoteAddress;
}
}
removeExpiredSessions(redisKey, userPermissions, results){
let promisePool = [];
for(const element of results){
Expand Down Expand Up @@ -108,9 +88,7 @@ class IAPI {
});
}
//Actual service functions
userLogin(req, username, password) {
let connIP = this.IP(req);
let userAgent = req.headers['user-agent'];
userLogin(ip, ua, username, password) {
return new Promise((resolve, reject) => {
password = blake3Hash(this.salt + password);
this.mysql.query(
Expand All @@ -137,8 +115,8 @@ class IAPI {
"permissions": user.permissions,
"statistics": {
"date": this.timestamp(),
"userAgent": userAgent,
"ip": connIP
"userAgent": ua,
"ip": ip
}
});
this.redis.lrange(redisKey, 0, -1, (err, results) => {
Expand Down Expand Up @@ -176,9 +154,7 @@ class IAPI {
);
});
}
userRegister(req, username, password, email, nickname) {
let connIP = this.IP(req);
let userAgent = req.headers['user-agent'];
userRegister(ip, ua, username, password, email, nickname) {
return new Promise((resolve, reject) => {
if (!basicPasswordRequirement(password)) {
reject("Password does not meet basic requirements(> 8 characters, contains at least one number, one letter)");
Expand Down
46 changes: 42 additions & 4 deletions modules/router.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,44 @@ import { join } from "path";
import { default as bodyParser } from "body-parser";

function initializeRouter(mysqlConnection, redisConnection, siteConfig, log, salt, redisPrefix){
let getReqInfo = function(req){
let ip = null;
let ua = null;
if(siteConfig.ip_detect_method == "connection"){
ip = req.connection.remoteAddress;
}else if(siteConfig.ip_detect_method == "header"){
//Default header X-Forwarded-From.
if(req.headers.hasOwnProperty(siteConfig.ip_detect_header)){
ip = req.headers[siteConfig.ip_detect_header];
}else{
log("error", "IAPI", "Dictated IP detection method is header, but header is not found.");
if(req.headers.hasOwnProperty("X-Forwarded-From")){
ip = req.headers["X-Forwarded-From"];
}else if(req.headers.hasOwnProperty("x-forwarded-from")){
ip = req.headers["x-forwarded-from"];
}else{
ip = req.connection.remoteAddress;
}
}
}else{
log("error", "IAPI", "Dictated IP detection method is not found.");
ip = req.connection.remoteAddress;
}
if(req.headers.hasOwnProperty("user-agent")){
ua = req.headers["user-agent"];
}else if(req.headers.hasOwnProperty("User-Agent")){
ua = req.headers["User-Agent"];
}else{
ua = "Unknown/0";
}
return {
"ip": ip,
"ua": ua
}
}

const iapi = new IAPI(mysqlConnection, redisConnection, siteConfig, log, salt, redisPrefix);

let blorumRouter = express();
let commonHeader = {
"X-Powered-By": "Blorum",
Expand Down Expand Up @@ -90,9 +127,10 @@ function initializeRouter(mysqlConnection, redisConnection, siteConfig, log, sal
res.set("Content-Type","application/json");
res.set(commonHeader);
let b = req.body;
let reqInfo = getReqInfo(req);
if(objHasAllProperties(b, "username", "password")){
if(isAllString(b.username, b.password)){
iapi.userLogin(req, b.username, b.password).then(function(result){
iapi.userLogin(reqInfo.ip, reqInfo.ua, b.username, b.password).then(function(result){
res.set(commonHeader);
res.status(200).send(result);
}).catch(function(err){
Expand All @@ -109,15 +147,15 @@ function initializeRouter(mysqlConnection, redisConnection, siteConfig, log, sal

blorumRouter.post('/user/register', function (req, res) {
let b = req.body;
let reqInfo = getReqInfo(req);
if(objHasAllProperties(b, "username", "password", "email")){
if(isAllString(b.username, b.password, b.email, b.nickname)){
try {
res.set("Content-Type","application/json");
res.set(commonHeader);
iapi.userRegister(req, b.username, b.password, b.email, b.nickname).then(function (result) {
iapi.userRegister(reqInfo.ip, reqInfo.ua, b.username, b.password, b.email, b.nickname).then(function (result) {
res.status(200).send(result);
}
).catch(function (error) {
}).catch(function (error) {
log("debug", "Router", "Failed to register user: " + error);
res.status(403).send(error);
});
Expand Down

0 comments on commit bc837db

Please sign in to comment.