1
0

adminTools.py 4.9 KB

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