mirror of
https://github.com/itdoginfo/allow-domains.git
synced 2025-04-13 01:09:08 +00:00
Get subnets logic
This commit is contained in:
parent
e3c87cb00d
commit
bfe83a7305
3 changed files with 101 additions and 2 deletions
4
.github/workflows/create-lists.yml
vendored
4
.github/workflows/create-lists.yml
vendored
|
@ -4,9 +4,9 @@ on:
|
|||
push:
|
||||
branches: [ "main" ]
|
||||
paths:
|
||||
- .github/**
|
||||
- .github/create-lists.yml
|
||||
- src/**
|
||||
- '*.py'
|
||||
- convert.py
|
||||
schedule:
|
||||
- cron: '29 */8 * * *'
|
||||
|
||||
|
|
33
.github/workflows/create-subnets.yml
vendored
Normal file
33
.github/workflows/create-subnets.yml
vendored
Normal file
|
@ -0,0 +1,33 @@
|
|||
name: Create subnets
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "subnets" ]
|
||||
paths:
|
||||
- .github/create-subnets.yml
|
||||
- get-subnets.py
|
||||
schedule:
|
||||
- cron: '15 7 * * 1'
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
jobs:
|
||||
generate:
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- uses: actions/checkout@v4.1.7
|
||||
- name: Generate subnets
|
||||
uses: actions/setup-python@v5.1.0
|
||||
with:
|
||||
python-version: '3.10'
|
||||
- run: |
|
||||
python get-subnets.py
|
||||
- name: Push subnets
|
||||
uses: EndBug/add-and-commit@v9.1.4
|
||||
with:
|
||||
add: 'Subnets'
|
||||
author_name: GitHub Action
|
||||
author_email: githubaction@githubaction.com
|
||||
message: 'Update subnet'
|
||||
push: true
|
66
get-subnets.py
Executable file
66
get-subnets.py
Executable file
|
@ -0,0 +1,66 @@
|
|||
#!/usr/bin/python3.10
|
||||
|
||||
import ipaddress
|
||||
import urllib.request
|
||||
|
||||
BGP_TOOLS_URL = 'https://bgp.tools/table.txt'
|
||||
USER_AGENT = 'itdog.info - hi@itdog.info'
|
||||
AS_FILE = 'AS.lst'
|
||||
IPv4_DIR = 'Subnets/IPv4'
|
||||
IPv6_DIR = 'Subnets/IPv6'
|
||||
|
||||
AS_META = '32934'
|
||||
AS_TWITTER = '13414'
|
||||
META = 'Meta.lst'
|
||||
TWITTER = 'Twitter.lst'
|
||||
|
||||
subnet_list = []
|
||||
|
||||
def subnet_summarization(subnet_list):
|
||||
subnets = [ipaddress.ip_network(subnet) for subnet in subnet_list]
|
||||
return list(ipaddress.collapse_addresses(subnets))
|
||||
|
||||
def process_subnets(subnet_list, target_as):
|
||||
ipv4_subnets = []
|
||||
ipv6_subnets = []
|
||||
|
||||
for subnet_str, as_number in subnet_list:
|
||||
try:
|
||||
subnet = ipaddress.ip_network(subnet_str)
|
||||
if as_number == target_as:
|
||||
if subnet.version == 4:
|
||||
ipv4_subnets.append(subnet_str)
|
||||
elif subnet.version == 6:
|
||||
ipv6_subnets.append(subnet_str)
|
||||
except ValueError:
|
||||
print(f"Invalid subnet: {subnet_str}")
|
||||
sys.exit(1)
|
||||
|
||||
ipv4_merged = subnet_summarization(ipv4_subnets)
|
||||
ipv6_merged = subnet_summarization(ipv6_subnets)
|
||||
|
||||
return ipv4_merged, ipv6_merged
|
||||
|
||||
def write_subnets_to_file(subnets, filename):
|
||||
with open(filename, 'w') as file:
|
||||
for subnet in subnets:
|
||||
file.write(f'{subnet}\n')
|
||||
|
||||
if __name__ == '__main__':
|
||||
request = urllib.request.Request(BGP_TOOLS_URL, headers={'User-Agent': USER_AGENT})
|
||||
|
||||
with urllib.request.urlopen(request) as response:
|
||||
for line in response:
|
||||
decoded_line = line.decode('utf-8').strip()
|
||||
subnet, as_number = decoded_line.split()
|
||||
subnet_list.append((subnet, as_number))
|
||||
|
||||
# Meta
|
||||
ipv4_merged_meta, ipv6_merged_meta = process_subnets(subnet_list, AS_META)
|
||||
write_subnets_to_file(ipv4_merged_meta, f'{IPv4_DIR}/{META}')
|
||||
write_subnets_to_file(ipv6_merged_meta, f'{IPv6_DIR}/{META}')
|
||||
|
||||
# Twitter
|
||||
ipv4_merged_twitter, ipv6_merged_twitter = process_subnets(subnet_list, AS_TWITTER)
|
||||
write_subnets_to_file(ipv4_merged_twitter, f'{IPv4_DIR}/{TWITTER}')
|
||||
write_subnets_to_file(ipv6_merged_twitter, f'{IPv6_DIR}/{TWITTER}')
|
Loading…
Add table
Reference in a new issue