101 lines
3.5 KiB
C#
101 lines
3.5 KiB
C#
using System.Text.Json;
|
|
using Debugger;
|
|
using Grpc.Core;
|
|
using Lagrange.Core;
|
|
using Lagrange.Core.Common;
|
|
using Lagrange.Core.Common.Interface;
|
|
using Lagrange.Core.Common.Interface.Api;
|
|
using Lagrange.Core.Message;
|
|
using Shrink.Logger;
|
|
using Shrink.Utility;
|
|
using Console = System.Console;
|
|
|
|
namespace Shrink.Service;
|
|
|
|
public class BotService
|
|
{
|
|
private static readonly Lazy<BotService> _instance = new(() => new BotService());
|
|
public static BotService Instance => _instance.Value;
|
|
|
|
public BotContext? Client;
|
|
private bool _isOnline;
|
|
private static string KeystoreFilePath => "Keystore.json";
|
|
private static string DeviceInfoFilePath => "DeviceInfo.json";
|
|
|
|
|
|
private static BotDeviceInfo GetDeviceInfo() =>
|
|
JsonUtility.ReadOrCreateJsonFile(DeviceInfoFilePath, BotDeviceInfo.GenerateInfo);
|
|
|
|
// 登录方法
|
|
public async Task Login()
|
|
{
|
|
const int port = 50051;
|
|
var server = new Server
|
|
{
|
|
Services = { APIService.BindService(new ApiService()) },
|
|
Ports = { new ServerPort("192.168.0.114", port, ServerCredentials.Insecure) }
|
|
};
|
|
|
|
var deviceInfo = File.Exists(DeviceInfoFilePath)
|
|
? JsonUtility.ReadJsonFromFile<BotDeviceInfo>(DeviceInfoFilePath)
|
|
: GetDeviceInfo();
|
|
|
|
var keyStore = File.Exists(KeystoreFilePath)
|
|
? JsonUtility.ReadJsonFromFile<BotKeystore>(KeystoreFilePath)
|
|
: new BotKeystore();
|
|
|
|
Client = BotFactory.Create(new BotConfig
|
|
{
|
|
UseIPv6Network = false,
|
|
GetOptimumServer = true,
|
|
AutoReconnect = true,
|
|
Protocol = Protocols.Linux
|
|
}, deviceInfo, keyStore);
|
|
|
|
// Log模式
|
|
Client.Invoker.OnBotLogEvent += (_, @event) =>
|
|
{
|
|
@event.Level.ChangeColorByTitle();
|
|
Console.WriteLine(@event.ToString());
|
|
};
|
|
|
|
// bot信息保存
|
|
Client.Invoker.OnBotOnlineEvent += async (_, @event) =>
|
|
{
|
|
Console.WriteLine(@event.ToString());
|
|
_isOnline = true;
|
|
server.Start();
|
|
Console.WriteLine($"[{DateTime.Now}] [gRPC Server] [Info]: Listening on port: {port}");
|
|
await Task.Delay(1000);
|
|
LogManager.Instance.LogChain.LogMessage($"{DateTime.Now}", "LoginEvent", LogLevel.Info, MsgType.Group,
|
|
620902312, "Login");
|
|
};
|
|
Client.Invoker.OnBotOfflineEvent += async (_, @event) =>
|
|
{
|
|
Console.WriteLine(@event.ToString());
|
|
await server.ShutdownAsync();
|
|
_isOnline = false;
|
|
};
|
|
|
|
Client.Invoker.OnBotCaptchaEvent += (_, @event) =>
|
|
{
|
|
Console.WriteLine(@event.ToString());
|
|
var captcha = Console.ReadLine();
|
|
var randStr = Console.ReadLine();
|
|
if (captcha != null && randStr != null) Client.SubmitCaptcha(captcha, randStr);
|
|
};
|
|
await Client.LoginByPassword();
|
|
if (!_isOnline)
|
|
{
|
|
Console.WriteLine("账密登录失败,请尝试二维码登录。");
|
|
var qrCode = await Client.FetchQrCode();
|
|
if (qrCode != null)
|
|
{
|
|
await File.WriteAllBytesAsync("qr.png", qrCode.Value.QrCode);
|
|
await Client.LoginByQrCode();
|
|
}
|
|
}
|
|
await File.WriteAllTextAsync(KeystoreFilePath, JsonSerializer.Serialize(Client.UpdateKeystore()));
|
|
await File.WriteAllTextAsync(DeviceInfoFilePath, JsonSerializer.Serialize(Client.UpdateDeviceInfo()));
|
|
}
|
|
} |