-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOsuBotTransferClient.cs
317 lines (271 loc) · 9.57 KB
/
OsuBotTransferClient.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using Sync.Client;
using Sync.MessageFilter;
using Sync.Source;
using Sync.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using PublicOsuBotTransfer.Attribute;
using Sync.Tools.ConfigurationAttribute;
using WebSocketSharp;
using Sync.Plugins;
using System.IO;
using System.Runtime.InteropServices;
namespace PublicOsuBotTransfer
{
struct WsCommand
{
public UInt16 Command;
}
enum MessageType
{
Command,
SyncMessage,
}
class Message
{
public MessageType type;
public IMessageBase syncMessage;
public WsCommand wsCommand;
}
public class OsuBotTransferClient : DefaultClient, IConfigurable
{
Queue<Message> messageBuffer = new Queue<Message>();
private Task messgaeBufferTimer;
private bool messgaeBufferTimerQuit = false;
private const string CONST_SYNC_NOTICE_HEADER = "\x01\x03\x01";
private const UInt16 REQ_TOKEN = 1;
private const UInt16 RPL_TOKEN = 2;
private string _token = "";
private bool _token_requsted = false;
public string Token
{
private set
{
_token = value;
}
get
{
if (!_token_requsted)
{
RequestToken();
_token_requsted = true;
}
return _token;
}
}
[Bool]
public static ConfigurationElement AutoReconnect { get; set; } = "True";
[Integer(MinValue = 3,MaxValue = 180)]
public static ConfigurationElement AutoReconnectInterval { get; set; } = "3";
[ServerUrl]
public static ConfigurationElement ServerPath { get; set; } = @"wss://osubot.kedamaovo.moe";
[Username]
public static ConfigurationElement Target_User_Name { get; set; } = "";
private WebSocket web_socket;
public OsuBotTransferClient() : base("MikiraSora", "OsuBotTransferClient")
{
messgaeBufferTimer = Task.Run(()=>
{
while (!messgaeBufferTimerQuit)
{
if(messageBuffer.Count > 0 && web_socket.IsAlive)
{
while(messageBuffer.Count > 0)
{
var msg = messageBuffer.Dequeue();
if (msg.type == MessageType.SyncMessage)
{
SendMessage(msg.syncMessage);
}
else if (msg.type == MessageType.Command)
{
SendCommand(msg.wsCommand);
}
Thread.Sleep(100);
}
}
Thread.Sleep(1000);
}
});
}
public void onConfigurationLoad()
{
}
public void onConfigurationReload()
{
Restart();
}
public void onConfigurationSave()
{
}
public override void SwitchOtherClient()
{
StopWork();
CurrentStatus = SourceStatus.IDLE;
}
public override void SwitchThisClient()
{
StartWork();
}
public override void Restart()
{
StopWork();
StartWork();
}
public override void StartWork()
{
if (web_socket != null)
return;
if (string.IsNullOrWhiteSpace(Target_User_Name))
{
IO.CurrentIO.WriteColor($"[OsuBotTransferClient]Target_User_Name(OSU! Username) is not set,Please set it in 'config.ini', or set through ConfigGUI.", ConsoleColor.Red);
return;
}
web_socket = new WebSocket(ServerPath);
web_socket.OnClose += Web_socket_OnClose;
web_socket.OnError += Web_socket_OnError;
web_socket.OnMessage += Web_socket_OnMessage;
web_socket.OnOpen += Web_socket_OnConnected;
web_socket.SetCookie(new WebSocketSharp.Net.Cookie("transfer_target_name", Target_User_Name));
web_socket.SetCookie(new WebSocketSharp.Net.Cookie("version", PublicOsuBotTransferPlugin.VERSION));
NickName = Target_User_Name;
web_socket.ConnectAsync();
}
public override void StopWork()
{
if (web_socket == null)
return;
try
{
web_socket.OnClose -= Web_socket_OnClose;
web_socket.OnError -= Web_socket_OnError;
web_socket.OnMessage -= Web_socket_OnMessage;
web_socket.OnOpen -= Web_socket_OnConnected;
web_socket.Close();
}
catch { }
finally
{
web_socket = null;
CurrentStatus = SourceStatus.USER_DISCONNECTED;
}
}
public override void SendMessage(IMessageBase message)
{
if (!web_socket.IsAlive)
{
messageBuffer.Enqueue(new Message
{
type = MessageType.SyncMessage,
syncMessage = message,
});
return;
}
web_socket.Send($"{message?.Message}");
}
//WebSocket
private void Web_socket_OnConnected(object sender, EventArgs e)
{
CurrentStatus = SourceStatus.CONNECTED_WORKING;
IO.CurrentIO.WriteColor($"[OsuBotTransferClient]Server Connected, Enjoy", ConsoleColor.Green);
SendMessage(new IRCMessage(Target_User_Name.ToString(), $"[OsuBotTransferClient]Connected Server, Enjoy"));
}
private void Web_socket_OnMessage(object sender, MessageEventArgs e)
{
string nick = Target_User_Name;
if (e.IsText)
{
string rawmsg = e.Data;
if (e.Data.StartsWith(CONST_SYNC_NOTICE_HEADER))
{
string notice = e.Data.Substring(CONST_SYNC_NOTICE_HEADER.Length);
IO.CurrentIO.WriteColor($"[OsuBotTransferClient][Notice]{notice}", ConsoleColor.Cyan);
}
else
{
IO.CurrentIO.WriteColor($"[OsuBotTransferClient]{e.Data}", ConsoleColor.Cyan);
Task.Run(() => Sync.SyncHost.Instance.Messages.RaiseMessage<ISourceClient>(new DanmakuMessage()
{
User = nick,
Message = rawmsg
}));
}
}
else if (e.IsBinary)
{
var data = e.RawData;
using (var ms = new MemoryStream(data))
using (var br = new BinaryReader(ms))
{
if(br.ReadUInt16() == RPL_TOKEN)
{
int len = br.ReadInt32();
byte[] token_bytes = br.ReadBytes(len);
var token = Encoding.UTF8.GetString(token_bytes);
Token = token;
Sync.Tools.IO.DefaultIO.WriteColor($"[OsuBotTransferClient] Get Token: {token}", ConsoleColor.Cyan);
}
}
}
}
private void Web_socket_OnError(object sender, WebSocketSharp.ErrorEventArgs e)
{
IO.CurrentIO.WriteColor($"[OsuBotTransferClient]{e.Message}", ConsoleColor.Red);
}
private void Web_socket_OnClose(object sender, CloseEventArgs e)
{
if(!string.IsNullOrEmpty(e.Reason))
IO.CurrentIO.WriteColor($"[OsuBotTransferClient][Server]{e.Reason}",ConsoleColor.Yellow);
IO.CurrentIO.WriteColor($"[OsuBotTransferClient]Disconnected", ConsoleColor.Green);
CurrentStatus = SourceStatus.REMOTE_DISCONNECTED;
if (AutoReconnect == "True")
{
//restart
Task.Run(() =>
{
StopWork();
Thread.Sleep(int.Parse(AutoReconnectInterval)*1000);
StartWork();
});
}
}
#region Command
byte[] getBytes<T>(T str) where T : struct
{
int size = Marshal.SizeOf(str);
byte[] arr = new byte[size];
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(str, ptr, true);
Marshal.Copy(ptr, arr, 0, size);
Marshal.FreeHGlobal(ptr);
return arr;
}
void SendCommand(WsCommand cmd)
{
web_socket.Send(getBytes(cmd));
}
void RequestToken()
{
WsCommand cmd = new WsCommand()
{
Command = REQ_TOKEN
};
if (web_socket.IsAlive)
{
messageBuffer.Enqueue(new Message
{
type = MessageType.Command,
wsCommand = cmd,
});
return;
}
SendCommand(cmd);
//SendMessage(new IRCMessage(Target_User_Name.ToString(), "[OsuBotTransferClient]Sync wants to request other services that the Token uses to access the Bot. Reply \"!assign_token\" to generate and send a token to Sync."));
}
#endregion
}
}