adminTools.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 peerStats(cjdns,up=False,verbose=False,human_readable=False):
  76. from publicToIp6 import PublicToIp6_convert;
  77. allPeers = []
  78. i = 0;
  79. while True:
  80. ps = cjdns.InterfaceController_peerStats(i);
  81. peers = ps['peers']
  82. for p in peers:
  83. tokens = p['addr'].split('.', 5)
  84. p['version'] = tokens[0].strip('v')
  85. p['switchLabel'] = '.'.join(tokens[1:5])
  86. p['publicKey'] = tokens[5]
  87. if p['state'] == 'UNRESPONSIVE' and up:
  88. continue
  89. allPeers.append(p)
  90. if (not 'more' in ps):
  91. break
  92. i += 1
  93. if verbose:
  94. STAT_FORMAT = '%s\tv%s\t%s\tin %s\tout %s\t%s\tdup %d los %d oor %d'
  95. for peer in allPeers:
  96. ip = PublicToIp6_convert(peer['publicKey'])
  97. b_in = peer['bytesIn']
  98. b_out = peer['bytesOut']
  99. if human_readable:
  100. b_in = sizeof_fmt(b_in)
  101. b_out = sizeof_fmt(b_out)
  102. p = STAT_FORMAT % (ip, peer['version'], peer['switchLabel'],
  103. str(b_in), str(b_out), peer['state'],
  104. peer['duplicates'], peer['lostPackets'],
  105. peer['receivedOutOfRange'])
  106. if 'user' in peer:
  107. p += '\t%r' % peer['user']
  108. print p
  109. return allPeers
  110. def sizeof_fmt(num):
  111. for x in ['B','KB','MB','GB','TB']:
  112. if num < 1024.0:
  113. return "%3.1f%s" % (num, x)
  114. num /= 1024.0
  115. def parseLabel(route):
  116. route = route.replace('.','')
  117. broute= int('0x' + route, 16);
  118. route = route.replace('0','x')
  119. route = route.replace('1','y')
  120. route = route.replace('f','1111')
  121. route = route.replace('e','1110')
  122. route = route.replace('d','1101')
  123. route = route.replace('c','1100')
  124. route = route.replace('b','1011')
  125. route = route.replace('a','1010')
  126. route = route.replace('9','1001')
  127. route = route.replace('8','1000')
  128. route = route.replace('7','0111')
  129. route = route.replace('6','0110')
  130. route = route.replace('5','0101')
  131. route = route.replace('4','0100')
  132. route = route.replace('3','0011')
  133. route = route.replace('2','0010')
  134. route = route.replace('y','0001')
  135. route = route.replace('x','0000')
  136. # reverse the string, strip trailing zeros, then strip the trailing 1
  137. route = route[::-1].rstrip('0')[:-1]
  138. return {'route':route,'broute':broute}