This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Program.cs
310 lines (270 loc) · 15 KB
/
Program.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
using CommandLine;
using EchoRelay.Core.Server;
using EchoRelay.Core.Server.Messages;
using EchoRelay.Core.Server.Services;
using EchoRelay.Core.Server.Storage;
using EchoRelay.Core.Server.Storage.Filesystem;
using EchoRelay.Core.Utils;
using Microsoft.AspNetCore.Components.Web;
using Newtonsoft.Json;
using System.Reflection;
namespace EchoRelay.Cli
{
class Program
{
/// <summary>
/// The parsed CLI argument options for the application.
/// </summary>
private static CliOptions? Options;
/// <summary>
/// The instance of the server hosting central services.
/// </summary>
private static Server Server;
/// <summary>
/// The update timer used to trigger a peer stats update on a given interval.
/// </summary>
private static System.Timers.Timer? peerStatsUpdateTimer;
/// <summary>
/// The time that the server was started.
/// </summary>
private static DateTime startedTime;
/// <summary>
/// A mutex lock object to be used when printing to console, to avoid color collisions.
/// </summary>
private static object _printLock = new object();
/// <summary>
/// The CLI argument options for the application.
/// </summary>
public class CliOptions
{
[Option('d', "database", Required = true, HelpText = "The database folder to use for server resources. If running for the first time, should be an existing, but empty folder.")]
public string DatabaseFolder { get; set; } = "";
[Option('g', "game", Required = false, HelpText = "The optional path to the game (echovr.exe). Extracts symbols to the server's symbol cache during initial deployment.")]
public string? GameExecutablePath { get; set; }
[Option('p', "port", Required = false, Default = 777, HelpText = "The TCP port to broadcast central services over.")]
public int Port { get; set; }
[Option("apikey", Required = false, Default = null, HelpText = "Requires a specific API key as part of the ServerDB connection URI query parameters.")]
public string? ServerDBApiKey { get; set; }
[Option("forcematching", Required = false, Default = true, HelpText = "Forces users to match to any available game, in the event of their requested game servers being unavailable.")]
public bool ForceMatching { get; set; }
[Option("lowpingmatching", Required = false, Default = false, HelpText = "Sets a preference for matching to game servers with low ping instead of high population.")]
public bool LowPingMatching { get; set; }
[Option("outputconfig", Required = false, HelpText = "Outputs the generated service config file to a given file path on disk.")]
public string? OutputConfigPath { get; set; } = null;
[Option("statsinterval", Required = false, Default = 3000, HelpText = "Sets the interval at which the CLI will output its peer stats (in milliseconds).")]
public double StatsUpdateInterval { get; set; }
[Option("noservervalidation", Required = false, Default = false, HelpText = "Disables validation of game servers using raw ping requests, ensuring their ports are exposed.")]
public bool ServerDBValidateGameServers { get; set; }
[Option("servervalidationtimeout", Required = false, Default = 3000, HelpText = "Sets the timeout for game server validation using raw ping requests. In milliseconds.")]
public int ServerDBValidateGameServersTimeout { get; set; }
[Option('v', "verbose", Required = false, Default = false, HelpText = "Verbose output for every message sent between clients and servers.")]
public bool Verbose { get; set; } = true;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
/// <param name="args">The command-line arguments the application was invoked with.</param>
static void Main(string[] args)
{
// Parse our command line arguments.
Parser.Default.ParseArguments<CliOptions>(args).WithParsed(options =>
{
// Set our options globally
Options = options;
// Verify the database folder exists.
if(!Directory.Exists(options.DatabaseFolder))
{
Error("Provided database folder does not exist. You must specify a valid directory.");
return;
}
// Verify other arguments
if (options.Port < 0 || options.Port > ushort.MaxValue)
{
Error($"Provided port is invalid. You must a value between 1 and {ushort.MaxValue}.");
return;
}
// Create our file system storage and open it.
ServerStorage serverStorage = new FilesystemServerStorage(options.DatabaseFolder);
serverStorage.Open();
// Check if initial deployment needs to be performed.
// If the database folder is empty, we deploy all resources.
// If it is non-empty, but missing critical resources, we ask whether to clear all resources but accounts.
bool allCriticalResourcesExist = serverStorage.AccessControlList.Exists() && serverStorage.ChannelInfo.Exists() && serverStorage.LoginSettings.Exists() && serverStorage.SymbolCache.Exists();
bool anyCriticalResourcesExist = serverStorage.AccessControlList.Exists() || serverStorage.ChannelInfo.Exists() || serverStorage.LoginSettings.Exists() || serverStorage.SymbolCache.Exists();
bool performInitialSetup = !allCriticalResourcesExist;
if (performInitialSetup && anyCriticalResourcesExist)
{
Warning("Critical resources are missing from storage, but storage is non-empty.\n" +
"Would you like to re-deploy initial setup resources? Warning: this will clear all storage except accounts! [y/N]");
performInitialSetup = Console.ReadKey(true).Key == ConsoleKey.Y;
}
// Perform initial setup of server resources if needed.
if (performInitialSetup)
{
Info("[SERVER] Performing initial setup: server resources to database folder..");
InitialDeployment.PerformInitialDeployment(serverStorage, options.GameExecutablePath, false);
}
// Create a server instance
Server = new Server(serverStorage,
new ServerSettings(
port: (ushort)options.Port,
serverDbApiKey: options.ServerDBApiKey,
serverDBValidateServerEndpoint: options.ServerDBValidateGameServers,
serverDBValidateServerEndpointTimeout: options.ServerDBValidateGameServersTimeout,
favorPopulationOverPing: !options.LowPingMatching,
forceIntoAnySessionIfCreationFails: options.ForceMatching
)
);
// Set up all event handlers.
Server.OnServerStarted += Server_OnServerStarted;
Server.OnServerStopped += Server_OnServerStopped;
Server.OnAuthorizationResult += Server_OnAuthorizationResult;
Server.OnServicePeerConnected += Server_OnServicePeerConnected;
Server.OnServicePeerDisconnected += Server_OnServicePeerDisconnected;
Server.OnServicePeerAuthenticated += Server_OnServicePeerAuthenticated;
Server.ServerDBService.Registry.OnGameServerRegistered += Registry_OnGameServerRegistered;
Server.ServerDBService.Registry.OnGameServerUnregistered += Registry_OnGameServerUnregistered;
Server.ServerDBService.OnGameServerRegistrationFailure += ServerDBService_OnGameServerRegistrationFailure;
// Set up all verbose event handlers.
if (options.Verbose)
{
Server.OnServicePacketSent += Server_OnServicePacketSent;
Server.OnServicePacketReceived += Server_OnServicePacketReceived;
}
// Start the server.
Server.Start().Wait();
});
}
private static void Server_OnServerStarted(Server server)
{
// Print our server started message
Info("[SERVER] Server started");
// Print our service config.
Core.Game.ServiceConfig serviceConfig = server.Settings.GenerateServiceConfig(server.PublicIPAddress?.ToString() ?? "localhost", serverConfig: true);
string serviceConfigSerialized = JsonConvert.SerializeObject(serviceConfig, Formatting.Indented, StreamIO.JsonSerializerSettings);
Info($"[SERVER] Generated service config:\n{serviceConfigSerialized}");
// Copy the service config to the clipboard if required.
if (Options?.OutputConfigPath != null)
{
// Save the service config to the provided file path.
try
{
File.WriteAllText(Options!.OutputConfigPath, serviceConfigSerialized);
Info($"[SERVER] Output generated service config to path \"{Options!.OutputConfigPath}\"");
}
catch(Exception ex)
{
Error($"[SERVER] Failed to output generated service config to path \"{Options!.OutputConfigPath}\":\n{ex}");
}
}
// Start the peer stats update timer
startedTime = DateTime.UtcNow;
peerStatsUpdateTimer = new System.Timers.Timer(Options!.StatsUpdateInterval);
peerStatsUpdateTimer.Start();
peerStatsUpdateTimer.Elapsed += PeerStatsUpdateTimer_Elapsed;
}
private static void PeerStatsUpdateTimer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
{
Info($"[PEERSTATS] " +
$"elapsed: {(DateTime.UtcNow - startedTime)}, " +
$"gameservers: {Server.ServerDBService.Registry.RegisteredGameServers.Count}, " +
$"login: {Server.LoginService.Peers.Count}, " +
$"config: {Server.ConfigService.Peers.Count}, " +
$"matching: {Server.MatchingService.Peers.Count}, " +
$"serverdb: {Server.ServerDBService.Peers.Count}, " +
$"transaction: {Server.TransactionService.Peers.Count}"
);
}
private static void Server_OnServerStopped(Server server)
{
// Stop the update timer.
peerStatsUpdateTimer?.Stop();
// Print our server stopped message
Info("[SERVER] Server stopped");
}
private static void Server_OnAuthorizationResult(Server server, System.Net.IPEndPoint client, bool authorized)
{
if(!authorized)
Error($"[SERVER] client({client.Address}:{client.Port}) failed authorization");
}
private static void Server_OnServicePeerConnected(Core.Server.Services.Service service, Core.Server.Services.Peer peer)
{
Info($"[{service.Name}] client({peer.Address}:{peer.Port}) connected");
}
private static void Server_OnServicePeerDisconnected(Core.Server.Services.Service service, Core.Server.Services.Peer peer)
{
Info($"[{service.Name}] client({peer.Address}:{peer.Port}) disconnected");
}
private static void Server_OnServicePeerAuthenticated(Core.Server.Services.Service service, Core.Server.Services.Peer peer, Core.Game.XPlatformId userId)
{
}
private static void Registry_OnGameServerRegistered(Core.Server.Services.ServerDB.RegisteredGameServer gameServer)
{
Info($"[{gameServer.Peer.Service.Name}] client({gameServer.Peer.Address}:{gameServer.Peer.Port}) registered game server (server_id={gameServer.ServerId}, region_symbol={gameServer.RegionSymbol}, version_lock={gameServer.VersionLock}, endpoint=<{gameServer.ExternalAddress}:{gameServer.Port}>)");
}
private static void Registry_OnGameServerUnregistered(Core.Server.Services.ServerDB.RegisteredGameServer gameServer)
{
Info($"[{gameServer.Peer.Service.Name}] client({gameServer.Peer.Address}:{gameServer.Peer.Port}) unregistered game server (server_id={gameServer.ServerId}, region_symbol={gameServer.RegionSymbol}, version_lock={gameServer.VersionLock}, endpoint=<{gameServer.ExternalAddress}:{gameServer.Port}>)");
}
private static void ServerDBService_OnGameServerRegistrationFailure(Peer peer, Core.Server.Messages.ServerDB.ERGameServerRegistrationRequest registrationRequest, string failureMessage)
{
Error($"[{peer.Service.Name}] client({peer.Address}:{peer.Port}) failed to register game server: \"{failureMessage}\"");
}
private static string getPacketDisplayString(Packet packet)
{
// Add every message to our result.
string result = "";
for(int i = 0; i < packet.Count; i++)
{
if (i == packet.Count - 1)
result += $"\t{packet[i]}";
else
result += $"\t{packet[i]}\n";
}
return result;
}
private static void Server_OnServicePacketSent(Core.Server.Services.Service service, Core.Server.Services.Peer sender, Core.Server.Messages.Packet packet)
{
Debug($"[{service.Name}] server->client({sender.Address}:{sender.Port}\n" + getPacketDisplayString(packet));
}
private static void Server_OnServicePacketReceived(Core.Server.Services.Service service, Core.Server.Services.Peer sender, Core.Server.Messages.Packet packet)
{
Debug($"[{service.Name}] client({sender.Address}:{sender.Port})->server:\n" + getPacketDisplayString(packet));
}
private static void Error(string msg)
{
lock (_printLock)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(msg);
Console.ResetColor();
}
}
private static void Warning(string msg)
{
lock (_printLock)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(msg);
Console.ResetColor();
}
}
private static void Info(string msg)
{
lock (_printLock)
{
Console.ResetColor();
Console.WriteLine(msg);
}
}
private static void Debug(string msg)
{
lock (_printLock)
{
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine(msg);
Console.ResetColor();
}
}
}
}