-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBridgePackets.cs
164 lines (158 loc) · 6.2 KB
/
BridgePackets.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using Net.Codecrete.QrCodeGenerator;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
namespace GraphicalMirai
{
/// <summary>
/// 通信桥数据包管理
///
/// 注意: GraphicalMirai 启动器与插件的数据包命名方式不同。
/// 一方的 In 对应另一方的 Out,一方的 Out 对应另一方的 In
/// In 永远是自身收到的包,Out 永远是自身发出去的包
/// </summary>
public class BridgePackets
{
public static readonly ReadOnlyDictionary<string, Type> packetsIn = new(new Dictionary<string, Type>() {
{ "SolveSliderCaptcha", typeof(InSolveSliderCaptcha) },
{ "LoginVerify", typeof(InLoginVerify) },
{ "SmsVerify", typeof(InSmsVerify) },
});
}
#pragma warning disable CS8618
public interface IPacketIn
{
public void handle();
}
public class InSolveSliderCaptcha : IPacketIn
{
public string url;
public void handle()
{
App.mirai.onDataReceived($"[通信桥] 滑块验证码请求 {url}");
Process process = new Process();
process.StartInfo = new(App.path("tools/LoginSolver/LoginSolver.exe"))
{
WorkingDirectory = App.path("tools/LoginSolver"),
Arguments = "--url=" + Convert.ToBase64String(Encoding.UTF8.GetBytes(url)),
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
StandardErrorEncoding = Encoding.UTF8,
StandardOutputEncoding = Encoding.UTF8,
CreateNoWindow = true
};
void processLogReceive(string data)
{
Console.WriteLine("[LoginSolver] " + data);
if (data.StartsWith("ticket: "))
{
string ticket = data.Substring(8);
// 回调滑块验证码
App.mirai.onDataReceived($"[通信桥] 回应滑块验证码 {ticket}");
App.mirai.WriteLine(ticket);
}
};
process.OutputDataReceived += (s, e) => processLogReceive(e.Data ?? "");
process.ErrorDataReceived += (s, e) => processLogReceive(e.Data ?? "");
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
}
}
public class InLoginVerify : IPacketIn
{
public string url;
public void handle()
{
Console.WriteLine("登录验证 " + url);
MainWindow.Instance.Dispatcher.BeginInvoke(async () =>
{
Geometry qrcode = await Task.Run(() => Geometry.Parse(QrCode.EncodeText(url, QrCode.Ecc.Medium).ToGraphicsPath(1)));
qrcode.Freeze();
await MainWindow.Msg.ShowAsync(() =>
{
List<Inline> content = new();
content.Add(new Run()
{
Text = "该账户有设备锁/不常用登录地点/不常用设备登录的问题\n" +
"请在手机 QQ 扫描以下二维码,确认后请点击「确定」\n"
});
content.Add(new InlineUIContainer(new Path()
{
Fill = App.hexBrush("#FFFFFF"),
Data = qrcode,
LayoutTransform = new ScaleTransform(2, 2)
}));
return content.ToArray();
}, "需要进行账户安全认证");
App.mirai.WriteLine("LoginVerfied");
});
}
}
public class InSmsVerify : IPacketIn
{
public string countryCode;
public string phoneNumber;
public async void handle()
{
string phone = countryCode.Length > 0 && phoneNumber.Length > 0 ? $"(+{countryCode}) {phoneNumber}" : "(无法获取到手机号码)";
if(await MainWindow.Msg.ShowAsync(
$"一条短信验证码将发送到你的手机 {phone}.\n" +
$"运营商可能会收取正常短信费用, 是否继续?",
"短信验证", System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes)
{
App.mirai.WriteLine("yes");
TextBox? tb = null;
await MainWindow.Msg.ShowAsync(() =>
{
tb = new()
{
Width = 100,
Padding = new(5, 3, 5, 3),
Background = App.hexBrush("#FFFFFF"),
Foreground = App.hexBrush("#000000")
};
List<Inline> content = new();
content.Add(new Run("验证码已发送,请注意查收。\n" +
"收到验证码后填入下方并点击「确定」完成短信验证。\n\n"));
content.Add(new InlineUIContainer(tb));
return content.ToArray();
}, "短信验证", System.Windows.MessageBoxButton.OK);
App.mirai.WriteLine(tb?.Dispatcher.Invoke<string>(() => tb.Text) ?? "");
}
else
{
App.mirai.WriteLine("no");
}
}
}
public interface IPacketOut
{
public string type();
}
public class OutLoginVerify : IPacketOut
{
public string type() => "LoginVerify";
}
#pragma warning restore CS8618
public static class SocketWrapperExt
{
public static bool SendPacket<T>(this SocketWrapper socket, T packet) where T : IPacketOut
{
var json = JObject.FromObject(packet);
json.Add(new JProperty("type", packet.type()));
string data = json.ToString(Formatting.None);
return socket.SendRawMessage(data);
}
}
}