Files: 7da1b2adbfa620cd35e8dcf31baf0f7d82bc9874 / modules_extra / network.js
3647 bytesRaw
1 | var isVisible = require('is-visible').isVisible |
2 | var h = require('hyperscript') |
3 | var plugs = require('../plugs') |
4 | |
5 | var avatar = plugs.first(exports.avatar = []) |
6 | var sbot_gossip_peers = plugs.first(exports.sbot_gossip_peers = []) |
7 | var sbot_gossip_connect = plugs.first(exports.sbot_gossip_connect = []) |
8 | //sbot_gossip_connect |
9 | //sbot_gossip_add |
10 | |
11 | var human = require('human-time') |
12 | |
13 | function legacyToMultiServer(addr) { |
14 | return 'net:'+addr.host + ':'+addr.port + '~shs:'+addr.key.substring(1).replace('.ed25519','') |
15 | } |
16 | |
17 | exports.menu_items = function () { |
18 | return h('a', {href: '#/network'}, '/network') |
19 | } |
20 | |
21 | //types of peers |
22 | |
23 | |
24 | //on the same wifi network |
25 | function isLocal (e) { |
26 | // don't rely on private ip address, because |
27 | // cjdns creates fake private ip addresses. |
28 | return ip.isPrivate(e.host) && e.type === 'local' |
29 | } |
30 | |
31 | |
32 | //pub is running scuttlebot >=8 |
33 | //have connected successfully. |
34 | function isLongterm (e) { |
35 | return e.ping && e.ping.rtt && e.ping.rtt.mean > 0 |
36 | } |
37 | |
38 | //pub is running scuttlebot < 8 |
39 | //have connected sucessfully |
40 | function isLegacy (peer) { |
41 | return /connect/.test(peer.state) || (peer.duration && peer.duration.mean) > 0 && !isLongterm(peer) |
42 | } |
43 | |
44 | //tried to connect, but failed. |
45 | function isInactive (e) { |
46 | return e.stateChange && (e.duration && e.duration.mean == 0) |
47 | } |
48 | |
49 | //havn't tried to connect peer yet. |
50 | function isUnattempted (e) { |
51 | return !e.stateChange |
52 | } |
53 | |
54 | function getType (e) { |
55 | return ( |
56 | isLongterm(e) ? 'modern' |
57 | : isLegacy(e) ? 'legacy' |
58 | : isInactive(e) ? 'inactive' |
59 | : isUnattempted(e) ? 'unattempted' |
60 | : 'other' //should never happen |
61 | ) |
62 | } |
63 | |
64 | var states = { |
65 | connected: 3, |
66 | connecting: 2 |
67 | } |
68 | |
69 | var types = { |
70 | modern: 4, |
71 | legacy: 3, |
72 | inactive: 2, |
73 | unattempted: 1, |
74 | other: 0 |
75 | } |
76 | |
77 | function round(n) { |
78 | return Math.round(n*100)/100 |
79 | } |
80 | |
81 | function duration (s) { |
82 | if(!s) return s |
83 | if (Math.abs(s) > 30000) |
84 | return round(s/60000)+'m' |
85 | else if (Math.abs(s) > 500) |
86 | return round(s/1000)+'s' |
87 | else |
88 | return round(s)+'ms' |
89 | } |
90 | |
91 | exports.screen_view = function (path) { |
92 | |
93 | if(path !== '/network') return |
94 | |
95 | var ol = h('ul.network') |
96 | |
97 | ;(function poll () { |
98 | |
99 | //if this tab isn't open, don't update. |
100 | //todo: make a better way to do this... |
101 | if(!isVisible(ol)) |
102 | return setTimeout(poll, 1000) |
103 | |
104 | sbot_gossip_peers(function (err, list) { |
105 | ol.innerHTML = '' |
106 | list.sort(function (a, b) { |
107 | return ( |
108 | (states[b.state] || 0) - (states[a.state] || 0) |
109 | || types[getType(b)] - types[getType(a)] |
110 | || b.stateChange - a.stateChange |
111 | ) |
112 | }).forEach(function (peer) { |
113 | ol.appendChild(h('div', |
114 | avatar(peer.key, 'thumbnail'), |
115 | h('div', |
116 | peer.state || 'not connected', |
117 | ' ', |
118 | getType(peer), |
119 | ' ', |
120 | //TODO: show nicer details, with labels. etc. |
121 | (peer.ping && peer.ping.rtt) ? duration(peer.ping.rtt.mean) : '', |
122 | ' ', |
123 | (peer.ping && peer.ping.skew) ? duration(peer.ping.skew.mean) : '', |
124 | h('label', |
125 | {title: new Date(peer.stateChange).toString()}, |
126 | peer.stateChange && ('(' + human(new Date(peer.stateChange))) + ')') |
127 | ), |
128 | 'source:'+peer.source, |
129 | h('pre', legacyToMultiServer(peer)), |
130 | h('button', 'connect', {onclick: function () { |
131 | sbot_gossip_connect(peer, function (err) { |
132 | if(err) console.error(err) |
133 | else console.log('connected to', peer) |
134 | }) |
135 | }}) |
136 | ) |
137 | ) |
138 | }) |
139 | |
140 | setTimeout(poll, 5000) |
141 | }) |
142 | |
143 | })() |
144 | |
145 | return h('div.column.scroll-y', ol) |
146 | } |
147 | |
148 | |
149 |
Built with git-ssb-web