This repository was archived by the owner on May 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
653 lines (549 loc) · 23.4 KB
/
Program.cs
File metadata and controls
653 lines (549 loc) · 23.4 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using Docker.DotNet;
using Docker.DotNet.Models;
using System.Net;
using System.Net.WebSockets;
using System.Threading;
public class RequestData
{
public string Lang { get; set; }
public string Code { get; set; }
public string ID { get; set; }
public List<Attachment> Attachments { get; set; }
}
public class Attachment
{
public string Name { get; set; }
public string Content { get; set; }
}
public class Startup
{
private const string PYTHON_LANG = "python";
private const string BASH_LANG = "bash";
private const string NB_PREFIX = "nb-";
private const string ROOT_PATH = "/root";
private readonly DockerClient _dockerClient;
public Startup()
{
_dockerClient = new DockerClientConfiguration(
new Uri("unix:///var/run/docker.sock"))
.CreateClient();
}
private Dictionary<string, (MultiplexedStream Stream, CancellationTokenSource CancellationSource, string FolderPath)> _containerData = new Dictionary<string, (MultiplexedStream, CancellationTokenSource, string)>();
public void Configure(IApplicationBuilder app)
{
app.UseWebSockets();
app.Use(async (context, next) =>
{
if (context.Request.Path == "/ws")
{
if (context.WebSockets.IsWebSocketRequest)
{
using var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await HandleWebSocketConnection(webSocket);
}
else
{
context.Response.StatusCode = 400;
}
}
else
{
await next();
}
});
app.Run(HandleRequest);
}
private async Task HandleRequest(HttpContext context)
{
string responseString = "";
try
{
if (context.Request.Method == "GET")
{
switch (context.Request.Path)
{
case "/nb_create":
responseString = await HandleNbCreate(context);
break;
case "/nb_list":
responseString = await HandleNbList(context);
break;
default:
responseString = JsonConvert.SerializeObject(new { error = "Invalid GET request path." });
break;
}
}
else if (context.Request.Method == "POST")
{
if (context.Request.ContentType != "application/json")
{
responseString = JsonConvert.SerializeObject(new { error = "Invalid content type. Only application/json is supported." });
}
else
{
switch (context.Request.Path)
{
case "/nb_run":
responseString = await HandleNbRun(context);
break;
case "/nb_delete":
responseString = await HandleNbDelete(context);
break;
case "/nb_pause":
responseString = await HandleNbPause(context);
break;
case "/nb_resume":
responseString = await HandleNbResume(context);
break;
case "/run":
responseString = await HandleRun(context);
break;
case "/run_interactive":
responseString = await HandleRunInteractive(context);
break;
default:
responseString = JsonConvert.SerializeObject(new { error = "Invalid POST request path." });
break;
}
}
}
else
{
responseString = JsonConvert.SerializeObject(new { error = "Invalid request method. Only GET and POST are supported." });
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
responseString = JsonConvert.SerializeObject(new { error = "An internal server error occurred." });
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
}
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(responseString);
}
private async Task<string> HandleRunInteractive(HttpContext context)
{
var data = await DeserializeRequestBody<RequestData>(context);
if (data == null) return JsonConvert.SerializeObject(new { error = "Invalid JSON format." });
if (data.Lang != PYTHON_LANG)
{
return JsonConvert.SerializeObject(new { error = "Only Python is supported for interactive mode." });
}
string randomString = GenerateRandomString(10);
string langPath = Path.Combine(ROOT_PATH, PYTHON_LANG, randomString);
string appPath = Path.Combine(langPath, "app");
string attachmentsPath = Path.Combine(langPath, "attachments");
Directory.CreateDirectory(appPath);
Directory.CreateDirectory(attachmentsPath);
if (data.Attachments != null)
{
foreach (var attachment in data.Attachments)
{
var fileContent = Convert.FromBase64String(attachment.Content);
File.WriteAllBytes(Path.Combine(attachmentsPath, attachment.Name), fileContent);
}
}
Process.Start("chmod", $"-R 777 {attachmentsPath}");
string filePath = Path.Combine(appPath, "launch.py");
File.WriteAllText(filePath, data.Code.Replace("\n", Environment.NewLine));
var createContainerResponse = await _dockerClient.Containers.CreateContainerAsync(new CreateContainerParameters
{
Image = $"{PYTHON_LANG}-docker",
Name = randomString,
HostConfig = new HostConfig
{
Binds = new List<string>
{
$"{appPath}:/app",
$"{attachmentsPath}:/home/appuser"
}
},
Tty = true,
OpenStdin = true,
AttachStdin = true,
AttachStdout = true,
AttachStderr = true,
Cmd = new[] { "bash", "-c", "sleep 0.1 && python3 /app/launch.py" }
});
var cts = new CancellationTokenSource();
_containerData[randomString] = (null, cts, langPath);
return JsonConvert.SerializeObject(new { id = randomString });
}
private async Task HandleWebSocketConnection(WebSocket webSocket)
{
var buffer = new byte[1024 * 4];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
var idMessage = Encoding.UTF8.GetString(buffer, 0, result.Count);
var idObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(idMessage);
if (!idObject.ContainsKey("id") || !_containerData.ContainsKey(idObject["id"]))
{
await webSocket.CloseAsync(WebSocketCloseStatus.InvalidPayloadData, "Invalid container ID", CancellationToken.None);
return;
}
var containerId = idObject["id"];
var (_, cts, _) = _containerData[containerId];
await _dockerClient.Containers.StartContainerAsync(containerId, null);
var containerStream = await _dockerClient.Containers.AttachContainerAsync(containerId, true, new ContainerAttachParameters
{
Stream = true,
Stdin = true,
Stdout = true,
Stderr = true
});
_containerData[containerId] = (containerStream, cts, _containerData[containerId].Item3);
_ = Task.Delay(TimeSpan.FromMinutes(1), cts.Token).ContinueWith(t =>
{
if (!t.IsCanceled)
{
_dockerClient.Containers.KillContainerAsync(containerId, new ContainerKillParameters()).Wait();
CleanupContainer(containerId);
}
});
var outputBuffer = new byte[1024];
var readTask = Task.Run(async () =>
{
try
{
while (!cts.Token.IsCancellationRequested)
{
var readResult = await containerStream.ReadOutputAsync(outputBuffer, 0, outputBuffer.Length, cts.Token);
if (readResult.Count > 0)
{
await webSocket.SendAsync(new ArraySegment<byte>(outputBuffer, 0, readResult.Count), WebSocketMessageType.Text, true, cts.Token);
}
else
{
await webSocket.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes("Python program has finished execution.")), WebSocketMessageType.Text, true, CancellationToken.None);
CleanupContainer(containerId);
break;
}
}
}
catch (OperationCanceledException)
{
await webSocket.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes("Execution time limit reached. Terminating.")), WebSocketMessageType.Text, true, CancellationToken.None);
}
finally
{
await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Execution finished", CancellationToken.None);
CleanupContainer(containerId);
}
});
while (webSocket.State == WebSocketState.Open && !cts.Token.IsCancellationRequested)
{
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), cts.Token);
if (result.MessageType == WebSocketMessageType.Close)
break;
if (result.MessageType == WebSocketMessageType.Text)
{
string command = Encoding.UTF8.GetString(buffer, 0, result.Count);
byte[] commandBytes = Encoding.UTF8.GetBytes(command + "\n");
await containerStream.WriteAsync(commandBytes, 0, commandBytes.Length, cts.Token);
}
}
cts.Cancel();
await readTask;
}
private void CleanupContainer(string containerId)
{
if (_containerData.TryGetValue(containerId, out var containerInfo))
{
var (stream, cts, folderPath) = containerInfo;
cts.Cancel();
stream?.Dispose();
_dockerClient.Containers.RemoveContainerAsync(containerId, new ContainerRemoveParameters { Force = true }).Wait();
Directory.Delete(folderPath, true);
_containerData.Remove(containerId);
}
}
private async Task<string> HandleNbCreate(HttpContext context)
{
try
{
string randomString = GenerateRandomString(10);
string nbPath = Path.Combine(ROOT_PATH, "nb", randomString);
Directory.CreateDirectory(nbPath);
Directory.CreateDirectory(Path.Combine(nbPath, "api"));
Directory.CreateDirectory(Path.Combine(nbPath, "attachments"));
string apiPath = Path.Combine(nbPath, "api");
string attachmentsPath = Path.Combine(nbPath, "attachments");
File.WriteAllText(Path.Combine(apiPath, "exe"), "");
File.WriteAllText(Path.Combine(apiPath, "out"), "");
Process.Start("chmod", $"-R 777 {nbPath}");
var createContainerResponse = await _dockerClient.Containers.CreateContainerAsync(new CreateContainerParameters
{
Image = "python-nb",
Name = randomString,
HostConfig = new HostConfig
{
Binds = new List<string>
{
$"{apiPath}:/api",
$"{attachmentsPath}:/home/appuser"
}
}
});
await _dockerClient.Containers.StartContainerAsync(createContainerResponse.ID, null);
return JsonConvert.SerializeObject(new { id = $"{NB_PREFIX}{randomString}"});
}
catch (Exception ex)
{
Console.WriteLine($"Erreur: {ex.Message}");
return JsonConvert.SerializeObject(new { error = "An error occured while creating the notebook" });
}
}
private async Task<string> HandleNbRun(HttpContext context)
{
var data = await DeserializeRequestBody<RequestData>(context);
if (data == null) return JsonConvert.SerializeObject(new { error = "Invalid JSON format." });
if (string.IsNullOrEmpty(data.ID) || !data.ID.StartsWith(NB_PREFIX))
{
return JsonConvert.SerializeObject(new { error = "Invalid notebook ID" });
}
string id = data.ID.Replace(NB_PREFIX, "");
string nbPath = Path.Combine(ROOT_PATH, "nb", id);
if (!Directory.Exists(nbPath))
{
return JsonConvert.SerializeObject(new { error = "Notebook not found" });
}
var containerInfo = await _dockerClient.Containers.InspectContainerAsync(id);
if (containerInfo.State.Status != "running")
{
return JsonConvert.SerializeObject(new { error = "This notebook is paused" });
}
if (data.Lang != PYTHON_LANG)
{
return JsonConvert.SerializeObject(new { error = "Invalid language for notebook" });
}
var stopwatch = Stopwatch.StartNew();
string apiPath = Path.Combine(nbPath, "api");
string attachmentsPath = Path.Combine(nbPath, "attachments");
if (data.Attachments != null)
{
foreach (var attachment in data.Attachments)
{
var fileContent = Convert.FromBase64String(attachment.Content);
File.WriteAllBytes(Path.Combine(attachmentsPath, attachment.Name), fileContent);
}
}
string outputPath = Path.Combine(apiPath, "out");
string exePath = Path.Combine(apiPath, "exe");
FileInfo fileInfo = new FileInfo(outputPath);
DateTime lastWriteTime = fileInfo.LastWriteTime;
File.WriteAllText(exePath, data.Code);
while (fileInfo.LastWriteTime <= lastWriteTime)
{
await Task.Delay(100);
fileInfo.Refresh();
}
string[] files = Directory.GetFiles(attachmentsPath);
if (files.Length > 0)
{
List<Attachment> attachments = new List<Attachment>();
foreach (string file in files)
{
attachments.Add(new Attachment
{
Name = Path.GetFileName(file),
Content = Convert.ToBase64String(File.ReadAllBytes(file))
});
}
stopwatch.Stop();
return JsonConvert.SerializeObject(new { result = File.ReadAllText(outputPath), attachments_out = attachments, executionTime = stopwatch.Elapsed.TotalSeconds });
}
stopwatch.Stop();
string output2 = File.ReadAllText(outputPath);
return JsonConvert.SerializeObject(new { result = output2, executionTime = stopwatch.Elapsed.TotalSeconds });
}
private async Task<string> HandleNbDelete(HttpContext context)
{
var data = await DeserializeRequestBody<RequestData>(context);
if (data == null) return JsonConvert.SerializeObject(new { error = "Invalid JSON format." });
string id = data.ID;
if (string.IsNullOrEmpty(id) || !id.StartsWith(NB_PREFIX))
{
return JsonConvert.SerializeObject(new { error = "Invalid notebook ID" });
}
string nbPath = Path.Combine(ROOT_PATH, "nb", id.Replace(NB_PREFIX, ""));
if (Directory.Exists(nbPath))
{
Directory.Delete(nbPath, true);
}
await _dockerClient.Containers.KillContainerAsync(id.Replace(NB_PREFIX, ""), new ContainerKillParameters());
await _dockerClient.Containers.RemoveContainerAsync(id.Replace(NB_PREFIX, ""), new ContainerRemoveParameters());
return JsonConvert.SerializeObject(new { result = "Notebook deleted" });
}
private async Task<string> HandleNbPause(HttpContext context)
{
var data = await DeserializeRequestBody<RequestData>(context);
if (data == null) return JsonConvert.SerializeObject(new { error = "Invalid JSON format." });
string id = data.ID;
if (string.IsNullOrEmpty(id) || !id.StartsWith(NB_PREFIX))
{
return JsonConvert.SerializeObject(new { error = "Invalid notebook ID" });
}
await _dockerClient.Containers.PauseContainerAsync(id.Replace(NB_PREFIX, ""));
return JsonConvert.SerializeObject(new { result = "Notebook paused" });
}
private async Task<string> HandleNbResume(HttpContext context)
{
var data = await DeserializeRequestBody<RequestData>(context);
if (data == null) return JsonConvert.SerializeObject(new { error = "Invalid JSON format." });
string id = data.ID;
if (string.IsNullOrEmpty(id) || !id.StartsWith(NB_PREFIX))
{
return JsonConvert.SerializeObject(new { error = "Invalid notebook ID" });
}
await _dockerClient.Containers.UnpauseContainerAsync(id.Replace(NB_PREFIX, ""));
return JsonConvert.SerializeObject(new { result = "Notebook resumed" });
}
private async Task<string> HandleNbList(HttpContext context)
{
var containers = await _dockerClient.Containers.ListContainersAsync(new ContainersListParameters { All = true });
var notebooks = containers
.Where(c => c.Names.Any(n => n.StartsWith("/")))
.Select(c => new Dictionary<string, string>
{
{ "id", $"{NB_PREFIX}{c.Names[0].TrimStart('/')}" },
{ "state", c.State }
})
.ToList();
foreach (var notebook in notebooks)
{
if (notebook["state"] == "exited")
{
notebook["state"] = "paused";
}
}
return JsonConvert.SerializeObject(new { notebooks });
}
private async Task<string> HandleRun(HttpContext context)
{
var data = await DeserializeRequestBody<RequestData>(context);
if (data == null) return JsonConvert.SerializeObject(new { error = "Invalid JSON format." });
switch (data.Lang)
{
case PYTHON_LANG:
return await Runner(PYTHON_LANG, data.Code, data.Attachments);
case BASH_LANG:
return await Runner(BASH_LANG, data.Code, data.Attachments);
default:
return JsonConvert.SerializeObject(new { error = "Invalid language" });
}
}
private async Task<T> DeserializeRequestBody<T>(HttpContext context)
{
using var reader = new StreamReader(context.Request.Body, Encoding.UTF8);
var json = await reader.ReadToEndAsync();
try
{
return JsonConvert.DeserializeObject<T>(json);
}
catch (JsonException e)
{
Console.WriteLine(e.Message);
return default;
}
}
private async Task<string> Runner(string lang, string code, List<Attachment> attachments)
{
string randomString = GenerateRandomString(10);
string langPath = Path.Combine(ROOT_PATH, lang, randomString);
string appPath = Path.Combine(langPath, "app");
string attachmentsPath = Path.Combine(langPath, "attachments");
Directory.CreateDirectory(appPath);
Directory.CreateDirectory(attachmentsPath);
if (attachments != null)
{
foreach (var attachment in attachments)
{
var fileContent = Convert.FromBase64String(attachment.Content);
File.WriteAllBytes(Path.Combine(attachmentsPath, attachment.Name), fileContent);
}
}
Process.Start("chmod", $"-R 777 {attachmentsPath}");
string fileExtension = lang == PYTHON_LANG ? "py" : "sh";
string filePath = Path.Combine(appPath, $"launch.{fileExtension}");
File.WriteAllText(filePath, code.Replace("\n", Environment.NewLine));
var createContainerResponse = await _dockerClient.Containers.CreateContainerAsync(new CreateContainerParameters
{
Image = $"{lang}-docker",
Name = randomString,
HostConfig = new HostConfig
{
Binds = new List<string>
{
$"{appPath}:/app",
$"{attachmentsPath}:/home/appuser"
}
},
Tty = true
});
var stopwatch = Stopwatch.StartNew();
await _dockerClient.Containers.StartContainerAsync(createContainerResponse.ID, null);
var waitTask = _dockerClient.Containers.WaitContainerAsync(createContainerResponse.ID);
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(10.20));
if (await Task.WhenAny(waitTask, timeoutTask) == timeoutTask)
{
await _dockerClient.Containers.StopContainerAsync(createContainerResponse.ID, new ContainerStopParameters());
}
stopwatch.Stop();
var logs = await _dockerClient.Containers.GetContainerLogsAsync(createContainerResponse.ID, new ContainerLogsParameters { ShowStdout = true, ShowStderr = true });
using var reader = new StreamReader(logs);
var result = await reader.ReadToEndAsync();
result = string.IsNullOrWhiteSpace(result) ? "CES: Empty output" : result.Replace("KeyboardInterrupt", "Execution timed out.");
if (attachments != null)
{
foreach (var attachment in attachments)
{
File.Delete(Path.Combine(appPath, attachment.Name));
}
}
var filesList = Directory.GetFiles(attachmentsPath)
.Take(10)
.Select(file => new Attachment
{
Name = Path.GetFileName(file),
Content = Convert.ToBase64String(File.ReadAllBytes(file))
})
.ToList();
Directory.Delete(langPath, true);
await _dockerClient.Containers.RemoveContainerAsync(createContainerResponse.ID, new ContainerRemoveParameters());
return JsonConvert.SerializeObject(new
{
result = result,
executionTime = stopwatch.Elapsed.TotalSeconds,
attachments_out = filesList
});
}
private string GenerateRandomString(int length)
{
const string chars = "abcdefghijklmnopqrstuvwxyz";
var random = new Random();
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
}
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseStartup<Startup>()
.UseUrls("http://0.0.0.0:8080")
.Build();
host.Run();
}
}