-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
388 lines (321 loc) · 12 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
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
using System.Diagnostics;
using System.Text.RegularExpressions;
using BanchoNET.Commands;
using BanchoNET.Models;
using BanchoNET.Models.Dtos;
using BanchoNET.Objects;
using BanchoNET.Objects.Channels;
using BanchoNET.Objects.Players;
using BanchoNET.Objects.Privileges;
using BanchoNET.Services;
using BanchoNET.Services.ClientPacketsHandler;
using BanchoNET.Services.Repositories;
using BanchoNET.Utils;
using dotenv.net;
using Hangfire;
using Hangfire.MySql;
using Microsoft.EntityFrameworkCore;
using MongoDB.Driver;
using MySql.Data.MySqlClient;
using StackExchange.Redis;
using static System.Data.IsolationLevel;
using IsolationLevel = System.Transactions.IsolationLevel;
namespace BanchoNET;
public class Program
{
public static void Main(string[] args)
{
var initStopwatch = new Stopwatch();
initStopwatch.Start();
var builder = WebApplication.CreateBuilder(args);
DotEnv.Load(); // Load .env when non dockerized
#region Domain Check
var domain = AppSettings.Domain;
if (string.IsNullOrEmpty(domain))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[Init] Please set the DOMAIN environment variable.");
Console.ForegroundColor = ConsoleColor.White;
return;
}
#endregion
#region Environment Variables Initialization
var requiredEnvVars = new[]
{
"MYSQL_HOST",
"MYSQL_PORT",
"MYSQL_USER",
"MYSQL_DB",
"HANGFIRE_HOST",
"HANGFIRE_PORT",
"HANGFIRE_USER",
"REDIS_HOST",
"REDIS_PORT",
"MONGO_HOST",
"MONGO_PORT",
};
var missing = false;
foreach (var requiredEnvVar in requiredEnvVars)
{
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(requiredEnvVar))) continue;
Console.WriteLine($"[Init] Missing environment variable: {requiredEnvVar}");
missing = true;
}
if (missing) return;
var dbConnections = new DbConnectionsModel
{
MysqlHost = Environment.GetEnvironmentVariable("MYSQL_HOST")!,
MysqlPort = Environment.GetEnvironmentVariable("MYSQL_PORT")!,
MysqlUser = Environment.GetEnvironmentVariable("MYSQL_USER")!,
MysqlPass = Environment.GetEnvironmentVariable("MYSQL_PASS")!,
MysqlDb = Environment.GetEnvironmentVariable("MYSQL_DB")!,
HangfireHost = Environment.GetEnvironmentVariable("HANGFIRE_HOST")!,
HangfirePort = Environment.GetEnvironmentVariable("HANGFIRE_PORT")!,
HangfireUser = Environment.GetEnvironmentVariable("HANGFIRE_USER")!,
HangfirePass = Environment.GetEnvironmentVariable("HANGFIRE_PASS")!,
RedisHost = Environment.GetEnvironmentVariable("REDIS_HOST")!,
RedisPort = Environment.GetEnvironmentVariable("REDIS_PORT")!,
RedisPass = Environment.GetEnvironmentVariable("REDIS_PASS")!,
MongoHost = Environment.GetEnvironmentVariable("MONGO_HOST")!,
MongoPort = Environment.GetEnvironmentVariable("MONGO_PORT")!,
MongoUser = Environment.GetEnvironmentVariable("MONGO_USER")!,
MongoPass = Environment.GetEnvironmentVariable("MONGO_PASS")!,
};
var mySqlConnectionString =
$"server={dbConnections.MysqlHost};" +
$"port={dbConnections.MysqlPort};" +
$"user={dbConnections.MysqlUser};" +
$"password={dbConnections.MysqlPass};";
EnsureHangfireDatabaseExists(mySqlConnectionString);
mySqlConnectionString += $"database={dbConnections.MysqlDb};";
var hangfirePass = dbConnections.HangfirePass;
var hangfireConnectionString =
$"server={dbConnections.HangfireHost};" +
$"port={dbConnections.HangfirePort};" +
$"user={dbConnections.HangfireUser};" +
$"{(string.IsNullOrEmpty(hangfirePass) ? "" : $"password={hangfirePass};")}" +
$"database=hangfire;" +
$"Allow User Variables=True";;
var redisConnectionString =
$"{dbConnections.RedisHost}:{dbConnections.RedisPort}," +
$"password={dbConnections.RedisPass}," +
$"allowAdmin=true";
var credentials = true;
var mongoUser = dbConnections.MongoUser;
var mongoPass = dbConnections.MongoPass;
if (string.IsNullOrEmpty(mongoUser)
&& !string.IsNullOrEmpty(mongoPass))
{
Console.WriteLine("[Init] You specified password but left username empty for MongoDB. Ignoring password.");
credentials = false;
}
else if (string.IsNullOrEmpty(mongoUser)
&& string.IsNullOrEmpty(mongoPass))
{
Console.WriteLine("[Init] No credentials specified for MongoDB.");
credentials = false;
}
else
{
mongoUser = EscapeMongoCharacters(mongoUser);
mongoPass = string.IsNullOrEmpty(mongoPass)
? ""
: EscapeMongoCharacters(mongoPass);
}
var mongoConnectionString =
$"mongodb://" +
$"{(credentials ? $"{mongoUser}:{mongoPass}@" : "")}" +
$"{dbConnections.MongoHost}:{dbConnections.MongoPort}";
#endregion
builder.Services.AddHangfire(config =>
{
config.UseStorage(new MySqlStorage(hangfireConnectionString, new MySqlStorageOptions
{
TransactionIsolationLevel = (IsolationLevel?)ReadCommitted,
QueuePollInterval = TimeSpan.FromSeconds(15),
JobExpirationCheckInterval = TimeSpan.FromHours(1),
CountersAggregateInterval = TimeSpan.FromMinutes(5),
PrepareSchemaIfNecessary = true,
}));
});
builder.Services.AddHangfireServer();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddAuthorization();
builder.Services.AddControllers();
var mongoSettings = MongoClientSettings.FromConnectionString(mongoConnectionString);
mongoSettings.LinqProvider = MongoDB.Driver.Linq.LinqProvider.V3;
builder.Services.AddSingleton<IConnectionMultiplexer>(ConnectionMultiplexer.Connect(redisConnectionString));
builder.Services.AddSingleton(new MongoClient(mongoSettings));
builder.Services.AddSingleton<HistoriesRepository>();
builder.Services.AddSingleton<OsuVersionService>();
builder.Services.AddSingleton<BackgroundTasks>();
builder.Services.AddDbContext<BanchoDbContext>(options =>
{
options.UseMySQL(mySqlConnectionString);
});
builder.Services.AddScoped<BeatmapsRepository>();
builder.Services.AddScoped<ClientRepository>();
builder.Services.AddScoped<MessagesRepository>();
builder.Services.AddScoped<PlayersRepository>();
builder.Services.AddScoped<ScoresRepository>();
builder.Services.AddScoped<GeolocService>();
builder.Services.AddScoped<BeatmapHandler>();
builder.Services.AddScoped<CommandProcessor>();
builder.Services.AddScoped<ClientPacketsHandler>();
builder.Services.AddHttpClient();
var app = builder.Build();
app.UseHangfireDashboard();
app.UseHttpsRedirection();
app.UseAuthorization();
app.UseMiddleware<SubdomainMiddleware>();
app.MapControllers();
app.Use(async (context, next) =>
{
context.Response.ApplyHeaders();
var stopwatch = new Stopwatch();
stopwatch.Start();
await next(context);
stopwatch.Stop();
//TODO some fancy coloring
Console.WriteLine($"[{context.Request.Method} {context.Response.StatusCode}]\t{context.Request.Host}{context.Request.Path} | Request took: {stopwatch.Elapsed.Seconds}s {stopwatch.Elapsed.Milliseconds}ms {stopwatch.Elapsed.Microseconds}μs");
});
#region Initialization
if (string.IsNullOrEmpty(AppSettings.OsuApiKey))
{
//TODO: Logging system || probably to change
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("[Init] OSU_API_KEY is not set. Some features will be disabled.");
Console.ForegroundColor = ConsoleColor.White;
}
EnsureDatabaseExists(app.Services.CreateScope());
InitBanchoBot(app.Services.CreateScope());
InitChannels(app.Services.CreateScope());
// Even if redis creates snapshots of rankings it isn't
// always 100% accurate with database so we need to update
// redis leaderboards on startup
InitRedis(app.Services.CreateScope(), dbConnections.RedisHost, dbConnections.RedisPort);
app.Services.GetRequiredService<OsuVersionService>().Init().Wait();
app.Services.GetRequiredService<BackgroundTasks>().Init().Wait();
#endregion
initStopwatch.Stop();
Console.WriteLine($"[Init] Initialization took: {initStopwatch.Elapsed}");
app.Run();
}
private static void EnsureHangfireDatabaseExists(string connectionString)
{
Console.WriteLine("[Init] Checking if Hangfire database exists.");
using var connection = new MySqlConnection(connectionString);
using var command = connection.CreateCommand();
connection.Open();
command.CommandText = "CREATE DATABASE IF NOT EXISTS `hangfire`";
command.ExecuteNonQuery();
}
private static void EnsureDatabaseExists(IServiceScope scope)
{
var db = scope.ServiceProvider.GetRequiredService<BanchoDbContext>();
Console.WriteLine("[Init] Checking if database exists.");
var created = db.Database.EnsureCreated();
Console.WriteLine(created
? "[Init] Database couldn't be found and was created."
: "[Init] Database already exists, creation was skipped.");
}
private static void InitBanchoBot(IServiceScope scope)
{
var db = scope.ServiceProvider.GetRequiredService<BanchoDbContext>();
var dbBanchoBot = db.Players.FirstOrDefault(p => p.Id == 1);
if (dbBanchoBot != null)
{
dbBanchoBot.RemainingSupporter = DateTime.Now.AddYears(100);
dbBanchoBot.LastActivityTime = DateTime.Now.AddYears(100);
db.SaveChanges();
BanchoSession.Instance.AppendBot(new Player(dbBanchoBot));
return;
}
var banchoBotName = AppSettings.BanchoBotName;
if (banchoBotName.Length > 16)
{
Console.WriteLine("[Init] Bancho bot name is too long, truncating to 16 characters");
banchoBotName = banchoBotName[..16];
Console.WriteLine("[Init] New bot name: " + banchoBotName);
}
var entry = db.Players.Add(new PlayerDto
{
Id = 1,
Username = banchoBotName,
SafeName = banchoBotName.MakeSafe(),
LoginName = banchoBotName.MakeSafe(),
Email = "ban@cho.bot",
PasswordHash = "1",
Country = "a2",
RemainingSupporter = DateTime.Now.AddYears(100),
CreationTime = DateTime.Now,
LastActivityTime = DateTime.Now.AddYears(100),
Privileges = (int)(Privileges.Verified | Privileges.Staff | Privileges.Unrestricted),
});
db.SaveChanges();
BanchoSession.Instance.AppendBot(new Player(entry.Entity));
}
private static void InitChannels(IServiceScope scope)
{
var db = scope.ServiceProvider.GetRequiredService<BanchoDbContext>();
var session = BanchoSession.Instance;
if (!db.Channels.Any())
{
Console.WriteLine("[Init] No channels found in database, creating default ones.");
foreach (var channel in ChannelExtensions.DefaultChannels)
{
db.Channels.Add(new ChannelDto
{
Name = channel.Name,
Description = channel.Description,
Hidden = channel.Hidden,
AutoJoin = channel.AutoJoin,
ReadOnly = channel.ReadOnly,
ReadPrivileges = (int)channel.ReadPrivileges,
WritePrivileges = (int)channel.WritePrivileges
});
session.InsertChannel(channel);
}
db.SaveChanges();
}
else
{
Console.WriteLine("[Init] Loading channels from database.");
foreach (var channel in db.Channels.ToList())
session.InsertChannel(new Channel(channel));
}
}
private static void InitRedis(IServiceScope scope, string redisHost, string redisPort)
{
var db = scope.ServiceProvider.GetRequiredService<BanchoDbContext>();
var redis = scope.ServiceProvider.GetRequiredService<IConnectionMultiplexer>();
var redisDb = redis.GetDatabase();
var stopwatch = new Stopwatch();
stopwatch.Start();
redis.GetServer($"{redisHost}:{redisPort}").FlushDatabase();
for (byte i = 0; i <= (byte)GameMode.AutopilotStd; i++)
{
if (i == 7) continue;
var mode = i;
var playersPpModeValues = db.Stats.Include(s => s.Player)
.Where(s => s.Mode == mode &&
(s.Player.Privileges & 1) == 1)
.Select(s => new { s.PlayerId, s.Player.Country, s.PP })
.ToArray();
foreach (var values in playersPpModeValues)
{
redisDb.SortedSetAdd($"bancho:leaderboard:{mode}", values.PlayerId, values.PP);
redisDb.SortedSetAdd($"bancho:leaderboard:{mode}:{values.Country}", values.PlayerId, values.PP);
}
}
stopwatch.Stop();
Console.WriteLine($"[Init] Redis leaderboards updated in {stopwatch.ElapsedMilliseconds}ms");
}
private static string EscapeMongoCharacters(string input)
{
return Regex.Replace(
Uri.EscapeDataString(input),
@"[$:/?$[\]@]",
m => Uri.HexEscape(Convert.ToChar(m.Value[0].ToString())));
}
}