"use strict";

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

var axios = require('axios');

var apiUrl = 'https://api.cloudflare.com/client/v4'; // Endpoint 

var MailinaboxClass = require('./Mailinabox.js');

var CloudFlare =
/*#__PURE__*/
function () {
  function CloudFlare(config) {
    _classCallCheck(this, CloudFlare);

    this.config = config ? config : {};

    if (!this.config.arrTokens) {
      this.config.arrTokens = process.env.CLOUDFLARE_TOKENS;
    }
  }

  _createClass(CloudFlare, [{
    key: "init",
    value: function init() {
      if (this.config.arrTokens) {
        // this.zones = this.fetchCloudflareZones(this.config.apiToken);
        this.zones = [];
        this.dnsRecords = null;
      }
    }
  }, {
    key: "fetchCloudflareZones",
    value: function fetchCloudflareZones(apiToken) {
      var response, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, zone, domain;

      return regeneratorRuntime.async(function fetchCloudflareZones$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.prev = 0;
              _context.next = 3;
              return regeneratorRuntime.awrap(axios.get(apiUrl + '/zones', {
                headers: {
                  'Authorization': "Bearer ".concat(apiToken),
                  // Autenticazione con token
                  'Content-Type': 'application/json' // Tipo di contenuto

                }
              }));

            case 3:
              response = _context.sent;
              // Estrai i dati dalla risposta
              this.zones = response.data.result;

              if (!(this.zones && this.zones.length > 0)) {
                _context.next = 33;
                break;
              }

              _iteratorNormalCompletion = true;
              _didIteratorError = false;
              _iteratorError = undefined;
              _context.prev = 9;
              _iterator = this.zones[Symbol.iterator]();

            case 11:
              if (_iteratorNormalCompletion = (_step = _iterator.next()).done) {
                _context.next = 19;
                break;
              }

              zone = _step.value;
              domain = zone.name;
              _context.next = 16;
              return regeneratorRuntime.awrap(this.findAndUpdateOnSite(domain, zone, apiToken));

            case 16:
              _iteratorNormalCompletion = true;
              _context.next = 11;
              break;

            case 19:
              _context.next = 25;
              break;

            case 21:
              _context.prev = 21;
              _context.t0 = _context["catch"](9);
              _didIteratorError = true;
              _iteratorError = _context.t0;

            case 25:
              _context.prev = 25;
              _context.prev = 26;

              if (!_iteratorNormalCompletion && _iterator["return"] != null) {
                _iterator["return"]();
              }

            case 28:
              _context.prev = 28;

              if (!_didIteratorError) {
                _context.next = 31;
                break;
              }

              throw _iteratorError;

            case 31:
              return _context.finish(28);

            case 32:
              return _context.finish(25);

            case 33:
              return _context.abrupt("return", this.zones);

            case 36:
              _context.prev = 36;
              _context.t1 = _context["catch"](0);
              console.error('Errore durante il recupero delle zone di Cloudflare:', _context.t1.message);

            case 39:
            case "end":
              return _context.stop();
          }
        }
      }, null, this, [[0, 36], [9, 21, 25, 33], [26,, 28, 32]]);
    }
  }, {
    key: "findAndUpdateOnSite",
    value: function findAndUpdateOnSite(domain, zone, apiToken) {
      var Site, normalizedDomain, recsite, modif, miab, dkim;
      return regeneratorRuntime.async(function findAndUpdateOnSite$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _context2.prev = 0;
              Site = require('../models/site');
              domain = domain.replace(/^https?:\/\//, '');
              normalizedDomain = domain.startsWith("http://") || domain.startsWith("https://") ? domain : "https://" + domain; // Trova il sito usando il dominio normalizzato

              _context2.next = 6;
              return regeneratorRuntime.awrap(Site.findOne({
                host: normalizedDomain
              }));

            case 6:
              recsite = _context2.sent;

              if (!recsite) {
                _context2.next = 21;
                break;
              }

              // Aggiorna i parametri token e zoneId
              modif = recsite.cf_token !== apiToken || recsite.cf_zoneId !== zone.id;

              if (!(modif && apiToken)) {
                _context2.next = 12;
                break;
              }

              _context2.next = 12;
              return regeneratorRuntime.awrap(Site.findOneAndUpdate({
                _id: recsite._id
              }, {
                $set: {
                  cf_token: apiToken,
                  cf_zoneId: zone.id
                }
              }, {
                "new": true
              }).then(function (updatedSite) {
                console.log('Site aggiornato:', updatedSite);
              })["catch"](function (error) {
                console.error('Errore durante l\'aggiornamento del Site:', error);
              }));

            case 12:
              if (!(recsite.enable_servermail && !recsite.dkim)) {
                _context2.next = 21;
                break;
              }

              miab = new MailinaboxClass(null);
              miab.init();
              _context2.next = 17;
              return regeneratorRuntime.awrap(miab.getDKIMRecord(recsite.host.replace(/^https?:\/\//, '')));

            case 17:
              dkim = _context2.sent;

              if (!dkim) {
                _context2.next = 21;
                break;
              }

              _context2.next = 21;
              return regeneratorRuntime.awrap(Site.findOneAndUpdate({
                _id: recsite._id
              }, {
                $set: {
                  dkim: dkim
                }
              }));

            case 21:
              _context2.next = 26;
              break;

            case 23:
              _context2.prev = 23;
              _context2.t0 = _context2["catch"](0);
              console.error('Errore durante il recupero dei record DNS di Cloudflare:', _context2.t0.message);

            case 26:
            case "end":
              return _context2.stop();
          }
        }
      }, null, null, [[0, 23]]);
    } // Funzione per estrarre i record DNS

  }, {
    key: "fetchDNSRecords",
    value: function fetchDNSRecords(apiToken, zoneId) {
      var apiUrlDNS, response;
      return regeneratorRuntime.async(function fetchDNSRecords$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              apiUrlDNS = apiUrl + "/zones/".concat(zoneId, "/dns_records");
              _context3.prev = 1;
              _context3.next = 4;
              return regeneratorRuntime.awrap(axios.get(apiUrlDNS, {
                headers: {
                  'Authorization': "Bearer ".concat(apiToken),
                  // Autenticazione con token
                  'Content-Type': 'application/json' // Tipo di contenuto

                }
              }));

            case 4:
              response = _context3.sent;
              this.dnsRecords = response.data.result;
              return _context3.abrupt("return", this.dnsRecords);

            case 9:
              _context3.prev = 9;
              _context3.t0 = _context3["catch"](1);
              console.error('Errore durante il recupero dei record DNS di Cloudflare:', _context3.t0.message);

            case 12:
            case "end":
              return _context3.stop();
          }
        }
      }, null, this, [[1, 9]]);
    } // Funzione per aggiornare un record DNS

  }, {
    key: "updateDNSRecord",
    value: function updateDNSRecord(apiToken, zoneId, dnsRecordId, newDnsRecordData) {
      var apiUrlDNS, response, updatedRecord;
      return regeneratorRuntime.async(function updateDNSRecord$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              apiUrlDNS = apiUrl + "/zones/".concat(zoneId, "/dns_records/").concat(dnsRecordId);
              _context4.prev = 1;
              _context4.next = 4;
              return regeneratorRuntime.awrap(axios.put(apiUrlDNS, newDnsRecordData, {
                headers: {
                  'Authorization': "Bearer ".concat(apiToken),
                  // Autenticazione con token
                  'Content-Type': 'application/json' // Tipo di contenuto

                }
              }));

            case 4:
              response = _context4.sent;
              updatedRecord = response.data.result; // Stampa il record DNS aggiornato

              console.log('Record DNS aggiornato:', updatedRecord);
              return _context4.abrupt("return", updatedRecord);

            case 10:
              _context4.prev = 10;
              _context4.t0 = _context4["catch"](1);
              console.error('Errore durante l\'aggiornamento del record DNS:', _context4.t0.message);

            case 13:
            case "end":
              return _context4.stop();
          }
        }
      }, null, null, [[1, 10]]);
    } // Funzione per creare un record DNS di CloudFlare

  }, {
    key: "createDNSRecord",
    value: function createDNSRecord(apiToken, zoneId, dnsRecordData) {
      var apiUrlDNS, response, createdRecord;
      return regeneratorRuntime.async(function createDNSRecord$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              apiUrlDNS = apiUrl + "/zones/".concat(zoneId, "/dns_records");
              _context5.prev = 1;
              _context5.next = 4;
              return regeneratorRuntime.awrap(axios.post(apiUrlDNS, dnsRecordData, {
                headers: {
                  'Authorization': "Bearer ".concat(apiToken),
                  // Autenticazione con token
                  'Content-Type': 'application/json' // Tipo di contenuto

                }
              }));

            case 4:
              response = _context5.sent;
              createdRecord = response.data.result;
              console.log('Record DNS creato:', createdRecord);
              return _context5.abrupt("return", createdRecord);

            case 10:
              _context5.prev = 10;
              _context5.t0 = _context5["catch"](1);
              console.error('Errore durante la creazione del record DNS:', _context5.t0.message);

            case 13:
            case "end":
              return _context5.stop();
          }
        }
      }, null, null, [[1, 10]]);
    } // Funzione per cancellare un record DNS di CloudFlare

  }, {
    key: "deleteDNSRecord",
    value: function deleteDNSRecord(apiToken, zoneId, dnsRecordId) {
      var apiUrlDNS, response, deletedRecord;
      return regeneratorRuntime.async(function deleteDNSRecord$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              apiUrlDNS = apiUrl + "/zones/".concat(zoneId, "/dns_records/").concat(dnsRecordId);
              _context6.prev = 1;
              _context6.next = 4;
              return regeneratorRuntime.awrap(axios["delete"](apiUrlDNS, {
                headers: {
                  'Authorization': "Bearer ".concat(apiToken),
                  // Autenticazione con token
                  'Content-Type': 'application/json' // Tipo di contenuto

                }
              }));

            case 4:
              response = _context6.sent;
              deletedRecord = response.data.result;
              console.log('Record DNS cancellato:', deletedRecord);
              return _context6.abrupt("return", deletedRecord);

            case 10:
              _context6.prev = 10;
              _context6.t0 = _context6["catch"](1);
              console.error('Errore durante la cancellazione del record DNS:', _context6.t0.message);

            case 13:
            case "end":
              return _context6.stop();
          }
        }
      }, null, null, [[1, 10]]);
    }
  }, {
    key: "setCorrectIpsOnDNS",
    value: function setCorrectIpsOnDNS(domainrec) {
      var arrparams, i;
      return regeneratorRuntime.async(function setCorrectIpsOnDNS$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              arrparams = [{
                urladd: '',
                paramsite: 'host_ip',
                type: 'A'
              }, {
                urladd: 'test.',
                paramsite: 'host_test_ip',
                type: 'A'
              }, {
                urladd: 'api.',
                paramsite: 'host_api_ip',
                type: 'A'
              }, {
                urladd: 'testapi.',
                paramsite: 'host_testapi_ip',
                type: 'A'
              }, {
                urladd: 'www.',
                paramsite: 'host',
                type: 'CNAME'
              }];
              i = 0;

            case 2:
              if (!(i < arrparams.length)) {
                _context7.next = 8;
                break;
              }

              _context7.next = 5;
              return regeneratorRuntime.awrap(this.setSingleIpsOnDNS(domainrec.name, arrparams[i]));

            case 5:
              i++;
              _context7.next = 2;
              break;

            case 8:
              _context7.next = 10;
              return regeneratorRuntime.awrap(this.setServerMail(domainrec));

            case 10:
              _context7.next = 12;
              return regeneratorRuntime.awrap(new Promise(function (resolve) {
                return setTimeout(resolve, 3000);
              }));

            case 12:
            case "end":
              return _context7.stop();
          }
        }
      }, null, this);
    }
  }, {
    key: "setServerMail",
    value: function setServerMail(domainrec) {
      var _this = this;

      var Site, domain, normalizedDomain;
      return regeneratorRuntime.async(function setServerMail$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              _context9.prev = 0;
              // get the parameters (Token and zoneId on Database)
              Site = require('../models/site');
              domain = domainrec.name.replace(/^https?:\/\//, '');
              normalizedDomain = domain.startsWith("http://") || domain.startsWith("https://") ? domain : "https://" + domain; // Trova il sito usando il dominio normalizzato

              _context9.next = 6;
              return regeneratorRuntime.awrap(Site.findOne({
                host: normalizedDomain
              }).lean().then(function _callee(site) {
                var myarrrecdns, recTypeMX, newRecord, recTypeTXTspf1, contentTXTspf1, _newRecord, nameDkimtoFind, recTypeTXTDKIM, contentTXTDKIM, _newRecord2, nameDmarctoFind, recTypeTXTdmarc, contentTXTdmarc, _newRecord3, myType, strfind, contentExpected, rectofind, _newRecord4, _newRecord5;

                return regeneratorRuntime.async(function _callee$(_context8) {
                  while (1) {
                    switch (_context8.prev = _context8.next) {
                      case 0:
                        if (!site) {
                          _context8.next = 91;
                          break;
                        }

                        if (!(site.cf_token && site.cf_zoneId)) {
                          _context8.next = 91;
                          break;
                        }

                        _context8.next = 4;
                        return regeneratorRuntime.awrap(_this.fetchDNSRecords(site.cf_token, site.cf_zoneId));

                      case 4:
                        myarrrecdns = _context8.sent;
                        _context8.next = 7;
                        return regeneratorRuntime.awrap(myarrrecdns.find(function (rec) {
                          return rec.type === 'MX' && rec.name === domain;
                        }));

                      case 7:
                        recTypeMX = _context8.sent;

                        if (!recTypeMX) {
                          _context8.next = 14;
                          break;
                        }

                        if (!(recTypeMX.content !== site.servermail)) {
                          _context8.next = 12;
                          break;
                        }

                        recTypeMX.content = site.servermail;
                        return _context8.abrupt("return", _this.updateDNSRecord(site.cf_token, site.cf_zoneId, recTypeMX.id, recTypeMX));

                      case 12:
                        _context8.next = 17;
                        break;

                      case 14:
                        // create a new record
                        newRecord = {
                          type: 'MX',
                          name: domain,
                          content: site.servermail,
                          ttl: 1,
                          proxied: false,
                          priority: 10
                        };
                        _context8.next = 17;
                        return regeneratorRuntime.awrap(_this.createDNSRecord(site.cf_token, site.cf_zoneId, newRecord));

                      case 17:
                        _context8.next = 19;
                        return regeneratorRuntime.awrap(myarrrecdns.find(function (rec) {
                          return rec.type === 'TXT' && rec.name === domain && rec.content.indexOf('v=spf1') > -1;
                        }));

                      case 19:
                        recTypeTXTspf1 = _context8.sent;

                        if (!site.servermailip) {
                          _context8.next = 31;
                          break;
                        }

                        contentTXTspf1 = "v=spf1 a mx:".concat(site.servermail, " ip4:").concat(site.servermailip, " ~all");

                        if (!recTypeTXTspf1) {
                          _context8.next = 28;
                          break;
                        }

                        if (!(recTypeTXTspf1.content !== contentTXTspf1)) {
                          _context8.next = 26;
                          break;
                        }

                        recTypeTXTspf1.content = contentTXTspf1;
                        return _context8.abrupt("return", _this.updateDNSRecord(site.cf_token, site.cf_zoneId, recTypeTXTspf1.id, recTypeTXTspf1));

                      case 26:
                        _context8.next = 31;
                        break;

                      case 28:
                        // create a new record
                        _newRecord = {
                          type: 'TXT',
                          name: domain,
                          content: contentTXTspf1,
                          ttl: 3600
                        };
                        _context8.next = 31;
                        return regeneratorRuntime.awrap(_this.createDNSRecord(site.cf_token, site.cf_zoneId, _newRecord));

                      case 31:
                        // mail._domainkey.nomedominio v=DKIM1; h=sha256; k=rsa; s=email; p=<dkim>
                        nameDkimtoFind = "mail._domainkey.".concat(domain);
                        _context8.next = 34;
                        return regeneratorRuntime.awrap(myarrrecdns.find(function (rec) {
                          return rec.type === 'TXT' && rec.name === nameDkimtoFind;
                        }));

                      case 34:
                        recTypeTXTDKIM = _context8.sent;

                        if (!site.dkim) {
                          _context8.next = 46;
                          break;
                        }

                        contentTXTDKIM = "v=DKIM1; h=sha256; k=rsa; s=email; p=".concat(site.dkim);

                        if (!recTypeTXTDKIM) {
                          _context8.next = 43;
                          break;
                        }

                        if (!(recTypeTXTDKIM.content !== contentTXTDKIM)) {
                          _context8.next = 41;
                          break;
                        }

                        recTypeTXTDKIM.content = contentTXTDKIM;
                        return _context8.abrupt("return", _this.updateDNSRecord(site.cf_token, site.cf_zoneId, recTypeTXTDKIM.id, recTypeTXTDKIM));

                      case 41:
                        _context8.next = 46;
                        break;

                      case 43:
                        // create a new record
                        _newRecord2 = {
                          type: 'TXT',
                          name: nameDkimtoFind,
                          content: contentTXTDKIM,
                          ttl: 1
                        };
                        _context8.next = 46;
                        return regeneratorRuntime.awrap(_this.createDNSRecord(site.cf_token, site.cf_zoneId, _newRecord2));

                      case 46:
                        // DMARC:
                        nameDmarctoFind = "_dmarc.".concat(domain);
                        _context8.next = 49;
                        return regeneratorRuntime.awrap(myarrrecdns.find(function (rec) {
                          return rec.type === 'TXT' && rec.name === nameDmarctoFind && rec.content.indexOf('v=DMARC1') > -1;
                        }));

                      case 49:
                        recTypeTXTdmarc = _context8.sent;

                        if (!site.servermailip) {
                          _context8.next = 61;
                          break;
                        }

                        contentTXTdmarc = "v=DMARC1; p=quarantine; ruf=mailto:dmarc@".concat(domain, ";");

                        if (!recTypeTXTdmarc) {
                          _context8.next = 58;
                          break;
                        }

                        if (!(recTypeTXTdmarc.content !== contentTXTdmarc)) {
                          _context8.next = 56;
                          break;
                        }

                        recTypeTXTdmarc.content = contentTXTdmarc;
                        return _context8.abrupt("return", _this.updateDNSRecord(site.cf_token, site.cf_zoneId, recTypeTXTspf1.id, recTypeTXTspf1));

                      case 56:
                        _context8.next = 61;
                        break;

                      case 58:
                        // create a new record
                        _newRecord3 = {
                          type: 'TXT',
                          name: nameDmarctoFind,
                          content: contentTXTdmarc,
                          ttl: 1
                        };
                        _context8.next = 61;
                        return regeneratorRuntime.awrap(_this.createDNSRecord(site.cf_token, site.cf_zoneId, _newRecord3));

                      case 61:
                        // AutoConfig:
                        myType = 'CNAME';
                        strfind = "autoconfig.".concat(domain);
                        contentExpected = site.servermail;
                        _context8.next = 66;
                        return regeneratorRuntime.awrap(myarrrecdns.find(function (rec) {
                          return rec.type === 'CNAME' && rec.name === strfind;
                        }));

                      case 66:
                        rectofind = _context8.sent;

                        if (!rectofind) {
                          _context8.next = 73;
                          break;
                        }

                        if (!(rectofind.content !== contentExpected)) {
                          _context8.next = 71;
                          break;
                        }

                        rectofind.content = contentExpected;
                        return _context8.abrupt("return", _this.updateDNSRecord(site.cf_token, site.cf_zoneId, rectofind.id, rectofind));

                      case 71:
                        _context8.next = 76;
                        break;

                      case 73:
                        // create a new record
                        _newRecord4 = {
                          type: myType,
                          name: strfind,
                          content: contentExpected,
                          ttl: 1
                        };
                        _context8.next = 76;
                        return regeneratorRuntime.awrap(_this.createDNSRecord(site.cf_token, site.cf_zoneId, _newRecord4));

                      case 76:
                        // AutoDiscover:
                        myType = 'CNAME';
                        strfind = "autodiscover.".concat(domain);
                        contentExpected = site.servermail;
                        _context8.next = 81;
                        return regeneratorRuntime.awrap(myarrrecdns.find(function (rec) {
                          return rec.type === 'CNAME' && rec.name === strfind;
                        }));

                      case 81:
                        rectofind = _context8.sent;

                        if (!rectofind) {
                          _context8.next = 88;
                          break;
                        }

                        if (!(rectofind.content !== contentExpected)) {
                          _context8.next = 86;
                          break;
                        }

                        rectofind.content = contentExpected;
                        return _context8.abrupt("return", _this.updateDNSRecord(site.cf_token, site.cf_zoneId, rectofind.id, rectofind));

                      case 86:
                        _context8.next = 91;
                        break;

                      case 88:
                        // create a new record
                        _newRecord5 = {
                          type: myType,
                          name: strfind,
                          content: contentExpected,
                          ttl: 1
                        };
                        _context8.next = 91;
                        return regeneratorRuntime.awrap(_this.createDNSRecord(site.cf_token, site.cf_zoneId, _newRecord5));

                      case 91:
                      case "end":
                        return _context8.stop();
                    }
                  }
                });
              }));

            case 6:
              _context9.next = 11;
              break;

            case 8:
              _context9.prev = 8;
              _context9.t0 = _context9["catch"](0);
              console.error('e', _context9.t0);

            case 11:
            case "end":
              return _context9.stop();
          }
        }
      }, null, null, [[0, 8]]);
    }
  }, {
    key: "setSingleIpsOnDNS",
    value: function setSingleIpsOnDNS(domain, paramobj) {
      var _this2 = this;

      var Site, normalizedDomain;
      return regeneratorRuntime.async(function setSingleIpsOnDNS$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              _context11.prev = 0;
              // get the parameters (Token and zoneId on Database)
              Site = require('../models/site');
              domain = domain.replace(/^https?:\/\//, '');
              normalizedDomain = domain.startsWith("http://") || domain.startsWith("https://") ? domain : "https://" + domain; // Trova il sito usando il dominio normalizzato

              _context11.next = 6;
              return regeneratorRuntime.awrap(Site.findOne({
                host: normalizedDomain
              }).lean().then(function _callee2(site) {
                var myarrrecdns, nametofind, recType, paramexpected, newRecord;
                return regeneratorRuntime.async(function _callee2$(_context10) {
                  while (1) {
                    switch (_context10.prev = _context10.next) {
                      case 0:
                        if (!site) {
                          _context10.next = 20;
                          break;
                        }

                        if (!(site.cf_token && site.cf_zoneId)) {
                          _context10.next = 20;
                          break;
                        }

                        _context10.next = 4;
                        return regeneratorRuntime.awrap(_this2.fetchDNSRecords(site.cf_token, site.cf_zoneId));

                      case 4:
                        myarrrecdns = _context10.sent;
                        nametofind = paramobj.urladd + domain; // find 

                        _context10.next = 8;
                        return regeneratorRuntime.awrap(myarrrecdns.find(function (rec) {
                          return rec.type === paramobj.type && rec.name === nametofind;
                        }));

                      case 8:
                        recType = _context10.sent;
                        paramexpected = '';

                        try {
                          paramexpected = site[paramobj.paramsite].replace(/^https?:\/\//, '');
                          ;
                        } catch (e) {}

                        if (!(recType && paramexpected)) {
                          _context10.next = 17;
                          break;
                        }

                        if (!(recType.content !== paramexpected)) {
                          _context10.next = 15;
                          break;
                        }

                        recType.content = paramexpected;
                        return _context10.abrupt("return", _this2.updateDNSRecord(site.cf_token, site.cf_zoneId, recType.id, recType));

                      case 15:
                        _context10.next = 20;
                        break;

                      case 17:
                        // create a new record
                        newRecord = {
                          type: paramobj.type,
                          name: nametofind,
                          content: paramexpected,
                          ttl: 1,
                          proxied: true
                        };
                        _context10.next = 20;
                        return regeneratorRuntime.awrap(_this2.createDNSRecord(site.cf_token, site.cf_zoneId, newRecord));

                      case 20:
                      case "end":
                        return _context10.stop();
                    }
                  }
                });
              }));

            case 6:
              _context11.next = 11;
              break;

            case 8:
              _context11.prev = 8;
              _context11.t0 = _context11["catch"](0);
              console.error('e', _context11.t0);

            case 11:
            case "end":
              return _context11.stop();
          }
        }
      }, null, null, [[0, 8]]);
    }
  }]);

  return CloudFlare;
}();

module.exports = CloudFlare;