53 行
1.2 KiB
Python
53 行
1.2 KiB
Python
|
import traceback
|
||
|
import logging
|
||
|
import requests
|
||
|
import json
|
||
|
from urllib.parse import urlparse
|
||
|
import re
|
||
|
from colorama import Fore, Style
|
||
|
import socket
|
||
|
|
||
|
def get_cloudflare_ips():
|
||
|
r = requests.get('https://www.cloudflare.com/ips-v4')
|
||
|
return r.text.split('\n')
|
||
|
|
||
|
|
||
|
cloudflare_ips = get_cloudflare_ips()
|
||
|
|
||
|
|
||
|
|
||
|
def ip2bin(ip): return "".join(
|
||
|
map(
|
||
|
str,
|
||
|
[
|
||
|
"{0:08b}".format(int(x)) for x in ip.split(".")
|
||
|
]
|
||
|
)
|
||
|
)
|
||
|
|
||
|
|
||
|
def is_cloudflare(url):
|
||
|
instance_ip = None
|
||
|
try:
|
||
|
instance_ip = socket.gethostbyname(urlparse(url).hostname)
|
||
|
if instance_ip is None:
|
||
|
return False
|
||
|
except Exception:
|
||
|
return False
|
||
|
instance_bin = ip2bin(instance_ip)
|
||
|
|
||
|
for cloudflare_ip_mask in cloudflare_ips:
|
||
|
cloudflare_ip = cloudflare_ip_mask.split('/')[0]
|
||
|
cloudflare_bin = ip2bin(cloudflare_ip)
|
||
|
|
||
|
mask = int(cloudflare_ip_mask.split('/')[1])
|
||
|
cloudflare_bin_masked = cloudflare_bin[:mask]
|
||
|
instance_bin_masked = instance_bin[:mask]
|
||
|
|
||
|
if cloudflare_bin_masked == instance_bin_masked:
|
||
|
print(url + ' is behind ' + Fore.RED +
|
||
|
'cloudflare' + Style.RESET_ALL)
|
||
|
return True
|
||
|
return False
|
||
|
|
||
|
is_cloudflare('https://pipedapi-libre.kavin.rocks')
|