暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

获取cloudflare出口ip列表

老柴杂货铺 2025-01-10
399
    import requests
    import json
    import ipaddress
    def get_local_ip():
        """从4.ipw.cn获取本地公网IP"""
        try:
            response = requests.get('http://4.ipw.cn', timeout=5)
            if response.status_code == 200:
                ip = response.text.strip()
                # 使用ip-api.com查询归属地
                geo_response = requests.get(f'http://ip-api.com/json/{ip}?lang=zh-CN')
                if geo_response.status_code == 200:
                    geo_data = geo_response.json()
                    return {
                        'ip': ip,
                        'location': f"{geo_data.get('country', '未知')} {geo_data.get('regionName', '')} {geo_data.get('city', '')}",
                        'isp': geo_data.get('isp''未知')
                    }
                return {
                    'ip': ip,
                    'location''未知',
                    'isp''未知'
                }
            return None
        except Exception as e:
            print(f"获取本地IP失败: {e}")
            return None
    def cidr_to_range(cidr):
        """将CIDR转换为IP范围"""
        network = ipaddress.ip_network(cidr)
        return f"{network[0]} - {network[-1]}"
    def get_cloudflare_ips():
        url = "https://api.cloudflare.com/client/v4/ips"
        response = requests.get(url)
        data = response.json()
        
        if data.get('success', False):
            ipv4_ranges = data['result']['ipv4_cidrs']
            # 将CIDR转换为IPv4Network对象并排序
            networks = sorted([ipaddress.ip_network(cidr) for cidr in ipv4_ranges])
            # 将每个CIDR转换为IP范围
            ranges = [f"{str(network)} => {cidr_to_range(str(network))}" for network in networks]
            return "\n".join(ranges)
        else:
            return "Failed to get Cloudflare IP ranges"
    if __name__ == "__main__":
        print("Cloudflare IPv4 Ranges:")
        print(get_cloudflare_ips())
        
        local_ip_info = get_local_ip()
        if local_ip_info:
            print("\n本地IP信息:")
            print(f"IP地址: {local_ip_info['ip']}")
            print(f"归属地: {local_ip_info['location']}")
            print(f"运营商: {local_ip_info['isp']}")
        else:
            print("\n无法获取本地IP信息")




    文章转载自老柴杂货铺,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

    评论