106
Login/QrCode.cs
Normal file
106
Login/QrCode.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Lagrange.Core;
|
||||
using Lagrange.Core.Common;
|
||||
using Lagrange.Core.Common.Interface;
|
||||
using Lagrange.Core.Common.Interface.Api;
|
||||
using Shrink.Utility;
|
||||
using Console = System.Console;
|
||||
|
||||
namespace Shrink.Login;
|
||||
|
||||
public class QrCode
|
||||
{
|
||||
private static QrCode? _instance;
|
||||
|
||||
private static readonly object Lock = new();
|
||||
|
||||
public static QrCode Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
// 双重检查锁定
|
||||
if (_instance != null) return _instance;
|
||||
lock (Lock)
|
||||
{
|
||||
_instance ??= new QrCode();
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
public BotContext? Client;
|
||||
public static void SaveKeystore(BotKeystore keystore) =>
|
||||
File.WriteAllText("Keystore.json", JsonSerializer.Serialize(keystore));
|
||||
|
||||
public static BotDeviceInfo GetDeviceInfo()
|
||||
{
|
||||
if (File.Exists("DeviceInfo.json"))
|
||||
{
|
||||
var info = JsonSerializer.Deserialize<BotDeviceInfo>(File.ReadAllText("DeviceInfo.json"));
|
||||
if (info != null) return info;
|
||||
|
||||
info = BotDeviceInfo.GenerateInfo();
|
||||
File.WriteAllText("DeviceInfo.json", JsonSerializer.Serialize(info));
|
||||
return info;
|
||||
}
|
||||
|
||||
var deviceInfo = BotDeviceInfo.GenerateInfo();
|
||||
File.WriteAllText("DeviceInfo.json", JsonSerializer.Serialize(deviceInfo));
|
||||
return deviceInfo;
|
||||
}
|
||||
public static BotKeystore? LoadKeystore()
|
||||
{
|
||||
try
|
||||
{
|
||||
var text = File.ReadAllText("Keystore.json");
|
||||
return JsonSerializer.Deserialize<BotKeystore>(text, new JsonSerializerOptions()
|
||||
{
|
||||
ReferenceHandler = ReferenceHandler.Preserve
|
||||
});
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
// 登录方法
|
||||
public async Task Login()
|
||||
{
|
||||
#region bot实例化
|
||||
|
||||
var deviceInfo = GetDeviceInfo();
|
||||
var keyStore = LoadKeystore() ?? new BotKeystore();
|
||||
|
||||
Client = BotFactory.Create(new BotConfig
|
||||
{
|
||||
UseIPv6Network = false,
|
||||
GetOptimumServer = true,
|
||||
AutoReconnect = true,
|
||||
Protocol = Protocols.Linux
|
||||
}, deviceInfo, keyStore);
|
||||
|
||||
#endregion
|
||||
|
||||
// Log模式
|
||||
Client.Invoker.OnBotLogEvent += (_, @event) =>
|
||||
{
|
||||
@event.Level.ChangeColorByTitle();
|
||||
Console.WriteLine(@event.ToString());
|
||||
};
|
||||
|
||||
// bot信息保存,但存在bug未修复
|
||||
Client.Invoker.OnBotOnlineEvent += (_, @event) =>
|
||||
{
|
||||
Console.WriteLine(@event.ToString());
|
||||
SaveKeystore(Client.UpdateKeystore());
|
||||
};
|
||||
// 二维码生成,启动后在根目录下生成qr.png
|
||||
var qrCode = await Client.FetchQrCode();
|
||||
if (qrCode != null)
|
||||
{
|
||||
await File.WriteAllBytesAsync("qr.png", qrCode.Value.QrCode);
|
||||
await Client.LoginByQrCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user