Skip to content

Instantly share code, notes, and snippets.

@mopeneko
Last active July 3, 2023 13:28
Show Gist options
  • Save mopeneko/75e3b8119e537d0d8efddf53138c9402 to your computer and use it in GitHub Desktop.
Save mopeneko/75e3b8119e537d0d8efddf53138c9402 to your computer and use it in GitHub Desktop.
[Nexuspipe] Add Google bots IP addresses to Allowed IPs
import ipaddress
import itertools
import requests
AUTHORIZATION_HEADER = "Bearer xxxxxx"
APP = "example.com"
def get_ips(url):
resp = requests.get(url).json()
ips = list(itertools.chain.from_iterable([
[str(ip) for ip in ipaddress.IPv4Network(prefix["ipv4Prefix"])]
for prefix in resp["prefixes"]
if "ipv4Prefix" in prefix.keys()
]))
return ips
def add_allowed_ips(ips):
headers = {
'authority': 'api.nexuspipe.com',
'accept': '*/*',
'accept-language': 'en,ja-JP;q=0.9,ja;q=0.8,en-US;q=0.7',
'authorization': AUTHORIZATION_HEADER,
'content-type': 'application/json',
'dnt': '1',
'if-none-match': 'W/"12a-GNLYL0HbM+Ocfz/X0Pz5IJKpfLk"',
'origin': 'https://dash.nexuspipe.com',
'referer': 'https://dash.nexuspipe.com/',
'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
}
response = requests.get('https://api.nexuspipe.com/v1/my/apps', headers=headers).json()
apps = [
result
for result in response["result"]
if result["name"] == APP
]
if len(apps) != 1:
raise Exception("app is not found")
app = apps[0]
app["ipsAllowed"].extend(ips)
headers = {
'authority': 'api.nexuspipe.com',
'accept': '*/*',
'accept-language': 'en,ja-JP;q=0.9,ja;q=0.8,en-US;q=0.7',
'authorization': AUTHORIZATION_HEADER,
'content-type': 'application/json',
'dnt': '1',
'origin': 'https://dash.nexuspipe.com',
'referer': 'https://dash.nexuspipe.com/',
'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
}
json_data = {
'status': app["status"],
'origin': app["origin"],
'ipsAllowed': app["ipsAllowed"],
'ipsBlocked': app["ipsBlocked"],
'filterMode': app["filterMode"],
'ipReputation': app["ipReputation"],
'ignoredURIs': app["ignoredURIs"],
'requestsPerMinute': app["requestsPerMinute"],
'uriRateLimiting': app["uriRateLimiting"],
'_ignored': [
'',
],
}
requests.patch(f"https://api.nexuspipe.com/v1/app/{APP}", headers=headers, json=json_data)
if __name__ == "__main__":
urls = [
"https://developers.google.com/search/apis/ipranges/googlebot.json",
"https://developers.google.com/static/search/apis/ipranges/special-crawlers.json",
"https://developers.google.com/static/search/apis/ipranges/user-triggered-fetchers.json",
]
ips = list(itertools.chain.from_iterable(
[get_ips(url) for url in urls]
))
add_allowed_ips(ips)
print(f"{len(ips)} ips added")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment