submodule对gitea不管用,所以直接拉了一份拉格兰

This commit is contained in:
2025-02-04 16:29:43 +08:00
parent b0bfc803e3
commit d149a2ea0f
1023 changed files with 43308 additions and 18 deletions

View File

@@ -0,0 +1,32 @@
using Lagrange.Core.Utility.Binary;
using Lagrange.Core.Utility.Extension;
namespace Lagrange.Core.Test.Tests;
public class BinaryTest
{
public class Binary
{
[BinaryProperty] public uint Value { get; set; } = 114514;
[BinaryProperty] public ulong Value2 { get; set; } = 1919810;
}
public void Test()
{
var binary = new Binary();
var bytes = BinarySerializer.Serialize(binary);
Console.WriteLine(bytes.ToArray().Hex());
Console.WriteLine(bytes.Length);
var binary2 = new BinaryPacket();
binary2.WriteUint(114514);
binary2.WriteUlong(1919810);
Console.WriteLine(binary2.ToArray().Hex());
var newPacket = new BinaryPacket(bytes.ToArray());
var binary3 = newPacket.Deserialize<Binary>();
Console.WriteLine(binary3.Value);
Console.WriteLine(binary3.Value2);
}
}

View File

@@ -0,0 +1,82 @@
using Lagrange.Core.Utility.Crypto.Provider.Ecdh;
namespace Lagrange.Core.Test.Tests;
public class EcdhTest
{
public static void Test()
{
{
var uncompressedPublicKey = new byte[]
{
0x04,
0xEB, 0xCA, 0x94, 0xD7, 0x33, 0xE3, 0x99, 0xB2,
0xDB, 0x96, 0xEA, 0xCD, 0xD3, 0xF6, 0x9A, 0x8B,
0xB0, 0xF7, 0x42, 0x24, 0xE2, 0xB4, 0x4E, 0x33,
0x57, 0x81, 0x22, 0x11, 0xD2, 0xE6, 0x2E, 0xFB,
0xC9, 0x1B, 0xB5, 0x53, 0x09, 0x8E, 0x25, 0xE3,
0x3A, 0x79, 0x9A, 0xDC, 0x7F, 0x76, 0xFE, 0xB2,
0x08, 0xDA, 0x7C, 0x65, 0x22, 0xCD, 0xB0, 0x71,
0x9A, 0x30, 0x51, 0x80, 0xCC, 0x54, 0xA8, 0x2E
};
var compressedPublicKey = new byte[]
{
0x02,
0xEB, 0xCA, 0x94, 0xD7, 0x33, 0xE3, 0x99, 0xB2,
0xDB, 0x96, 0xEA, 0xCD, 0xD3, 0xF6, 0x9A, 0x8B,
0xB0, 0xF7, 0x42, 0x24, 0xE2, 0xB4, 0x4E, 0x33,
0x57, 0x81, 0x22, 0x11, 0xD2, 0xE6, 0x2E, 0xFB
};
EcdhProvider ecdhProvider = new EcdhProvider(EllipticCurve.Prime256V1);
var unpacked1 = ecdhProvider.UnpackPublic(uncompressedPublicKey);
var unpacked2 = ecdhProvider.UnpackPublic(compressedPublicKey);
if (unpacked1.X == unpacked2.X && unpacked1.Y == unpacked2.Y)
{
Console.WriteLine("UnpackPublic() works correctly");
}
else
{
Console.WriteLine("UnpackPublic() does not work correctly");
}
}
{
// from https://github.com/KonataDev/Konata.Core/blob/develop/Konata.Core.Test/UtilTest/EcdhTest.cs
var uncompressedPublicKey = new byte[]
{
0x04,
0xD5, 0xCF, 0xB0, 0x2D, 0x5D, 0x4F, 0xCA, 0x2C,
0x84, 0xF6, 0xF1, 0x29, 0x4B, 0x45, 0x5B, 0xAB,
0x4C, 0x96, 0x98, 0xDD, 0x57, 0x2B, 0xF8, 0x63,
0x82, 0xA9, 0xDA, 0xF8, 0xAD, 0xE9, 0xD4, 0x5A,
0x57, 0xDE, 0x14, 0x04, 0xFA, 0x5D, 0x41, 0x29,
0x1E, 0x0A, 0x56, 0xCB, 0x45, 0x08, 0xD3, 0x2F
};
var compressedPublicKey = new byte[]
{
0x03,
0xD5, 0xCF, 0xB0, 0x2D, 0x5D, 0x4F, 0xCA, 0x2C,
0x84, 0xF6, 0xF1, 0x29, 0x4B, 0x45, 0x5B, 0xAB,
0x4C, 0x96, 0x98, 0xDD, 0x57, 0x2B, 0xF8, 0x63
};
EcdhProvider ecdhProvider = new EcdhProvider(EllipticCurve.Secp192K1);
var unpacked1 = ecdhProvider.UnpackPublic(uncompressedPublicKey);
var unpacked2 = ecdhProvider.UnpackPublic(compressedPublicKey);
if (unpacked1.X == unpacked2.X && unpacked1.Y == unpacked2.Y)
{
Console.WriteLine("UnpackPublic() works correctly");
}
else
{
Console.WriteLine("UnpackPublic() does not work correctly");
}
}
}
}

View File

@@ -0,0 +1,65 @@
using Lagrange.Core.Common;
using Lagrange.Core.Common.Interface;
using Lagrange.Core.Common.Interface.Api;
using Lagrange.Core.Message;
namespace Lagrange.Core.Test.Tests;
// ReSharper disable once InconsistentNaming
public class NTLoginTest
{
public async Task LoginByPassword()
{
var deviceInfo = WtLoginTest.GetDeviceInfo();
var keyStore = WtLoginTest.LoadKeystore();
if (keyStore == null)
{
Console.WriteLine("Please login by QrCode first");
return;
}
var bot = BotFactory.Create(new BotConfig()
{
UseIPv6Network = false,
GetOptimumServer = true,
AutoReconnect = true,
Protocol = Protocols.Linux
}, deviceInfo, keyStore);
bot.Invoker.OnBotLogEvent += (_, @event) =>
{
Utility.Console.ChangeColorByTitle(@event.Level);
Console.WriteLine(@event.ToString());
};
bot.Invoker.OnBotOnlineEvent += (_, @event) =>
{
Console.WriteLine(@event.ToString());
WtLoginTest.SaveKeystore(bot.UpdateKeystore());
};
bot.Invoker.OnBotCaptchaEvent += (_, @event) =>
{
Console.WriteLine(@event.ToString());
var captcha = Console.ReadLine();
var randStr = Console.ReadLine();
if (captcha != null && randStr != null) bot.SubmitCaptcha(captcha, randStr);
};
bot.Invoker.OnGroupInvitationReceived += (_, @event) =>
{
Console.WriteLine(@event.ToString());
};
await bot.LoginByPassword();
var friendChain = MessageBuilder.Group(411240674)
.Text("This is the friend message sent by Lagrange.Core")
.Mention(1925648680);
await bot.SendMessage(friendChain.Build());
await Task.Delay(1000);
}
}

View File

@@ -0,0 +1,79 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Lagrange.Core.Common;
using Lagrange.Core.Common.Interface;
using Lagrange.Core.Common.Interface.Api;
namespace Lagrange.Core.Test.Tests;
public class WtLoginTest
{
public async Task FetchQrCode()
{
var deviceInfo = GetDeviceInfo();
var keyStore = LoadKeystore() ?? new BotKeystore();
var bot = BotFactory.Create(new BotConfig
{
UseIPv6Network = false,
GetOptimumServer = true,
AutoReconnect = true,
Protocol = Protocols.Linux
}, deviceInfo, keyStore);
bot.Invoker.OnBotLogEvent += (context, @event) =>
{
Utility.Console.ChangeColorByTitle(@event.Level);
Console.WriteLine(@event.ToString());
};
bot.Invoker.OnBotOnlineEvent += (context, @event) =>
{
Console.WriteLine(@event.ToString());
SaveKeystore(bot.UpdateKeystore());
};
var qrCode = await bot.FetchQrCode();
if (qrCode != null)
{
await File.WriteAllBytesAsync("qr.png", qrCode.Value.QrCode);
await bot.LoginByQrCode();
}
}
public static BotDeviceInfo GetDeviceInfo()
{
if (File.Exists("Test/DeviceInfo.json"))
{
var info = JsonSerializer.Deserialize<BotDeviceInfo>(File.ReadAllText("Test/DeviceInfo.json"));
if (info != null) return info;
info = BotDeviceInfo.GenerateInfo();
File.WriteAllText("Test/DeviceInfo.json", JsonSerializer.Serialize(info));
return info;
}
var deviceInfo = BotDeviceInfo.GenerateInfo();
File.WriteAllText("Test/DeviceInfo.json", JsonSerializer.Serialize(deviceInfo));
return deviceInfo;
}
public static void SaveKeystore(BotKeystore keystore) =>
File.WriteAllText("Test/Keystore.json", JsonSerializer.Serialize(keystore));
public static BotKeystore? LoadKeystore()
{
try
{
var text = File.ReadAllText("Test/Keystore.json");
return JsonSerializer.Deserialize<BotKeystore>(text, new JsonSerializerOptions()
{
ReferenceHandler = ReferenceHandler.Preserve
});
}
catch
{
return null;
}
}
}