Skip to content

Commit

Permalink
Refactored Sign to SignProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Oct 7, 2023
1 parent 7fb9aa0 commit b9db643
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 41 deletions.
39 changes: 39 additions & 0 deletions Lagrange.Core/Utility/Sign/LinuxSigner.cs
Original file line number Diff line number Diff line change
@@ -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<string, string>
{
{ "cmd", cmd },
{ "seq", seq.ToString() },
{ "src", body.Hex() },
};
string response = Http.GetAsync(Url, payload).GetAwaiter().GetResult();
var json = JsonSerializer.Deserialize<JsonObject>(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
}
}
}
42 changes: 42 additions & 0 deletions Lagrange.Core/Utility/Sign/MacSigner.cs
Original file line number Diff line number Diff line change
@@ -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<string, string>
{
{ "cmd", cmd },
{ "seq", seq.ToString() },
{ "src", body.Hex() },
};
string response = Http.GetAsync(MacOsUrl, payload).GetAwaiter().GetResult();
var json = JsonSerializer.Deserialize<JsonObject>(response);

ver = json?["value"]?["extra"]?.ToString().UnHex() ?? Array.Empty<byte>();
token = Encoding.ASCII.GetString(json?["value"]?["token"]?.ToString().UnHex() ?? Array.Empty<byte>());
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
}
}
}
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -54,32 +45,5 @@ internal static class Signature
"OidbSvcTrpcTcp.0xf67_5"
};

/// <summary>
/// Get O3Signature
/// </summary>
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<string, string>
{
{ "cmd", cmd },
{ "seq", seq.ToString() },
{ "src", body.Hex() },
};
string response = Http.GetAsync(Url, payload).GetAwaiter().GetResult();
var json = JsonSerializer.Deserialize<JsonObject>(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);
}
42 changes: 42 additions & 0 deletions Lagrange.Core/Utility/Sign/WindowsSigner.cs
Original file line number Diff line number Diff line change
@@ -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<string, string>
{
{ "cmd", cmd },
{ "seq", seq.ToString() },
{ "src", body.Hex() },
};
string response = Http.GetAsync(WindowsUrl, payload).GetAwaiter().GetResult();
var json = JsonSerializer.Deserialize<JsonObject>(response);

ver = json?["value"]?["extra"]?.ToString().UnHex() ?? Array.Empty<byte>();
token = Encoding.ASCII.GetString(json?["value"]?["token"]?.ToString().UnHex() ?? Array.Empty<byte>());
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
}
}
}

0 comments on commit b9db643

Please sign in to comment.