#!/usr/bin/env python3

# Script to batch add AS entries to the WHOIS database

import os
import sys
import re
from datetime import datetime

# Ensure the AS database directory exists
os.system("mkdir -p db/as")

# Function to convert SCION AS to decimal if in BGP range
def scion_as_to_decimal(as_id):
    """Convert SCION AS format to decimal if in BGP AS range (0 - 2^32-1)"""
    # Handle plain decimal AS numbers
    if ':' not in as_id:
        return as_id
    
    # Parse SCION format
    parts = as_id.split(':')
    
    # Check if this looks like ISD:AS:Instance format (first part is single digit ISD)
    if len(parts) == 3 and len(parts[0]) == 1 and parts[0].isdigit():
        # ISD:AS:Instance format - convert the AS part (middle) and instance (last)
        try:
            # AS is 16-bit hex, instance is 16-bit hex
            # Combine them as 32-bit number: AS << 16 | Instance
            as_num = int(parts[1], 16)
            instance = int(parts[2], 16)
            total = (as_num << 16) | instance
            
            if 0 <= total <= 0xFFFFFFFF:  # BGP AS range
                return str(total)
        except ValueError:
            pass
        return ""
    
    # For standard colon-separated hex format (e.g., 0:1:f)
    # Each part is up to 16 bits
    try:
        total = 0
        for part in parts:
            total = (total << 16) + int(part, 16)
        
        # Check if in BGP AS range
        if 0 <= total <= 0xFFFFFFFF:  # 2^32 - 1
            return str(total)
    except ValueError:
        pass
    
    return ""

# Function to add an AS entry
def add_as_entry(as_id, organization, comment="", isd="64", country="CH", uid="", status="ASSIGNED"):
    # Prepare AS data
    n = "\r\n"
    body = "% SCION AS WHOIS Information" + n + n
    body += "AS Identifier:  " + as_id + n
    body += "ISD:           " + isd + n
    
    # Automatically calculate decimal representation
    as_decimal = scion_as_to_decimal(as_id)
    body += "AS decimal:    " + as_decimal + n
    
    body += "Organization:  " + organization + n
    body += "Country:       " + country + n
    
    if uid:
        body += "UID:           " + uid + n
    
    body += "Status:        " + status + n
    
    if comment:
        body += "Description:   " + comment + n
    
    body += "Contact:       " + "noc@" + organization.lower().replace(" ", "-").replace(".", "").replace(",", "").replace("(", "").replace(")", "") + ".com" + n
    body += "DateAllocated: " + datetime.now().strftime("%Y-%m-%d") + n

    # Save to file
    file_path = "db/as/" + as_id
    d = open(file_path, "w+")
    d.write(body)
    d.close()
    print(f"Added AS {as_id} - {organization}")

