access.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. const DatabaseModel = require("./DatabaseModel.js").DatabaseModel;
  2. const Security = require('../src/security.js');
  3. function AccessModel(privId, publicId, ipAddress) {
  4. this.accessTime = new Date();
  5. this.publicId = publicId;
  6. this.privId = privId;
  7. this.reverseIp = null;
  8. this.ipAddress = ipAddress;
  9. this.ipRegion = null;
  10. }
  11. Object.setPrototypeOf(AccessModel.prototype, DatabaseModel.prototype);
  12. AccessModel.prototype.resolveIp = async function() {
  13. try {
  14. this.ipRegion = Security.geoIp(this.ipAddress);
  15. } catch (err) {
  16. console.error("AccessModel::resolveIp: Cannot geolocalize ip " +this.ipAddress +": ", err);
  17. this.ipRegion = "{}";
  18. }
  19. try {
  20. this.reverseIp = await Security.reverseDns(this.ipAddress);
  21. } catch (err) {
  22. console.error("AccessModel::resolveIp: Cannot reverse ip " +this.ipAddress +": ", err);
  23. this.reverseIp = "";
  24. }
  25. }
  26. AccessModel.prototype.getTableName = function() {
  27. return "access";
  28. }
  29. AccessModel.prototype.createOrUpdateBase = async function(dbHelper) {
  30. await dbHelper.runSql(`CREATE TABLE IF NOT EXISTS 'access' (
  31. privId string not null,
  32. publicId string not null,
  33. accessTime datetime not null,
  34. ipAddress string not null,
  35. reverseIp string,
  36. ipRegion string,
  37. PRIMARY KEY(publicId, accessTime, ipAddress),
  38. FOREIGN KEY (privId) REFERENCES pasteContent(privId)
  39. )`);
  40. }
  41. AccessModel.prototype.describe = function() {
  42. return {
  43. "privId": this.privId,
  44. "publicId": this.publicId,
  45. "accessTime": this.accessTime.getTime(),
  46. "ipAddress": this.ipAddress,
  47. "reverseIp": this.reverseIp,
  48. "ipRegion": JSON.stringify(this.ipRegion)
  49. };
  50. }
  51. AccessModel.prototype.fromDb = function(dbObj) {
  52. this.privId = dbObj['privId'];
  53. this.publicId = "" + dbObj['publicId'];
  54. this.accessTime = new Date(dbObj['accessTime']);
  55. this.ipAddress = dbObj['ipAddress'];
  56. this.reverseIp = dbObj['reverseIp'];
  57. this.ipRegion = JSON.parse(dbObj['ipRegion']);
  58. }
  59. module.exports.AccessModel = AccessModel;