Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
583 changes: 465 additions & 118 deletions GenOnlineService/Constants.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public override Type GetReturnType()
public string refresh_token { get; set; } = "";
public Int64 user_id { get; set; } = -1;
public string display_name { get; set; } = "";
public string ban_reason { get; set; } = "";

public string ws_uri { get; set; } = "";
}
Expand Down Expand Up @@ -176,12 +177,14 @@ public async Task<APIResult> Post_InternalHandler(string jsonData, string ipAddr
Int64 user_id = loginEntry.user_id;

// ban check
bool bIsBanned = await Database.Users.IsUserBanned(db, user_id);
if (bIsBanned)
UserBanStatus? banStatus = await Database.Users.GetUserBanStatus(db, user_id);
if (banStatus?.IsBanned == true)
{
await TokenRevocationManager.RevokeAllTokensForUser(user_id, "user is banned");
await ModerationManager.DisconnectUser(user_id, EModerationAction.Ban, banStatus.BanReason);

result.result = EPendingLoginState.LoginFailed;
result.ban_reason = banStatus.BanReason;
Response.StatusCode = (int)HttpStatusCode.Locked;
return result;
}
Expand Down
65 changes: 8 additions & 57 deletions GenOnlineService/Controllers/Lobbies/LobbiesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ public override Type GetReturnType()
public class LobbiesController : ControllerBase
{
private readonly ILogger<LobbiesController> _logger;
private static List<RoomData>? s_cachedRooms = null;
private static readonly object s_roomsLock = new object();

private readonly LobbyManager _lobbyManager;
private readonly IDbContextFactory<AppDbContext> _dbFactory;
Expand All @@ -75,23 +73,6 @@ public LobbiesController(LobbyManager lobbyManager, IDbContextFactory<AppDbConte
_dbFactory = dbFactory;
}

// Cache rooms.json data to avoid disk I/O on every request
private static async Task<List<RoomData>?> GetCachedRooms(JsonSerializerOptions options)
{
if (s_cachedRooms == null)
{
lock (s_roomsLock)
{
if (s_cachedRooms == null)
{
string strFileData = System.IO.File.ReadAllText(Path.Combine("data", "rooms.json"));
s_cachedRooms = JsonSerializer.Deserialize<List<RoomData>>(strFileData, options);
}
}
}
return await Task.FromResult(s_cachedRooms);
}

// FOR LATENCY ESTIMATIONS
// Convert degrees to radians
public static double ToRadians(double angleInDegrees)
Expand Down Expand Up @@ -139,22 +120,15 @@ public async Task<APIResult> Get()
using (var reader = new StreamReader(HttpContext.Request.Body))
{
string jsonData = await reader.ReadToEndAsync();
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};

try
{
// find our network room ID
Int16 networkRoomID = -1;
Int16 selectedRoomID = -1;
bool bIncludeAllNetworkRooms = false;
List<Lobby>? lstLobbies = null;
List<int> lstLatencies = new();

List<LatencyEntry> lstPlayerLatencies = new();



Int64 user_id = TokenHelper.GetUserID(this);
EUserSessionType sessionType = TokenHelper.GetSessionType(this);
if (user_id != -1 && SessionHelpers.SessionTypeHasAccessTo(sessionType, ESessionAccessType.ServerListReadOnly))
Expand All @@ -163,34 +137,14 @@ public async Task<APIResult> Get()

if (sourceData != null)
{
// Use cached rooms data
List<RoomData>? lstRooms = await GetCachedRooms(options);
if (lstRooms != null)
{
foreach (RoomData room in lstRooms)
{
if (room.id == sourceData.networkRoomID)
{
if (room.flags == ERoomFlags.ROOM_FLAGS_SHOW_ALL_MATCHES)
{
bIncludeAllNetworkRooms = true;
}

break;
}
}
}
else
{
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
}
selectedRoomID = sourceData.selectedNetworkRoomID;
}
else
{
bIncludeAllNetworkRooms = true;
}

lstLobbies = _lobbyManager.GetAllLobbies(networkRoomID, true, true, false, false, bIncludeAllNetworkRooms);
lstLobbies = _lobbyManager.GetAllLobbies(selectedRoomID, true, true, false, false, bIncludeAllNetworkRooms);

List<Lobby> lstLobbiesToRemove = new();

Expand Down Expand Up @@ -257,17 +211,17 @@ public async Task<APIResult> Get()
}
else if (this.User.IsInRole("Monitor"))
{
networkRoomID = 0;
selectedRoomID = -1;
bIncludeAllNetworkRooms = true;

lstLobbies = _lobbyManager.GetAllLobbies(networkRoomID, true, true, true, true, bIncludeAllNetworkRooms);
lstLobbies = _lobbyManager.GetAllLobbies(selectedRoomID, true, true, true, true, bIncludeAllNetworkRooms);
}
else
{
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
}


result.lobbies = lstLobbies;
result.latencies = lstLatencies;
result.playerlatencies = lstPlayerLatencies;
Expand Down Expand Up @@ -357,7 +311,7 @@ public async Task<APIResult> Put()
return result;
}



// get requesting user data from session token
Int64 user_id = TokenHelper.GetUserID(this);
Expand Down Expand Up @@ -388,9 +342,6 @@ public async Task<APIResult> Put()
result.result = 1;
result.lobby_id = newLobbyID;

// mark lobby list as dirty
await WebSocketManager.SendNewOrDeletedLobbyToAllNetworkRoomMembers(playerSession.networkRoomID);

// TODO: What if this fails? just let them proceed? just means only direct connect people will be able to play
// get some turn credentials
TURNCredentialContainer? turnCredentials = await TURNCredentialManager.CreateCredentialsForUser(user_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public override Type GetReturnType()
public string refresh_token { get; set; } = "";
public Int64 user_id { get; set; } = -1;
public string display_name { get; set; } = "";
public string ban_reason { get; set; } = "";

public string ws_uri { get; set; } = "";
}
Expand Down Expand Up @@ -129,13 +130,15 @@ public async Task<APIResult> Post_InternalHandler(string jsonData, string ipAddr
}

// ban check
bool bIsBanned = await Database.Users.IsUserBanned(db, user_id);
if (bIsBanned)
UserBanStatus? banStatus = await Database.Users.GetUserBanStatus(db, user_id);
if (banStatus?.IsBanned == true)
{
// kill every token they hold, not just this request
await TokenRevocationManager.RevokeAllTokensForUser(user_id, "user is banned");
await ModerationManager.DisconnectUser(user_id, EModerationAction.Ban, banStatus.BanReason);

result.result = EPendingLoginState.LoginFailed;
result.ban_reason = banStatus.BanReason;
Response.StatusCode = (int)HttpStatusCode.Locked;
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public override Type GetReturnType()
public string refresh_token { get; set; } = "";
public Int64 user_id { get; set; } = -1;
public string display_name { get; set; } = "";
public string ban_reason { get; set; } = "";
}

// Pure token rotation. Unlike LoginWithToken this does NOT establish a session - the caller's
Expand Down Expand Up @@ -97,13 +98,15 @@ public async Task<APIResult> Post_InternalHandler(string ipAddr)

// re-check the ban on every rotation so a ban applied since the last refresh takes
// effect immediately rather than waiting for the periodic reconcile
bool bIsBanned = await Database.Users.IsUserBanned(db, user_id);
if (bIsBanned)
UserBanStatus? banStatus = await Database.Users.GetUserBanStatus(db, user_id);
if (banStatus?.IsBanned == true)
{
// kill every token they hold, not just this request
await TokenRevocationManager.RevokeAllTokensForUser(user_id, "user is banned");
await ModerationManager.DisconnectUser(user_id, EModerationAction.Ban, banStatus.BanReason);

result.result = EPendingLoginState.LoginFailed;
result.ban_reason = banStatus.BanReason;
Response.StatusCode = (int)HttpStatusCode.Locked;
return result;
}
Expand Down
55 changes: 16 additions & 39 deletions GenOnlineService/Controllers/Rooms/RoomsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,60 +19,37 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Net.WebSockets;
using System.Text;
using System.Text.Json;

namespace GenOnlineService.Controllers
{
public class RouteHandler_GET_Rooms_Result : APIResult
{
public override Type GetReturnType()
{
return this.GetType();
}
public override Type GetReturnType() => GetType();

public List<RoomData>? rooms { get; set; } = null;
}
public List<RoomData> rooms { get; set; } = [];
public bool supports_moderation_commands { get; set; } = true;
public bool supports_room_selection_results { get; set; } = true;
}

[ApiController]
[Route("env/{environment}/contract/{contract_version}/[controller]")]
public class RoomsController : ControllerBase
{
private readonly ILogger<RoomsController> _logger;

public RoomsController(ILogger<RoomsController> logger)
{
_logger = logger;
}

[HttpGet(Name = "GetRooms")]
[Authorize(Roles = "GameClient,ChatClient,GameLauncher,Monitor")]
public async Task<APIResult> Get()
public APIResult Get()
{
RouteHandler_GET_Rooms_Result result = new RouteHandler_GET_Rooms_Result();

using (var reader = new StreamReader(HttpContext.Request.Body))
List<RoomData> rooms = RoomCatalog.Rooms.Select((room, index) => new RoomData
{
string jsonData = await reader.ReadToEndAsync();
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};

try
{
string strFileData = await System.IO.File.ReadAllTextAsync(Path.Combine("data", "rooms.json"));
List<RoomData>? lstRooms = JsonSerializer.Deserialize<List<RoomData>>(strFileData, options);
result.rooms = lstRooms;
}
catch
{
return result;
}

return result;
}
id = room.ID,
name = room.Name,
parent_id = room.ParentID,
flags = index == 0
? ERoomFlags.ROOM_FLAGS_SHOW_ALL_MATCHES
: ERoomFlags.ROOM_FLAGS_NONE
}).ToList();

return new RouteHandler_GET_Rooms_Result { rooms = rooms };
}
}
}
Loading
Loading