Skip to content

Commit 8f7692b

Browse files
committed
[Core] Improve group nudge event
1 parent f180d39 commit 8f7692b

5 files changed

Lines changed: 161 additions & 28 deletions

File tree

Lagrange.Core.Runner/Program.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
using System.Diagnostics;
2-
using System.Text;
1+
using System.Text;
32
using System.Text.Json;
43
using Lagrange.Core.Common;
5-
using Lagrange.Core.Common.Entity;
64
using Lagrange.Core.Common.Interface;
75
using Lagrange.Core.Events.EventArgs;
8-
using Lagrange.Core.Internal.Packets.Message;
9-
using Lagrange.Core.Message;
10-
using Lagrange.Core.Utility;
116

127
namespace Lagrange.Core.Runner;
138

149
internal static class Program
1510
{
1611
private static async Task Main()
1712
{
18-
var sign = new InteropSignProvider();
13+
var sign = new UrlSignProvider();
1914

2015
Console.OutputEncoding = Encoding.UTF8;
2116
Console.InputEncoding = Encoding.UTF8;
@@ -26,7 +21,7 @@ private static async Task Main()
2621
{
2722
context = BotFactory.Create(new BotConfig
2823
{
29-
Protocol = Protocols.Windows,
24+
Protocol = Protocols.Linux,
3025
SignProvider = sign,
3126
LogLevel = LogLevel.Trace
3227
}, JsonSerializer.Deserialize<BotKeystore>(await File.ReadAllTextAsync("keystore.json")) ?? throw new InvalidOperationException());
@@ -35,7 +30,7 @@ private static async Task Main()
3530
{
3631
context = BotFactory.Create(new BotConfig
3732
{
38-
Protocol = Protocols.Windows,
33+
Protocol = Protocols.Linux,
3934
SignProvider = sign,
4035
LogLevel = LogLevel.Trace
4136
});
@@ -64,8 +59,7 @@ private static async Task Main()
6459

6560
await context.Login();
6661

67-
var builder = new MessageBuilder().Text("Awoo");
68-
var message = await context.SendFriendMessage(1925648680, builder.Build());
62+
6963

7064
await Task.Delay(-1);
7165
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using System.Net.Http.Json;
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
4+
using Lagrange.Core.Common;
5+
6+
namespace Lagrange.Core.Runner;
7+
8+
public class UrlSignProvider : BotSignProvider
9+
{
10+
private static readonly HashSet<string> PcWhiteListCommand =
11+
[
12+
"trpc.o3.ecdh_access.EcdhAccess.SsoEstablishShareKey",
13+
"trpc.o3.ecdh_access.EcdhAccess.SsoSecureAccess",
14+
"trpc.o3.report.Report.SsoReport",
15+
"MessageSvc.PbSendMsg",
16+
"wtlogin.trans_emp",
17+
"wtlogin.login",
18+
"wtlogin.exchange_emp",
19+
"trpc.login.ecdh.EcdhService.SsoKeyExchange",
20+
"trpc.login.ecdh.EcdhService.SsoNTLoginPasswordLogin",
21+
"trpc.login.ecdh.EcdhService.SsoNTLoginEasyLogin",
22+
"trpc.login.ecdh.EcdhService.SsoNTLoginPasswordLoginNewDevice",
23+
"trpc.login.ecdh.EcdhService.SsoNTLoginEasyLoginUnusualDevice",
24+
"trpc.login.ecdh.EcdhService.SsoNTLoginPasswordLoginUnusualDevice",
25+
"trpc.login.ecdh.EcdhService.SsoNTLoginRefreshTicket",
26+
"trpc.login.ecdh.EcdhService.SsoNTLoginRefreshA2",
27+
"OidbSvcTrpcTcp.0x11ec_1",
28+
"OidbSvcTrpcTcp.0x758_1", // create group
29+
"OidbSvcTrpcTcp.0x7c1_1",
30+
"OidbSvcTrpcTcp.0x7c2_5", // request friend
31+
"OidbSvcTrpcTcp.0x10db_1",
32+
"OidbSvcTrpcTcp.0x8a1_7", // request group
33+
"OidbSvcTrpcTcp.0x89a_0",
34+
"OidbSvcTrpcTcp.0x89a_15",
35+
"OidbSvcTrpcTcp.0x88d_0", // fetch group detail
36+
"OidbSvcTrpcTcp.0x88d_14",
37+
"OidbSvcTrpcTcp.0x112a_1",
38+
"OidbSvcTrpcTcp.0x587_74",
39+
"OidbSvcTrpcTcp.0x1100_1",
40+
"OidbSvcTrpcTcp.0x1102_1",
41+
"OidbSvcTrpcTcp.0x1103_1",
42+
"OidbSvcTrpcTcp.0x1107_1",
43+
"OidbSvcTrpcTcp.0x1105_1",
44+
"OidbSvcTrpcTcp.0xf88_1",
45+
"OidbSvcTrpcTcp.0xf89_1",
46+
"OidbSvcTrpcTcp.0xf57_1",
47+
"OidbSvcTrpcTcp.0xf57_106",
48+
"OidbSvcTrpcTcp.0xf57_9",
49+
"OidbSvcTrpcTcp.0xf55_1",
50+
"OidbSvcTrpcTcp.0xf67_1",
51+
"OidbSvcTrpcTcp.0xf67_5",
52+
"OidbSvcTrpcTcp.0x6d9_4"
53+
];
54+
55+
private readonly HttpClient _client = new();
56+
57+
private readonly string _base = "https://sign.lagrangecore.org/api/sign";
58+
private readonly string _version = "30366";
59+
60+
public override bool IsWhiteListCommand(string cmd) => PcWhiteListCommand.Contains(cmd);
61+
62+
public override async Task<SsoSecureInfo?> GetSecSign(long uin, string cmd, int seq, ReadOnlyMemory<byte> body)
63+
{
64+
var response = await GetSign<PcSecSignRequest, PcSecSignResponse>(
65+
$"{_base}/{_version}",
66+
new PcSecSignRequest(cmd, seq, Convert.ToHexString(body.Span))
67+
);
68+
69+
return new SsoSecureInfo
70+
{
71+
SecSign = Convert.FromHexString(response.Value.Sign),
72+
SecToken = Convert.FromHexString(response.Value.Token),
73+
SecExtra = Convert.FromHexString(response.Value.Extra)
74+
};
75+
}
76+
77+
private async Task<TResponse> GetSign<TRequest, TResponse>(string url, TRequest requestJson) where TRequest : class where TResponse : class
78+
{
79+
using var request = new HttpRequestMessage();
80+
request.Method = HttpMethod.Post;
81+
request.RequestUri = new Uri(url);
82+
request.Content = JsonContent.Create(requestJson);
83+
using var response = await _client.SendAsync(request);
84+
if (!response.IsSuccessStatusCode) throw new Exception($"Unexpected http status code({response.StatusCode})");
85+
86+
var result = JsonSerializer.Deserialize<TResponse>(await response.Content.ReadAsStreamAsync());
87+
if (result == null) throw new NullReferenceException("Result is null");
88+
89+
return result;
90+
}
91+
}
92+
93+
public class PcSecSignRequest(string cmd, int seq, string src)
94+
{
95+
[JsonPropertyName("cmd")]
96+
public string Cmd { get; } = cmd;
97+
98+
[JsonPropertyName("seq")]
99+
public int Seq { get; } = seq;
100+
101+
[JsonPropertyName("src")]
102+
public string Src { get; } = src;
103+
}
104+
105+
public class PcSecSignResponse(PcSecSignResponseValue value)
106+
{
107+
[JsonRequired]
108+
[JsonPropertyName("value")]
109+
public PcSecSignResponseValue Value { get; init; } = value;
110+
}
111+
112+
public class PcSecSignResponseValue(string sign, string token, string extra)
113+
{
114+
[JsonRequired]
115+
[JsonPropertyName("sign")]
116+
public string Sign { get; init; } = sign;
117+
118+
[JsonRequired]
119+
[JsonPropertyName("token")]
120+
public string Token { get; init; } = token;
121+
122+
[JsonRequired]
123+
[JsonPropertyName("extra")]
124+
public string Extra { get; init; } = extra;
125+
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
namespace Lagrange.Core.Events.EventArgs;
22

3-
public class BotGroupNudgeEvent(long groupUin, long operatorUin, long targetUin) : EventBase
3+
public class BotGroupNudgeEvent(long groupUin, long operatorUin, string action, long targetUin, string suffix) : EventBase
44
{
55
public long GroupUin { get; } = groupUin;
66

77
public long OperatorUin { get; } = operatorUin;
88

9+
public string Action { get; } = action;
10+
911
public long TargetUin { get; } = targetUin;
1012

13+
public string Suffix { get; } = suffix;
14+
1115
public override string ToEventMessage()
1216
{
13-
return $"{nameof(BotGroupNudgeEvent)}: GroupUin={GroupUin}, OperatorUin={OperatorUin}, TargetUin={TargetUin}";
17+
return $"{nameof(BotGroupNudgeEvent)}: In Group {GroupUin}, {OperatorUin} {Action} {TargetUin} {Suffix}";
1418
}
1519
}

Lagrange.Core/Internal/Logic/PushLogic.cs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -191,20 +191,25 @@ public async ValueTask Incoming(ProtocolEvent e)
191191
_ = packet.Read<byte>(); // unknown byte
192192
var proto = packet.ReadBytes(Prefix.Int16 | Prefix.LengthOnly);
193193
var greyTip = ProtoHelper.Deserialize<NotifyMessageBody>(proto);
194-
var templates = greyTip.GeneralGrayTip.MsgTemplParam.ToDictionary(x => x.Name, x => x.Value);
195194

196-
if (!templates.TryGetValue("action_str", out var actionStr) && !templates.TryGetValue("alt_str1", out actionStr))
195+
switch ((Event0x2DCSubType20SubType)greyTip.SubType)
197196
{
198-
actionStr = string.Empty;
199-
}
197+
case Event0x2DCSubType20SubType.GroupNudgeNotice:
198+
{
199+
var @params = greyTip.GeneralGrayTip.MsgTemplParam.ToDictionary(x => x.Name, x => x.Value);
200200

201-
if (greyTip.GeneralGrayTip.BusiType == 12) // poke
202-
{
203-
context.EventInvoker.PostEvent(new BotGroupNudgeEvent(
204-
groupUin,
205-
uint.Parse(templates["uin_str1"]),
206-
uint.Parse(templates["uin_str2"]))
207-
);
201+
if (greyTip.GeneralGrayTip.BusiType == 12) // poke
202+
{
203+
context.EventInvoker.PostEvent(new BotGroupNudgeEvent(
204+
groupUin,
205+
long.Parse(@params["uin_str1"]),
206+
@params["action_str"],
207+
long.Parse(@params["uin_str2"]),
208+
@params["suffix_str"]
209+
));
210+
}
211+
break;
212+
}
208213
}
209214
break;
210215
}
@@ -296,6 +301,11 @@ private enum Event0x2DCSubType16SubType
296301
GroupReactionNotice = 35,
297302
}
298303

304+
private enum Event0x2DCSubType20SubType
305+
{
306+
GroupNudgeNotice = 19,
307+
}
308+
299309
private enum Event0x210SubType
300310
{
301311
FriendRequestNotice = 35,

Lagrange.Milky/Entity/Event/GroupNudgeEvent.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ namespace Lagrange.Milky.Entity.Event;
44

55
public class GroupNudgeEvent(long time, long selfId, GroupNudgeEventData data) : EventBase<GroupNudgeEventData>(time, selfId, "group_nudge", data) { }
66

7-
public class GroupNudgeEventData(Int64 groupID, Int64 sender_id, Int64 receiver_id)
7+
public class GroupNudgeEventData(long groupID, long sender_id, long receiver_id)
88
{
99
[JsonPropertyName("group_id")]
10-
public Int64 GroupID { get; } = groupID;
10+
public long GroupID { get; } = groupID;
1111

1212
[JsonPropertyName("sender_id")]
13-
public Int64 SenderID { get; } = sender_id;
13+
public long SenderID { get; } = sender_id;
1414

1515
[JsonPropertyName("receiver_id")]
16-
public Int64 ReceiverID { get; } = receiver_id;
16+
public long ReceiverID { get; } = receiver_id;
1717
}

0 commit comments

Comments
 (0)