luci-base: return ipv6 lease time from getExpiry()
authorhitech95 <nicveronese@gmail.com>
Sat, 27 Apr 2024 09:25:42 +0000 (11:25 +0200)
committerPaul Donald <newtwen+github@gmail.com>
Tue, 14 May 2024 18:06:18 +0000 (20:06 +0200)
This commit introduces a streamlined method for retrieving lease time
 information on IPv6 interfaces.

While IPv4 interfaces typically have a single lease time (available
 in the interface data), IPv6 interfaces commonly feature multiple
 lease times due to factors like delegated prefixes or multiple /64 subnet.

Users typically receive only one prefix delegation from their ISP, leading
 this PR to the retrieval of lease time from the first available prefix
 or IPv6 address.

To enhance usability and provide users with a more comprehensive view
 of lease time information, however, alternative approaches
 should be discussed.

Suggestions include returning an array of lease times or aggregating
 the various possible values using methods like min or max.

These enhancements aim to improve the clarity the lease time
 management on IPv6 interfaces.

Signed-off-by: hitech95 <nicveronese@gmail.com>
modules/luci-base/htdocs/luci-static/resources/network.js

index 2a70fe9a043024384e566a311f4116476e9f4800..b6b996818a54b2f7d22b2e8e0937ad8373f79d2b 100644 (file)
@@ -2141,12 +2141,27 @@ Protocol = baseclass.extend(/** @lends LuCI.network.Protocol.prototype */ {
         */
        getExpiry: function() {
                var u = this._ubus('uptime'),
-                   d = this._ubus('data');
+                   d = this._ubus('data'),
+                   v6_prefixes = this._ubus('ipv6-prefix'),
+                   v6_addresses = this._ubus('ipv6-address');
 
-               if (typeof(u) == 'number' && d != null &&
-                   typeof(d) == 'object' && typeof(d.leasetime) == 'number') {
-                       var r = d.leasetime - (u % d.leasetime);
-                       return (r > 0 ? r : 0);
+               if (typeof(u) == 'number' && d != null) {
+
+                       // DHCPv4 or leasetime in data
+                       if(typeof(d) == 'object' && typeof(d.leasetime) == 'number') {
+                               var r = d.leasetime - (u % d.leasetime);
+                               return (r > 0 ? r : 0);
+                       }
+
+                       // DHCPv6, we can have multiple IPs and prefixes
+                       if (Array.isArray(v6_prefixes) || Array.isArray(v6_addresses)) {
+                               var prefixes = [...v6_prefixes, ...v6_addresses];
+
+                               if(prefixes.length && typeof(prefixes[0].valid) == 'number') {
+                               var r = prefixes[0].valid;
+                               return (r > 0 ? r : 0);
+                               }
+                       }
                }
 
                return -1;