Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Lagrange.Core.NativeAPI/LoginEntryPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Runtime.InteropServices;
using System.Text;
using Lagrange.Core.Common.Interface;
using Lagrange.Core.NativeAPI.NativeModel.Common;

namespace Lagrange.Core.NativeAPI
{
public static class LoginEntryPoint
{
[UnmanagedCallersOnly(EntryPoint = "SubmitCaptcha")]
public static bool SubmitCaptcha(int index, ByteArrayNative ticket, ByteArrayNative randStr)
{
if (Program.Contexts.Count <= index)
{
return false;
}

var ticketData = ticket.ToByteArrayWithoutFree();
var randStrData = randStr.ToByteArrayWithoutFree();

BotContext context = Program.Contexts[index].BotContext;
return context.SubmitCaptcha(Encoding.UTF8.GetString(ticketData), Encoding.UTF8.GetString(randStrData));
}

[UnmanagedCallersOnly(EntryPoint = "SubmitSMSCode")]
public static bool SubmitSMSCode(int index, ByteArrayNative code)
{
if (Program.Contexts.Count <= index)
{
return false;
}

var codeData = code.ToByteArrayWithoutFree();

BotContext context = Program.Contexts[index].BotContext;
return context.SubmitSMSCode(Encoding.UTF8.GetString(codeData));
}
};
};
11 changes: 6 additions & 5 deletions Lagrange.Core.NativeAPI/NativeModel/Common/BotSignProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ internal class AndroidSignProvider(string? signUrl) : AndroidBotSignProvider, ID
["seq"] = seq,
["buffer"] = Convert.ToHexString(body.Span),
["guid"] = Convert.ToHexString(Context.Keystore.Guid),
["version"] = Context.AppInfo.PtVersion
["version"] = Context.AppInfo.PtVersion,
["qua"] = Context.AppInfo.Qua
};

var response = await _client.PostAsync($"{_url}/sign", new StringContent(payload.ToJsonString(), Encoding.UTF8, "application/json"));
Expand Down Expand Up @@ -305,7 +306,8 @@ public override async Task<byte[]> GetEnergy(long uin, string data)
["data"] = data,
["guid"] = Convert.ToHexString(Context.Keystore.Guid),
["ver"] = Context.AppInfo.SdkInfo.SdkVersion,
["version"] = Context.AppInfo.PtVersion
["version"] = Context.AppInfo.PtVersion,
["qua"] = Context.AppInfo.Qua
};

var response = await _client.PostAsync($"{_url}/energy", new StringContent(payload.ToJsonString(), Encoding.UTF8, "application/json"));
Expand All @@ -330,7 +332,8 @@ public override async Task<byte[]> GetDebugXwid(long uin, string data)
["uin"] = uin,
["data"] = data,
["guid"] = Convert.ToHexString(Context.Keystore.Guid),
["version"] = Context.AppInfo.PtVersion
["version"] = Context.AppInfo.PtVersion,
["qua"] = Context.AppInfo.Qua
};

var response = await _client.PostAsync($"{_url}/get_tlv553", new StringContent(payload.ToJsonString(), Encoding.UTF8, "application/json"));
Expand Down Expand Up @@ -371,12 +374,10 @@ internal class SignResponse
internal static partial class JsonHelper
{
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Default, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]

[JsonSerializable(typeof(AndroidSignProvider.ResponseRoot<AndroidSignProvider.SignResponse>))]
[JsonSerializable(typeof(AndroidSignProvider.ResponseRoot<string>))]
[JsonSerializable(typeof(LinuxSignProvider.Root))]
[JsonSerializable(typeof(LinuxSignProvider.Response))]

[JsonSerializable(typeof(JsonObject))]
[JsonSerializable(typeof(LightApp))]
private partial class CoreSerializerContext : JsonSerializerContext;
Expand Down
21 changes: 18 additions & 3 deletions Lagrange.Core.NativeAPI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using System.Text;
using Lagrange.Core.Common;
using Lagrange.Core.Common.Interface;
using Lagrange.Core.NativeAPI.NativeModel.Common;
Expand Down Expand Up @@ -37,8 +38,9 @@ public static int Initialize(IntPtr botConfigPtr, IntPtr keystorePtr, IntPtr app
return index;
}

//uin, password皆可选,传入 0 或结构体内Data/Length置0
[UnmanagedCallersOnly(EntryPoint = "Start")]
public static StatusCode Start(int index)
public static StatusCode Start(int index, long uin, ByteArrayNative password)
{
if (Contexts.Count <= index)
{
Expand All @@ -50,9 +52,22 @@ public static StatusCode Start(int index)
return StatusCode.AlreadyStarted;
}

if (uin == 0 || password.IsEmpty())
{
Task.Run(async () =>
{
await Contexts[index].BotContext.Login();
await Task.Delay(Timeout.Infinite);
});

return StatusCode.Success;
}

var passwordData = Encoding.UTF8.GetString(password.ToByteArrayWithoutFree());

Task.Run(async () =>
{
await Contexts[index].BotContext.Login();
await Contexts[index].BotContext.Login(uin, passwordData);
await Task.Delay(Timeout.Infinite);
});

Expand Down Expand Up @@ -80,4 +95,4 @@ public static void FreeMemory(IntPtr ptr)
Marshal.FreeHGlobal(ptr);
}
}
}
}
6 changes: 3 additions & 3 deletions Lagrange.Core.NativeAPI/ReverseEvent/EventEntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public static IntPtr GetBotGroupReactionEvent(int index)

