Пример #1
0
    def receive(self, text=None, bytes=None, **kwargs):
        """
        Called when a message is received with either text or bytes
        filled out.
        """
        #self.close()

        self.clientip = '127.0.0.1'
        self.username = self.message.user.username
        try:
            self.role = self.message.user.userprofile.role
        except:
            self.role = 'none'

        data = json.loads(self.message['text'])
        step = 0

        for record in data['records']:
            step += 1
            return_info           = {}
            return_info['record'] = record
            return_info['step']   = step
            return_info['permission'] = True

            #判断是否有权限
            if not HasDnsPermission(self.message, "cf", record['product'], "change"):
                return_info['permission'] = False
                return_info['result'] = False
                self.message.reply_channel.send({'text': json.dumps(return_info)})
                continue

            cf_acc = cf_account.objects.filter(name=record['product']).first()
            cfapi  = CfApi(CF_URL, cf_acc.email, cf_acc.key)
            if data['proxied'] == 'true':
                proxied = True
            else:
                proxied = False

            result = cfapi.UpdateZoneRecord(record['zone_id'], data['type'], record['name'], data['content'], proxied=proxied, record_id=record['record_id'])
            if not result['success']:
                return_info['result'] = False
            else:
                return_info['result'] = True
            logger.info("req_ip: %s | user: %s | updaterecord: { 'type':%s, 'name': %s, 'content': %s, 'proxied':%s } ---> { 'type':%s, 'name': %s, 'content': %s, 'proxied':%s }" %(self.clientip, self.username, record['type'], record['name'], record['content'], record['proxied'], data['type'], record['name'], data['content'], proxied))

            insert_ah(self.clientip, self.username, 
                    "'type':%s, 'name': %s, 'content': %s, 'proxied':%s" %(record['type'], record['name'], record['content'], record['proxied']),
                    "'type':%s, 'name': %s, 'content': %s, 'proxied':%s" %(data['type'], record['name'], data['content'], proxied),
                    return_info['result'])

            self.message.reply_channel.send({'text': json.dumps(return_info)})
Пример #2
0
def UpdateApiRoute(request):
    if request.method == 'POST':
        clientip = request.META['REMOTE_ADDR']
        data = json.loads(request.body)
        logger.info('%s is requesting. %s data: %s' %
                    (clientip, request.get_full_path(), data))

        domain_l = domain_info.objects.filter(domain=data['domain']).all()
        zone_id = domain_l[0].zone_id
        record_id = domain_l[0].record_id
        cf_account_name = domain_l[0].cf_account_name
        return_info = {}
        r_type = 'CNAME'

        cf_acc = cf_account.objects.filter(name=cf_account_name).first()
        cfapi = CfApi(CF_URL, cf_acc.email, cf_acc.key)

        if data['route'] == 'cloudflare':
            proxied = True
        else:
            proxied = False

        if data['route'] == 'nginx':
            content = [
                domain_i.content for domain_i in domain_l
                if domain_i.route == 'nginx'
            ]
        elif data['route'] == 'cloudflare':
            content = [
                domain_i.content for domain_i in domain_l
                if domain_i.route == 'cloudflare'
            ]
        elif data['route'] == 'aegins':
            content = [
                domain_i.content for domain_i in domain_l
                if domain_i.route == 'aegins'
            ]
        elif data['route'] == 'wangsu':
            content = [
                domain_i.content for domain_i in domain_l
                if domain_i.route == 'wangsu'
            ]

        result = cfapi.UpdateZoneRecord(zone_id,
                                        r_type,
                                        data['domain'],
                                        content[0],
                                        proxied=proxied,
                                        record_id=record_id)

        #logger.info(result)

        if not result['success']:
            return_info['result'] = False
            logger.error(result)
        else:
            return_info['result'] = True
            #api_list = domain_info.objects.filter(domain=data['domain'], status=1).all()
            api_list = domain_info.objects.filter(domain=data['domain']).all()
            for info in api_list:
                if info.route == data['route']:
                    info.route_status = 1
                    info.save()
                else:
                    info.route_status = 0
                    info.save()

        #logger.info(return_info)
        return HttpResponse(json.dumps(return_info))
Пример #3
0
def UpdateRecords(request):
    if request.is_websocket():
        global username, role, clientip
        if request.META.has_key('HTTP_X_FORWARDED_FOR'):
            clientip = request.META['HTTP_X_FORWARDED_FOR']
        else:
            clientip = request.META['REMOTE_ADDR']
        username = request.user.username
        try:
            role = request.user.userprofile.role
        except:
            role = 'none'
        if not username:
            request.websocket.send('userNone')
            logger.info('user: 用户名未知 | [POST]%s is requesting. %s' %
                        (clientip, request.get_full_path()))
            ### close websocket ###
            request.websocket.close()

        logger.info('user:%s | [POST]%s is requesting. %s' %
                    (username, clientip, request.get_full_path()))
        for postdata in request.websocket:
            #logger.info(type(postdata))
            if not postdata:
                logger.info('this is test!')
                break
            data = json.loads(postdata)
            step = 0

            for record in data['records']:
                step += 1
                return_info = {}
                return_info['record'] = record
                return_info['step'] = step
                return_info['permission'] = True

                #判断是否有权限
                if not HasDnsPermission(request, "cf", record['product'],
                                        "change"):
                    return_info['permission'] = False
                    return_info['result'] = False
                    request.websocket.send(json.dumps(return_info))
                    continue

                cf_acc = cf_account.objects.filter(
                    name=record['product']).first()
                cfapi = CfApi(CF_URL, cf_acc.email, cf_acc.key)
                if data['proxied'] == 'true':
                    proxied = True
                else:
                    proxied = False

                result = cfapi.UpdateZoneRecord(record['zone_id'],
                                                data['type'],
                                                record['name'],
                                                data['content'],
                                                proxied=proxied,
                                                record_id=record['record_id'])
                if not result['success']:
                    return_info['result'] = False
                else:
                    return_info['result'] = True
                logger.info(
                    "req_ip: %s | user: %s | updaterecord: { 'type':%s, 'name': %s, 'content': %s, 'proxied':%s } ---> { 'type':%s, 'name': %s, 'content': %s, 'proxied':%s }"
                    % (clientip, username, record['type'], record['name'],
                       record['content'], record['proxied'], data['type'],
                       record['name'], data['content'], proxied))

                insert_h = alter_history(
                    time=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
                    req_ip=clientip,
                    user=username,
                    pre_rec="'type':%s, 'name': %s, 'content': %s, 'proxied':%s"
                    % (record['type'], record['name'], record['content'],
                       record['proxied']),
                    now_rec="'type':%s, 'name': %s, 'content': %s, 'proxied':%s"
                    % (data['type'], record['name'], data['content'], proxied))

                insert_h.save()

                request.websocket.send(json.dumps(return_info))

            ### close websocket ###
            request.websocket.close()