腾讯云cdn回源ip获取

背景:

安全性考虑(假装safe)源站做了访问策略,限制了访问ip,接入cdn后回源的都是cdn节点信息,那么如何获取呢???


找到了一篇接口文章(https://cloud.tencent.com/document/product/228/7364)用于查询 CDN 所有回源层 IP 列表。

post示例

首先搞鉴权:

参考:https://cloud.tencent.com/document/product/228/1725

import time
import random
import hmac
import hashlib
import binascii
import base64
import json
class Auth():
    def __init__(self,Action,SecretId,Secretkey,Region='ap-guangzhou'):
        self.Action,self.SecretId,self.Secretkey,self.Region = Action,SecretId,Secretkey,Region
        self.Timestamp,self.Nonce = int(time.time()),random.randint(0,999999999)
        print(self.Timestamp)
        print(self.Nonce)
    def make(self,requestHost,requestUri,params,method='POST'):
        params['Action'] = self.Action
        params['SecretId'] = self.SecretId
        params['Timestamp'] = self.Timestamp
        params['Nonce'] = self.Nonce
        # params['Region'] = self.Region
        ps = '&'.join('%s=%s'%(i,params[i]) for i in sorted(params))
        source_str = '%s%s%s?%s' % (method.upper(), requestHost, requestUri, ps)
        print(source_str)
        source_str = bytes(source_str,encoding='utf-8')
        secretkey = bytes(self.Secretkey,encoding='utf-8')
        hexstring = hmac.new(secretkey, source_str, hashlib.sha1)
        base64s = binascii.b2a_base64(hexstring.digest())[:-1]
        return  base64s.decode()
if __name__ == '__main__':
    a = Auth('GetCdnMiddleSourceList','AKIDxxxxxxxxxxxx','keyxxxxx','ap-guangzhou')
    sign = a.make('cdn.api.qcloud.com','/v2/index.php',{})
    print(sign)

替换其中的API密钥即可,密钥获取:https://console.cloud.tencent.com/cam/capi

输出参数:

请求示例:

得到最终所有回源IP,添加到源站白名单即可。

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

编辑于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券