return eventPtr;
}

[UnmanagedCallersOnly(EntryPoint = "GetBotGroupRecallEvent")]
public static IntPtr GetBotGroupRecallEvent(int index)
{
Expand All @@ -183,7 +183,7 @@ public static IntPtr GetBotGroupRecallEvent(int index)

var botGroupRecallEvent = Program.Contexts[index].EventInvoker.BotGroupRecallEvent;

IntPtr eventPtr = GetEventStructPtr<BotGroupReactionEventStruct>(botGroupRecallEvent);
IntPtr eventPtr = GetEventStructPtr<BotGroupRecallEventStruct>(botGroupRecallEvent);

return eventPtr;
}
Expand Down Expand Up @@ -369,4 +369,4 @@ private static IntPtr GetEventStructPtr<T>(ReverseEventBase reverseEvent) where
return resultPtr;
}
}
}
}
12 changes: 6 additions & 6 deletions Lagrange.Core/Internal/Services/Login/LoginService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ internal class LoginService : BaseService<LoginEventReq, LoginEventResp>
protected override ValueTask<ReadOnlyMemory<byte>> Build(LoginEventReq input, BotContext context)
{
if (!_packet.IsValueCreated) _packet = new Lazy<WtLogin>(() => new WtLogin(context));

return input.Cmd switch
{
LoginEventReq.Command.Tgtgt => new ValueTask<ReadOnlyMemory<byte>>(_packet.Value.BuildOicq09()),
LoginEventReq.Command.Tgtgt => new ValueTask<ReadOnlyMemory<byte>>(_packet.Value.BuildOicq09()),
_ => throw new ArgumentOutOfRangeException(nameof(input), $"Unknown command: {input.Cmd}")
};
}
Expand All @@ -48,7 +48,7 @@ protected override async ValueTask<ReadOnlyMemory<byte>> Build(LoginEventReq inp

return input.Cmd switch
{
LoginEventReq.Command.Tgtgt => await _packet.Value.BuildOicq09Android(input.Password),
LoginEventReq.Command.Tgtgt => await _packet.Value.BuildOicq09Android(input.Password),
LoginEventReq.Command.Captcha => await _packet.Value.BuildOicq02Android(input.Ticket),
LoginEventReq.Command.FetchSMSCode => await _packet.Value.BuildOicq08Android(),
LoginEventReq.Command.SubmitSMSCode => await _packet.Value.BuildOicq07Android(input.Code),
Expand All @@ -72,7 +72,7 @@ public static LoginEventResp Parse(WtLogin packet, ReadOnlyMemory<byte> input, B
var payload = packet.Parse(input.Span, out ushort command);
var reader = new BinaryPacket(payload);
Debug.Assert(command == 0x810);

ushort internalCmd = reader.Read<ushort>();
byte state = reader.Read<byte>();
var tlvs = ProtocolHelper.TlvUnPack(ref reader);
Expand All @@ -83,7 +83,7 @@ public static LoginEventResp Parse(WtLogin packet, ReadOnlyMemory<byte> input, B
uint _ = errorReader.Read<uint>(); // error code
string errorTitle = errorReader.ReadString(Prefix.Int16 | Prefix.LengthOnly);
string errorMessage = errorReader.ReadString(Prefix.Int16 | Prefix.LengthOnly);

return new LoginEventResp(state, (errorTitle, errorMessage));
}

Expand All @@ -93,7 +93,7 @@ public static LoginEventResp Parse(WtLogin packet, ReadOnlyMemory<byte> input, B
var tlv119 = TeaProvider.CreateDecryptSpan(tgtgt);
var tlv119Reader = new BinaryPacket(tlv119);
var tlvCollection = ProtocolHelper.TlvUnPack(ref tlv119Reader);

return new LoginEventResp(state, tlvCollection);
}

Expand Down
10 changes: 5 additions & 5 deletions Lagrange.Core/Utility/ProtocolHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ public static Dictionary<ushort, byte[]> TlvUnPack(ref BinaryPacket reader)
{
ushort tag = reader.Read<ushort>();
ushort length = reader.Read<ushort>();

var data = new byte[length];
reader.ReadBytes(data.AsSpan());
tlv[tag] = data;
}

return tlv;
}

public static string UInt32ToIPV4Addr(uint i)
{
Span<byte> ip = stackalloc byte[4];

ip[0] = (byte)(i & 0xFF);
ip[1] = (byte)((i >> 8) & 0xFF);
ip[2] = (byte)((i >> 16) & 0xFF);
ip[3] = (byte)((i >> 24) & 0xFF);

return $"{ip[0]}.{ip[1]}.{ip[2]}.{ip[3]}";
}
}
Loading