adminTools.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/usr/bin/env python2
  2. # You may redistribute this program and/or modify it under the terms of
  3. # the GNU General Public License as published by the Free Software Foundation,
  4. # either version 3 of the License, or (at your option) any later version.
  5. #
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. #
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. import os
  14. import json
  15. from time import sleep
  16. def anonConnect(ip='127.0.0.1', port=11234):
  17. from cjdnsadmin import connect
  18. path = os.path.expanduser('~/.cjdnsadmin')
  19. try:
  20. with open(path, 'r') as adminInfo:
  21. data = json.load(adminInfo)
  22. return connect(data['addr'], data['port'], '')
  23. except IOError:
  24. return connect(ip, int(port), '')
  25. def connect(ip='127.0.0.1', port=11234, password=''):
  26. from cjdnsadmin import connectWithAdminInfo
  27. return connectWithAdminInfo()
  28. def disconnect(cjdns):
  29. cjdns.disconnect()
  30. def whoami(cjdns):
  31. from publicToIp6 import PublicToIp6_convert;
  32. resp=cjdns.NodeStore_nodeForAddr(0)
  33. key=resp['result']['key']
  34. ver=resp['result']['protocolVersion']
  35. IP=PublicToIp6_convert(key)
  36. return {'IP':IP,'key':key,'version':ver}
  37. def dumpTable(cjdns,verbose=False,unique_ip=False,nodes=[]):
  38. if nodes == []: nodes=[]
  39. rt = []
  40. i = 0;
  41. while True:
  42. table = cjdns.NodeStore_dumpTable(i)
  43. res=table['routingTable']
  44. for t in res:
  45. ip=t['ip']
  46. if (not ip in nodes) and unique_ip:
  47. nodes.append(ip)
  48. rt.append(t)
  49. if verbose:
  50. print(t['ip'] + ' ' + t['path'] + ' ' + str(t['link']) + ' ' + str(t['version']));
  51. if not unique_ip:
  52. nodes.append(ip)
  53. rt.append(t)
  54. if verbose:
  55. print(t['ip'] + ' ' + t['path'] + ' ' + str(t['link']) + ' ' + str(t['version']));
  56. if not 'more' in table:
  57. break
  58. i += 1
  59. return rt
  60. def streamRoutingTable(cjdns, delay=10):
  61. known = []
  62. while True:
  63. i = 0
  64. while True:
  65. table = cjdns.NodeStore_dumpTable(i)
  66. routes = table['routingTable']
  67. for entry in routes:
  68. if entry['ip'] not in known:
  69. known.append(entry['ip'])
  70. yield entry
  71. if 'more' not in table:
  72. break
  73. i += 1
  74. sleep(delay)
  75. def parseAddr(addr):
  76. tokens = addr.split('.', 5)
  77. res = {
  78. 'version': tokens[0].strip('v'),
  79. 'switchLabel': '.'.join(tokens[1:5]),
  80. 'publicKey': tokens[5],
  81. }
  82. return res
  83. def peerStats(cjdns,up=False,verbose=False,human_readable=False):
  84. from publicToIp6 import PublicToIp6_convert;
  85. allPeers = []
  86. i = 0;
  87. while True:
  88. ps = cjdns.InterfaceController_peerStats(page=i);
  89. peers = ps['peers']
  90. for p in peers:
  91. p.update(parseAddr(p['addr']))
  92. if p['state'] == 'UNRESPONSIVE' and up:
  93. continue
  94. allPeers.append(p)
  95. if (not 'more' in ps):
  96. break
  97. i += 1
  98. if verbose:
  99. STAT_FORMAT = '%s\t%s\tv%s\t%s\tin %s\tout %s\t%s\tdup %d los %d oor %d'
  100. for peer in allPeers:
  101. ip = PublicToIp6_convert(peer['publicKey'])
  102. b_in = peer['bytesIn']
  103. b_out = peer['bytesOut']
  104. if human_readable:
  105. b_in = sizeof_fmt(b_in)
  106. b_out = sizeof_fmt(b_out)
  107. p = STAT_FORMAT % (peer['lladdr'], ip, peer['version'], peer['switchLabel'],
  108. str(b_in), str(b_out), peer['state'],
  109. peer['duplicates'], peer['lostPackets'],
  110. peer['receivedOutOfRange'])
  111. if 'user' in peer:
  112. p += '\t%r' % peer['user']
  113. print p
  114. return allPeers
  115. def sizeof_fmt(num):
  116. for x in ['B','KB','MB','GB','TB']:
  117. if num < 1024.0:
  118. return "%3.1f%s" % (num, x)
  119. num /= 1024.0
  120. def parseLabel(route):
  121. route = route.replace('.','')
  122. broute= int('0x' + route, 16);
  123. route = route.replace('0','x')
  124. route = route.replace('1','y')
  125. route = route.replace('f','1111')
  126. route = route.replace('e','1110')
  127. route = route.replace('d','1101')
  128. route = route.replace('c','1100')
  129. route = route.replace('b','1011')
  130. route = route.replace('a','1010')
  131. route = route.replace('9','1001')
  132. route = route.replace('8','1000')
  133. route = route.replace('7','0111')
  134. route = route.replace('6','0110')
  135. route = route.replace('5','0101')
  136. route = route.replace('4','0100')
  137. route = route.replace('3','0011')
  138. route = route.replace('2','0010')
  139. route = route.replace('y','0001')
  140. route = route.replace('x','0000')
  141. # reverse the string, strip trailing zeros, then strip the trailing 1
  142. route = route[::-1].rstrip('0')[:-1]
  143. return {'route':route,'broute':broute}