host.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 'use strict';
  2. const _ = require('lodash');
  3. const error = require('../lib/error');
  4. const proxyHostModel = require('../models/proxy_host');
  5. const redirectionHostModel = require('../models/redirection_host');
  6. const deadHostModel = require('../models/dead_host');
  7. const internalHost = {
  8. allowed_ssl_files: ['other_certificate', 'other_certificate_key'],
  9. /**
  10. * Internal use only, checks to see if the domain is already taken by any other record
  11. *
  12. * @param {String} hostname
  13. * @param {String} [ignore_type] 'proxy', 'redirection', 'dead'
  14. * @param {Integer} [ignore_id] Must be supplied if type was also supplied
  15. * @returns {Promise}
  16. */
  17. isHostnameTaken: function (hostname, ignore_type, ignore_id) {
  18. let promises = [
  19. proxyHostModel
  20. .query()
  21. .where('is_deleted', 0)
  22. .andWhere('domain_names', 'like', '%' + hostname + '%'),
  23. redirectionHostModel
  24. .query()
  25. .where('is_deleted', 0)
  26. .andWhere('domain_names', 'like', '%' + hostname + '%'),
  27. deadHostModel
  28. .query()
  29. .where('is_deleted', 0)
  30. .andWhere('domain_names', 'like', '%' + hostname + '%')
  31. ];
  32. return Promise.all(promises)
  33. .then(promises_results => {
  34. let is_taken = false;
  35. if (promises_results[0]) {
  36. // Proxy Hosts
  37. if (internalHost._checkHostnameRecordsTaken(hostname, promises_results[0], ignore_type === 'proxy' && ignore_id ? ignore_id : 0)) {
  38. is_taken = true;
  39. }
  40. }
  41. if (promises_results[1]) {
  42. // Redirection Hosts
  43. if (internalHost._checkHostnameRecordsTaken(hostname, promises_results[1], ignore_type === 'redirection' && ignore_id ? ignore_id : 0)) {
  44. is_taken = true;
  45. }
  46. }
  47. if (promises_results[1]) {
  48. // Dead Hosts
  49. if (internalHost._checkHostnameRecordsTaken(hostname, promises_results[2], ignore_type === 'dead' && ignore_id ? ignore_id : 0)) {
  50. is_taken = true;
  51. }
  52. }
  53. return {
  54. hostname: hostname,
  55. is_taken: is_taken
  56. };
  57. });
  58. },
  59. /**
  60. * Cleans the ssl keys from the meta object and sets them to "true"
  61. *
  62. * @param {Object} meta
  63. * @returns {*}
  64. */
  65. cleanMeta: function (meta) {
  66. internalHost.allowed_ssl_files.map(key => {
  67. if (typeof meta[key] !== 'undefined' && meta[key]) {
  68. meta[key] = true;
  69. }
  70. });
  71. return meta;
  72. },
  73. /**
  74. * Private call only
  75. *
  76. * @param {String} hostname
  77. * @param {Array} existing_rows
  78. * @param {Integer} [ignore_id]
  79. * @returns {Boolean}
  80. */
  81. _checkHostnameRecordsTaken: function (hostname, existing_rows, ignore_id) {
  82. let is_taken = false;
  83. if (existing_rows && existing_rows.length) {
  84. existing_rows.map(function (existing_row) {
  85. existing_row.domain_names.map(function (existing_hostname) {
  86. // Does this domain match?
  87. if (existing_hostname.toLowerCase() === hostname.toLowerCase()) {
  88. if (!ignore_id || ignore_id !== existing_row.id) {
  89. is_taken = true;
  90. }
  91. }
  92. });
  93. });
  94. }
  95. return is_taken;
  96. }
  97. };
  98. module.exports = internalHost;