From d14d35e2d3165d7bde3f315368ac19f2fc57bea2 Mon Sep 17 00:00:00 2001 From: xinnige Date: Fri, 6 Sep 2024 15:01:30 +0800 Subject: [PATCH] Fix duid uuid --- plugins/interface/intf_base_plugin.js | 43 +++++++++++++++++---------- tests/plugins/test_intf_base.js | 34 +++++++++++++-------- 2 files changed, 50 insertions(+), 27 deletions(-) diff --git a/plugins/interface/intf_base_plugin.js b/plugins/interface/intf_base_plugin.js index 8e16a2ef..ab3d9034 100644 --- a/plugins/interface/intf_base_plugin.js +++ b/plugins/interface/intf_base_plugin.js @@ -19,6 +19,7 @@ const Plugin = require('../plugin.js'); const pl = require('../plugin_loader.js'); const _ = require('lodash'); const url = require('url'); +const { v4: uuidv4 } = require("uuid"); const r = require('../../util/firerouter'); @@ -382,7 +383,7 @@ class InterfaceBasePlugin extends Plugin { if (this.networkConfig.dhcp6.duidType) { await this._genDuid(this.networkConfig.dhcp6.duidType); } else { - await this._unsetDuid(); + await this._resetDuid(); } // start dhcpcd for SLAAC and stateful DHCPv6 if necessary await exec(`sudo systemctl restart firerouter_dhcpcd6@${this.name}`).catch((err) => { @@ -482,15 +483,24 @@ class InterfaceBasePlugin extends Plugin { return await fs.readFileAsync(`${r.getRuntimeFolder()}/dhcpcd.duid`, {encoding: 'utf8'}).then(content => content.trim()).catch((err) => null); } - async _unsetDuid() { - const lastDuid = await this.getLastDuid(); - if (!lastDuid) { - return; + async _resetDuid() { + let duidType = 'DUID-UUID'; + const arch = await exec("uname -m", {encoding: 'utf8'}).then(result => result.stdout.trim()).catch((err) => { + this.log.error(`Failed to get architecture`, err.message); + return null; + }); + if (arch) { + switch (arch) { + case 'x86_64': + break; + case 'aarch64': + duidType = 'DUID-LLT'; + break; + default: + break; + } } - const currentDuid = await this._getDuid(); - this.log.info("unset duid", this.name, currentDuid); - await rclient.delAsync(`duid_record`); - await exec(`cat /dev/null | sudo tee ${r.getRuntimeFolder()}/dhcpcd.duid`).catch((err) => {}); + return await this._genDuid(duidType); } // Generate DHCP Unique Identifier (DUID), see RFC8415 @@ -523,7 +533,7 @@ class InterfaceBasePlugin extends Plugin { break; case 'DUID-UUID': // 00:04 DUID-Type (DUID-UUID), DUID Based on UUID, see rfc6355 - const uuid = await fs.readFileAsync("/var/lib/dbus/machine-id", {encoding: "utf8"}).then((content) => content.trim()).catch((err) => null); + const uuid = await this._genDuidUuid(); if (uuid) { duid = '00:04:' + this._formatDuid(uuid); } @@ -540,12 +550,15 @@ class InterfaceBasePlugin extends Plugin { return duid; } - async getLastDuid(){ - const results = await rclient.zrevrangeAsync(`duid_record`, 0, 0); - if (results.length > 0) { - return results[0]; + async _genDuidUuid() { + const existUuid = await fs.readFileAsync(`${r.getRuntimeFolder()}/dhcpcd.duid_uuid`, {encoding: "utf8"}).then((content) => content.trim()).catch((err) => null); + if (existUuid) { + return existUuid; } - return null; + + const newUuid = uuidv4(); + await fs.writeFileAsync(`${r.getRuntimeFolder()}/dhcpcd.duid_uuid`, newUuid).catch((err) => {this.log.warn("fail to persistently save duid uuid", err.message)}); + return newUuid; } async saveDuidRecord(record){ diff --git a/tests/plugins/test_intf_base.js b/tests/plugins/test_intf_base.js index 0c502397..81d83252 100644 --- a/tests/plugins/test_intf_base.js +++ b/tests/plugins/test_intf_base.js @@ -18,7 +18,10 @@ let chai = require('chai'); let expect = chai.expect; +const fs = require('fs'); const exec = require('child-process-promise').exec; + +const r = require('../../util/firerouter'); let log = require('../../util/logger.js')(__filename, 'info'); let InterfaceBasePlugin = require('../../plugins/interface/intf_base_plugin.js'); @@ -30,7 +33,7 @@ describe('Test interface base dhcp6', function(){ async() => { this.plugin = new InterfaceBasePlugin("eth0"); this.plugin.configure({dhcp6:{}}); - await this.plugin._unsetDuid(); + await exec(`cat /dev/null | sudo tee ${r.getRuntimeFolder()}/dhcpcd.duid`).catch((err) => {}); done(); })() ); @@ -63,19 +66,26 @@ describe('Test interface base dhcp6', function(){ expect(duid).to.contains('00:04:'); }); - it('should get last duid', async() => { - let duid = await this.plugin._getDuid(); - log.debug("current duid", duid); - let lastDuid = await this.plugin.getLastDuid(); - log.debug("last duid", lastDuid); - expect(lastDuid).to.be.contains("00:03:"); + it('should gen duid uuid', async() => { + const t1 = await this.plugin._genDuidUuid(); + const t2 = await this.plugin._genDuidUuid(); + const duuuid = await fs.readFileAsync(`${r.getRuntimeFolder()}/dhcpcd.duid_uuid`, {encoding: "utf8"}).then((content) => content.trim()).catch((err) => null); + log.debug("duid uuid generated", duuuid); + expect(t1).to.be.equal(duuuid); + expect(t2).to.be.equal(duuuid); }); - it('should unset duid', async() => { - await this.plugin._unsetDuid(); - expect(await this.plugin.getLastDuid()).to.be.equal(null); - let duid = await this.plugin._getDuid(); - expect(duid).to.be.empty; + it('should reset duid', async() => { + await this.plugin._resetDuid(); + let duidType = await this.plugin._getDuidType(await this.plugin._getDuid()); + const arch = await exec("uname -m", {encoding: 'utf8'}).then(result => result.stdout.trim()).catch((err) => {}); switch (arch) { + case 'x86_64': + expect(duidType).to.equal('DUID-UUID'); + break; + case 'aarch64': + expect(duidType).to.equal('DUID-LLT'); + break; + } }); it('should get duid type', async() => {