Skip to content

Commit b9db643

Browse files
LinwenxuanLinwenxuan
authored andcommitted
Refactored Sign to SignProvider
1 parent 7fb9aa0 commit b9db643

File tree

4 files changed

+128
-41
lines changed

4 files changed

+128
-41
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Nodes;
3+
using Lagrange.Core.Utility.Extension;
4+
using Lagrange.Core.Utility.Network;
5+
6+
namespace Lagrange.Core.Utility.Sign;
7+
8+
internal class LinuxSigner : SignProvider
9+
{
10+
private const string Url = "http://cn-chengdu.qwqq.moe:20747/api/sign";
11+
12+
public override byte[]? Sign(string cmd, uint seq, byte[] body, out byte[]? ver, out string? token)
13+
{
14+
ver = null;
15+
token = null;
16+
if (!WhiteListCommand.Contains(cmd)) return null;
17+
if (Available || string.IsNullOrEmpty(Url)) return new byte[20]; // Dummy signature
18+
19+
try
20+
{
21+
var payload = new Dictionary<string, string>
22+
{
23+
{ "cmd", cmd },
24+
{ "seq", seq.ToString() },
25+
{ "src", body.Hex() },
26+
};
27+
string response = Http.GetAsync(Url, payload).GetAwaiter().GetResult();
28+
var json = JsonSerializer.Deserialize<JsonObject>(response);
29+
30+
return json?["value"]?["sign"]?.ToString().UnHex() ?? new byte[20];
31+
}
32+
catch (Exception)
33+
{
34+
Available = false;
35+
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{nameof(LinuxSigner)}] Failed to get signature, using dummy signature");
36+
return new byte[20]; // Dummy signature
37+
}
38+
}
39+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Text;
2+
using System.Text.Json;
3+
using System.Text.Json.Nodes;
4+
using Lagrange.Core.Utility.Extension;
5+
using Lagrange.Core.Utility.Network;
6+
7+
namespace Lagrange.Core.Utility.Sign;
8+
9+
internal class MacSigner : SignProvider
10+
{
11+
private const string MacOsUrl = "http://127.0.0.1:7458/api/sign";
12+
13+
public override byte[]? Sign(string cmd, uint seq, byte[] body, out byte[]? ver, out string? token)
14+
{
15+
ver = null;
16+
token = null;
17+
if (!WhiteListCommand.Contains(cmd)) return null;
18+
if (!Available || string.IsNullOrEmpty(MacOsUrl)) return new byte[35]; // Dummy signature
19+
20+
try
21+
{
22+
var payload = new Dictionary<string, string>
23+
{
24+
{ "cmd", cmd },
25+
{ "seq", seq.ToString() },
26+
{ "src", body.Hex() },
27+
};
28+
string response = Http.GetAsync(MacOsUrl, payload).GetAwaiter().GetResult();
29+
var json = JsonSerializer.Deserialize<JsonObject>(response);
30+
31+
ver = json?["value"]?["extra"]?.ToString().UnHex() ?? Array.Empty<byte>();
32+
token = Encoding.ASCII.GetString(json?["value"]?["token"]?.ToString().UnHex() ?? Array.Empty<byte>());
33+
return json?["value"]?["sign"]?.ToString().UnHex() ?? new byte[35];
34+
}
35+
catch (Exception)
36+
{
37+
Available = false;
38+
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{nameof(MacSigner)}] Failed to get signature, using dummy signature");
39+
return new byte[35]; // Dummy signature
40+
}
41+
}
42+
}
Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
using System.Text.Json;
2-
using System.Text.Json.Nodes;
3-
using Lagrange.Core.Utility.Extension;
4-
using Lagrange.Core.Utility.Network;
1+
namespace Lagrange.Core.Utility.Sign;
52

6-
#pragma warning disable CS8618
7-
8-
namespace Lagrange.Core.Utility;
9-
10-
internal static class Signature
3+
internal abstract class SignProvider
114
{
12-
private const string Url = "http://cn-chengdu.qwqq.moe:20747/api/sign";
13-
14-
private static bool _available = true;
5+
protected bool Available = true;
156

16-
private static readonly string[] WhiteListCommand =
7+
protected static readonly string[] WhiteListCommand =
178
{
189
"trpc.o3.ecdh_access.EcdhAccess.SsoEstablishShareKey",
1910
"trpc.o3.ecdh_access.EcdhAccess.SsoSecureAccess",
@@ -54,32 +45,5 @@ internal static class Signature
5445
"OidbSvcTrpcTcp.0xf67_5"
5546
};
5647

57-
/// <summary>
58-
/// Get O3Signature
59-
/// </summary>
60-
public static byte[]? GetSignature(string cmd, uint seq, byte[] body)
61-
{
62-
if (!WhiteListCommand.Contains(cmd)) return null;
63-
if (!_available || string.IsNullOrEmpty(Url)) return new byte[20]; // Dummy signature
64-
65-
try
66-
{
67-
var payload = new Dictionary<string, string>
68-
{
69-
{ "cmd", cmd },
70-
{ "seq", seq.ToString() },
71-
{ "src", body.Hex() },
72-
};
73-
string response = Http.GetAsync(Url, payload).GetAwaiter().GetResult();
74-
var json = JsonSerializer.Deserialize<JsonObject>(response);
75-
76-
return json?["value"]?["sign"]?.ToString().UnHex() ?? new byte[20];
77-
}
78-
catch (Exception)
79-
{
80-
_available = false;
81-
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{nameof(Signature)}] Failed to get signature, using dummy signature");
82-
return new byte[20]; // Dummy signature
83-
}
84-
}
48+
public abstract byte[]? Sign(string cmd, uint seq, byte[] body, out byte[]? ver, out string? token);
8549
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Text;
2+
using System.Text.Json;
3+
using System.Text.Json.Nodes;
4+
using Lagrange.Core.Utility.Extension;
5+
using Lagrange.Core.Utility.Network;
6+
7+
namespace Lagrange.Core.Utility.Sign;
8+
9+
internal class WindowsSigner : SignProvider
10+
{
11+
private const string WindowsUrl = "";
12+
13+
public override byte[]? Sign(string cmd, uint seq, byte[] body, out byte[]? ver, out string? token)
14+
{
15+
ver = null;
16+
token = null;
17+
if (!WhiteListCommand.Contains(cmd)) return null;
18+
if (!Available || string.IsNullOrEmpty(WindowsUrl)) return new byte[35]; // Dummy signature
19+
20+
try
21+
{
22+
var payload = new Dictionary<string, string>
23+
{
24+
{ "cmd", cmd },
25+
{ "seq", seq.ToString() },
26+
{ "src", body.Hex() },
27+
};
28+
string response = Http.GetAsync(WindowsUrl, payload).GetAwaiter().GetResult();
29+
var json = JsonSerializer.Deserialize<JsonObject>(response);
30+
31+
ver = json?["value"]?["extra"]?.ToString().UnHex() ?? Array.Empty<byte>();
32+
token = Encoding.ASCII.GetString(json?["value"]?["token"]?.ToString().UnHex() ?? Array.Empty<byte>());
33+
return json?["value"]?["sign"]?.ToString().UnHex() ?? new byte[35];
34+
}
35+
catch (Exception)
36+
{
37+
Available = false;
38+
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{nameof(WindowsSigner)}] Failed to get signature, using dummy signature");
39+
return new byte[35]; // Dummy signature
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)