Example #1
0
 def get_context_data(self, **kwargs):
     context = super().get_context_data(**kwargs)
     context['result'] = None
     domain_name = self.request.GET.get('domain_name')
     context['domain_name'] = domain_name
     if domain_name:
         if not zdomains.is_valid(domain_name):
             messages.error(self.request, 'Domain name is not valid')
             return context
         domain_tld = domain_name.split('.')[-1].lower()
         if not zzones.is_supported(domain_tld):
             messages.error(self.request, f'Domain zone "{domain_tld}" is not supported.')
             return context
         domain_available = zdomains.is_domain_available(domain_name)
         if domain_available:
             check_result = zmaster.domains_check(domain_names=[domain_name, ], )
             if check_result is None:
                 context['result'] = 'error'
             else:
                 if check_result.get(domain_name):
                     context['result'] = 'exist'
                 else:
                     context['result'] = 'not exist'
         else:
             context['result'] = 'exist'
     return context
Example #2
0
    def form_valid(self, form):
        if self.request.temporarily_blocked:
            messages.error(self.request,
                           'Too many attempts made, please try again later')
            return super().form_valid(form)
        result = None

        domain_name = form.cleaned_data.get('domain_name').strip().lower()

        if domain_name:
            if not zdomains.is_valid(domain_name):
                messages.error(self.request, 'Domain name is not valid')
                return self.render_to_response(
                    self.get_context_data(form=form,
                                          domain_name=domain_name,
                                          result=result))
            domain_tld = domain_name.split('.')[-1].lower()
            if not zzones.is_supported(domain_tld):
                messages.error(self.request,
                               f'Domain zone "{domain_tld}" is not supported')
                return self.render_to_response(
                    self.get_context_data(form=form,
                                          domain_name=domain_name,
                                          result=result))
            domain_available = zdomains.is_domain_available(domain_name)
            if domain_available:
                check_result = zmaster.domains_check(domain_names=[
                    domain_name,
                ], )
                if check_result is None:
                    messages.error(
                        self.request,
                        mark_safe(
                            'Service is unavailable at this moment. <br /> Please try it later.'
                        ))
                elif check_result == 'non-supported-zone':
                    messages.error(self.request,
                                   'Domain zone is not supported')
                else:
                    if check_result.get(domain_name):
                        messages.warning(
                            self.request,
                            mark_safe(
                                f'<b>{domain_name}</b> is already registered.')
                        )
                    else:
                        result = 'not exist'
            else:
                messages.warning(
                    self.request,
                    mark_safe(f'<b>{domain_name}</b> is already registered.'))
        return self.render_to_response(
            self.get_context_data(form=form,
                                  domain_name=domain_name,
                                  result=result))
Example #3
0
    def form_valid(self, form):
        result = None
        if settings.BRUTE_FORCE_PROTECTION_ENABLED:
            client_ip = self._get_client_ip()
            brute_force = BruteForceProtection(
                cache_key_prefix=settings.
                BRUTE_FORCE_PROTECTION_DOMAIN_LOOKUP_KEY_PREFIX,
                key=client_ip,
                max_attempts=settings.
                BRUTE_FORCE_PROTECTION_DOMAIN_LOOKUP_MAX_ATTEMPTS,
                timeout=settings.BRUTE_FORCE_PROTECTION_DOMAIN_LOOKUP_TIMEOUT)
            try:
                brute_force.register_attempt()
            except ExceededMaxAttemptsException:
                messages.error(
                    self.request,
                    'Too many attempts made, please try again later')
                return super().form_valid(form)

        domain_name = form.cleaned_data.get('domain_name')

        if domain_name:
            if not zdomains.is_valid(domain_name):
                messages.error(self.request, 'Domain name is not valid')
                return self.render_to_response(
                    self.get_context_data(form=form,
                                          domain_name=domain_name,
                                          result=result))
            domain_tld = domain_name.split('.')[-1].lower()
            if not zzones.is_supported(domain_tld):
                messages.error(self.request,
                               f'Domain zone "{domain_tld}" is not supported')
                return self.render_to_response(
                    self.get_context_data(form=form,
                                          domain_name=domain_name,
                                          result=result))
            domain_available = zdomains.is_domain_available(domain_name)
            if domain_available:
                check_result = zmaster.domains_check(domain_names=[
                    domain_name,
                ], )
                if check_result is None:
                    result = 'error'
                else:
                    if check_result.get(domain_name):
                        result = 'exist'
                    else:
                        result = 'not exist'
            else:
                result = 'exist'
        return self.render_to_response(
            self.get_context_data(form=form,
                                  domain_name=domain_name,
                                  result=result))
