Skip to content

Commit

Permalink
[Core] Implemented GroupFSUpload
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan04 committed Mar 2, 2024
1 parent dca81f6 commit abe76bc
Show file tree
Hide file tree
Showing 13 changed files with 385 additions and 28 deletions.
4 changes: 4 additions & 0 deletions Lagrange.Core/Common/Interface/Api/GroupExt.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Lagrange.Core.Common.Entity;
using Lagrange.Core.Event.EventArg;
using Lagrange.Core.Message.Entity;

namespace Lagrange.Core.Common.Interface.Api;

Expand Down Expand Up @@ -77,6 +78,9 @@ public static Task<string> FetchGroupFSDownload(this BotContext bot, uint groupU

public static Task<bool> GroupFSMove(this BotContext bot, uint groupUin, string fileId, string parentDirectory, string targetDirectory)
=> bot.ContextCollection.Business.OperationLogic.GroupFSMove(groupUin, fileId, parentDirectory, targetDirectory);

public static Task<bool> GroupFSUpload(this BotContext bot, uint groupUin, FileEntity fileEntity)
=> bot.ContextCollection.Business.OperationLogic.GroupFSUpload(groupUin, fileEntity);

#endregion
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Lagrange.Core.Common.Entity;
using Lagrange.Core.Internal.Context.Attributes;
using Lagrange.Core.Internal.Context.Uploader;
using Lagrange.Core.Internal.Event.Action;
using Lagrange.Core.Internal.Event.Message;
using Lagrange.Core.Internal.Event.System;
using Lagrange.Core.Message;
using Lagrange.Core.Message.Entity;

namespace Lagrange.Core.Internal.Context.Logic.Implementation;

Expand Down Expand Up @@ -144,6 +146,18 @@ public async Task<bool> GroupFSMove(uint groupUin, string fileId, string parentD
return events.Count != 0 && ((GroupFSMoveEvent)events[0]).ResultCode == 0;
}

public Task<bool> GroupFSUpload(uint groupUin, FileEntity fileEntity)
{
try
{
return FileUploader.UploadGroup(Collection, MessageBuilder.Group(groupUin).Build(), fileEntity);
}
catch
{
return Task.FromResult(false);
}
}

public async Task<bool> RecallGroupMessage(uint groupUin, MessageResult result)
{
if (result.Sequence == null) return false;
Expand Down
83 changes: 75 additions & 8 deletions Lagrange.Core/Internal/Context/Uploader/FileUploader.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,90 @@
using Lagrange.Core.Internal.Event.Message;
using Lagrange.Core.Internal.Event.System;
using Lagrange.Core.Internal.Packets.Service.Highway;
using Lagrange.Core.Message;
using Lagrange.Core.Message.Entity;
using Lagrange.Core.Utility.Extension;

namespace Lagrange.Core.Internal.Context.Uploader;

[HighwayUploader(typeof(FileEntity))]
internal class FileUploader : IHighwayUploader
/// <summary>
/// This FileUploader should be called manually
/// </summary>
internal static class FileUploader
{
public Task UploadPrivate(ContextCollection context, MessageChain chain, IMessageEntity entity)
public static Task<bool> UploadPrivate(ContextCollection context, MessageChain chain, IMessageEntity entity)
{
throw new NotImplementedException();
}

public async Task UploadGroup(ContextCollection context, MessageChain chain, IMessageEntity entity)
public static async Task<bool> UploadGroup(ContextCollection context, MessageChain chain, IMessageEntity entity)
{
if (entity is FileEntity { FileStream: not null } file)
if (entity is not FileEntity { FileStream: not null } file) return false;

var uploadEvent = GroupFSUploadEvent.Create(chain.GroupUin ?? 0, file);
var result = await context.Business.SendEvent(uploadEvent);
var uploadResp = (GroupFSUploadEvent)result[0];

var hwUrlEvent = HighwayUrlEvent.Create();
var highwayUrlResult = await context.Business.SendEvent(hwUrlEvent);
var ticketResult = (HighwayUrlEvent)highwayUrlResult[0];

var ext = new FileUploadExt
{
var uploadEvent = GroupFSUploadEvent.Create(chain.GroupUin ?? 0, file);
var result = await context.Business.SendEvent(uploadEvent);
}
Unknown1 = 100,
Unknown2 = 1,
Entry = new FileUploadEntry
{
BusiBuff = new ExcitingBusiInfo
{
SenderUin = context.Keystore.Uin,
ReceiverUin = chain.GroupUin ?? 0,
GroupCode = chain.GroupUin ?? 0
},
FileEntry = new ExcitingFileEntry
{
FileSize = file.FileStream.Length,
Md5 = file.FileMd5,
CheckKey = uploadResp.CheckKey,
Md5S2 = file.FileMd5,
FileId = uploadResp.FileId,
UploadKey = uploadResp.UploadKey
},
ClientInfo = new ExcitingClientInfo
{
ClientType = 3,
AppId = "100",
TerminalType = 3,
ClientVer = "1.1.1",
Unknown = 4
},
FileNameInfo = new ExcitingFileNameInfo
{
FileName = file.FileName
},
Host = new ExcitingHostConfig
{
Hosts = new List<ExcitingHostInfo>
{
new()
{
Url = new ExcitingUrlInfo
{
Unknown = 1,
Host = uploadResp.Ip
},
Port = uploadResp.Port
}
}
}
}
};

bool hwSuccess = await context.Highway.UploadSrcByStreamAsync(71, file.FileStream, ticketResult.SigSession, file.FileMd5, ext.Serialize().ToArray());
if (!hwSuccess) return false;

var sendEvent = GroupSendFileEvent.Create(chain.GroupUin ?? 0, uploadResp.FileId);
var sendResult = await context.Business.SendEvent(sendEvent);
return sendResult.Count != 0 && ((GroupSendFileEvent)sendResult[0]).ResultCode == 0;
}
}
21 changes: 20 additions & 1 deletion Lagrange.Core/Internal/Event/Message/GroupFSUploadEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,36 @@ namespace Lagrange.Core.Internal.Event.Message;
internal class GroupFSUploadEvent : ProtocolEvent
{
public uint GroupUin { get; }

public FileEntity Entity { get; }

public string FileId { get; }

public byte[] UploadKey { get; }

public byte[] CheckKey { get; }

public string Ip { get; }

public uint Port { get; }

private GroupFSUploadEvent(uint groupUin, FileEntity entity) : base(true)
{
GroupUin = groupUin;
Entity = entity;
}

private GroupFSUploadEvent(int resultCode) : base(resultCode)
private GroupFSUploadEvent(int resultCode, string fileId, byte[] uploadKey, byte[] checkKey, string ip, uint port) : base(resultCode)
{
FileId = fileId;
UploadKey = uploadKey;
CheckKey = checkKey;
Ip = ip;
Port = port;
}

public static GroupFSUploadEvent Create(uint groupUin, FileEntity entity) => new(groupUin, entity);

public static GroupFSUploadEvent Result(int resultCode, string fileId, byte[] uploadKey, byte[] checkKey, string ip, uint port)
=> new(resultCode, fileId, uploadKey, checkKey, ip, port);
}
20 changes: 20 additions & 0 deletions Lagrange.Core/Internal/Event/Message/GroupSendFileEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Lagrange.Core.Internal.Event.Message;

internal class GroupSendFileEvent : ProtocolEvent
{
public uint GroupUin { get; }

public string FileKey { get; } = string.Empty;

private GroupSendFileEvent(uint groupUin, string fileKey) : base(true)
{
GroupUin = groupUin;
FileKey = fileKey;
}

private GroupSendFileEvent(int resultCode) : base(resultCode) { }

public static GroupSendFileEvent Create(uint groupUin, string fileKey) => new(groupUin, fileKey);

public static GroupSendFileEvent Result(int resultCode) => new(resultCode);
}
103 changes: 103 additions & 0 deletions Lagrange.Core/Internal/Packets/Service/Highway/FileUploadExt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using ProtoBuf;

namespace Lagrange.Core.Internal.Packets.Service.Highway;

#pragma warning disable CS8618

[ProtoContract]
internal class FileUploadExt
{
[ProtoMember(1)] public int Unknown1 { get; set; }

[ProtoMember(2)] public int Unknown2 { get; set; }

[ProtoMember(3)] public int Unknown3 { get; set; }

[ProtoMember(100)] public FileUploadEntry Entry { get; set; }

[ProtoMember(200)] public int Unknown200 { get; set; }
}

[ProtoContract]
internal class FileUploadEntry
{
[ProtoMember(100)] public ExcitingBusiInfo BusiBuff { get; set; }

[ProtoMember(200)] public ExcitingFileEntry FileEntry { get; set; }

[ProtoMember(300)] public ExcitingClientInfo ClientInfo { get; set; }

[ProtoMember(400)] public ExcitingFileNameInfo FileNameInfo { get; set; }

[ProtoMember(500)] public ExcitingHostConfig Host { get; set; }
}

[ProtoContract]
internal class ExcitingBusiInfo
{
[ProtoMember(1)] public int BusId { get; set; }

[ProtoMember(100)] public long SenderUin { get; set; }

[ProtoMember(200)] public long ReceiverUin { get; set; }

[ProtoMember(400)] public long GroupCode { get; set; }
}

[ProtoContract]
internal class ExcitingFileEntry
{
[ProtoMember(100)] public long FileSize { get; set; }

[ProtoMember(200)] public byte[] Md5 { get; set; }

[ProtoMember(300)] public byte[] CheckKey { get; set; }

[ProtoMember(400)] public byte[] Md5S2 { get; set; }

[ProtoMember(600)] public string FileId { get; set; }

[ProtoMember(700)] public byte[] UploadKey { get; set; }
}

[ProtoContract]
internal class ExcitingClientInfo
{
[ProtoMember(100)] public int ClientType { get; set; }

[ProtoMember(200)] public string AppId { get; set; }

[ProtoMember(300)] public int TerminalType { get; set; }

[ProtoMember(400)] public string ClientVer { get; set; }

[ProtoMember(600)] public int Unknown { get; set; }
}

[ProtoContract]
internal class ExcitingFileNameInfo
{
[ProtoMember(100)] public string FileName { get; set; }
}

[ProtoContract]
internal class ExcitingHostConfig
{
[ProtoMember(200)] public List<ExcitingHostInfo> Hosts { get; set; }
}

[ProtoContract]
internal class ExcitingHostInfo
{
[ProtoMember(1)] public ExcitingUrlInfo Url { get; set; }

[ProtoMember(2)] public uint Port { get; set; }
}

[ProtoContract]
internal class ExcitingUrlInfo
{
[ProtoMember(1)] public int Unknown { get; set; }

[ProtoMember(2)] public string Host { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using ProtoBuf;

namespace Lagrange.Core.Internal.Packets.Service.Oidb.Request;

#pragma warning disable CS8618
// ReSharper disable InconsistentNaming

/// <summary>
/// Group Send File
/// </summary>
[ProtoContract]
[OidbSvcTrpcTcp(0x6D9, 4)]
internal class OidbSvcTrpcTcp0x6D9_4
{
[ProtoMember(5)] public OidbSvcTrpcTcp0x6D9_4Body Body { get; set; }
}

[ProtoContract]
internal class OidbSvcTrpcTcp0x6D9_4Body
{
[ProtoMember(1)] public uint GroupUin { get; set; }

[ProtoMember(2)] public uint Type { get; set; } // 2

[ProtoMember(3)] public OidbSvcTrpcTcp0x6D9_4Info Info { get; set; }
}

[ProtoContract]
internal class OidbSvcTrpcTcp0x6D9_4Info
{
[ProtoMember(1)] public uint BusiType { get; set; } // 102

[ProtoMember(2)] public string FileId { get; set; }

[ProtoMember(3)] public uint Field3 { get; set; } // random

[ProtoMember(4)] public string? Field4 { get; set; } // null

[ProtoMember(5)] public bool Field5 { get; set; } // true
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ namespace Lagrange.Core.Internal.Packets.Service.Oidb.Response;
[ProtoContract]
internal class OidbSvcTrpcTcp0x6D6Response
{
[ProtoMember(1)] public OidbSvcTrpcTcp0x6D6_0Response Upload { get; set; }

[ProtoMember(3)] public OidbSvcTrpcTcp0x6D6_2Response Download { get; set; }
}
Loading

0 comments on commit abe76bc

Please sign in to comment.