-
Notifications
You must be signed in to change notification settings - Fork 0
/
KitchenOverlay.cs
331 lines (280 loc) · 12.3 KB
/
KitchenOverlay.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection.Emit;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using TShockAPI;
using static System.Net.Mime.MediaTypeNames;
namespace TerrariaKitchen
{
public class KitchenOverlay
{
private TcpListener? wsServer;
private HttpListener? httpListener;
private bool ShuttingDown = false;
private List<NetworkStream> wsStreams;
private KitchenConfig Config;
public event EventHandler<NetworkStream>? NewConnection;
public KitchenOverlay(KitchenConfig config)
{
wsStreams = new List<NetworkStream>();
Config = config;
}
public void StartServer(int httpPort, int wsPort)
{
if (httpPort < 1024 || httpPort > 49151 || wsPort < 1024 || wsPort > 49151)
{
Console.WriteLine("(Terraria Kitchen) Ports out of range, cannot start web server.");
return;
}
httpListener?.Close();
wsServer?.Stop();
var httpUrl = $"http://localhost:{httpPort}/";
httpListener = new HttpListener();
httpListener.Prefixes.Add(httpUrl);
httpListener.Start();
HttpLoop(httpListener);
wsServer = new TcpListener(IPAddress.Parse("127.0.0.1"), wsPort);
wsServer.Start();
wsServer.BeginAcceptTcpClient(AcceptClient, wsServer);
Console.WriteLine($"(Terraria Kitchen) Servers started. Overlay address: {httpUrl} ws address: ws://127.0.0.1:{wsPort}");
}
private string MenuHtml()
{
return MenuGenerator.GenerateMenu(Config.Entries, Config.Events);
}
private void HttpLoop(HttpListener httpListener)
{
new Thread(async () =>
{
while (!ShuttingDown)
{
try
{
HttpListenerContext ctx = await httpListener.GetContextAsync();
// Peel out the requests and response objects
HttpListenerRequest req = ctx.Request;
HttpListenerResponse resp = ctx.Response;
byte[] data = req.Url?.AbsolutePath == "/menu" ? Encoding.UTF8.GetBytes(MenuHtml()) : Encoding.UTF8.GetBytes(KitchenOverlayHtml.GeneratePage($"{req.Url?.Host}:{(wsServer?.LocalEndpoint as IPEndPoint)?.Port ?? Config.OverlayWsPort}"));
resp.ContentType = "text/html";
resp.ContentEncoding = Encoding.UTF8;
resp.ContentLength64 = data.LongLength;
// Write out to the response stream (asynchronously), then close it
await resp.OutputStream.WriteAsync(data, 0, data.Length);
resp.Close();
}
catch
{
if (!ShuttingDown)
{
Console.WriteLine("(Terraria Kitchen) An error is detected with the HTTP server.");
}
}
}
})
{ IsBackground = true }.Start();
}
public enum EOpcodeType
{
/* Denotes a continuation code */
Fragment = 0,
/* Denotes a text code */
Text = 1,
/* Denotes a binary code */
Binary = 2,
/* Denotes a closed connection */
ClosedConnection = 8,
/* Denotes a ping*/
Ping = 9,
/* Denotes a pong */
Pong = 10
}
public static byte[] GetFrameFromString(string Message, EOpcodeType Opcode = EOpcodeType.Text)
{
byte[] response;
byte[] bytesRaw = Encoding.Default.GetBytes(Message);
byte[] frame = new byte[10];
int indexStartRawData = -1;
int length = bytesRaw.Length;
frame[0] = (byte)(128 + (int)Opcode);
if (length <= 125)
{
frame[1] = (byte)length;
indexStartRawData = 2;
}
else if (length >= 126 && length <= 65535)
{
frame[1] = (byte)126;
frame[2] = (byte)((length >> 8) & 255);
frame[3] = (byte)(length & 255);
indexStartRawData = 4;
}
else
{
frame[1] = (byte)127;
frame[2] = (byte)((length >> 56) & 255);
frame[3] = (byte)((length >> 48) & 255);
frame[4] = (byte)((length >> 40) & 255);
frame[5] = (byte)((length >> 32) & 255);
frame[6] = (byte)((length >> 24) & 255);
frame[7] = (byte)((length >> 16) & 255);
frame[8] = (byte)((length >> 8) & 255);
frame[9] = (byte)(length & 255);
indexStartRawData = 10;
}
response = new byte[indexStartRawData + length];
int i, reponseIdx = 0;
//Add the frame bytes to the reponse
for (i = 0; i < indexStartRawData; i++)
{
response[reponseIdx] = frame[i];
reponseIdx++;
}
//Add the data bytes to the response
for (i = 0; i < length; i++)
{
response[reponseIdx] = bytesRaw[i];
reponseIdx++;
}
return response;
}
private void AcceptClient(IAsyncResult ar)
{
var listener = (TcpListener?)ar.AsyncState;
if (ShuttingDown || listener == null)
{
return;
}
new Thread(() =>
{
var client = listener.EndAcceptTcpClient(ar);
//Console.WriteLine("(TEST WEBSOCKET) Got connection from {0}", (client.Client.RemoteEndPoint as IPEndPoint)?.Address.ToString() ?? "Address not found");
using (var stream = client.GetStream())
{
while (!ShuttingDown)
{
while (!stream.DataAvailable)
{
if (!client.Connected || ShuttingDown)
{
// Dispose if no longer connected
//Console.WriteLine("(TEST WEBSOCKET) {0} Disconnected", (client.Client.RemoteEndPoint as IPEndPoint)?.Address.ToString() ?? "Address not found");
lock (wsStreams)
{
wsStreams.Remove(stream);
}
return;
}
}
while (client.Available < 3)
{
if (!client.Connected || ShuttingDown)
{
// Dispose if no longer connected
//Console.WriteLine("(TEST WEBSOCKET) {0} Disconnected", (client.Client.RemoteEndPoint as IPEndPoint)?.Address.ToString() ?? "Address not found");
lock (wsStreams)
{
wsStreams.Remove(stream);
}
return;
}
}
byte[] bytes = new byte[client.Available];
stream.Read(bytes, 0, bytes.Length);
String data = Encoding.UTF8.GetString(bytes);
if (Regex.IsMatch(data, "^GET", RegexOptions.IgnoreCase))
{
string swk = Regex.Match(data, "Sec-WebSocket-Key: (.*)").Groups[1].Value.Trim();
string swka = swk + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
byte[] swkaSha1 = System.Security.Cryptography.SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(swka));
string swkaSha1Base64 = Convert.ToBase64String(swkaSha1);
//Console.WriteLine("(TEST WEBSOCKET) GET Received - {0}", (client.Client.RemoteEndPoint as IPEndPoint)?.Address.ToString() ?? "Address not found");
// HTTP/1.1 defines the sequence CR LF as the end-of-line marker
byte[] response = Encoding.UTF8.GetBytes(
"HTTP/1.1 101 Switching Protocols\r\n" +
"Connection: Upgrade\r\n" +
"Upgrade: websocket\r\n" +
"Sec-WebSocket-Accept: " + swkaSha1Base64 + "\r\n\r\n");
stream.Write(response, 0, response.Length);
NewConnection?.Invoke(client, stream);
lock (wsStreams)
{
wsStreams.Add(stream);
}
}
else
{
bool fin = (bytes[0] & 0b10000000) != 0,
mask = (bytes[1] & 0b10000000) != 0; // must be true, "All messages from the client to the server have this bit set"
int opcode = bytes[0] & 0b00001111, // expecting 1 - text message
offset = 2;
ulong msglen = bytes[1] & (ulong)0b01111111;
if (msglen == 126)
{
msglen = BitConverter.ToUInt16(new byte[] { bytes[3], bytes[2] }, 0);
offset = 4;
}
else if (msglen == 127)
{
msglen = BitConverter.ToUInt64(new byte[] { bytes[9], bytes[8], bytes[7], bytes[6], bytes[5], bytes[4], bytes[3], bytes[2] }, 0);
offset = 10;
}
if (msglen == 0)
{
//Console.WriteLine("(TEST WEBSOCKET) - EMPTY MESSAGE");
}
else if (mask)
{
byte[] decoded = new byte[msglen];
byte[] masks = new byte[4] { bytes[offset], bytes[offset + 1], bytes[offset + 2], bytes[offset + 3] };
offset += 4;
for (ulong i = 0; i < msglen; ++i)
decoded[i] = (byte)(bytes[(ulong) offset + i] ^ masks[i % 4]);
string text = Encoding.UTF8.GetString(decoded);
var response = GetFrameFromString("PONG " + text);
stream.Write(response, 0, response.Length);
}
}
}
lock (wsStreams)
{
wsStreams.Remove(stream);
}
}
})
{ IsBackground = true }.Start();
listener.BeginAcceptTcpClient(AcceptClient, listener);
}
public void SendPacket(object packet)
{
lock (wsStreams)
{
foreach (var stream in wsStreams)
{
if (stream.CanWrite)
{
try
{
var response = GetFrameFromString(JsonConvert.SerializeObject(packet));
stream.Write(response, 0, response.Length);
}
catch
{
}
}
}
}
}
public void Dispose()
{
ShuttingDown = true;
httpListener?.Abort();
wsServer?.Stop();
}
}
}