Example #4
0
def domain_regenerate_from_csv_row(csv_row, headers, wanted_registrar='whois_ai', dry_run=True):
    """
    """
    errors = []
    try:
        csv_record = split_csv_row(csv_row, headers)
        csv_info = get_csv_domain_info(csv_row, headers)
        domain = csv_info['name']
    except Exception as exc:
        errors.append('failed processing csv record: ' + str(exc))
        return errors

    if not zdomains.is_valid(domain):
    #--- invalid domain name
        errors.append('invalid domain name')
        return errors

    #--- lookup existing domain
    known_domain = zdomains.domain_find(domain)
    real_registrar_id = csv_record.get('registrar_id_9')

    if wanted_registrar and real_registrar_id != wanted_registrar:
    #--- belong to another registrar
        errors.append('%s: csv record belongs to another registrar %s' % (domain, real_registrar_id, ))
        return errors

    real_expiry_date = csv_info['expiry_date']
    real_create_date = csv_info['create_date']
    real_epp_id = csv_record.get('roid_0')
    real_auth_key = csv_record.get('auth_info_password_2')
    real_registrant_contact_id = csv_record.get('registrant_contact_id_24')
    real_admin_contact_id = csv_record.get('admin_contact_id_54')
    real_tech_contact_id = csv_record.get('tech_contact_id_69')
    real_billing_contact_id = csv_record.get('billing_contact_id_39')
    real_registrant_email = csv_info['registrant']['contact_email']
    real_admin_email = csv_info['admin']['contact_email']
    real_tech_email = csv_info['tech']['contact_email']
    real_billing_email = csv_info['billing']['contact_email']
    real_nameservers = csv_info['nameservers']

    known_expiry_date = None
    known_create_date = None
    known_epp_id = None
    known_auth_key = None
    known_registrant_contact_id = None 
    known_admin_contact_id = None
    known_tech_contact_id = None
    known_billing_contact_id = None
    known_nameservers = ['', ] * 4

    new_domain = None
    new_registrant_contact = None
    new_admin_contact = None
    new_tech_contact = None
    new_billing_contact = None

    need_registrant = False
    need_admin_contact = False
    need_tech_contact = False
    need_billing_contact = False

    owner_account = zusers.find_account(real_registrant_email)
    if not owner_account:
        if dry_run:
            errors.append('account %s not exist' % real_registrant_email)
            return errors
    #--- account check/create
        new_password=zusers.generate_password(length=10)
        owner_account = zusers.create_account(
            email=real_registrant_email,
            account_password=new_password,
            is_active=True,
        )
        logger.info('generated new account and password for %s : %s', real_registrant_email, new_password)

    if known_domain:
        known_expiry_date = known_domain.expiry_date
        known_create_date = known_domain.create_date
        known_epp_id = known_domain.epp_id
        known_auth_key = known_domain.auth_key
        known_registrant_contact_id = None if not known_domain.registrant else known_domain.registrant.epp_id
        known_admin_contact_id = None if not known_domain.contact_admin else known_domain.contact_admin.epp_id
        known_billing_contact_id = None if not known_domain.contact_billing else known_domain.contact_billing.epp_id
        known_tech_contact_id = None if not known_domain.contact_tech else known_domain.contact_tech.epp_id
        known_nameservers = known_domain.list_nameservers()

    if real_admin_contact_id or real_tech_contact_id or real_billing_contact_id:
        if known_domain:
            if not known_tech_contact_id and not known_admin_contact_id and not known_billing_contact_id:
                if dry_run:
                    errors.append('%s: no contacts present for known domain' % domain)
                    return errors
    else:
        errors.append('%s: no csv contacts provided for domain' % domain)
        return errors

    if real_registrant_contact_id:
    #--- registrant check
        _errs, need_registrant = check_contact_to_be_created(
            domain_name=domain,
            known_epp_contact_id=known_registrant_contact_id,
            real_epp_contact_id=real_registrant_contact_id,
            real_owner=owner_account,
        )
        if dry_run:
            errors.extend(_errs)

    if real_admin_contact_id:
    #--- admin contact check
        _errs, need_admin_contact = check_contact_to_be_created(
            domain_name=domain,
            known_epp_contact_id=known_admin_contact_id,
            real_epp_contact_id=real_admin_contact_id,
            real_owner=owner_account,
        )
        if dry_run:
            errors.extend(_errs)

    if real_tech_contact_id:
    #--- tech contact check
        _errs, need_tech_contact = check_contact_to_be_created(
            domain_name=domain,
            known_epp_contact_id=known_tech_contact_id,
            real_epp_contact_id=real_tech_contact_id,
            real_owner=owner_account,
        )
        if dry_run:
            errors.extend(_errs)

    if real_billing_contact_id:
    #--- billing contact check
        _errs, need_billing_contact = check_contact_to_be_created(
            domain_name=domain,
            known_epp_contact_id=known_billing_contact_id,
            real_epp_contact_id=real_billing_contact_id,
            real_owner=owner_account,
        )
        if dry_run:
            errors.extend(_errs)

    if not dry_run:
        if need_registrant:
    #--- registrant create
            new_registrant_contact = zcontacts.registrant_create(
                epp_id=real_registrant_contact_id,
                owner=owner_account,
                **csv_info['registrant'],
            )
            # TODO: make sure contact was assigned to the domain
        else:
            zcontacts.registrant_update(
                epp_id=real_registrant_contact_id,
                **csv_info['registrant'],
            )
    
        if need_admin_contact:
    #--- admin contact create
            new_admin_contact = zcontacts.contact_create(
                epp_id=real_admin_contact_id,
                owner=owner_account,
                **csv_info['admin'],
            )
            # TODO: make sure contact was assigned to the domain
        else:
            if real_admin_contact_id and real_admin_email:
                zcontacts.contact_update(
                    epp_id=real_admin_contact_id,
                    **csv_info['admin'],
                )

        if need_tech_contact:
    #--- tech contact create
            new_tech_contact = zcontacts.contact_create(
                epp_id=real_tech_contact_id,
                owner=owner_account,
                **csv_info['tech'],
            )
            # TODO: make sure contact was assigned to the domain
        else:
            if real_tech_contact_id and real_tech_email:
                zcontacts.contact_update(
                    epp_id=real_tech_contact_id,
                    **csv_info['tech'],
                )
    
        if need_billing_contact:
    #--- billing contact create
            new_billing_contact = zcontacts.contact_create(
                epp_id=real_billing_contact_id,
                owner=owner_account,
                **csv_info['billing'],
            )
            # TODO: make sure contact was assigned to the domain
        else:
            if real_billing_contact_id and real_billing_email:
                zcontacts.contact_update(
                    epp_id=real_billing_contact_id,
                    **csv_info['billing'],
                )
    
    if not known_domain:
        if dry_run:
    #--- domain not found
            errors.append('%s: domain not exist' % domain)
            return errors
    #--- create new domain
        new_domain = zdomains.domain_create(
            domain_name=domain,
            owner=owner_account,
            expiry_date=real_expiry_date,
            create_date=real_create_date,
            epp_id=real_epp_id,
            auth_key=real_auth_key,
            registrar=real_registrar_id,
            registrant=new_registrant_contact,
            contact_admin=new_admin_contact,
            contact_tech=new_tech_contact,
            contact_billing=new_billing_contact,
            nameservers=real_nameservers,
        )

    if new_domain:
    #--- DONE, new domain created
        return []

    if known_expiry_date:
        dt = real_expiry_date - known_expiry_date
        dt_hours = float(dt.total_seconds()) / (60.0 * 60.0)
        if dt_hours >= 24:
    #--- domain expiry date not in sync
            if dry_run:
                errors.append('expiry date not in sync for %s, known is %s, real is %s' % (
                    domain, known_expiry_date, real_expiry_date, ))
                return errors
            known_domain.expiry_date = real_expiry_date
            known_domain.save()
            logger.debug('known expiry date updated for %s : %s', known_domain, real_expiry_date)
    else:
        if known_domain:
    #--- expiry date was not set
            if real_expiry_date:
                if dry_run:
                    errors.append('expiry date was not set for %s, real is %s' % (
                        domain, real_expiry_date, ))
                    return errors
                known_domain.expiry_date = real_expiry_date
                known_domain.save()
                logger.debug('expiry date was not set, now updated for %s : %s', known_domain, real_expiry_date)

    if known_create_date:
        dt = real_create_date - known_create_date
        dt_hours = float(dt.total_seconds()) / (60.0 * 60.0)
        if dt_hours >= 24:
    #--- domain create date not in sync
            if dry_run:
                errors.append('create date not in sync for %s, known is %s, real is %s' % (
                    domain, known_create_date, real_create_date, ))
                return errors
            known_domain.create_date = real_create_date
            known_domain.save()
            logger.debug('known create date updated for %s : %s', known_domain, real_create_date)
    else:
        if known_domain:
            if real_create_date:
    #--- create date was not set
                if dry_run:
                    errors.append('create date was not set for %s, real is %s' % (
                        domain, real_create_date, ))
                    return errors
                known_domain.create_date = real_create_date
                known_domain.save()
                logger.debug('create date was not set, now updated for %s : %s', known_domain, real_create_date)

    #--- check known epp_id
    if known_epp_id:
        if known_epp_id != real_epp_id:
            if dry_run:
                errors.append('epp_id not in sync for %s, known is %s, real is %s' % (
                    domain, known_epp_id, real_epp_id, ))
                return errors
            known_domain.epp_id = real_epp_id
            known_domain.save()
            logger.debug('known epp_id for %s updated : %s', known_domain, real_epp_id)
    else:
        if real_epp_id:
            if known_domain:
                if dry_run:
                    errors.append('epp_id was not set for %s, real is %s' % (
                        domain, real_epp_id, ))
                    return errors
                known_domain.epp_id = real_epp_id
                known_domain.save()
                logger.debug('epp_id was not set for %s, now updated : %s', known_domain, real_epp_id)

    #--- check auth_key
    if known_auth_key:
        if known_auth_key != real_auth_key:
            if dry_run:
                errors.append('auth_key not in sync for %s, known is %s, real is %s' % (
                    domain, known_auth_key, real_auth_key, ))
                return errors
            known_domain.auth_key = real_auth_key
            known_domain.save()
            logger.debug('known auth_key for %s updated : %s', known_domain, real_auth_key)
    else:
        if real_auth_key:
            if known_domain:
                if dry_run:
                    errors.append('auth_key was not set for %s, real is %s' % (
                        domain, real_auth_key, ))
                    return errors
                known_domain.auth_key = real_auth_key
                known_domain.save()
                logger.debug('auth_key was not set for %s, now updated : %s', known_domain, real_auth_key)

    #--- check nameservers
    for i in range(4):
        if real_nameservers[i] != known_nameservers[i]:
            if dry_run:
                errors.append('nameserver at position %s not in sync for %s, known is %s, real is %s' % (
                    domain, known_nameservers[i], real_nameservers[i], ))
                return errors

    #--- update nameservers
    if not dry_run:
        zdomains.update_nameservers(known_domain, real_nameservers)

    if errors and dry_run:
        return errors

    #--- DONE, existing domain updated
    return errors
Example #5
0
def test_domain_is_not_valid():
    assert zdomains.is_valid('-not-valid-domain-.com') is False
    assert zdomains.is_valid('test..ai') is False
Example #6
0
def test_domain_is_valid():
    assert zdomains.is_valid('test.com') is True