1855ee6422f9ec53691cba6b04faae945d8e6366
[oweals/luci.git] / modules / luci-mod-network / htdocs / luci-static / resources / view / network / diagnostics.js
1 'use strict';
2 'require view';
3 'require dom';
4 'require fs';
5 'require ui';
6 'require uci';
7
8 return view.extend({
9         handleCommand: function(exec, args) {
10                 var buttons = document.querySelectorAll('.diag-action > .cbi-button');
11
12                 for (var i = 0; i < buttons.length; i++)
13                         buttons[i].setAttribute('disabled', 'true');
14
15                 return fs.exec(exec, args).then(function(res) {
16                         var out = document.querySelector('.command-output');
17                             out.style.display = '';
18
19                         dom.content(out, [ res.stdout || '', res.stderr || '' ]);
20                 }).catch(function(err) {
21                         ui.addNotification(null, E('p', [ err ]))
22                 }).finally(function() {
23                         for (var i = 0; i < buttons.length; i++)
24                                 buttons[i].removeAttribute('disabled');
25                 });
26         },
27
28         handlePing: function(ev, cmd) {
29                 var exec = cmd || 'ping',
30                     addr = ev.currentTarget.parentNode.previousSibling.value,
31                     args = (exec == 'ping') ? [ '-4', '-c', '5', '-W', '1', addr ] : [ '-6', '-c', '5', addr ];
32
33                 return this.handleCommand(exec, args);
34         },
35
36         handleTraceroute: function(ev, cmd) {
37                 var exec = cmd || 'traceroute',
38                     addr = ev.currentTarget.parentNode.previousSibling.value,
39                     args = (exec == 'traceroute') ? [ '-q', '1', '-w', '1', '-n', addr ] : [ '-q', '1', '-w', '2', '-n', addr ];
40
41                 return this.handleCommand(exec, args);
42         },
43
44         handleNslookup: function(ev, cmd) {
45                 var addr = ev.currentTarget.parentNode.previousSibling.value;
46
47                 return this.handleCommand('nslookup', [ addr ]);
48         },
49
50         load: function() {
51                 return Promise.all([
52                         L.resolveDefault(fs.stat('/bin/ping6'), {}),
53                         L.resolveDefault(fs.stat('/usr/bin/ping6'), {}),
54                         L.resolveDefault(fs.stat('/bin/traceroute6'), {}),
55                         L.resolveDefault(fs.stat('/usr/bin/traceroute6'), {}),
56                         uci.load('luci')
57                 ]);
58         },
59
60         render: function(res) {
61                 var has_ping6 = res[0].path || res[1].path,
62                     has_traceroute6 = res[2].path || res[3].path,
63                         dns_host = uci.get('luci', 'diag', 'dns') || 'openwrt.org',
64                         ping_host = uci.get('luci', 'diag', 'ping') || 'openwrt.org',
65                         route_host = uci.get('luci', 'diag', 'route') || 'openwrt.org';
66
67                 return E([], [
68                         E('h2', {}, [ _('Network Utilities') ]),
69                         E('div', { 'class': 'table' }, [
70                                 E('div', { 'class': 'tr' }, [
71                                         E('div', { 'class': 'td left' }, [
72                                                 E('input', {
73                                                         'style': 'margin:5px 0',
74                                                         'type': 'text',
75                                                         'value': ping_host
76                                                 }),
77                                                 E('span', { 'class': 'diag-action' }, [
78                                                         has_ping6 ? new ui.ComboButton('ping', {
79                                                                 'ping': '%s %s'.format(_('IPv4'), _('Ping')),
80                                                                 'ping6': '%s %s'.format(_('IPv6'), _('Ping')),
81                                                         }, {
82                                                                 'click': ui.createHandlerFn(this, 'handlePing'),
83                                                                 'classes': {
84                                                                         'ping': 'btn cbi-button cbi-button-action',
85                                                                         'ping6': 'btn cbi-button cbi-button-action'
86                                                                 }
87                                                         }).render() : E('button', {
88                                                                 'class': 'cbi-button cbi-button-action',
89                                                                 'click': ui.createHandlerFn(this, 'handlePing')
90                                                         }, [ _('Ping') ])
91                                                 ])
92                                         ]),
93
94                                         E('div', { 'class': 'td left' }, [
95                                                 E('input', {
96                                                         'style': 'margin:5px 0',
97                                                         'type': 'text',
98                                                         'value': route_host
99                                                 }),
100                                                 E('span', { 'class': 'diag-action' }, [
101                                                         has_traceroute6 ? new ui.ComboButton('traceroute', {
102                                                                 'traceroute': '%s %s'.format(_('IPv4'), _('Traceroute')),
103                                                                 'traceroute6': '%s %s'.format(_('IPv6'), _('Traceroute')),
104                                                         }, {
105                                                                 'click': ui.createHandlerFn(this, 'handleTraceroute'),
106                                                                 'classes': {
107                                                                         'traceroute': 'btn cbi-button cbi-button-action',
108                                                                         'traceroute6': 'btn cbi-button cbi-button-action'
109                                                                 }
110                                                         }).render() : E('button', {
111                                                                 'class': 'cbi-button cbi-button-action',
112                                                                 'click': ui.createHandlerFn(this, 'handleTraceroute')
113                                                         }, [ _('Traceroute') ])
114                                                 ])
115                                         ]),
116
117                                         E('div', { 'class': 'td left' }, [
118                                                 E('input', {
119                                                         'style': 'margin:5px 0',
120                                                         'type': 'text',
121                                                         'value': dns_host
122                                                 }),
123                                                 E('span', { 'class': 'diag-action' }, [
124                                                         E('button', {
125                                                                 'class': 'cbi-button cbi-button-action',
126                                                                 'click': ui.createHandlerFn(this, 'handleNslookup')
127                                                         }, [ _('Nslookup') ])
128                                                 ])
129                                         ])
130                                 ])
131                         ]),
132                         E('pre', { 'class': 'command-output', 'style': 'display:none' })
133                 ]);
134         },
135
136         handleSaveApply: null,
137         handleSave: null,
138         handleReset: null
139 });