From 25d6255d7d0de4fb6842f9cb75324eef700298f8 Mon Sep 17 00:00:00 2001 From: Gutemberg Ribeiro Date: Mon, 26 Apr 2021 19:02:23 -0300 Subject: [PATCH] Allow HEX Serial and SLID values. --- .vscode/launch.json | 8 +++++++- README.md | 8 +++++--- src/UFiber.Configurator/NVRAM.cs | 33 +++++++++++++++++++++++++----- src/UFiber.Configurator/Program.cs | 2 +- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index f8f34cb..f081af2 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,13 @@ "args": [ "--host", "192.168.200.2", - "--dry-run" + "--dry-run", + "--vendor", + "HWTC", + "--serial", + "41-4C-43-4C-90-12-34-5a", + "--slid", + "12345" ], "cwd": "${workspaceFolder}/src/UFiber.Configurator", "console": "internalConsole", diff --git a/README.md b/README.md index ac13800..53c5976 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ Usage: UFiber.Configurator [options] Options: - --host IP or hostname of the target UFiber device. + --host IP or hostname of the target UFiber device. [default: 192.168.1.1] --user SSH user name. [default: ubnt] --pw SSH password. [default: ubnt] --port SSH port of the target UFiber device. [default: 22] @@ -67,8 +67,10 @@ Options: --slid The SLID (or PLOAM Password). --vendor 4-digit Vendor Id (e.g. HWTC, MTSC, etc.). Combined with --serial, a GPON Serial Number is built. - --serial 8-digit serial number (e.g. 01234567). Combined with --vendor, a GPON Serial Number is - built. + --serial 8-digit (e.g. 01234567) serial number or 16-digit (e.g. 41-4C-43-4C-xx-xx-xx-xx) HEX serial + number. Combined with --vendor, a GPON Serial Number is built. Note: If a 16-digit HEX + value is provided, the first 4 bytes (8 digits) will replace whatever value was passed to + Vendor Id with '--vendor'. --mac The desired MAC address to clone. --version Show version information -?, -h, --help Show help and usage information diff --git a/src/UFiber.Configurator/NVRAM.cs b/src/UFiber.Configurator/NVRAM.cs index e069628..f9a1e5d 100644 --- a/src/UFiber.Configurator/NVRAM.cs +++ b/src/UFiber.Configurator/NVRAM.cs @@ -1,6 +1,7 @@ using System; using System.Security.Cryptography; using System.Text; +using System.Text.RegularExpressions; using static System.BitConverter; namespace UFiber.Configurator @@ -11,6 +12,8 @@ namespace UFiber.Configurator private const int NvRamCrcLength = 4; private const uint NvRamOffset = 0x580; private const uint NvRamLength = 0x400; + private static readonly Regex _snMatcher = new Regex(@"^(([a-fA-F0-9]){2}-){7}([a-fA-F0-9]){2}$"); + private static readonly Regex _pwMatcher = new Regex(@"^(([a-fA-F0-9]){2}-){9}([a-fA-F0-9]){2}$"); public uint Checksum { get; private set; } public byte[] AfeId { get; } public byte[] VoiceBoardId { get; } @@ -118,7 +121,16 @@ namespace UFiber.Configurator public void SetGponSerialNumber(string serialNumber) { - SetGponSerialNumber(Encoding.UTF8.GetBytes(serialNumber)); + if (_snMatcher.IsMatch(serialNumber)) + { + this.SetGponId(AsBytes(serialNumber.Replace("-", "")[0..8])); + SetGponSerialNumber(Encoding.UTF8.GetBytes(serialNumber.Replace("-", "")[8..])); + } + else + { + if (serialNumber.Contains("-")) throw new InvalidOperationException($"Invalid serial number: {serialNumber}."); + SetGponSerialNumber(Encoding.UTF8.GetBytes(serialNumber)); + } } public void SetGponSerialNumber(ReadOnlySpan serialNumber) @@ -149,7 +161,18 @@ namespace UFiber.Configurator throw new ArgumentOutOfRangeException(nameof(password)); } - var bits = Encoding.UTF8.GetBytes(password); + byte[] bits = default!; + + if (_pwMatcher.IsMatch(password)) + { + password = password.Replace("-", "").PadLeft(MaxPasswordLength, '0'); + bits = AsBytes(password); + } + else + { + password = password.PadLeft(MaxPasswordLength, '0'); + bits = Encoding.UTF8.GetBytes(password); + } SetBytes(bits.AsSpan(), PasswordOffset, password.Length); @@ -245,14 +268,14 @@ namespace UFiber.Configurator while (dataIndex < data.Length) { - var highNibble = ToHex(data[dataIndex++]); - var lowNibble = ToHex(data[dataIndex++]); + var highNibble = FromHex(data[dataIndex++]); + var lowNibble = FromHex(data[dataIndex++]); bits[bitsIndex++] = (byte)(highNibble << 4 | lowNibble); } return bits; - static int ToHex(char c) => + static int FromHex(char c) => c switch { >= 'a' and <= 'f' => 10 + (c - 'a'), diff --git a/src/UFiber.Configurator/Program.cs b/src/UFiber.Configurator/Program.cs index 568c977..55adaa9 100644 --- a/src/UFiber.Configurator/Program.cs +++ b/src/UFiber.Configurator/Program.cs @@ -34,7 +34,7 @@ var rootCommand = new RootCommand("Apply configuration changes to UFiber devices "4-digit Vendor Id (e.g. HWTC, MTSC, etc.). Combined with --serial, a GPON Serial Number is built.", ArgumentArity.ZeroOrOne), new Option( "--serial", - "8-digit serial number (e.g. 01234567). Combined with --vendor, a GPON Serial Number is built.", ArgumentArity.ZeroOrOne), + "8-digit (e.g. 01234567) serial number or 16-digit (e.g. 41-4C-43-4C-xx-xx-xx-xx) HEX serial number. Combined with --vendor, a GPON Serial Number is built. Note: If a 16-digit HEX value is provided, the first 4 bytes (8 digits) will replace whatever value was passed to Vendor Id with '--vendor'.", ArgumentArity.ZeroOrOne), new Option( "--mac", "The desired MAC address to clone.", ArgumentArity.ZeroOrOne),