index.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>Network</title>
  5. <script type="text/javascript" src="vis-network.min.js"></script>
  6. <style>
  7. html, body {
  8. height: 100%;
  9. overflow: hidden;
  10. }
  11. /* Define the styles for the network container */
  12. #network {
  13. width: 100%;
  14. height: 100%;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="network"></div>
  20. <script type="text/javascript">
  21. const servers = {
  22. 'machteburch.social': {
  23. domain: 'machteburch.social',
  24. accountCount: undefined,
  25. botCount: undefined,
  26. peers: undefined
  27. },
  28. };
  29. // Create nodes and edges for the network
  30. const nodes = new vis.DataSet([]);
  31. const edges = new vis.DataSet([]);
  32. // Initialize the network
  33. const container = document.getElementById('network');
  34. const data = {
  35. nodes,
  36. edges,
  37. };
  38. const options = {
  39. nodes: {
  40. color: {
  41. background: 'white',
  42. border: 'black',
  43. },
  44. },
  45. };
  46. const network = new vis.Network(container, data, options);
  47. network.on("selectNode", function (params) {
  48. if (params.nodes.length === 1) {
  49. var node = nodes.get(params.nodes[0]);
  50. window.open(`https://${params.nodes[0]}`, '_blank');
  51. }
  52. });
  53. async function fetchDataFromDirectory(domain, origin) {
  54. if(servers[domain] && servers[domain].accountCount != undefined)
  55. return;
  56. const server = {
  57. domain: domain,
  58. accountCount: 0,
  59. botCount: 0,
  60. peers: []
  61. }
  62. servers[domain] = server
  63. try {
  64. const response = await fetch(`https://${domain}/api/v1/directory`);
  65. if (response.ok) {
  66. const directoryData = await response.json();
  67. // Process directoryData and determine the percentage of bot accounts
  68. server.botCount = directoryData.filter(account => account.bot).length;
  69. server.accountCount = directoryData.length;
  70. // Determine the node color based on the percentage of bot accounts
  71. const color = determineNodeColor(server.botCount / server.accountCount);
  72. // Update the node with the new information
  73. const updatedNodes = [{
  74. id: server.domain,
  75. label: `${server.domain}\n Bots: ${server.botCount}/${server.accountCount}`,
  76. value: server.botCount / server.accountCount * 100,
  77. color: server.accountCount > 0 ? color : undefined,
  78. }];
  79. if(!nodes.get(origin))
  80. updatedNodes.push({
  81. id: origin,
  82. label: origin,
  83. })
  84. nodes.update(updatedNodes);
  85. const newEdge = {
  86. from: origin,
  87. to: domain,
  88. };
  89. edges.add(newEdge);
  90. return
  91. } else {
  92. console.error('Failed to fetch data from the directory');
  93. }
  94. } catch (error) {
  95. console.error('An error occurred:', error);
  96. }
  97. return
  98. }
  99. // Function to determine the node color based on the bot percentage
  100. function determineNodeColor(percentage) {
  101. // Implement a logic to determine the color based on the bot percentage
  102. const hue = 120 - percentage * 120;
  103. return `hsl(${hue}, 100%, 80%)`;
  104. }
  105. // Function to fetch peers data from server
  106. async function fetchPeers(server) {
  107. const domain = server.domain
  108. server.peers = [];
  109. try {
  110. const response = await fetch(`https://${domain}/api/v1/instance/peers`);
  111. if (response.ok) {
  112. server.peers = await response.json();
  113. } else {
  114. console.error(`Failed to fetch data from peer domain: ${domain}`);
  115. }
  116. } catch (error) {
  117. console.error(`An error occurred while fetching data from peer domain: ${domain}`, error);
  118. }
  119. }
  120. async function queryPeer(server, domain) {
  121. if(!servers[domain]) {
  122. await fetchDataFromDirectory(domain, server.domain)
  123. await fetchPeers(servers[domain])
  124. }
  125. }
  126. async function start() {
  127. for (const domain in servers) {
  128. await fetchDataFromDirectory(domain)
  129. await fetchPeers(servers[domain])
  130. }
  131. let hasWork = true
  132. while(hasWork) {
  133. hasWork = false
  134. for(const domain in servers) {
  135. const server = servers[domain]
  136. const unseenPeers = server.peers.filter(peerDomain => !servers[peerDomain])
  137. for(const peerDomain of unseenPeers.slice(0, 20)) {
  138. hasWork = true
  139. queryPeer(server, peerDomain)
  140. await new Promise(resolve => setTimeout(resolve, 20));
  141. }
  142. }
  143. }
  144. }
  145. start()
  146. </script>
  147. </body>
  148. </html>