diff --git a/Lagrange.Core/Utility/Sign/LinuxSigner.cs b/Lagrange.Core/Utility/Sign/LinuxSigner.cs new file mode 100644 index 000000000..fbfb5be64 --- /dev/null +++ b/Lagrange.Core/Utility/Sign/LinuxSigner.cs @@ -0,0 +1,39 @@ +using System.Text.Json; +using System.Text.Json.Nodes; +using Lagrange.Core.Utility.Extension; +using Lagrange.Core.Utility.Network; + +namespace Lagrange.Core.Utility.Sign; + +internal class LinuxSigner : SignProvider +{ + private const string Url = "http://cn-chengdu.qwqq.moe:20747/api/sign"; + + public override byte[]? Sign(string cmd, uint seq, byte[] body, out byte[]? ver, out string? token) + { + ver = null; + token = null; + if (!WhiteListCommand.Contains(cmd)) return null; + if (Available || string.IsNullOrEmpty(Url)) return new byte[20]; // Dummy signature + + try + { + var payload = new Dictionary + { + { "cmd", cmd }, + { "seq", seq.ToString() }, + { "src", body.Hex() }, + }; + string response = Http.GetAsync(Url, payload).GetAwaiter().GetResult(); + var json = JsonSerializer.Deserialize(response); + + return json?["value"]?["sign"]?.ToString().UnHex() ?? new byte[20]; + } + catch (Exception) + { + Available = false; + Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{nameof(LinuxSigner)}] Failed to get signature, using dummy signature"); + return new byte[20]; // Dummy signature + } + } +} \ No newline at end of file diff --git a/Lagrange.Core/Utility/Sign/MacSigner.cs b/Lagrange.Core/Utility/Sign/MacSigner.cs new file mode 100644 index 000000000..f35599f3b --- /dev/null +++ b/Lagrange.Core/Utility/Sign/MacSigner.cs @@ -0,0 +1,42 @@ +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; +using Lagrange.Core.Utility.Extension; +using Lagrange.Core.Utility.Network; + +namespace Lagrange.Core.Utility.Sign; + +internal class MacSigner : SignProvider +{ + private const string MacOsUrl = "http://127.0.0.1:7458/api/sign"; + + public override byte[]? Sign(string cmd, uint seq, byte[] body, out byte[]? ver, out string? token) + { + ver = null; + token = null; + if (!WhiteListCommand.Contains(cmd)) return null; + if (!Available || string.IsNullOrEmpty(MacOsUrl)) return new byte[35]; // Dummy signature + + try + { + var payload = new Dictionary + { + { "cmd", cmd }, + { "seq", seq.ToString() }, + { "src", body.Hex() }, + }; + string response = Http.GetAsync(MacOsUrl, payload).GetAwaiter().GetResult(); + var json = JsonSerializer.Deserialize(response); + + ver = json?["value"]?["extra"]?.ToString().UnHex() ?? Array.Empty(); + token = Encoding.ASCII.GetString(json?["value"]?["token"]?.ToString().UnHex() ?? Array.Empty()); + return json?["value"]?["sign"]?.ToString().UnHex() ?? new byte[35]; + } + catch (Exception) + { + Available = false; + Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{nameof(MacSigner)}] Failed to get signature, using dummy signature"); + return new byte[35]; // Dummy signature + } + } +} \ No newline at end of file diff --git a/Lagrange.Core/Utility/Signature.cs b/Lagrange.Core/Utility/Sign/SignProvider.cs similarity index 52% rename from Lagrange.Core/Utility/Signature.cs rename to Lagrange.Core/Utility/Sign/SignProvider.cs index b9d5d2b4b..9906f18ef 100644 --- a/Lagrange.Core/Utility/Signature.cs +++ b/Lagrange.Core/Utility/Sign/SignProvider.cs @@ -1,19 +1,10 @@ -using System.Text.Json; -using System.Text.Json.Nodes; -using Lagrange.Core.Utility.Extension; -using Lagrange.Core.Utility.Network; +namespace Lagrange.Core.Utility.Sign; -#pragma warning disable CS8618 - -namespace Lagrange.Core.Utility; - -internal static class Signature +internal abstract class SignProvider { - private const string Url = "http://cn-chengdu.qwqq.moe:20747/api/sign"; - - private static bool _available = true; + protected bool Available = true; - private static readonly string[] WhiteListCommand = + protected static readonly string[] WhiteListCommand = { "trpc.o3.ecdh_access.EcdhAccess.SsoEstablishShareKey", "trpc.o3.ecdh_access.EcdhAccess.SsoSecureAccess", @@ -54,32 +45,5 @@ internal static class Signature "OidbSvcTrpcTcp.0xf67_5" }; - /// - /// Get O3Signature - /// - public static byte[]? GetSignature(string cmd, uint seq, byte[] body) - { - if (!WhiteListCommand.Contains(cmd)) return null; - if (!_available || string.IsNullOrEmpty(Url)) return new byte[20]; // Dummy signature - - try - { - var payload = new Dictionary - { - { "cmd", cmd }, - { "seq", seq.ToString() }, - { "src", body.Hex() }, - }; - string response = Http.GetAsync(Url, payload).GetAwaiter().GetResult(); - var json = JsonSerializer.Deserialize(response); - - return json?["value"]?["sign"]?.ToString().UnHex() ?? new byte[20]; - } - catch (Exception) - { - _available = false; - Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{nameof(Signature)}] Failed to get signature, using dummy signature"); - return new byte[20]; // Dummy signature - } - } + public abstract byte[]? Sign(string cmd, uint seq, byte[] body, out byte[]? ver, out string? token); } \ No newline at end of file diff --git a/Lagrange.Core/Utility/Sign/WindowsSigner.cs b/Lagrange.Core/Utility/Sign/WindowsSigner.cs new file mode 100644 index 000000000..8f8ad151d --- /dev/null +++ b/Lagrange.Core/Utility/Sign/WindowsSigner.cs @@ -0,0 +1,42 @@ +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; +using Lagrange.Core.Utility.Extension; +using Lagrange.Core.Utility.Network; + +namespace Lagrange.Core.Utility.Sign; + +internal class WindowsSigner : SignProvider +{ + private const string WindowsUrl = ""; + + public override byte[]? Sign(string cmd, uint seq, byte[] body, out byte[]? ver, out string? token) + { + ver = null; + token = null; + if (!WhiteListCommand.Contains(cmd)) return null; + if (!Available || string.IsNullOrEmpty(WindowsUrl)) return new byte[35]; // Dummy signature + + try + { + var payload = new Dictionary + { + { "cmd", cmd }, + { "seq", seq.ToString() }, + { "src", body.Hex() }, + }; + string response = Http.GetAsync(WindowsUrl, payload).GetAwaiter().GetResult(); + var json = JsonSerializer.Deserialize(response); + + ver = json?["value"]?["extra"]?.ToString().UnHex() ?? Array.Empty(); + token = Encoding.ASCII.GetString(json?["value"]?["token"]?.ToString().UnHex() ?? Array.Empty()); + return json?["value"]?["sign"]?.ToString().UnHex() ?? new byte[35]; + } + catch (Exception) + { + Available = false; + Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{nameof(WindowsSigner)}] Failed to get signature, using dummy signature"); + return new byte[35]; // Dummy signature + } + } +} \ No newline at end of file