Files: 3003c7f2e378ab411b09eaa11adef56245af1868 / modules_extra / network.js
4227 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 | function origin (e) { |
73 | return e.source === 'local' ? 0 : 1 |
74 | } |
75 | |
76 | var states = { |
77 | connected: 3, |
78 | connecting: 2 |
79 | } |
80 | |
81 | var types = { |
82 | modern: 4, |
83 | legacy: 3, |
84 | inactive: 2, |
85 | unattempted: 1, |
86 | other: 0 |
87 | } |
88 | |
89 | function round(n) { |
90 | return Math.round(n*100)/100 |
91 | } |
92 | |
93 | function duration (s) { |
94 | if(!s) return s |
95 | if (Math.abs(s) > 30000) |
96 | return round(s/60000)+'m' |
97 | else if (Math.abs(s) > 500) |
98 | return round(s/1000)+'s' |
99 | else |
100 | return round(s)+'ms' |
101 | } |
102 | |
103 | |
104 | |
105 | exports.create = function (api) { |
106 | |
107 | return { |
108 | menu_items: function () { |
109 | return h('a', {href: '#/network'}, '/network') |
110 | }, |
111 | |
112 | builtin_tabs: function () { |
113 | return ['/network'] |
114 | }, |
115 | |
116 | screen_view: function (path) { |
117 | |
118 | if(path !== '/network') return |
119 | |
120 | var ol = h('ul.network') |
121 | |
122 | ;(function poll () { |
123 | |
124 | //if this tab isn't open, don't update. |
125 | //todo: make a better way to do this... |
126 | if(!isVisible(ol)) |
127 | return setTimeout(poll, 1000) |
128 | |
129 | api.sbot_gossip_peers(function (err, list) { |
130 | ol.innerHTML = '' |
131 | list.sort(function (a, b) { |
132 | return ( |
133 | (states[b.state] || 0) - (states[a.state] || 0) |
134 | || origin(b) - origin(a) |
135 | || types[getType(b)] - types[getType(a)] |
136 | || b.stateChange - a.stateChange |
137 | ) |
138 | }).forEach(function (peer) { |
139 | ol.appendChild(h('div', |
140 | api.avatar(peer.key, 'thumbnail'), |
141 | h('div', |
142 | peer.state || 'not connected', |
143 | ' ', |
144 | getType(peer), |
145 | ' ', |
146 | //TODO: show nicer details, with labels. etc. |
147 | (peer.ping && peer.ping.rtt) ? duration(peer.ping.rtt.mean) : '', |
148 | ' ', |
149 | (peer.ping && peer.ping.skew) ? duration(peer.ping.skew.mean) : '', |
150 | h('label', |
151 | {title: new Date(peer.stateChange).toString()}, |
152 | peer.stateChange && ('(' + human(new Date(peer.stateChange))) + ')') |
153 | ), |
154 | 'source:'+peer.source, |
155 | h('pre', legacyToMultiServer(peer)), |
156 | h('button', 'connect', {onclick: function () { |
157 | api.sbot_gossip_connect(peer, function (err) { |
158 | if(err) console.error(err) |
159 | else console.log('connected to', peer) |
160 | }) |
161 | }}) |
162 | ) |
163 | ) |
164 | }) |
165 | |
166 | setTimeout(poll, 5000) |
167 | }) |
168 | |
169 | })() |
170 | |
171 | return h('div.column.scroll-y', ol) |
172 | } |
173 | } |
174 | } |
175 | |
176 | |
177 |
Built with git-ssb-web