Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python: reformatted, fixed bugs #2504

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 34 additions & 31 deletions utils/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,41 @@
import urllib.request
import netaddr

GOOG_URL="https://www.gstatic.com/ipranges/goog.json"
CLOUD_URL="https://www.gstatic.com/ipranges/cloud.json"
GOOG_URL = "https://www.gstatic.com/ipranges/goog.json"
CLOUD_URL = "https://www.gstatic.com/ipranges/cloud.json"


def read_url(url):
try:
s = urllib.request.urlopen(url).read()
return json.loads(s)
except urllib.error.HTTPError:
print("Invalid HTTP response from %s" % url)
return {}
except json.decoder.JSONDecodeError:
print("Could not parse HTTP response from %s" % url)
return {}
try:
s = urllib.request.urlopen(url).read()
return json.loads(s)
except urllib.request.HTTPError:
print("Invalid HTTP response from %s" % url)
return {}
except json.decoder.JSONDecodeError:
print("Could not parse HTTP response from %s" % url)
return {}


def main():
goog_json=read_url(GOOG_URL)
cloud_json=read_url(CLOUD_URL)

if goog_json and cloud_json:
# print("{} published: {}".format(GOOG_URL,goog_json.get('creationTime')))
# print("{} published: {}".format(CLOUD_URL,cloud_json.get('creationTime')))
goog_cidrs = netaddr.IPSet()
for pref in goog_json['prefixes']:
if pref.get('ipv4Prefix'):
goog_cidrs.add(pref.get('ipv4Prefix'))
cloud_cidrs = netaddr.IPSet()
for pref in cloud_json['prefixes']:
if pref.get('ipv4Prefix'):
cloud_cidrs.add(pref.get('ipv4Prefix'))
# print("IP ranges for Google APIs and services default domains:")
for i in goog_cidrs.difference(cloud_cidrs).iter_cidrs():
print(i)

if __name__=='__main__':
main()
goog_json = read_url(GOOG_URL)
cloud_json = read_url(CLOUD_URL)

if goog_json and cloud_json:
# print("{} published: {}".format(GOOG_URL,goog_json.get('creationTime')))
# print("{} published: {}".format(CLOUD_URL,cloud_json.get('creationTime')))
goog_cidrs = netaddr.IPSet()
for pref in goog_json['prefixes']:
if pref.get('ipv4Prefix'):
goog_cidrs.add(pref.get('ipv4Prefix'))
cloud_cidrs = netaddr.IPSet()
for pref in cloud_json['prefixes']:
if pref.get('ipv4Prefix'):
cloud_cidrs.add(pref.get('ipv4Prefix'))
# print("IP ranges for Google APIs and services default domains:")
for i in goog_cidrs.difference(cloud_cidrs).iter_cidrs():
print(i)


if __name__ == '__main__':
main()
65 changes: 34 additions & 31 deletions utils/google6.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,41 @@
import urllib.request
import netaddr

GOOG_URL="https://www.gstatic.com/ipranges/goog.json"
CLOUD_URL="https://www.gstatic.com/ipranges/cloud.json"
GOOG_URL = "https://www.gstatic.com/ipranges/goog.json"
CLOUD_URL = "https://www.gstatic.com/ipranges/cloud.json"


def read_url(url):
try:
s = urllib.request.urlopen(url).read()
return json.loads(s)
except urllib.error.HTTPError:
print("Invalid HTTP response from %s" % url)
return {}
except json.decoder.JSONDecodeError:
print("Could not parse HTTP response from %s" % url)
return {}
try:
s = urllib.request.urlopen(url).read()
return json.loads(s)
except urllib.request.HTTPError:
print("Invalid HTTP response from %s" % url)
return {}
except json.decoder.JSONDecodeError:
print("Could not parse HTTP response from %s" % url)
return {}


def main():
goog_json=read_url(GOOG_URL)
cloud_json=read_url(CLOUD_URL)

if goog_json and cloud_json:
# print("{} published: {}".format(GOOG_URL,goog_json.get('creationTime')))
# print("{} published: {}".format(CLOUD_URL,cloud_json.get('creationTime')))
goog_cidrs = netaddr.IPSet()
for pref in goog_json['prefixes']:
if pref.get('ipv6Prefix'):
goog_cidrs.add(pref.get('ipv6Prefix'))
cloud_cidrs = netaddr.IPSet()
for pref in cloud_json['prefixes']:
if pref.get('ipv6Prefix'):
cloud_cidrs.add(pref.get('ipv6Prefix'))
# print("IP ranges for Google APIs and services default domains:")
for i in goog_cidrs.difference(cloud_cidrs).iter_cidrs():
print(i)

if __name__=='__main__':
main()
goog_json = read_url(GOOG_URL)
cloud_json = read_url(CLOUD_URL)

if goog_json and cloud_json:
# print("{} published: {}".format(GOOG_URL,goog_json.get('creationTime')))
# print("{} published: {}".format(CLOUD_URL,cloud_json.get('creationTime')))
goog_cidrs = netaddr.IPSet()
for pref in goog_json['prefixes']:
if pref.get('ipv6Prefix'):
goog_cidrs.add(pref.get('ipv6Prefix'))
cloud_cidrs = netaddr.IPSet()
for pref in cloud_json['prefixes']:
if pref.get('ipv6Prefix'):
cloud_cidrs.add(pref.get('ipv6Prefix'))
# print("IP ranges for Google APIs and services default domains:")
for i in goog_cidrs.difference(cloud_cidrs).iter_cidrs():
print(i)


if __name__ == '__main__':
main()
12 changes: 5 additions & 7 deletions utils/hostname2list.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
#!/usr/bin/env python3

import sys
import socket, struct

# This scripts is used to create "hostname/sni -> protocols" lists.
if len(sys.argv) < 6:
print("Usage: {} <file> <name> <protocol> <category> <breed>".format(sys.argv[0]))
sys.exit (1)
sys.exit(1)

name = sys.argv[2]
proto = sys.argv[3]
category = sys.argv[4]
breed = sys.argv[5]


print("""/*
*
* This file is generated automatically and part of nDPI
Expand All @@ -37,23 +35,23 @@

""")

print("static ndpi_protocol_match "+proto.lower()+"_hostname_list[] = {")
print("static ndpi_protocol_match " + proto.lower() + "_hostname_list[] = {")

lines = 0
with open(sys.argv[1]) as fp:
for cnt, line in enumerate(fp):
line = line.rstrip()

if(line != ""):
if line != "":
lines += 1
x = line.split("/")

if(len(x) == 2):
if len(x) == 2:
host = x[0]
else:
host = line

if(host != ""):
if host != "":
print(' { ' + f'"{host}", "{name}", {proto}, {category}, {breed}, NDPI_PROTOCOL_DEFAULT_LEVEL' + ' },')

print(" /* End */")
Expand Down
8 changes: 3 additions & 5 deletions utils/mergeipaddrlist.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
#!/usr/bin/env python3

import sys
import socket
import struct
import netaddr

if len (sys.argv) == 3:
if len(sys.argv) == 3:
proto = sys.argv[2]

if len(sys.argv) < 2:
print("Usage: mergeipaddrlist.py <file>")
sys.exit (1)
sys.exit(1)

ipFile = open(sys.argv[1])
ipAddresses = list(ipFile.readlines())
ipAddresses = [x.replace("\n","") for x in ipAddresses]
ipAddresses = [x.replace("\n", "") for x in ipAddresses]
ipAddresses = sorted(ipAddresses)
cidrs = netaddr.cidr_merge(ipAddresses)

Expand Down
Loading