Exemplo n.º 1
0
def refleshGetProject(request):
    if request.method == 'POST':
        clientip = getIp(request)
        logger.info('%s is requesting. %s' %
                    (clientip, request.get_full_path()))

        data = {'cdn_proj': [], 'cdn': []}
        cdn_projs = cdn_proj_t.objects.all()
        cdns = cdn_t.objects.all()
        for prot in cdn_projs:
            tmpdict = {}
            tmpdict['project'] = prot.get_project_display()
            tmpdict['domain'] = [{
                'id':
                domain.id,
                'name':
                urlparse.urlsplit(domain.name).scheme + "://" +
                urlparse.urlsplit(domain.name).netloc,
                'product':
                domain.get_product_display(),
                'customer':
                domain.get_customer_display()
            } for domain in prot.domain.all()]
            #tmpdict['cdn']     = [ {'name': cdn.get_name_display(),
            #                        'account': cdn.account} for cdn in cdn_t.objects.all() ]
            data['cdn_proj'].append(tmpdict)
        for cdn in cdns:
            tmpdict = {
                'id': cdn.id,
                'name': cdn.get_name_display(),
                'account': cdn.account,
                'domain': [],
            }
            if cdn.get_name_display() == "tencent":
                req = tcApi(cdn.secretid, cdn.secretkey)
                results, status = req.getdomains()
                for line in results['data']['hosts']:
                    if line['disabled'] == 0 and line['status'] in [3, 4, 5]:
                        tmpdict['domain'].append({
                            'name':
                            line['host'],
                            'ssl':
                            1 if line['ssl_type'] != 0 else 0,
                        })
            elif cdn.get_name_display() == "wangsu":
                req = wsApi(cdn.secretid, cdn.secretkey)
                results, status = req.getdomains()
                for line in results:
                    if line['enabled'] == 'true':
                        tmpdict['domain'].append({
                            'name':
                            line['domain-name'],
                            'ssl':
                            1 if line['service-type'] == 'web-https' else 0,
                        })
            else:
                tmpdict['domain'] = []

            data['cdn'].append(tmpdict)

        return HttpResponse(json.dumps(data))

    elif request.method == 'GET':
        return HttpResponse('You get nothing!')
    else:
        return HttpResponse('nothing!')
