import socket,struct,time,json,urllib.request,urllib.error

def tcp(h,p,to=4):
    t0=time.time(); s=socket.socket(); s.settimeout(to)
    try:
        s.connect((h,p)); return "OPEN %.3fs"%(time.time()-t0)
    except socket.timeout: return "TIMEOUT %.1fs"%(time.time()-t0)
    except OSError as e: return "ERR %s:%s"%(getattr(e,'errno',None),e.strerror or e)
    finally:
        try:s.close()
        except:pass

def route():
    try:
        out=[]
        for l in open('/proc/net/route').read().splitlines()[1:]:
            f=l.split(); 
            def ip(x): return socket.inet_ntoa(struct.pack('<I',int(x,16)))
            out.append("%s dst=%s gw=%s flags=%s"%(f[0],ip(f[1]),ip(f[2]),f[3]))
        return " | ".join(out)
    except Exception as e: return "ERR %r"%e

def resolv():
    try: return " ".join(l for l in open('/etc/resolv.conf').read().splitlines() if l.strip() and not l.startswith('#'))
    except Exception as e: return "ERR %r"%e

def get(u,to=6,hdr=None):
    try:
        r=urllib.request.urlopen(urllib.request.Request(u,headers=hdr or {}),timeout=to)
        return "H%d %r"%(r.status, r.read(180)[:150])
    except urllib.error.HTTPError as e:
        try: b=e.read(120)
        except: b=b""
        return "H%d %r"%(e.code,b[:100])
    except Exception as e: return "ERR "+type(e).__name__

print("###NET host=%s"%socket.gethostname())
print("###ROUTE "+route())
print("###RESOLV "+resolv()[:260])

tests=[
 ("ext-cf443","1.1.1.1",443),("ext-cf80","1.1.1.1",80),("ext-cf53","1.1.1.1",53),
 ("ext-cf22","1.1.1.1",22),("ext-cf5432","1.1.1.1",5432),("ext-gg53","8.8.8.8",53),
 ("gw4000","10.222.42.25",4000),("gw3000","10.222.42.25",3000),("gw5432","10.222.42.25",5432),
 ("gw80","10.222.42.25",80),("gw443","10.222.42.25",443),
 ("pg5432","10.222.220.27",5432),("pg80","10.222.220.27",80),
 ("cb3000","10.222.26.231",3000),("cb4000","10.222.26.231",4000),
 ("api443","10.222.0.1",443),("api6443","10.222.0.1",6443),("api2379","10.222.0.1",2379),
 ("acme8089","10.222.225.42",8089),
 ("meta80","169.254.169.254",80),("meta443","169.254.169.254",443),
 ("ali80","100.100.100.200",80),
]
for n,h,p in tests:
    print("T %-11s %s:%s %s"%(n,h,p,tcp(h,p)))

print("###HTTP_ACME")
for u in ["http://10.222.225.42:8089/","http://10.222.225.42:8089/health"]:
    print("A",u,get(u,6))
print("###HTTP_LITELLM_UNAUTH")
print("L",get("http://10.222.42.25:4000/health/readiness",6))

print("###META_IAM")
meta=[
 ("aws-root","http://169.254.169.254/latest/meta-data/",None),
 ("aws-iam","http://169.254.169.254/latest/meta-data/iam/security-credentials/",None),
 ("gcp-token","http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/token",{"Metadata-Flavor":"Google"}),
 ("yandex-token","http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/token",{"Metadata-Flavor":"Google"}),
 ("azure-token","http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://management.azure.com/",{"Metadata":"true"}),
 ("hetzner","http://169.254.169.254/hetzner/v1/metadata",None),
 ("do","http://169.254.169.254/metadata/v1/id",None),
 ("ali-ram","http://100.100.100.200/latest/meta-data/ram/security-credentials/",None),
 ("gcphost-token","http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token",{"Metadata-Flavor":"Google"}),
]
for n,u,h in meta:
    print("M %-13s %s"%(n,get(u,6,h)))
print("###END")
