1
0

cjdlua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/usr/bin/env lua
  2. --[[ Variables ]]--
  3. local confpath = "../../cjdroute.conf"
  4. local cjdns = require "cjdns/init"
  5. local conf = cjdns.ConfigFile.new(confpath)
  6. local admin = conf:makeInterface()
  7. local keypunch = ""
  8. local params = ""
  9. -- Make some new stuff up.
  10. randomQuote = "<@cjd> I fucking hate Ducttape.c :( \n"
  11. .. "<werecat> Well, it has to be better than krazyglue.c"
  12. -- " It's not a product, its a process. -cjd"
  13. --[[ Functions ]]--
  14. function ping(path,timeout)
  15. if timeout then timeout = timeout else timeout = 2000 end
  16. local response, err = admin:auth({ q = "SwitchPinger_ping", path = path, timeout = timeout })
  17. if err then -- ai:recv > timeout
  18. local response = {}
  19. response.err = err
  20. response.path = path
  21. response.result = "timeout"
  22. return response
  23. else
  24. return response
  25. end
  26. end
  27. function publictoip6(publicKey)
  28. local process = io.popen("../../publictoip6 " .. publicKey, "r")
  29. local ipv6 = process:read()
  30. process:close()
  31. return ipv6
  32. end
  33. function peerStats(include_ping)
  34. local page = 0
  35. local peers = {}
  36. while page do
  37. local response, err = admin:auth({
  38. q = "InterfaceController_peerStats",
  39. page = page
  40. })
  41. if err or response.error then
  42. print(err, response.error)
  43. page = nil
  44. else
  45. for i,peer in pairs(response.peers) do
  46. peer.ipv6 = publictoip6(peer.publicKey)
  47. if include_ping then
  48. local pong = ping(peer.switchLabel,500)
  49. if pong.result == 'pong' then
  50. pong.ms = pong.ms
  51. end
  52. peer.version = pong.version
  53. end
  54. peers[#peers + 1] = peer
  55. end
  56. if response.more then
  57. page = page + 1
  58. else
  59. page = nil
  60. end
  61. end
  62. end
  63. return peers
  64. end
  65. function bytesToSize (bytes, precision)
  66. if precision == bytes then
  67. return bytes -- for admin/graphing
  68. end
  69. if not precision then precision = 2 end
  70. kilobyte = 1024;
  71. megabyte = kilobyte * 1024;
  72. gigabyte = megabyte * 1024;
  73. terabyte = gigabyte * 1024;
  74. if ((bytes >= 0) and (bytes < kilobyte)) then
  75. return( bytes .. ' B')
  76. elseif ((bytes >= kilobyte) and (bytes < megabyte)) then
  77. return( math.floor(bytes / kilobyte, precision) .. ' KB')
  78. elseif ((bytes >= megabyte) and (bytes < gigabyte)) then
  79. return( math.floor(bytes / megabyte, precision) .. ' MB')
  80. elseif ((bytes >= gigabyte) and (bytes < terabyte)) then
  81. return( math.floor(bytes / gigabyte, precision) .. ' GB')
  82. elseif (bytes >= terabyte) then
  83. return( math.floor(bytes / terabyte, precision) .. ' TB')
  84. else
  85. return( bytes .. ' B')
  86. end
  87. end
  88. function available_functions (t)
  89. -- banner
  90. -- print(meshLua_cmd.v['info'][1])
  91. print(t.v['info'][1])
  92. for k,v in pairs(t) do
  93. -- skip banner.
  94. if(k ~= "v") then print("\t"..k.." .. "..v['info'][1]) end
  95. end
  96. end
  97. local meshLua_cmd = {}
  98. --[[ Menu ]]--
  99. meshLua_cmd = {
  100. v = { info = { "\n\t--[[ Project Meshnet ]]--\n" .. randomQuote .. "\n\n" } },
  101. --[[ Other functions not include ]]--
  102. -- h = { info = { "[h]yperboria - hype or not." },
  103. -- b = { info = { "[b]roadcast meshbox-cjdns Wi-Fi" },
  104. -- splash = { info = { "[splash] Turn Wi-Fi splash on: /www-federated-wiki/" },
  105. -- ping = { info = { "[ping] all connected nodes" },
  106. l = { -- fcf3:0de9:cb5e:5edd:d74d:9d67:1c94:fbf6 (Up: 39MB / Down: 38MB) (Ping: 30ms) [ESTABLISHED/Incoming]
  107. info = { "[l]ist all connected nodes" },
  108. exec = {
  109. function(x)
  110. local dkjson = require "dkjson" -- http://dkolf.de/src/dkjson-lua.fsl/home
  111. local cjdroute = io.open(confpath)
  112. local conf, pos, err = dkjson.decode(cjdroute:read("*a"), 1, nil)
  113. local page = 0
  114. local totalpeers = 0
  115. local node = {}
  116. -- local pingtimeout = 500
  117. while page do
  118. local response, err = admin:auth({
  119. q = "InterfaceController_peerStats",
  120. page = page,
  121. })
  122. if response.total then
  123. os.execute("echo " .. tonumber(response.total) .. " >/tmp/connectedpeers" )
  124. end
  125. for k,v in pairs(response.peers) do
  126. node[#node+1] = {}
  127. node[#node].ipv6 = publictoip6(v['publicKey'])
  128. node[#node].switchLabel = v['switchLabel']
  129. node[#node].state = v['state']
  130. node[#node].ident = {
  131. function (x)
  132. if v['user'] ~= nil
  133. and v['user'] == 'Local Peers'
  134. then return('Auto/Beacon')
  135. elseif v['isIncoming'] == 1
  136. then return('Incoming')
  137. elseif v['isIncoming'] == 0
  138. then return('UDPInterface')
  139. else
  140. return('Unknown')
  141. end
  142. end }
  143. node[#node].bytesOut = bytesToSize(v['bytesOut'], 2)
  144. node[#node].bytesIn = bytesToSize(v['bytesIn'], 2)
  145. node[#node].pingtime = {
  146. function (pingtimeout)
  147. local pingtimeout = 500
  148. local p = ping(v['switchLabel'],pingtimeout).ms
  149. -- if p and p.ms and p.ms ~= nil then
  150. if p ~= nil then
  151. return(p)
  152. elseif p >= pingtimout then
  153. return('timeout')
  154. end
  155. end }
  156. print (
  157. '['..node[#node].state..'] \t'
  158. ..node[#node].ipv6..' (Up: '..node[#node].bytesOut..' / Down: '..node[#node].bytesIn..' / '
  159. ..'Ping: '..node[#node].pingtime[1]()..'ms / '
  160. ..'Mode: '..node[#node].ident[1]()..')'
  161. )
  162. end
  163. if response.more then
  164. page = page + 1
  165. else
  166. page = nil
  167. end
  168. end
  169. -- returns json (Example (Either fill a table each page, or print for each page))
  170. -- print(dkjson.encode({ peers = peerStats(true) }, { indent = true }))
  171. end
  172. }
  173. }
  174. }
  175. --[[ Main ]]--
  176. if #arg == 0 then
  177. available_functions(meshLua_cmd)
  178. repeat
  179. io.write("\n[meshLua]> ")
  180. io.flush()
  181. keypunch = io.read()
  182. until keypunch:len() ~= 0
  183. end
  184. if arg[1] then
  185. keypunch = arg[1]
  186. params = table.concat(arg, " ", 2, end_index)
  187. end
  188. meshLua_cmd[keypunch]['exec'][1](params)