Ejemplo n.º 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()

        return_info = {}
        return_info['permission'] = True
        data = json.loads(self.message['text'])

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

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

        step = 0

        for sub_domain in data['sub_domain']:
            step += 1
            return_info['domain'] = sub_domain + '.' + data[
                'zone'] if sub_domain != "@" else data['zone']
            return_info['step'] = step
            cf_acc = cf_account.objects.get(name=data['product'])
            try:
                cfapi = CfApi(CF_URL, cf_acc.email, cf_acc.key)
            except Exception as e:
                logger.error("新增 %s 域名失败!" % return_info['domain'])
                return_info['result'] = False
            else:
                result = cfapi.CreateZoneRecord(
                    zone_id=data['zone_id'],
                    record_name=return_info['domain'],
                    record_type=data['type'],
                    record_content=data['content'],
                    proxied=True
                    if data['proxied'].lower() == 'true' else False,
                )

                return_info['result'] = result['success']

                if return_info['result']:
                    insert_ah(
                        self.clientip, self.username, "null",
                        "'type':%s, 'name': %s, 'content': %s, 'enabled':%s" %
                        (data['type'], sub_domain + '.' + data['zone'],
                         data['content'], '1'), return_info['result'], 'add')

            self.message.reply_channel.send({'text': json.dumps(return_info)})
Ejemplo n.º 2
0
def CreateRecords(request):
    if request.method == 'POST':
        clientip = getIp(request)
        username = request.user.username
        try:
            role = request.user.userprofile.role
        except:
            role = 'none'
        #if not username:
        #    logger.info('user: 用户名未知 | [POST]%s is requesting. %s' %(clientip, request.get_full_path()))
        #    return HttpResponseServerError("用户名未知!")

        logger.info('user:%s | [POST]%s is requesting. %s' %
                    (username, clientip, request.get_full_path()))
        data = json.loads(request.body)

        #判断是否有权限
        #if not HasDnsPermission(request, "cf", data['product'], "add"):
        #    return HttpResponseServerError("抱歉,您没有新增账号[%s]解析的权限!" %data['product'])

        result_list = []
        for sub_domain in data['sub_domain']:
            cf_acc = cf_account.objects.get(name=data['product'])
            record_name = sub_domain + '.' + data[
                'zone'] if sub_domain != '@' else data['zone']

            try:
                cfapi = CfApi(CF_URL, cf_acc.email, cf_acc.key)
            except Exception as e:
                info = "新增 %s 域名失败: %s" % (record_name, str(e))
                logger.error(info)
                result = {'result': None, 'errors': str(e), 'success': False}
            else:
                result = cfapi.CreateZoneRecord(
                    zone_id=data['zone_id'],
                    record_name=record_name,
                    record_type=data['type'],
                    record_content=data['content'],
                    proxied=True
                    if data['proxied'].lower() == 'true' else False,
                )
            result_list.append(result)

            insert_ah(
                clientip, username, "null",
                "'type':%s, 'name': %s, 'content': %s, 'enabled':%s" %
                (data['type'], record_name, data['content'], '1'),
                result['success'], 'add')

            if not result['success']:
                return HttpResponseServerError(result_list)
        return HttpResponse(json.dumps(result_list))

    elif request.is_websocket():
        clientip = getIp(request)
        username = request.user.username
        try:
            role = request.user.userprofile.role
        except:
            role = 'none'
        if not username:
            request.websocket.send('userNone')
            logger.info('user: 用户名未知 | [WS]%s is requesting. %s' %
                        (clientip, request.get_full_path()))
            ### close websocket ###
            request.websocket.close()

        logger.info('user:%s | [WS]%s is requesting. %s' %
                    (username, clientip, request.get_full_path()))
        for postdata in request.websocket:
            if not postdata:
                ### close websocket ###
                request.websocket.close()
                break
            data = json.loads(postdata)

            #判断是否有权限
            if not HasDnsPermission(request, "cf", data['product'], "add"):
                request.websocket.send('noPermission')
                ### close websocket ###
                request.websocket.close()
                break

            step = 0

            for sub_domain in data['sub_domain']:
                step += 1
                return_info = {}
                return_info['domain'] = sub_domain + '.' + data[
                    'zone'] if sub_domain != "@" else data['zone']
                return_info['step'] = step
                cf_acc = cf_account.objects.get(name=data['product'])
                try:
                    cfapi = CfApi(CF_URL, cf_acc.email, cf_acc.key)
                except Exception as e:
                    logger.error("新增 %s 域名失败!" % return_info['domain'])
                    return_info['result'] = False
                else:
                    result = cfapi.CreateZoneRecord(
                        zone_id=data['zone_id'],
                        record_name=return_info['domain'],
                        record_type=data['type'],
                        record_content=data['content'],
                        proxied=True
                        if data['proxied'].lower() == 'true' else False,
                    )

                    return_info['result'] = result['success']

                insert_ah(
                    clientip, username, "null",
                    "'type':%s, 'name': %s, 'content': %s, 'enabled':%s" %
                    (data['type'], sub_domain + '.' + data['zone'],
                     data['content'], '1'), return_info['result'], 'add')

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

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

    elif request.method == 'GET':
        return HttpResponse('You get nothing!')
    else:
        return HttpResponse('nothing!')