Exemplo n.º 2
0
def refleshExecuteCdn(request):
    username = request.user.username
    try:
        role = request.user.userprofile.role
    except:
        role = 'none'
    clientip = getIp(request)

    if request.is_websocket():
        for postdata in request.websocket:
            data = json.loads(postdata)
            logger.info('%s is requesting. %s 执行参数:%s' %
                        (clientip, request.get_full_path(), data))
            ### step one ##
            info = {}
            info['step'] = 'one'
            request.websocket.send(json.dumps(info))
            #time.sleep(2)
            ### two step ###
            info['step'] = 'two'
            cdn_d = {}
            cdn = cdn_t.objects.get(id=data['id'])
            cdn_d[cdn.get_name_display() + "_" + cdn.account] = {
                'name': cdn.get_name_display(),
                'domain': data['domain'],
                'secretid': str(cdn.secretid),
                'secretkey': str(cdn.secretkey),
                'failed': [],
                'sccess': [],
            }

            #logger.info(cdn_d)
            for cdn in cdn_d:
                info['cdn'] = cdn
                if cdn_d[cdn]['domain']:
                    #开始清缓存,判断CDN接口是否存在
                    if cdn_d[cdn]['name'] == "tencent":
                        req = tcApi(cdn_d[cdn]['secretid'],
                                    cdn_d[cdn]['secretkey'])
                    elif cdn_d[cdn]['name'] == "wangsu":
                        req = wsApi(cdn_d[cdn]['secretid'],
                                    cdn_d[cdn]['secretkey'])
                    else:
                        info['result'] = ["CDN 接口不存在!"]
                        cdn_d[cdn]['failed'].append("%s: 接口不存在!" % cdn)
                        request.websocket.send(json.dumps(info))
                        continue

                    while len(cdn_d[cdn]['domain']) != 0:
                        domains_c = cdn_d[cdn]['domain'][:10]
                        cdn_d[cdn]['domain'] = cdn_d[cdn]['domain'][10:]

                        for uri in data['uri']:
                            result, status = req.purge(domains_c, uri)
                            if status:
                                info['result'] = [
                                    domain + uri + ": 清缓存成功。"
                                    for domain in domains_c
                                ]
                                cdn_d[cdn]['sccess'] += [
                                    domain + uri for domain in domains_c
                                ]
                            else:
                                info['result'] = [
                                    domain + uri + ": 清缓存失败!"
                                    for domain in domains_c
                                ]
                                cdn_d[cdn]['failed'] += [
                                    domain + uri for domain in domains_c
                                ]
                            request.websocket.send(json.dumps(info))
            info['step'] = 'final'
            request.websocket.send(json.dumps(info))
            for cdn in cdn_d:
                if cdn_d[cdn]['failed']:
                    message["text"] = cdn_d[cdn]['failed']
                    message['caption'] = cdn + ': 域名缓存清理失败!'
                    sendTelegramRe(message)
                if cdn_d[cdn]['sccess']:
                    message["text"] = cdn_d[cdn]['sccess']
                    message['caption'] = cdn + ': 域名缓存清理成功。'
                    sendTelegramRe(message)
            break
            ### close websocket ###
            request.websocket.close()

    else:
        return HttpResponse('nothing!', status=500)
    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

        ### step one ##
        info = {}
        info['step'] = 'one'
        self.message.reply_channel.send({'text': json.dumps(info)})
        #time.sleep(2)
        ### two step ###
        info['step'] = 'two'
        cdn_d = {}
        cdn = cdn_t.objects.get(id=data['id'])
        cdn_d[cdn.get_name_display() + "_" + cdn.account] = {
            'name': cdn.get_name_display(),
            'domain': data['domain'],
            'secretid': str(cdn.secretid),
            'secretkey': str(cdn.secretkey),
            'failed': [],
            'success': [],
        }

        #logger.info(cdn_d)
        for cdn in cdn_d:
            info['cdn'] = cdn
            if cdn_d[cdn]['domain']:
                #开始清缓存,判断CDN接口是否存在
                if cdn_d[cdn]['name'] == "tencent":
                    req = tcApi(cdn_d[cdn]['secretid'],
                                cdn_d[cdn]['secretkey'])
                elif cdn_d[cdn]['name'] == "wangsu":
                    req = wsApi(cdn_d[cdn]['secretid'],
                                cdn_d[cdn]['secretkey'])
                else:
                    info['result'] = ["CDN 接口不存在!"]
                    cdn_d[cdn]['failed'].append("%s: 接口不存在!" % cdn)
                    self.message.reply_channel.send({'text': json.dumps(info)})
                    continue

                while len(cdn_d[cdn]['domain']) != 0:
                    domains_c = cdn_d[cdn]['domain'][:10]
                    cdn_d[cdn]['domain'] = cdn_d[cdn]['domain'][10:]

                    for uri in data['uri']:
                        result, status = req.purge(domains_c, uri)
                        if status:
                            info['result'] = [
                                domain + uri + ": 清缓存成功。"
                                for domain in domains_c
                            ]
                            cdn_d[cdn]['success'] += [
                                domain + uri for domain in domains_c
                            ]
                        else:
                            info['result'] = [
                                domain + uri + ": 清缓存失败!"
                                for domain in domains_c
                            ]
                            cdn_d[cdn]['failed'] += [
                                domain + uri for domain in domains_c
                            ]
                        self.message.reply_channel.send(
                            {'text': json.dumps(info)})
        info['step'] = 'final'
        self.message.reply_channel.send({'text': json.dumps(info)})
        for cdn in cdn_d:
            if cdn_d[cdn]['failed']:
                message["text"] = cdn_d[cdn]['failed']
                message['caption'] = cdn + ': 域名缓存清理失败!'
                sendTelegramRe(message)
            if cdn_d[cdn]['success']:
                message["text"] = cdn_d[cdn]['success']
                message['caption'] = cdn + ': 域名缓存清理成功。'
                sendTelegramRe(message)
        self.close()
