Repository navigation
Expand file tree
/
Copy pathMobileServer.cs
More file actions
375 lines (347 loc) · 13.8 KB
/
Copy pathMobileServer.cs
File metadata and controls
375 lines (347 loc) · 13.8 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
namespace KeyParcel
{
internal sealed class MobileInbox
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern bool MoveFileEx(string source, string destination, uint flags);
private const uint ReplaceExisting = 0x1, WriteThrough = 0x8;
private readonly string credentialFile;
private readonly object gate = new object();
private string credential, pairCode, latest;
private DateTime expiry;
internal MobileInbox(string folder)
{
credentialFile = Path.Combine(folder, "phone-credential.txt");
try
{
var saved = File.ReadAllText(credentialFile);
if (saved.Length == 43) credential = saved;
}
catch (IOException) { }
catch (UnauthorizedAccessException) { }
}
internal string Latest { get { lock (gate) return latest; } }
internal bool Paired { get { lock (gate) return credential != null; } }
internal string PairCode
{
get { lock (gate) return DateTime.UtcNow < expiry ? pairCode : null; }
}
internal void NewCode()
{
lock (gate)
{
pairCode = RandomToken(16);
expiry = DateTime.UtcNow.AddMinutes(5);
}
}
internal void CancelCode() { lock (gate) pairCode = null; }
internal void Forget()
{
lock (gate)
{
File.Delete(credentialFile);
credential = pairCode = latest = null;
}
}
internal string Pair(string submitted)
{
lock (gate)
{
if (DateTime.UtcNow >= expiry || !Matches(pairCode, submitted)) return null;
var next = RandomToken(32);
SaveSetting(credentialFile, next);
credential = next;
pairCode = null;
return next;
}
}
internal bool Accept(string submitted, string value)
{
lock (gate)
{
if (!Matches(credential, submitted)) return false;
if (value != null) latest = value;
return true;
}
}
internal static void SaveSetting(string path, string value)
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
string temp = path + "." + Guid.NewGuid().ToString("N");
try
{
File.WriteAllText(temp, value, new UTF8Encoding(false));
if (!MoveFileEx(temp, path, ReplaceExisting | WriteThrough))
throw new IOException("Could not replace the settings file.", new Win32Exception(Marshal.GetLastWin32Error()));
}
finally { File.Delete(temp); }
}
private static string RandomToken(int size)
{
var data = new byte[size];
using (var random = RandomNumberGenerator.Create()) random.GetBytes(data);
return Convert.ToBase64String(data).TrimEnd('=').Replace('+', '-').Replace('/', '_');
}
private static bool Matches(string expected, string actual)
{
if (expected == null || actual == null || expected.Length != actual.Length) return false;
int diff = 0;
for (int i = 0; i < expected.Length; i++) diff |= expected[i] ^ actual[i];
return diff == 0;
}
}
internal sealed class MobileServer : IDisposable
{
internal const int PreferredPort = 8765;
private const int MaxTextBytes = 32768;
private const int MaxHeaderBytes = 8192;
private const int RequestTimeoutMs = 6000;
private static readonly UTF8Encoding Utf8 = new UTF8Encoding(false, true);
private readonly object gate = new object();
private readonly TcpListener listener;
private readonly MobileInbox inbox;
private readonly byte[] page;
private readonly string authority;
private readonly HashSet<TcpClient> clients = new HashSet<TcpClient>();
private bool closed;
internal string Address { get { return "http://" + authority; } }
internal int ListeningPort { get; private set; }
internal MobileServer(IPAddress ip, int port, MobileInbox inbox)
{
this.inbox = inbox;
using (var resource = typeof(MobileServer).Assembly.GetManifestResourceStream("KeyParcel.Phone.html"))
using (var buffer = new MemoryStream())
{
resource.CopyTo(buffer);
page = buffer.ToArray();
}
try { listener = Bind(ip, port); }
catch (SocketException error)
{
if (port == 0 || (error.SocketErrorCode != SocketError.AddressAlreadyInUse &&
error.SocketErrorCode != SocketError.AccessDenied)) throw;
// Port zero lets Windows select and reserve a free port in a single operation.
listener = Bind(ip, 0);
}
var endpoint = (IPEndPoint)listener.LocalEndpoint;
ListeningPort = endpoint.Port;
authority = endpoint.ToString();
var acceptor = new Thread(AcceptConnections) { IsBackground = true, Name = "KeyParcel LAN" };
acceptor.Start();
}
private static TcpListener Bind(IPAddress ip, int port)
{
var candidate = new TcpListener(ip, port);
try
{
candidate.ExclusiveAddressUse = true;
candidate.Start(8);
return candidate;
}
catch
{
candidate.Server.Close();
throw;
}
}
private void AcceptConnections()
{
while (true)
{
TcpClient client;
try { client = listener.AcceptTcpClient(); }
catch (SocketException) { return; }
catch (InvalidOperationException) { return; }
lock (gate)
{
if (closed || clients.Count >= 8) { client.Close(); continue; }
clients.Add(client);
}
ThreadPool.QueueUserWorkItem(ServeClient, client);
}
}
private void ServeClient(object state)
{
var client = (TcpClient)state;
try
{
client.SendTimeout = 3000;
using (var stream = client.GetStream()) Handle(stream);
}
catch (IOException) { }
catch (SocketException) { }
catch (ObjectDisposedException) { }
finally
{
client.Close();
lock (gate) clients.Remove(client);
}
}
private void Handle(NetworkStream stream)
{
var request = new RequestReader(stream);
string[] line = request.Line().Split(' ');
if (line.Length != 3 || (line[2] != "HTTP/1.1" && line[2] != "HTTP/1.0"))
throw new IOException("Invalid request line");
var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
string header;
while ((header = request.Line()).Length != 0)
{
int split = header.IndexOf(':');
if (split < 1 || headers.ContainsKey(header.Substring(0, split)))
throw new IOException("Invalid request header");
headers.Add(header.Substring(0, split), header.Substring(split + 1).Trim());
}
string host;
if (!headers.TryGetValue("Host", out host) || host != authority ||
headers.ContainsKey("Transfer-Encoding"))
{
Respond(stream, "400 Bad Request");
return;
}
if (line[0] == "GET" && line[1] == "/")
{
Respond(stream, "200 OK", page, "text/html; charset=utf-8");
return;
}
if (line[0] != "POST" || (line[1] != "/pair" && line[1] != "/check" && line[1] != "/text"))
{
Respond(stream, "404 Not Found");
return;
}
string origin, authorization;
if (!headers.TryGetValue("Origin", out origin) || origin != Address ||
!headers.TryGetValue("Authorization", out authorization) ||
!authorization.StartsWith("Bearer ", StringComparison.Ordinal))
{
Respond(stream, "403 Forbidden");
return;
}
string size;
int length;
if (!headers.TryGetValue("Content-Length", out size) ||
!Int32.TryParse(size, NumberStyles.None, CultureInfo.InvariantCulture, out length))
{
Respond(stream, "400 Bad Request");
return;
}
if (length > MaxTextBytes)
{
Respond(stream, "413 Content Too Large");
return;
}
bool textRequest = line[1] == "/text";
if (textRequest != (length > 0))
{
Respond(stream, "400 Bad Request");
return;
}
string value = null;
if (textRequest)
{
try { value = Utf8.GetString(request.Body(length)); }
catch (DecoderFallbackException) { Respond(stream, "400 Bad Request"); return; }
if (value.IndexOf('\0') >= 0) { Respond(stream, "400 Bad Request"); return; }
}
string token = authorization.Substring(7);
string newCredential = null;
bool accepted;
try
{
lock (gate)
{
if (closed) return;
if (line[1] == "/pair")
{
newCredential = inbox.Pair(token);
accepted = newCredential != null;
}
else accepted = inbox.Accept(token, value);
}
}
catch (IOException) { Respond(stream, "500 Internal Server Error"); return; }
catch (UnauthorizedAccessException) { Respond(stream, "500 Internal Server Error"); return; }
if (!accepted) Respond(stream, "403 Forbidden");
else Respond(stream, "200 OK", newCredential == null ? null : Utf8.GetBytes(newCredential));
}
private static void Respond(NetworkStream stream, string status, byte[] body = null,
string type = "text/plain; charset=utf-8")
{
string headers = "HTTP/1.1 " + status + "\r\nConnection: close\r\nCache-Control: no-store\r\n" +
"X-Content-Type-Options: nosniff\r\nReferrer-Policy: no-referrer\r\n" +
"Content-Security-Policy: default-src 'none'; style-src 'unsafe-inline'; " +
"script-src 'unsafe-inline'; connect-src 'self'; form-action 'none'; frame-ancestors 'none'\r\n" +
"Content-Type: " + type + "\r\nContent-Length: " + (body == null ? 0 : body.Length) + "\r\n\r\n";
var bytes = Encoding.ASCII.GetBytes(headers);
stream.Write(bytes, 0, bytes.Length);
if (body != null) stream.Write(body, 0, body.Length);
}
public void Dispose()
{
lock (gate)
{
if (closed) return;
closed = true;
listener.Stop();
foreach (var client in clients) client.Close();
inbox.CancelCode();
}
}
private sealed class RequestReader
{
private readonly NetworkStream stream;
private readonly Stopwatch clock = Stopwatch.StartNew();
private readonly byte[] buffer = new byte[4096];
private int index, count, headerBytes;
internal RequestReader(NetworkStream stream) { this.stream = stream; }
internal string Line()
{
var line = new StringBuilder();
while (++headerBytes <= MaxHeaderBytes)
{
int value = Next();
if (value == '\r')
{
headerBytes++;
if (Next() != '\n') break;
return line.ToString();
}
if (value < 32 && value != '\t' || value >= 127) break;
line.Append((char)value);
}
throw new IOException("Invalid or oversized header");
}
internal byte[] Body(int length)
{
var body = new byte[length];
for (int i = 0; i < body.Length; i++) body[i] = (byte)Next();
return body;
}
private int Next()
{
if (index == count)
{
long remaining = RequestTimeoutMs - clock.ElapsedMilliseconds;
if (remaining <= 0) throw new IOException("Request timed out");
stream.ReadTimeout = (int)remaining;
count = stream.Read(buffer, 0, buffer.Length);
index = 0;
if (count == 0) throw new EndOfStreamException();
}
return buffer[index++];
}
}
}
}