Beispiel #1
0
def refleshGetDomains(request):
    if request.method == 'POST':
        clientip = getIp(request)
        logger.info('%s is requesting. %s' %(clientip, request.get_full_path()))

        secretkey='c5QehaK1bQ9oKoDpOsNsiPvHSbdYQKB1'
        secretid='AKID75tX0ViCMVbcVJoqmbFjCfx35wNsshIs'
        req = tcApi(secretid, secretkey)
        results, status = req.getdomains()
        #secretkey='speedfeng@123'
        #secretid='speedfeng'
        #tcapi = wsApi(secretid, secretkey)
        #results, status= tcapi.getdomains()
        
        return HttpResponse(json.dumps(results))

    elif request.method == 'GET':
        return HttpResponse('You get nothing!')
    else:
        return HttpResponse('nothing!')
Beispiel #2
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.filter(status=1).all()

        # 获取 cdn缓存项目
        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)

        # 获取指定 cdn 的域名
        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()
                if status:
                    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,
                                })
            elif cdn.get_name_display() == "aws":
                req = awsApi(cdn.secretid, cdn.secretkey)
                results, status = req.getdomains(['fenghuang'])
                if status:
                    for item in results:
                        tmpdict['domain'].append({
                                'Id': item['Id'],
                                'name': item['domains'],
                                'ssl' : 0,
                                'product':  item['product'],
                                'customer': item['customer']
                            })

            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!')
Beispiel #3
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'], status=1)
            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)
Beispiel #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.filter(status=1).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, 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!')