Merge pull request #3 from Unifi-Tools/allow-hex-serial

Allow HEX Serial and SLID values.
This commit is contained in:
Gutemberg Ribeiro 2021-04-26 19:03:20 -03:00 committed by GitHub
commit 92d9754c73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 10 deletions

8
.vscode/launch.json vendored
View File

@ -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",

View File

@ -59,7 +59,7 @@ Usage:
UFiber.Configurator [options]
Options:
--host <host> IP or hostname of the target UFiber device.
--host <host> IP or hostname of the target UFiber device. [default: 192.168.1.1]
--user <user> SSH user name. [default: ubnt]
--pw <pw> SSH password. [default: ubnt]
--port <port> SSH port of the target UFiber device. [default: 22]
@ -67,8 +67,10 @@ Options:
--slid <slid> The SLID (or PLOAM Password).
--vendor <vendor> 4-digit Vendor Id (e.g. HWTC, MTSC, etc.). Combined with --serial, a GPON Serial Number is
built.
--serial <serial> 8-digit serial number (e.g. 01234567). Combined with --vendor, a GPON Serial Number is
built.
--serial <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 <mac> The desired MAC address to clone.
--version Show version information
-?, -h, --help Show help and usage information

View File

@ -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<byte> 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'),

View File

@ -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<string>(
"--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<string>(
"--mac",
"The desired MAC address to clone.", ArgumentArity.ZeroOrOne),