"""アクセスログのIPが、各社の公開しているクローラーのIP範囲に入っているかを調べる。

使い方: python3 verify_ip.py 13.66.216.180
名前だけ（User-Agent）では偽装できるので、IPでも確かめる。
"""
import ipaddress, json, sys, urllib.request

LISTS = {
    "OAI-SearchBot": "https://openai.com/searchbot.json",
    "GPTBot": "https://openai.com/gptbot.json",
    "ChatGPT-User": "https://openai.com/chatgpt-user.json",
    "Anthropic（3種共通）": "https://claude.com/crawling/bots.json",
}
ip = ipaddress.ip_address(sys.argv[1])
for name, url in LISTS.items():
    req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0 (ip-range-check)"})
    data = json.load(urllib.request.urlopen(req))
    nets = [ipaddress.ip_network(p.get("ipv4Prefix") or p["ipv6Prefix"]) for p in data["prefixes"]]
    print(f"{name}: {'公式の範囲内' if any(ip in n for n in nets) else '範囲外'}")
