Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

luci-mod-status: split dhcp status in dhcp and dhcpv6 includes #6713

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ var callLuciDHCPLeases = rpc.declare({
});

return baseclass.extend({
title: '',
title: 'Active DHCP Leases',

isMACStatic: {},
isDUIDStatic: {},

load: function() {
return Promise.all([
Expand All @@ -40,29 +39,8 @@ return baseclass.extend({
.then(L.bind(L.ui.changes.displayChanges, L.ui.changes));
},

handleCreateStaticLease6: function(lease, ev) {
ev.currentTarget.classList.add('spinning');
ev.currentTarget.disabled = true;
ev.currentTarget.blur();

var cfg = uci.add('dhcp', 'host'),
ip6arr = lease.ip6addrs[0] ? validation.parseIPv6(lease.ip6addrs[0]) : null;

uci.set('dhcp', cfg, 'name', lease.hostname);
uci.set('dhcp', cfg, 'duid', lease.duid.toUpperCase());
uci.set('dhcp', cfg, 'mac', lease.macaddr);
if (ip6arr)
uci.set('dhcp', cfg, 'hostid', (ip6arr[6] * 0xFFFF + ip6arr[7]).toString(16));

return uci.save()
.then(L.bind(L.ui.changes.init, L.ui.changes))
.then(L.bind(L.ui.changes.displayChanges, L.ui.changes));
},

renderLeases: function(data) {
var leases = Array.isArray(data[0].dhcp_leases) ? data[0].dhcp_leases : [],
leases6 = Array.isArray(data[0].dhcp6_leases) ? data[0].dhcp6_leases : [],
machints = data[1].getMACHints(false),
hosts = uci.sections('dhcp', 'host'),
isReadonlyView = !L.hasViewPermission();

Expand All @@ -76,10 +54,6 @@ return baseclass.extend({
this.isMACStatic[mac] = true;
}
}
if (host.duid) {
var duid = host.duid.toUpperCase();
this.isDUIDStatic[duid] = true;
}
};

var table = E('table', { 'id': 'status_leases', 'class': 'table lases' }, [
Expand Down Expand Up @@ -129,61 +103,7 @@ return baseclass.extend({
return rows;
}, this)), E('em', _('There are no active leases')));

var table6 = E('table', { 'id': 'status_leases6', 'class': 'table leases6' }, [
E('tr', { 'class': 'tr table-titles' }, [
E('th', { 'class': 'th' }, _('Host')),
E('th', { 'class': 'th' }, _('IPv6 address')),
E('th', { 'class': 'th' }, _('DUID')),
E('th', { 'class': 'th' }, _('Lease time remaining')),
isReadonlyView ? E([]) : E('th', { 'class': 'th cbi-section-actions' }, _('Static Lease'))
])
]);

cbi_update_table(table6, leases6.map(L.bind(function(lease) {
var exp, rows;

if (lease.expires === false)
exp = E('em', _('unlimited'));
else if (lease.expires <= 0)
exp = E('em', _('expired'));
else
exp = '%t'.format(lease.expires);

var hint = lease.macaddr ? machints.filter(function(h) { return h[0] == lease.macaddr })[0] : null,
host = null;

if (hint && lease.hostname && lease.hostname != hint[1] && lease.ip6addr != hint[1])
host = '%s (%s)'.format(lease.hostname, hint[1]);
else if (lease.hostname)
host = lease.hostname;
else if (hint)
host = hint[1];

rows = [
host || '-',
lease.ip6addrs ? lease.ip6addrs.join(' ') : lease.ip6addr,
lease.duid,
exp
];

if (!isReadonlyView && lease.duid != null) {
var duid = lease.duid.toUpperCase();
rows.push(E('button', {
'class': 'cbi-button cbi-button-apply',
'click': L.bind(this.handleCreateStaticLease6, this, lease),
'disabled': this.isDUIDStatic[duid]
}, [ _('Set Static') ]));
}

return rows;
}, this)), E('em', _('There are no active leases')));

return E([
E('h3', _('Active DHCP Leases')),
table,
E('h3', _('Active DHCPv6 Leases')),
table6
]);
return E(table);
},

render: function(data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
'use strict';
'require baseclass';
'require rpc';
'require uci';
'require network';
'require validation';

var callLuciDHCPLeases = rpc.declare({
object: 'luci-rpc',
method: 'getDHCPLeases',
expect: { '': {} }
});

return baseclass.extend({
title: 'Active DHCPv6 Leases',

isDUIDStatic: {},

load: function() {
return Promise.all([
callLuciDHCPLeases(),
network.getHostHints(),
L.resolveDefault(uci.load('dhcp'))
]);
},

handleCreateStaticLease6: function(lease, ev) {
ev.currentTarget.classList.add('spinning');
ev.currentTarget.disabled = true;
ev.currentTarget.blur();

var cfg = uci.add('dhcp', 'host'),
ip6arr = lease.ip6addrs[0] ? validation.parseIPv6(lease.ip6addrs[0]) : null;

uci.set('dhcp', cfg, 'name', lease.hostname);
uci.set('dhcp', cfg, 'duid', lease.duid.toUpperCase());
uci.set('dhcp', cfg, 'mac', lease.macaddr);
if (ip6arr)
uci.set('dhcp', cfg, 'hostid', (ip6arr[6] * 0xFFFF + ip6arr[7]).toString(16));

return uci.save()
.then(L.bind(L.ui.changes.init, L.ui.changes))
.then(L.bind(L.ui.changes.displayChanges, L.ui.changes));
},

renderLeases: function(data) {
var leases6 = Array.isArray(data[0].dhcp6_leases) ? data[0].dhcp6_leases : [],
machints = data[1].getMACHints(false),
hosts = uci.sections('dhcp', 'host'),
isReadonlyView = !L.hasViewPermission();

for (var i = 0; i < hosts.length; i++) {
var host = hosts[i];

if (host.duid) {
var duid = host.duid.toUpperCase();
this.isDUIDStatic[duid] = true;
}
};

var table6 = E('table', { 'id': 'status_leases6', 'class': 'table leases6' }, [
E('tr', { 'class': 'tr table-titles' }, [
E('th', { 'class': 'th' }, _('Host')),
E('th', { 'class': 'th' }, _('IPv6 address')),
E('th', { 'class': 'th' }, _('DUID')),
E('th', { 'class': 'th' }, _('Lease time remaining')),
isReadonlyView ? E([]) : E('th', { 'class': 'th cbi-section-actions' }, _('Static Lease'))
])
]);

cbi_update_table(table6, leases6.map(L.bind(function(lease) {
var exp, rows;

if (lease.expires === false)
exp = E('em', _('unlimited'));
else if (lease.expires <= 0)
exp = E('em', _('expired'));
else
exp = '%t'.format(lease.expires);

var hint = lease.macaddr ? machints.filter(function(h) { return h[0] == lease.macaddr })[0] : null,
host = null;

if (hint && lease.hostname && lease.hostname != hint[1] && lease.ip6addr != hint[1])
host = '%s (%s)'.format(lease.hostname, hint[1]);
else if (lease.hostname)
host = lease.hostname;
else if (hint)
host = hint[1];

rows = [
host || '-',
lease.ip6addrs ? lease.ip6addrs.join(' ') : lease.ip6addr,
lease.duid,
exp
];

if (!isReadonlyView && lease.duid != null) {
var duid = lease.duid.toUpperCase();
rows.push(E('button', {
'class': 'cbi-button cbi-button-apply',
'click': L.bind(this.handleCreateStaticLease6, this, lease),
'disabled': this.isDUIDStatic[duid]
}, [ _('Set Static') ]));
}

return rows;
}, this)), E('em', _('There are no active leases')));

return E(table6);
},

render: function(data) {
if (L.hasSystemFeature('dnsmasq') || L.hasSystemFeature('odhcpd'))
return this.renderLeases(data);

return E([]);
}
});