-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.cs
141 lines (132 loc) · 3.23 KB
/
Server.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
using System;
using System.Text;
using System.Net;
using System.Threading;
using System.Security.Principal;
using System.Runtime.InteropServices;
namespace StreamLabs_Helper
{
class Server : IDisposable
{
static volatile HttpListener _httpListener = new HttpListener();
Thread _responseThread;
private string responseString;
private object threadLock = new object();
private bool shouldRun = true;
public Server(string message = null)
{
responseString = WebSite.FormSite(message);
}
public bool StartServer(string mode)
{
mode ??= "local"; //sets mode to local if null argument was supplied
ProgramManager.Print("Starting server in " + mode + " mode");
switch (mode)
{
case "local":
_httpListener.Prefixes.Add("http://+:80/Temporary_Listen_Addresses/2525/");
break;
case "network":
if (IsAdministrator())
{
try
{
_httpListener.Prefixes.Add("http://localhost:5000/");
}
catch
{
ProgramManager.Error("Failed to add prefix, switching to run in local mode");
goto case "local";
}
}
else
{
ProgramManager.Error("Program requires administrator to run in this mode.", fatal: true);
return false;
}
break;
default:
ProgramManager.Error("Invalid mode argument");
goto case "local";
}
try {
_httpListener.Start(); // requires exe to be run as administrator for network mode
}
catch {
ProgramManager.Error("failed to start server", true);
return false;
}
ProgramManager.Print("Server started.");
_responseThread = new Thread(new ThreadStart(ResponseThread));
_responseThread.Start(); // start the response thread
return true;
}
void ResponseThread()
{
while (ProgramManager.ProgramStatus() && shouldRun)
{
try
{
if (_httpListener is not null)
{
HttpListenerContext context = _httpListener.GetContext();
byte[] _responseArray = Encoding.UTF8.GetBytes(responseString); // encodes string in UTF-8
context.Response.OutputStream.Write(_responseArray, 0, _responseArray.Length);
context.Response.KeepAlive = false;
context.Response.Close();
ProgramManager.Print("Response given to a request.");
}
}
catch
{
ProgramManager.Print("httpListener killed");
ProgramManager.CloseProgram();
}
}
}
public void UpdateResponse(string newResponse) //updates the reponse that the server will display
{
responseString = WebSite.FormSite(newResponse);
}
public static bool IsAdministrator()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
else
return true;
}
public void Close()
{
shouldRun = false;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposing)
{
return;
}
if (_httpListener == null)
{
return;
}
lock (threadLock)
{
if (_httpListener == null)
{
return;
}
_httpListener?.Stop();//double check
_httpListener = null;
}
}
}
}