# Add all the AS entries
as_entries = [
    {"as_id": "88", "organization": "Princeton University - CITP", "comment": ""},
    {"as_id": "225", "organization": "University of Virginia", "comment": ""},
    {"as_id": "559", "organization": "SWITCH", "comment": ""},
    {"as_id": "1140", "organization": "SIDN Labs", "comment": ""},
    {"as_id": "1349", "organization": "British Telecommunications PLC", "comment": ""},
    {"as_id": "1916", "organization": "Rede Nacional de Ensino e Pesquisa - RNP", "comment": ""},
    {"as_id": "2546", "organization": "NCSR Demokritos", "comment": ""},
    {"as_id": "3303", "organization": "Swisscom", "comment": ""},
    {"as_id": "3786", "organization": "LG Uplus Corp.", "comment": ""},
    {"as_id": "4158", "organization": "City University of Hong Kong", "comment": ""},
    {"as_id": "6730", "organization": "Sunrise UPC GmbH", "comment": ""},
    {"as_id": "8300", "organization": "Swisscom (Suisse) SA", "comment": ""},
    {"as_id": "8883", "organization": "UBS", "comment": ""},
    {"as_id": "9025", "organization": "SIX Group Services AG", "comment": ""},
    {"as_id": "12350", "organization": "VTX Services SA", "comment": ""},
    {"as_id": "12429", "organization": "Swisscom (Suisse) SA", "comment": ""},
    {"as_id": "12511", "organization": "PostFinance AG", "comment": ""},
    {"as_id": "12649", "organization": "Banque Pictet & Cie SA", "comment": ""},
    {"as_id": "12928", "organization": "Banque Cantonale Vaudoise", "comment": ""},
    {"as_id": "13030", "organization": "Init7", "comment": ""},
    {"as_id": "13267", "organization": "Zürcher Kantonalbank", "comment": ""},
    {"as_id": "13282", "organization": "Bank Julius Bär", "comment": ""},
    {"as_id": "13283", "organization": "Julius Bär", "comment": ""},
    {"as_id": "15361", "organization": "Union Bancaire Privée, UBP SA", "comment": ""},
    {"as_id": "15532", "organization": "Raiffeisen Schweiz Genossenschaft", "comment": ""},
    {"as_id": "15623", "organization": "Cyberlink AG", "comment": "https://www.cyberlink.ch/de/connectivity/scion"},
    {"as_id": "20965", "organization": "GEANT", "comment": ""},
    {"as_id": "21047", "organization": "Deutsche Bank AG Frankfurt a.M. - Zurich Branch", "comment": ""},
    {"as_id": "24951", "organization": "EveryWare", "comment": ""},
    {"as_id": "25289", "organization": "BNP Paribas (Suisse) SA", "comment": ""},
    {"as_id": "28928", "organization": "Schwyzer Kantonalbank", "comment": ""},
    {"as_id": "29641", "organization": "Allianz", "comment": ""},
    {"as_id": "31004", "organization": "Schweizerische Bundesbahnen SBB", "comment": ""},
    {"as_id": "31097", "organization": "Cornèr Banca S.A.", "comment": ""},
    {"as_id": "35240", "organization": "HSBC PB Services (Suisse) SA", "comment": ""},
    {"as_id": "39932", "organization": "Ergon Informatik AG", "comment": ""},
    {"as_id": "41623", "organization": "Dukascopy Bank SA", "comment": ""},
    {"as_id": "41632", "organization": "Glarner Kantonalbank", "comment": ""},
    {"as_id": "43577", "organization": "Hypothekarbank Lenzburg", "comment": ""},
    {"as_id": "44346", "organization": "Swissquote Bank SA", "comment": ""},
    {"as_id": "46646", "organization": "Bottomline Technologies Sàrl", "comment": ""},
    {"as_id": "50999", "organization": "King Abdullah University of Science and Technology", "comment": ""},
    {"as_id": "57965", "organization": "International Committee of the Red Cross", "comment": ""},
    {"as_id": "59414", "organization": "Cyberlink AG", "comment": ""},
    {"as_id": "59647", "organization": "EKT AG", "comment": ""},
    {"as_id": "60284", "organization": "Axpo WZ-Systems AG", "comment": ""},
    {"as_id": "64580", "organization": "Frankfurter Bankgesellschaft (Schweiz) AG", "comment": ""},
    {"as_id": "196722", "organization": "Schweizerische Nationalbank", "comment": ""},
    {"as_id": "197312", "organization": "Avaloq Sourcing Switzerlang & Liechtenstein SA", "comment": ""},
    {"as_id": "200888", "organization": "InfoGuard AG", "comment": ""},
    {"as_id": "202405", "organization": "Liechtensteinische Landesbank Aktiengesellschaft", "comment": ""},
    {"as_id": "202908", "organization": "LGT Financial Services AG", "comment": ""},
    {"as_id": "203311", "organization": "NATO Cooperative Cyber Defence Centre of Excellence (CCDCoE)", "comment": ""},
    {"as_id": "205755", "organization": "Sdnbucks B.V.", "comment": ""},
    {"as_id": "206662", "organization": "Inventx AG", "comment": ""},
    {"as_id": "208305", "organization": "Viseca Payment Services AG", "comment": ""},
    {"as_id": "208836", "organization": "Banca Popolare di Sondrio (Suisse) SA", "comment": ""},
    {"as_id": "210018", "organization": "Bank CIC (Switzerland) AG", "comment": ""},
    {"as_id": "212777", "organization": "Schweizerische Mobiliar Versicherungsgesellschaft AG", "comment": ""},
    {"as_id": "2:0:0", "organization": "Anapaya Systems AG", "comment": ""},
    {"as_id": "2:0:1", "organization": "Schweizerische Nationalbank", "comment": ""},
    {"as_id": "2:0:2", "organization": "SIX Group Services AG", "comment": ""},
    {"as_id": "2:0:3", "organization": "Zürcher Kantonalbank", "comment": ""},
    {"as_id": "2:0:4", "organization": "Swisscom (Suisse) SA", "comment": ""},
    {"as_id": "2:0:5", "organization": "Swisscom (Suisse) SA", "comment": ""},
    {"as_id": "2:0:6", "organization": "deprecated", "comment": ""},
    {"as_id": "2:0:7", "organization": "deprecated", "comment": ""},
    {"as_id": "2:0:8", "organization": "deprecated", "comment": ""},
    {"as_id": "2:0:9", "organization": "ETH Zurich", "comment": ""},
    {"as_id": "2:0:a", "organization": "deprecated", "comment": ""},
    {"as_id": "2:0:b", "organization": "deprecated", "comment": ""},
    {"as_id": "2:0:c", "organization": "Swiss National Supercomputing Centre", "comment": ""},
    {"as_id": "2:0:d", "organization": "deprecated", "comment": ""},
    {"as_id": "2:0:e", "organization": "deprecated", "comment": ""},
    {"as_id": "2:0:f", "organization": "Anapaya Systems AG", "comment": ""},
    {"as_id": "2:0:10", "organization": "Anapaya Systems AG", "comment": ""},
    {"as_id": "2:0:11", "organization": "Anapaya Systems AG", "comment": ""},
    {"as_id": "2:0:12", "organization": "deprecated", "comment": ""},
    {"as_id": "2:0:13", "organization": "Anapaya Systems AG", "comment": ""},
    {"as_id": "2:0:14", "organization": "deprecated", "comment": ""},
    {"as_id": "2:0:15", "organization": "deprecated", "comment": ""},
    {"as_id": "2:0:16", "organization": "Eidgenössisches Departement für auswärtige Angelegenheiten", "comment": ""},
    {"as_id": "2:0:17", "organization": "Axpo WZ-Systems AG", "comment": ""},
    {"as_id": "2:0:18", "organization": "SEC", "comment": ""},
    {"as_id": "2:0:19", "organization": "Anapaya Systems AG", "comment": ""},
    {"as_id": "2:0:1a", "organization": "Anapaya Systems AG", "comment": ""},
    {"as_id": "2:0:1b", "organization": "Sunrise UPC GmbH", "comment": ""},
    {"as_id": "2:0:1c", "organization": "Swisscom (Suisse) SA", "comment": ""},
    {"as_id": "2:0:1d", "organization": "Swisscom (Suisse) SA", "comment": ""},
    {"as_id": "2:0:1e", "organization": "Swisscom (Suisse) SA", "comment": ""},
    {"as_id": "2:0:1f", "organization": "Sunrise UPC GmbH", "comment": ""},
    {"as_id": "2:0:20", "organization": "Proximus Luxembourg S.A.", "comment": ""},
    {"as_id": "2:0:21", "organization": "Sunrise UPC GmbH", "comment": ""},
    {"as_id": "2:0:22", "organization": "Sunrise UPC GmbH", "comment": ""},
    {"as_id": "2:0:23", "organization": "InterCloud SAS", "comment": ""},
    {"as_id": "2:0:24", "organization": "InterCloud SAS", "comment": ""},
    {"as_id": "2:0:25", "organization": "InterCloud SAS", "comment": ""},
    {"as_id": "2:0:26", "organization": "InterCloud SAS", "comment": ""},
    {"as_id": "2:0:27", "organization": "Cyberlink AG", "comment": ""},
    {"as_id": "2:0:28", "organization": "Cyberlink AG", "comment": ""},
    {"as_id": "2:0:29", "organization": "VBS", "comment": ""},
    {"as_id": "2:0:2a", "organization": "CyberLink", "comment": ""},
    {"as_id": "2:0:2b", "organization": "Armasuisse", "comment": ""},
    {"as_id": "2:0:2c", "organization": "Armasuisse", "comment": ""},
    {"as_id": "2:0:2d", "organization": "Armasuisse", "comment": ""},
    {"as_id": "2:0:2e", "organization": "Health Info Net AG", "comment": ""},
    {"as_id": "2:0:2f", "organization": "deprecated", "comment": ""},
    {"as_id": "2:0:30", "organization": "deprecated", "comment": ""},
    {"as_id": "2:0:33", "organization": "Health Info Net AG", "comment": ""},
    {"as_id": "2:0:34", "organization": "BNC", "comment": ""},
    {"as_id": "2:0:35", "organization": "BRIDGES", "comment": ""},
    {"as_id": "2:0:36", "organization": "RUAG AG", "comment": ""},
    {"as_id": "2:0:37", "organization": "UBS", "comment": ""},
    {"as_id": "2:0:38", "organization": "UBS", "comment": ""},
    {"as_id": "2:0:39", "organization": "Bottomline", "comment": ""},
    {"as_id": "2:0:3a", "organization": "Mysten Labs", "comment": ""},
    {"as_id": "2:0:3b", "organization": "KREONET", "comment": ""},
    {"as_id": "2:0:3c", "organization": "KREONET", "comment": ""},
    {"as_id": "2:0:3d", "organization": "KREONET", "comment": ""},
    {"as_id": "2:0:3e", "organization": "KREONET", "comment": ""},
    {"as_id": "2:0:3f", "organization": "KREONET", "comment": ""},
    {"as_id": "2:0:40", "organization": "KREONET", "comment": ""},
    {"as_id": "2:0:41", "organization": "RUAG AG", "comment": ""},
    {"as_id": "2:0:42", "organization": "Mysten Labs", "comment": ""},
    {"as_id": "2:0:43", "organization": "Mysten Labs", "comment": ""},
    {"as_id": "2:0:44", "organization": "VTX", "comment": ""},
    {"as_id": "2:0:45", "organization": "VTX", "comment": ""},
    {"as_id": "2:0:46", "organization": "Swisscom (Suisse) SA", "comment": ""},
    {"as_id": "2:0:47", "organization": "Mysten", "comment": ""},
    {"as_id": "2:0:48", "organization": "Equinix", "comment": ""},
    {"as_id": "2:0:49", "organization": "CybExer Technologies", "comment": ""},
    {"as_id": "2:0:4a", "organization": "Otto-von-Guericke-Universität Magdeburg", "comment": ""},
    {"as_id": "2:0:4b", "organization": "Martincoit Networks", "comment": ""},
    {"as_id": "2:0:4c", "organization": "AWS PoC Anapaya", "comment": ""},
    {"as_id": "2:0:4d", "organization": "Korea University", "comment": ""},
    {"as_id": "2:0:4f", "organization": "Infoguard", "comment": ""},
    {"as_id": "2:0:50", "organization": "Mainstreaming", "comment": ""},
    {"as_id": "2:0:51", "organization": "InterCloud SAS", "comment": ""},
    {"as_id": "2:0:53", "organization": "Everyware", "comment": ""},
    {"as_id": "2:0:54", "organization": "Everyware", "comment": ""},
    {"as_id": "2:0:55", "organization": "InterCloud SAS", "comment": ""},
    {"as_id": "2:0:56", "organization": "Armasuisse", "comment": ""},
    {"as_id": "2:0:57", "organization": "Mysten Labs", "comment": ""},
    {"as_id": "2:0:58", "organization": "Mysten Labs", "comment": ""},
    {"as_id": "2:0:59", "organization": "Mysten Labs", "comment": ""},
    {"as_id": "2:0:5a", "organization": "Mysten Labs", "comment": ""},
    {"as_id": "2:0:5b", "organization": "Mysten Labs", "comment": ""},
    {"as_id": "2:0:5c", "organization": "Universidade Federal de Mato Grosso do Sul", "comment": ""},
    {"as_id": "2:0:5d", "organization": "BIS", "comment": ""},
    {"as_id": "2:0:5e", "organization": "deprecated", "comment": ""},
    {"as_id": "2:0:5f", "organization": "deprecated", "comment": ""},
    {"as_id": "2:0:60", "organization": "deprecated", "comment": ""},
    {"as_id": "2:0:61", "organization": "National University of Singapore", "comment": ""},
    {"as_id": "2:0:63", "organization": "smaro GmbH", "comment": ""},
    {"as_id": "2:0:64", "organization": "Swisscom (Suisse) SA", "comment": ""},
    {"as_id": "2:0:65", "organization": "HEIG-VD (Haute école d'ingénierie et de gestion du canton de Vaud)", "comment": ""},
    {"as_id": "2:0:66", "organization": "Kanton Solothurn", "comment": ""},
    {"as_id": "2:0:67", "organization": "Colt Netherlands", "comment": ""},
    {"as_id": "2:0:68", "organization": "Colt United Kingdom", "comment": ""},
    {"as_id": "2:0:69", "organization": "Colt Germany", "comment": ""},
    {"as_id": "2:0:6a", "organization": "Colt United Kingdom Test AS", "comment": ""},
    {"as_id": "2:0:6b", "organization": "Colt Germany Test AS", "comment": ""},
    {"as_id": "2:0:6c", "organization": "Cyberlink AG", "comment": ""},
    {"as_id": "2:0:6d", "organization": "Cyberlink AG", "comment": ""},
    {"as_id": "2:0:6e", "organization": "Cyberlink AG", "comment": ""},
    {"as_id": "2:0:6f", "organization": "Schweizerische Nationalbank", "comment": ""},
    {"as_id": "2:0:70", "organization": "Anapaya Azure Test AS", "comment": ""},
    {"as_id": "2:0:71", "organization": "InterCloud SAS", "comment": ""},
    {"as_id": "2:0:72", "organization": "Anapaya Systems AG", "comment": ""},
    {"as_id": "2:0:73", "organization": "Anapaya Systems AG", "comment": ""},
    {"as_id": "2:0:74", "organization": "Anapaya Systems AG", "comment": ""},
    {"as_id": "2:0:75", "organization": "Anapaya Systems AG", "comment": ""},
    {"as_id": "2:0:76", "organization": "Axpo WZ-Systems AG", "comment": ""},
    {"as_id": "2:0:77", "organization": "Mysten Validator", "comment": ""},
    {"as_id": "2:0:78", "organization": "Mysten Validator", "comment": ""},
    {"as_id": "2:0:79", "organization": "Mysten Validator", "comment": ""},
    {"as_id": "2:0:7a", "organization": "Mysten Validator", "comment": ""},
    {"as_id": "2:0:7b", "organization": "Mysten Validator", "comment": ""},
    {"as_id": "2:0:7c", "organization": "Mysten Validator", "comment": ""},
    {"as_id": "2:0:7d", "organization": "Mysten Validator", "comment": ""},
    {"as_id": "2:0:7e", "organization": "Mysten Validator", "comment": ""},
    {"as_id": "2:0:7f", "organization": "Mysten Validator", "comment": ""},
    {"as_id": "2:0:80", "organization": "Mysten Validator", "comment": ""},
    {"as_id": "2:0:81", "organization": "deprecated", "comment": ""},
    {"as_id": "2:0:82", "organization": "Anapaya Systems AG", "comment": ""},
    {"as_id": "2:0:83", "organization": "deprecated", "comment": ""},
    {"as_id": "2:0:84", "organization": "Secure EFTPOS Network Association", "comment": ""},
    {"as_id": "2:0:85", "organization": "BIS", "comment": ""},
    {"as_id": "2:0:86", "organization": "Axpo", "comment": ""},
    {"as_id": "2:0:87", "organization": "Anapaya Systems AG", "comment": ""},
    {"as_id": "2:0:88", "organization": "Mysten Validator", "comment": ""},
    {"as_id": "2:0:89", "organization": "Mysten Validator", "comment": ""},
    {"as_id": "2:0:8a", "organization": "Mysten Validator", "comment": ""},
    {"as_id": "2:0:8b", "organization": "Mysten Validator", "comment": ""},
    {"as_id": "2:0:8c", "organization": "Mysten Validator", "comment": ""},
    {"as_id": "2:0:8d", "organization": "Mysten Validator", "comment": ""},
    {"as_id": "2:0:8e", "organization": "Mysten Validator", "comment": ""},
    {"as_id": "2:0:8f", "organization": "Mysten Validator", "comment": ""},
    {"as_id": "2:0:90", "organization": "Mysten Validator", "comment": ""},
    {"as_id": "2:0:91", "organization": "Mysten Validator", "comment": ""},
    {"as_id": "2:0:92", "organization": "PCB", "comment": ""},
    {"as_id": "2:0:96", "organization": "Cyberlink AG", "comment": ""},
    {"as_id": "2:0:97", "organization": "Cyberlink AG", "comment": ""},
    {"as_id": "2:0:98", "organization": "Worldline Schweiz AG", "comment": ""},
    {"as_id": "2:0:99", "organization": "Worldline Schweiz AG", "comment": ""},
    {"as_id": "2:0:9a", "organization": "Sunrise UPC GmbH", "comment": ""},
    {"as_id": "2:0:9b", "organization": "Schweizerische Nationalbank", "comment": ""},
    {"as_id": "2:0:9c", "organization": "ETH Zurich", "comment": ""},
    {"as_id": "2:0:9d", "organization": "VBS Lab", "comment": ""},
    {"as_id": "2:0:a0", "organization": "Worldline Schweiz AG", "comment": ""},
    {"as_id": "2:0:a1", "organization": "Worldline Schweiz AG", "comment": ""}
]

# Process all entries
for entry in as_entries:
    add_as_entry(
        as_id=entry["as_id"],
        organization=entry["organization"],
        comment=entry["comment"]
    )

print(f"Added {len(as_entries)} AS entries to ISD64")
