Skip to content

Commit

Permalink
[Core] Kill Console.WriteLine and update internal signer implementa…
Browse files Browse the repository at this point in the history
…tion (#620)

Co-authored-by: DarkRRb <[email protected]>
  • Loading branch information
DarkRRb and DarkRRb authored Oct 1, 2024
1 parent 2380b1b commit 80a893d
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 154 deletions.
3 changes: 0 additions & 3 deletions Lagrange.Core/Internal/Service/Message/PushMessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ protected override bool Parse(Span<byte> input, BotKeystore keystore, BotAppInfo
}
default:
{
Console.WriteLine($"Unknown message type: {packetType}: {input.Hex()}");
break;
}
}
Expand Down Expand Up @@ -231,7 +230,6 @@ private static void ProcessEvent0x2DC(Span<byte> payload, PushMsg msg, List<Prot
}
default:
{
Console.WriteLine($"Unknown Event0x2DC message type: {pkgType}: {payload.Hex()}");
break;
}
}
Expand Down Expand Up @@ -293,7 +291,6 @@ private static void ProcessEvent0x210(Span<byte> payload, PushMsg msg, List<Prot
}
default:
{
Console.WriteLine($"Unknown Event0x210 message type: {pkgType}: {payload.Hex()}");
break;
}
}
Expand Down
1 change: 0 additions & 1 deletion Lagrange.Core/Utility/Binary/Tlv/TlvPacker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public BinaryPacket PackQrCode(ushort[] cmd)
{
if (!Tlvs.TryGetValue(cmd, out type))
{
Console.WriteLine(packet.ToArray().Hex());
return null;
}
}
Expand Down
2 changes: 0 additions & 2 deletions Lagrange.Core/Utility/Crypto/TeaImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ internal class TeaImpl
{
public byte[] Encrypt(byte[] data, byte[] key)
{
// Console.WriteLine("Data: " + data.Hex() + $" Key: {key.Hex()}");
return TeaProvider.Encrypt(data, key);
}

public Span<byte> Decrypt(Span<byte> data, Span<byte> key)
{
var decipher = TeaProvider.Decrypt(data, key);
// Console.WriteLine("Decrypted Data: " + decipher.Hex() + $" Key: {key.Hex()}");
return decipher;
}
}
66 changes: 2 additions & 64 deletions Lagrange.Core/Utility/Sign/LinuxSigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,7 @@

namespace Lagrange.Core.Utility.Sign;

internal class LinuxSigner : SignProvider
internal class LinuxSigner : UrlSigner
{
private readonly HttpClient _client = new();

private const string Url = "https://sign.lagrangecore.org/api/sign/25765";

private readonly Timer _timer;

public LinuxSigner()
{
_timer = new Timer(_ =>
{
bool reconnect = Available = Test();
if (reconnect) _timer?.Change(-1, 5000);
});
}

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[35]; // Dummy signature

var payload = new JsonObject
{
{ "cmd", cmd },
{ "seq", seq },
{ "src", body.Hex() },
};

try
{
var message = _client.PostAsJsonAsync(Url, payload).Result;
string response = message.Content.ReadAsStringAsync().Result;
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[20];
}
catch (Exception)
{
Available = false;
_timer.Change(0, 5000);

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
}
}

public bool Test()
{
try
{
string response = Http.GetAsync($"{Url}/ping").GetAwaiter().GetResult();
if (JsonSerializer.Deserialize<JsonObject>(response)?["code"]?.GetValue<int>() == 0) return true;
}
catch
{
return false;
}

return false;
}
public LinuxSigner() : base("https://sign.lagrangecore.org/api/sign/25765") { }
}
44 changes: 2 additions & 42 deletions Lagrange.Core/Utility/Sign/MacSigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,7 @@

namespace Lagrange.Core.Utility.Sign;

internal class MacSigner : SignProvider
internal class MacSigner : UrlSigner
{
private readonly HttpClient _client = new();

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

var payload = new JsonObject
{
{ "cmd", cmd },
{ "seq", seq },
{ "src", body.Hex() },
};

try
{
var message = _client.PostAsJsonAsync(MacOsUrl, payload).Result;
string response = message.Content.ReadAsStringAsync().Result;
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
}
}

public bool Test()
{
throw new NotImplementedException();
}
public MacSigner() : base(null) { }
}
57 changes: 57 additions & 0 deletions Lagrange.Core/Utility/Sign/UrlSigner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Net;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace Lagrange.Core.Utility.Sign;

internal class UrlSigner : SignProvider
{
private readonly string? _signServer;

private readonly HttpClient _client = new();

public UrlSigner(string? url)
{
_signServer = url;
}

public override byte[]? Sign(string cmd, uint seq, byte[] body, out byte[]? e, out string? t)
{

e = null;
t = null;

if (!WhiteListCommand.Contains(cmd)) return null;
if (_signServer == null) throw new Exception("Sign server is not configured");

using var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(_signServer),
Content = JsonContent.Create(new JsonObject
{
{ "cmd", cmd },
{ "seq", seq },
{ "src", Convert.ToHexString(body) }
})
};

using var message = _client.Send(request);
if (message.StatusCode != HttpStatusCode.OK) throw new Exception($"Signer server returned a {message.StatusCode}");
var json = JsonDocument.Parse(message.Content.ReadAsStream()).RootElement;

var valueJson = json.GetProperty("value");
var extraJson = valueJson.GetProperty("extra");
var tokenJson = valueJson.GetProperty("token");
var signJson = valueJson.GetProperty("sign");

string? token = tokenJson.GetString();
string? extra = extraJson.GetString();
e = extra != null ? Convert.FromHexString(extra) : Array.Empty<byte>();
t = token != null ? Encoding.UTF8.GetString(Convert.FromHexString(token)) : "";
string sign = signJson.GetString() ?? throw new Exception("Signer server returned an empty sign");
return Convert.FromHexString(sign);
}
}
44 changes: 2 additions & 42 deletions Lagrange.Core/Utility/Sign/WindowsSigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,7 @@

namespace Lagrange.Core.Utility.Sign;

internal class WindowsSigner : SignProvider
internal class WindowsSigner : UrlSigner
{
private readonly HttpClient _client = new();

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

var payload = new JsonObject
{
{ "cmd", cmd },
{ "seq", seq },
{ "src", body.Hex() },
};

try
{
var message = _client.PostAsJsonAsync(WindowsUrl, payload).Result;
string response = message.Content.ReadAsStringAsync().Result;
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
}
}

public bool Test()
{
throw new NotImplementedException();
}
public WindowsSigner() : base(null) { }
}

0 comments on commit 80a893d

Please sign in to comment.