adminTools.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <http://www.gnu.org/licenses/>.
  13. from time import sleep
  14. def anonConnect(ip='127.0.0.1', port=11234):
  15. from cjdnsadmin import connect
  16. return connect(ip, int(port), '')
  17. def connect(ip='127.0.0.1', port=11234, password=''):
  18. from cjdnsadmin import connectWithAdminInfo
  19. return connectWithAdminInfo()
  20. def disconnect(cjdns):
  21. cjdns.disconnect()
  22. def whoami(cjdns):
  23. from publicToIp6 import PublicToIp6_convert;
  24. resp=cjdns.NodeStore_nodeForAddr(0)
  25. key=resp['result']['key']
  26. ver=resp['result']['protocolVersion']
  27. IP=PublicToIp6_convert(key)
  28. return {'IP':IP,'key':key,'version':ver}
  29. def dumpTable(cjdns,verbose=False,unique_ip=False,nodes=[]):
  30. if nodes == []: nodes=[]
  31. rt = []
  32. i = 0;
  33. while True:
  34. table = cjdns.NodeStore_dumpTable(i)
  35. res=table['routingTable']
  36. for t in res:
  37. ip=t['ip']
  38. if (not ip in nodes) and unique_ip:
  39. nodes.append(ip)
  40. rt.append(t)
  41. if verbose:
  42. print(t['ip'] + ' ' + t['path'] + ' ' + str(t['link']) + ' ' + str(t['version']));
  43. if not unique_ip:
  44. nodes.append(ip)
  45. rt.append(t)
  46. if verbose:
  47. print(t['ip'] + ' ' + t['path'] + ' ' + str(t['link']) + ' ' + str(t['version']));
  48. if not 'more' in table:
  49. break
  50. i += 1
  51. return rt
  52. def streamRoutingTable(cjdns, delay=10):
  53. known = []
  54. while True:
  55. i = 0
  56. while True:
  57. table = cjdns.NodeStore_dumpTable(i)
  58. routes = table['routingTable']
  59. for entry in routes:
  60. if entry['ip'] not in known:
  61. known.append(entry['ip'])
  62. yield entry
  63. if 'more' not in table:
  64. break
  65. i += 1
  66. sleep(delay)
  67. def peerStats(cjdns,up=False,verbose=False,human_readable=False):
  68. from publicToIp6 import PublicToIp6_convert;
  69. allPeers = []
  70. i = 0;
  71. while True:
  72. ps = cjdns.InterfaceController_peerStats(i);
  73. peers = ps['peers']
  74. for p in peers:
  75. if p['state'] == 'UNRESPONSIVE' and up:
  76. continue
  77. allPeers.append(p)
  78. if (not 'more' in ps):
  79. break
  80. i += 1
  81. if verbose:
  82. STAT_FORMAT = '%s\tv%s\t%s\tin %s\tout %s\t%s\tdup %d los %d oor %d'
  83. for peer in allPeers:
  84. ip = PublicToIp6_convert(peer['publicKey'])
  85. b_in = peer['bytesIn']
  86. b_out = peer['bytesOut']
  87. if human_readable:
  88. b_in = sizeof_fmt(b_in)
  89. b_out = sizeof_fmt(b_out)
  90. p = STAT_FORMAT % (ip, peer['version'], peer['switchLabel'],
  91. str(b_in), str(b_out), peer['state'],
  92. peer['duplicates'], peer['lostPackets'],
  93. peer['receivedOutOfRange'])
  94. if 'user' in peer:
  95. p += '\t%r' % peer['user']
  96. print p
  97. return allPeers
  98. def sizeof_fmt(num):
  99. for x in ['B','KB','MB','GB','TB']:
  100. if num < 1024.0:
  101. return "%3.1f%s" % (num, x)
  102. num /= 1024.0
  103. def parseLabel(route):
  104. route = route.replace('.','')
  105. broute= int('0x' + route, 16);
  106. route = route.replace('0','x')
  107. route = route.replace('1','y')
  108. route = route.replace('f','1111')
  109. route = route.replace('e','1110')
  110. route = route.replace('d','1101')
  111. route = route.replace('c','1100')
  112. route = route.replace('b','1011')
  113. route = route.replace('a','1010')
  114. route = route.replace('9','1001')
  115. route = route.replace('8','1000')
  116. route = route.replace('7','0111')
  117. route = route.replace('6','0110')
  118. route = route.replace('5','0101')
  119. route = route.replace('4','0100')
  120. route = route.replace('3','0011')
  121. route = route.replace('2','0010')
  122. route = route.replace('y','0001')
  123. route = route.replace('x','0000')
  124. # reverse the string, strip trailing zeros, then strip the trailing 1
  125. route = route[::-1].rstrip('0')[:-1]
  126. return {'route':route,'broute':broute}