const DatabaseModel = require("./DatabaseModel.js").DatabaseModel; const Security = require('../src/security.js'); function AccessModel(privId, publicId, ipAddress) { this.accessTime = new Date(); this.publicId = publicId; this.privId = privId; this.reverseIp = null; this.ipAddress = ipAddress; this.ipRegion = null; } Object.setPrototypeOf(AccessModel.prototype, DatabaseModel.prototype); AccessModel.prototype.resolveIp = async function() { try { this.ipRegion = Security.geoIp(this.ipAddress); } catch (err) { console.error("AccessModel::resolveIp: Cannot geolocalize ip " +this.ipAddress +": ", err); this.ipRegion = "{}"; } try { this.reverseIp = await Security.reverseDns(this.ipAddress); } catch (err) { console.error("AccessModel::resolveIp: Cannot reverse ip " +this.ipAddress +": ", err); this.reverseIp = ""; } } AccessModel.prototype.getTableName = function() { return "access"; } AccessModel.prototype.createOrUpdateBase = async function(dbHelper) { await dbHelper.runSql(`CREATE TABLE IF NOT EXISTS 'access' ( privId string not null, publicId string not null, accessTime datetime not null, ipAddress string not null, reverseIp string, ipRegion string, PRIMARY KEY(publicId, accessTime, ipAddress), FOREIGN KEY (privId) REFERENCES pasteContent(privId) )`); } AccessModel.prototype.describe = function() { return { "privId": this.privId, "publicId": this.publicId, "accessTime": this.accessTime.getTime(), "ipAddress": this.ipAddress, "reverseIp": this.reverseIp, "ipRegion": JSON.stringify(this.ipRegion) }; } AccessModel.prototype.fromDb = function(dbObj) { this.privId = dbObj['privId']; this.publicId = "" + dbObj['publicId']; this.accessTime = new Date(dbObj['accessTime']); this.ipAddress = dbObj['ipAddress']; this.reverseIp = dbObj['reverseIp']; this.ipRegion = JSON.parse(dbObj['ipRegion']); } module.exports.AccessModel = AccessModel;