Exemplo n.º 4
0
def refleshPurge(request):
    if request.method == 'POST':
        clientip = getIp(request)
        cdn_d = {}
        info = {'failed': [], 'sccess': []}
        data = json.loads(request.body)
        logger.info('%s is requesting. %s 执行参数:%s' %
                    (clientip, request.get_full_path(), data))
        cdns = cdn_t.objects.all()
        for cdn in cdns:
            cdn_d[cdn.get_name_display() + "_" + cdn.account] = {
                'name': cdn.get_name_display(),
                'domain': [],
                'secretid': str(cdn.secretid),
                'secretkey': str(cdn.secretkey),
                'failed': [],
                'sccess': [],
            }

        #按照项目进行缓存清理
        #cdn_proj_l = cdn_proj_t.objects.filter(project__in = data['cdn_proj']).all()
        #for cdn_proj in cdn_proj_l:
        #    for domain in cdn_proj.domain.all():
        #        for cdn in domain.cdn.all():
        #            cdn_d[cdn.get_name_display()+"_"+cdn.account]['domain'].append(urlparse.urlsplit(domain.name).scheme+"://"+urlparse.urlsplit(domain.name).netloc)

        try:
            if 'cdn_proj' in data.keys():
                cdn_proj_l = cdn_proj_t.objects.filter(
                    project__in=data['cdn_proj']).all()
                for cdn_proj in cdn_proj_l:
                    for domain in cdn_proj.domain.all():
                        for cdn in domain.cdn.all():
                            cdn_d[cdn.get_name_display() + "_" +
                                  cdn.account]['domain'].append(
                                      urlparse.urlsplit(domain.name).scheme +
                                      "://" +
                                      urlparse.urlsplit(domain.name).netloc)
            else:

                for name in data['domain']:
                    #name = domain if urlparse.urlsplit(domain).netloc == "" else urlparse.urlsplit(domain).netloc
                    domain_s = domains.objects.filter(
                        name__icontains=name.rstrip("/"), status=1).first()
                    for cdn in domain_s.cdn.all():
                        cdn_d[cdn.get_name_display() + "_" +
                              cdn.account]['domain'].append(
                                  urlparse.urlsplit(domain_s.name).scheme +
                                  "://" +
                                  urlparse.urlsplit(domain_s.name).netloc)
                if not isinstance(data['uri'], list):
                    return HttpResponseServerError("uri错误!")

        except Exception as e:
            logger.error("执行清缓存失败: %s" % str(e))
            return HttpResponseServerError("执行清缓存失败: %s" % str(e))

        for cdn in cdn_d:
            info['cdn'] = cdn
            if cdn_d[cdn]['domain']:
                #开始清缓存,判断CDN接口是否存在
                if cdn_d[cdn]['name'] == "tencent":
                    req = tcApi(cdn_d[cdn]['secretid'],
                                cdn_d[cdn]['secretkey'])
                elif cdn_d[cdn]['name'] == "wangsu":
                    req = wsApi(cdn_d[cdn]['secretid'],
                                cdn_d[cdn]['secretkey'])
                else:
                    cdn_d[cdn]['failed'].append("%s: 接口不存在!" % cdn)
                    continue

                while len(cdn_d[cdn]['domain']) != 0:
                    domains_c = cdn_d[cdn]['domain'][:10]
                    cdn_d[cdn]['domain'] = cdn_d[cdn]['domain'][10:]

                    for uri in data['uri']:
                        result, status = req.purge(domains_c, uri)
                        if status:
                            cdn_d[cdn]['sccess'] += [
                                domain + uri for domain in domains_c
                            ]
                        else:
                            cdn_d[cdn]['failed'] += [
                                domain + uri for domain in domains_c
                            ]
        for cdn in cdn_d:
            if cdn_d[cdn]['failed']:
                message["text"] = cdn_d[cdn]['failed']
                message['caption'] = cdn + ': 域名缓存清理失败!'
                sendTelegramRe(message)
            if cdn_d[cdn]['sccess']:
                message["text"] = cdn_d[cdn]['sccess']
                message['caption'] = cdn + ': 域名缓存清理成功。'
                sendTelegramRe(message)

        return HttpResponse('success!')
    elif request.method == 'GET':
        return HttpResponse('You get nothing!')
    else:
        return HttpResponse('nothing!')
    def receive(self, text=None, bytes=None, **kwargs):
        """
        Called when a message is received with either text or bytes
        filled out.
        """
        #self.close()
        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'

        ### step one ##
        info = {}
        info['result'] = []
        info['step'] = 'one'
        self.message.reply_channel.send({'text': json.dumps(info)})
        #time.sleep(2)
        ### two step ###
        info['step'] = 'two'
        cdn_d = {}
        cdns = cdn_t.objects.all()
        for cdn in cdns:
            cdn_d[cdn.get_name_display() + "_" + cdn.account] = {
                'name': cdn.get_name_display(),
                'domain': [],
                'secretid': str(cdn.secretid),
                'secretkey': str(cdn.secretkey),
                'failed': [],
                'success': [],
            }
        cf_d = {}
        cfs = cf_t.objects.all()
        for cf in cfs:
            cf_d[cf.name] = {
                'name': cf.name,
                'domain': [],
                'email': str(cf.email),
                'key': str(cf.key),
                'failed': [],
                'success': [],
            }

        domain_l = domains.objects.filter(id__in=data['domain']).all()
        logger.info(domain_l)
        for domain in domain_l:
            for cdn in domain.cdn.all():
                cdn_d[cdn.get_name_display() + "_" +
                      cdn.account]['domain'].append(
                          urlparse.urlsplit(domain.name).scheme + "://" +
                          urlparse.urlsplit(domain.name).netloc)
            for cf in domain.cf.all():
                cf_d[cf.name]['domain'].append(
                    urlparse.urlsplit(domain.name).scheme + "://" +
                    urlparse.urlsplit(domain.name).netloc)
        #logger.info(cdn_d)
        for cdn in cdn_d:
            info['cdn'] = cdn
            if cdn_d[cdn]['domain']:
                #开始清缓存,判断CDN接口是否存在
                if cdn_d[cdn]['name'] == "tencent":
                    req = tcApi(cdn_d[cdn]['secretid'],
                                cdn_d[cdn]['secretkey'])
                elif cdn_d[cdn]['name'] == "wangsu":
                    req = wsApi(cdn_d[cdn]['secretid'],
                                cdn_d[cdn]['secretkey'])
                else:
                    info['result'] = ["CDN 接口不存在!"]
                    cdn_d[cdn]['failed'].append("%s: 接口不存在!" % cdn)
                    self.message.reply_channel.send({'text': json.dumps(info)})
                    continue

                while len(cdn_d[cdn]['domain']) != 0:
                    domains_c = cdn_d[cdn]['domain'][:10]
                    cdn_d[cdn]['domain'] = cdn_d[cdn]['domain'][10:]

                    for uri in data['uri']:
                        result, status = req.purge(domains_c, uri)
                        if status:
                            info['result'] = [
                                cdn + ": " + domain + uri + ": 清缓存成功。"
                                for domain in domains_c
                            ]
                            cdn_d[cdn]['success'] += [
                                domain + uri for domain in domains_c
                            ]
                        else:
                            info['result'] = [
                                cdn + ": " + domain + uri + ": 清缓存失败!"
                                for domain in domains_c
                            ]
                            cdn_d[cdn]['failed'] += [
                                domain + uri for domain in domains_c
                            ]
                        self.message.reply_channel.send(
                            {'text': json.dumps(info)})

        for cf in cf_d:
            info['cf'] = cf
            if cf_d[cf]['domain']:
                #开始清CF缓存
                req = CfApi(CF_URL, cf_d[cf]['email'], cf_d[cf]['key'])

                for domain in cf_d[cf]['domain']:
                    zone = ".".join(domain.split(".")[-2:])
                    zone_id = req.GetZoneId(zone)['zone_id']
                    if not zone_id:
                        info['result'] = [
                            "CloudFlare_" + cf + ": " + domain + ": 清缓存失败!"
                        ]
                        cf_d[cf]['failed'] += [domain]

                    result = req.purge(zone_id)
                    if result['success']:
                        info['result'] = [
                            "CloudFlare_" + cf + ": " + domain + ": 清缓存成功。"
                        ]
                        cf_d[cf]['success'] += [domain]
                    else:
                        info['result'] = [
                            "CloudFlare_" + cf + ": " + domain + ": 清缓存失败!"
                        ]
                        cf_d[cf]['failed'] += [domain]
                    self.message.reply_channel.send({'text': json.dumps(info)})

        info['step'] = 'final'
        self.message.reply_channel.send({'text': json.dumps(info)})
        for cdn in cdn_d:
            if cdn_d[cdn]['failed']:
                message["text"] = cdn_d[cdn]['failed']
                message['caption'] = cdn + ': 域名缓存清理失败!'
                sendTelegramRe(message)
            if cdn_d[cdn]['success']:
                message["text"] = cdn_d[cdn]['success']
                message['caption'] = cdn + ': 域名缓存清理成功。'
                sendTelegramRe(message)
        for cf in cf_d:
            if cf_d[cf]['failed']:
                message["text"] = cf_d[cf]['failed']
                message['caption'] = "CloudFlare_" + cf + ': 域名缓存清理失败!'
                sendTelegramRe(message)
            if cf_d[cf]['success']:
                message["text"] = cf_d[cf]['success']
                message['caption'] = "CloudFlare_" + cf + ': 域名缓存清理成功。'
                sendTelegramRe(message)
        self.close()