summaryrefslogtreecommitdiffstats
path: root/retiolum/scripts/adv_graphgen/tinc_graphs/Supernodes.py
blob: bc66b337e994767a6619f420109c3de8ce5857ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/python3

def find_potential_super(path="/etc/tinc/retiolum/hosts"):
  import os
  import re

  needle_addr = re.compile("Address\s*=\s*(.*)")
  needle_port = re.compile("Port\s*=\s*(.*)")
  for f in os.listdir(path):
    try:
      with open(path+"/"+f) as of:
        addrs = []
        port = "655"

        for line in of.readlines():

          addr_found = needle_addr.match(line)
          if addr_found:
            addrs.append(addr_found.group(1))

          port_found = needle_port.match(line)
          if port_found:
            port = port_found.group(1)

        if addrs : yield (f ,[(addr ,int(port)) for addr in addrs])
    except FileNotFoundError as e:
        print("Cannot open hosts directory to be used to find potential supernodes")
        print("Directory used: {}".format(path))
        raise


def try_connect(addr):
  try:
    from socket import socket,AF_INET,SOCK_STREAM
    s = socket(AF_INET,SOCK_STREAM)
    s.settimeout(2)
    s.connect(addr)
    s.settimeout(None)
    s.close()
    return addr
  except Exception as e:
    pass


def check_one_super(ha):
    host,addrs = ha
    valid_addrs = []
    for addr in addrs:
      ret = try_connect(addr)
      if ret: valid_addrs.append(ret)
    if valid_addrs: return (host,valid_addrs)


def check_all_the_super(path):
  from multiprocessing import Pool
  p = Pool(20)
  return filter(None,p.map(check_one_super,find_potential_super(path)))


def main():
  import os
  hostpath=os.environ.get("TINC_HOSTPATH", "/etc/tinc/retiolum/hosts")

  for host,addrs in check_all_the_super(hostpath):
    print("%s %s" %(host,str(addrs)))

if __name__ == "__main__":
    main()

# vim: set expandtab:ts=:sw=2