This repository was archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
89 lines (80 loc) · 2.49 KB
/
Program.cs
File metadata and controls
89 lines (80 loc) · 2.49 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
using System.Net;
using System.Net.Sockets;
namespace WebsiteProxy
{
public static class Program
{
public static void Main()
{
/*foreach (string test in new string[]
{
@"c:\dir1\",
@"c:\dir1\dir2",
@"c:\temp\..\dir1\dir2",
@"c:\dir1\..\windows\system32\",
Path.Combine(Util.currentDirectory, "website"),
Path.Combine(Util.currentDirectory, ".env"),
Util.currentDirectory,
@"\test\test"
})
{
Log testLog = new Log(writeTimeTaken: false);
testLog.Add(test + ":");
bool status = Util.IsInCurrentDirectory(test);
testLog.Add(status, status);
testLog.Write();
}*/
// Refreshes all internal git repositories.
List<Task> pullTasks = new List<Task>();
foreach (string directory in Directory.GetDirectories(Path.Combine(Util.currentDirectory, "repositories")))
{
Task task = new Task(() =>
{
Log log = new Log();
GitApi.Pull(directory, log);
log.Write();
});
task.Start();
pullTasks.Add(task);
}
if (!Task.WhenAll(pullTasks).Wait(5000))
{
Log.Write("Some pull processes failed.", LogColor.Error);
}
Log.Write();
// Certificate setup: https://stackoverflow.com/a/33905011
// Note to self: Certbot makes certificates, openssl combines certificate and key to .pfx file,
// wich is loaded in with Windows MMC, and then bound to app with netsh http add sslcert. Phew!
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 80);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(endPoint);
socket.Listen(10); // Starts listening on port with a max queue of 10.
Log log = new Log(writeTimeTaken: false);
log.Add("Server running...");
#if DEBUG
log.Add("(Debug mode enabled)", LogColor.Success);
log.AddRow("DO NOT USE DEBUG MODE IN PRODUCTION!", LogColor.Error);
#else
log.Add("(Debug mode disabled)", LogColor.Error);
#endif
log.Write();
Log.WriteRestartTime();
while (true)
{
Socket clientSocket = socket.Accept();
log = new Log(true, clientSocket.RemoteEndPoint);
clientSocket.ReceiveTimeout = 2000;
RequestHeaders? requestHeaders = RequestHeaders.ReadFromSocket(clientSocket, log);
if (requestHeaders == null)
{
continue;
}
#if DEBUG
Website.HandleConnection(clientSocket, requestHeaders, log); // Handles connection synchronously.
#else
Task.Run(() => Website.HandleConnection(clientSocket, requestHeaders, log)); // Handles connection asynchronously.
#endif
}
}
}
}