From cbe982ba7707dc07abcdd9a1088a0b85b0d4cd7f Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 12 Apr 2023 04:19:12 +0200 Subject: [PATCH] Adjusted BoatCom protocol use, I have not tested this :D --- BackupManager/Config.cs | 1 + BackupManager/Program.cs | 39 +++++++++++++++++---------------------- do-backup.sh | 2 +- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/BackupManager/Config.cs b/BackupManager/Config.cs index 327c6dd..dfd2506 100644 --- a/BackupManager/Config.cs +++ b/BackupManager/Config.cs @@ -16,6 +16,7 @@ public string SatoriHost { get; set; } public ushort SatoriPort { get; set; } public string SatoriSecret { get; set; } + public string SatoriFormat { get; set; } = @"/msg flash {0}"; public bool SatoriErrorsOnly { get; set; } = true; } } diff --git a/BackupManager/Program.cs b/BackupManager/Program.cs index 0c26142..459a977 100644 --- a/BackupManager/Program.cs +++ b/BackupManager/Program.cs @@ -287,39 +287,34 @@ namespace BackupManager { EndPoint endPoint = new IPEndPoint(ip, Config.SatoriPort); - StringBuilder textBuilder = new StringBuilder(); + StringBuilder textBuilder = new(); textBuilder.AppendFormat(@"[b]Backup System[/b] [{0}]: ", Environment.MachineName); - if (error) + if(error) textBuilder.Append(@"[color=red]"); textBuilder.Append(text); - if (error) + if(error) textBuilder.Append(@"[/color]"); - text = textBuilder.ToString(); + text = string.Format(Config.SatoriFormat, textBuilder); - StringBuilder messageBuilder = new StringBuilder(); - - using (HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(Config.SatoriSecret))) { - byte[] hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(text)); - - foreach (byte b in hash) - messageBuilder.AppendFormat(@"{0:x2}", b); + byte[] opcode = new byte[1] { 1 }; + byte[] hash; + using(HMACSHA256 hmac = new(Encoding.UTF8.GetBytes(Config.SatoriSecret))) { + hmac.TransformBlock(opcode, 0, opcode.Length, null, 0); + hash = Encoding.UTF8.GetBytes(text); + hmac.TransformFinalBlock(hash, 0, hash.Length); + hash = hmac.Hash; } - messageBuilder.Append(text); - string message = messageBuilder.ToString(); - byte[] messageBytes = new byte[Encoding.UTF8.GetByteCount(message) + 2]; - messageBytes[0] = messageBytes[messageBytes.Length - 1] = 0x0F; - Encoding.UTF8.GetBytes(message).CopyTo(messageBytes, 1); - - using (Socket sock = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { - sock.NoDelay = sock.Blocking = true; - sock.Connect(endPoint); - sock.Send(messageBytes); - } + using Socket sock = new(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); + sock.NoDelay = sock.Blocking = true; + sock.Connect(endPoint); + sock.Send(hash); + sock.Send(opcode); + sock.Send(Encoding.UTF8.GetBytes(text)); } } } diff --git a/do-backup.sh b/do-backup.sh index 67b545c..4af1438 100755 --- a/do-backup.sh +++ b/do-backup.sh @@ -1,5 +1,5 @@ cwd="$(pwd)" export DOTNET_CLI_TELEMETRY_OPTOUT=1 cd "$(dirname "$0")" -dotnet run --project BackupManager -c Release -f net5.0 -- -cron +dotnet run --project BackupManager -c Release -f net6.0 -- -cron cd "$cwd"