main.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # This file is part of DarkFi (https://dark.fi)
  2. #
  3. # Copyright (C) 2020-2023 Dyne.org foundation
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import sys, toml, urwid, asyncio, logging
  18. import model
  19. from rpc import JsonRpc
  20. from view import Dnetview
  21. async def get_info(rpc, name, port):
  22. while True:
  23. try:
  24. await rpc.start("localhost", port)
  25. break
  26. except OSError:
  27. pass
  28. response = await rpc._make_request("p2p.get_info", [])
  29. info = response["result"]
  30. channels = info["channels"]
  31. channel_lookup = {}
  32. for channel in channels:
  33. id = channel["id"]
  34. channel_lookup[id] = channel
  35. logging.debug(f"{name}")
  36. logging.debug("inbound")
  37. for channel in channels:
  38. if channel["session"] != "inbound":
  39. continue
  40. url = channel["url"]
  41. logging.debug(f" {url}")
  42. logging.debug("outbound")
  43. for i, id in enumerate(info["outbound_slots"]):
  44. if id == 0:
  45. logging.debug(f" {i}: none")
  46. continue
  47. assert id in channel_lookup
  48. url = channel_lookup[id]["url"]
  49. logging.debug(f" {i}: {url}")
  50. logging.debug("seed")
  51. for channel in channels:
  52. if channel["session"] != "seed":
  53. continue
  54. url = channel["url"]
  55. logging.debug(f" {i}: {url}")
  56. logging.debug("manual")
  57. for channel in channels:
  58. if channel["session"] != "manual":
  59. continue
  60. url = channel["url"]
  61. logging.debug(f" {i}: {url}")
  62. await rpc.stop()
  63. def get_config():
  64. with open("config.toml") as f:
  65. cfg = toml.load(f)
  66. return cfg
  67. if __name__ == '__main__':
  68. logging.basicConfig(filename='dnet.log', encoding='utf-8', level=logging.DEBUG)
  69. config = get_config()
  70. nodes = config.get("nodes")
  71. ev = asyncio.get_event_loop()
  72. rpc = JsonRpc()
  73. for node in nodes:
  74. ev.create_task(get_info(rpc, node['name'], node['port']))
  75. dnet = Dnetview()
  76. ev.create_task(dnet.render_info())
  77. loop = urwid.MainLoop(dnet.view, dnet.palette,
  78. event_loop=urwid.AsyncioEventLoop(loop=ev))
  79. loop.run()