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