Skip to content

Commit

Permalink
Merge pull request #97 from Gml-Launcher/develop
Browse files Browse the repository at this point in the history
Update to v1.0.3
  • Loading branch information
GamerVII-NET authored Dec 31, 2024
2 parents dfff83a + 680cff6 commit 2ecc468
Show file tree
Hide file tree
Showing 25 changed files with 468 additions and 141 deletions.
2 changes: 1 addition & 1 deletion src/Gml.Web.Api.Domains/Integrations/AzuriomAuthResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class AzuriomAuthResult
public bool EmailVerified { get; set; }

[JsonProperty("money")]
public int Money { get; set; }
public decimal Money { get; set; }

[JsonProperty("banned")]
public bool Banned { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions src/Gml.Web.Api.Domains/Integrations/UnicoreAuthResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public class User
public string TwoFactorSecretTemp { get; set; }

[JsonProperty("real")]
public int Real { get; set; }
public decimal Real { get; set; }

[JsonProperty("virtual")]
public int Virtual { get; set; }
public decimal Virtual { get; set; }

[JsonProperty("perms")]
public object Perms { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/Gml.Web.Api.Dto/Minecraft/AuthLib/Textures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Gml.Web.Api.Dto.Minecraft.AuthLib;

public class Textures
{
[JsonProperty("SKIN")] public SkinCape Skin { get; set; }
[JsonProperty("SKIN", NullValueHandling = NullValueHandling.Ignore)] public SkinCape Skin { get; set; }

[JsonProperty("CAPE", NullValueHandling = NullValueHandling.Ignore)] public SkinCape Cape { get; set; }
}
12 changes: 12 additions & 0 deletions src/Gml.Web.Api.Dto/Player/AuthUserHistoryDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Gml.Web.Api.Dto.Player;

public class AuthUserHistoryDto
{
public DateTime Date { get; set; }
public string Device { get; set; }
public string? Address { get; set; }
public string Protocol { get; set; }
public string? Hwid { get; set; }
}
10 changes: 10 additions & 0 deletions src/Gml.Web.Api.Dto/Player/ExtendedPlayerReadDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace Gml.Web.Api.Dto.Player;

public class ExtendedPlayerReadDto : PlayerReadDto
{
public bool IsBanned { get; set; }
public List<AuthUserHistoryDto> AuthHistory { get; set; } = new();
public List<ServerJoinHistoryDto> ServerJoinHistory { get; set; } = new();
}
2 changes: 1 addition & 1 deletion src/Gml.Web.Api.Dto/Player/PlayerReadDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace Gml.Web.Api.Dto.Player;

public class PlayerReadDto : PlayerTextureDto
{
public string Uuid { get; set; }
public string Name { get; set; } = null!;
public string AccessToken { get; set; }
public string Uuid { get; set; }
public DateTime ExpiredDate { get; set; }
}
9 changes: 9 additions & 0 deletions src/Gml.Web.Api.Dto/Player/ServerJoinHistoryDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Gml.Web.Api.Dto.Player;

public record ServerJoinHistoryDto
{
public string ServerUuid { get; set; }
public DateTime Date { get; set; }
}
4 changes: 3 additions & 1 deletion src/Gml.Web.Api.Dto/Profile/ProfileReadInfoDto.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using Gml.Web.Api.Dto.Files;
using Gml.Web.Api.Dto.Player;
using GmlCore.Interfaces.Enums;
using GmlCore.Interfaces.System;

Expand All @@ -15,13 +16,14 @@ public class ProfileReadInfoDto
public string IconBase64 { get; set; }
public string Description { get; set; }
public string Arguments { get; set; }
public string IsEnabled { get; set; }
public bool IsEnabled { get; set; }
public string JvmArguments { get; set; }
public string GameArguments { get; set; }
public bool HasUpdate { get; set; }
public ProfileState State { get; set; }
public List<ProfileFileReadDto> Files { get; set; }
public List<ProfileFolderReadDto> WhiteListFolders { get; set; }
public List<ProfileFileReadDto> WhiteListFiles { get; set; }
public List<PlayerReadDto> UsersWhiteList { get; set; }
public string Background { get; set; }
}
182 changes: 133 additions & 49 deletions src/Gml.Web.Api/Core/Extensions/EndpointsExtensions.cs

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions src/Gml.Web.Api/Core/Handlers/AuthIntegrationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ public static async Task<IResult> Auth(
authResult.Uuid,
context.Request.Headers["X-HWID"]);

if (player.IsBanned)
{
return Results.BadRequest(ResponseMessage.Create(
"Пользователь заблокирован!",
HttpStatusCode.BadRequest));
}

await gmlManager.Profiles.CreateUserSessionAsync(null, player);

player.TextureSkinUrl ??= (await gmlManager.Integrations.GetSkinServiceAsync())
Expand Down Expand Up @@ -116,6 +123,13 @@ public static async Task<IResult> AuthWithToken(
{
var player = user;

if (player.IsBanned)
{
return Results.BadRequest(ResponseMessage.Create(
"Пользователь заблокирован!",
HttpStatusCode.BadRequest));
}

player.TextureSkinUrl ??= (await gmlManager.Integrations.GetSkinServiceAsync())
.Replace("{userName}", player.Name)
.Replace("{userUuid}", player.Uuid);
Expand Down
6 changes: 6 additions & 0 deletions src/Gml.Web.Api/Core/Handlers/IPlayersHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Gml.Web.Api.Core.Handlers;

public interface IPlayersHandler
{

}
4 changes: 2 additions & 2 deletions src/Gml.Web.Api/Core/Handlers/MinecraftHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static async Task<IResult> HasJoined(HttpContext context, IGmlManager gml
{
var user = await gmlManager.Users.GetUserByName(userName);

if (user is null || string.IsNullOrEmpty(userName) || await gmlManager.Users.CanJoinToServer(user, serverId) == false)
if (user is null || string.IsNullOrEmpty(userName) || user.IsBanned || await gmlManager.Users.CanJoinToServer(user, serverId) == false)
return Results.NoContent();

var profile = new Profile
Expand Down Expand Up @@ -147,7 +147,7 @@ public static async Task<IResult> GetProfile(HttpContext context, IGmlManager gm

var user = await gmlManager.Users.GetUserByUuid(guidUuid);

if (user is null || string.IsNullOrEmpty(guidUuid)) return Results.NoContent();
if (user is null || string.IsNullOrEmpty(guidUuid) || user.IsBanned) return Results.NoContent();

var profile = new Profile
{
Expand Down
64 changes: 64 additions & 0 deletions src/Gml.Web.Api/Core/Handlers/PlayersHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Net;
using AutoMapper;
using Gml.Web.Api.Dto.Messages;
using Gml.Web.Api.Dto.Player;
using GmlCore.Interfaces;

namespace Gml.Web.Api.Core.Handlers;

public class PlayersHandler : IPlayersHandler
{
public static async Task<IResult> GetPlayers(IGmlManager gmlManager, IMapper mapper, int? take, int? offset, string? findName)
{
var players = await gmlManager.Users.GetUsers(take ?? 20, offset ?? 0, findName ?? string.Empty);

return Results.Ok(ResponseMessage.Create(mapper.Map<List<ExtendedPlayerReadDto>>(players), "Список пользователей успешно получен", HttpStatusCode.OK));
}

public static async Task<IResult> BanPlayer(
IGmlManager gmlManager,
IMapper mapper,
IList<string> playerUuids,
bool deviceBlock = false)
{
if (!playerUuids.Any())
{
return Results.BadRequest(ResponseMessage.Create("Не передан ни один пользователь для блокировки",
HttpStatusCode.BadRequest));
}

foreach (var playerUuid in playerUuids)
{
var player = await gmlManager.Users.GetUserByUuid(playerUuid);

if (player is null) continue;
player.IsBanned = true;
await gmlManager.Users.UpdateUser(player);
}

return Results.Ok(ResponseMessage.Create("Пользователь(и) успешно заблокированы", HttpStatusCode.OK));
}

public static async Task<IResult> PardonPlayer(
IGmlManager gmlManager,
IMapper mapper,
IList<string> playerUuids)
{
if (!playerUuids.Any())
{
return Results.BadRequest(ResponseMessage.Create("Не передан ни один пользователь для блокировки",
HttpStatusCode.BadRequest));
}

foreach (var playerUuid in playerUuids)
{
var player = await gmlManager.Users.GetUserByUuid(playerUuid);

if (player is null) continue;
player.IsBanned = false;
await gmlManager.Users.UpdateUser(player);
}

return Results.Ok(ResponseMessage.Create("Пользователь(и) успешно разблокированы", HttpStatusCode.OK));
}
}
102 changes: 99 additions & 3 deletions src/Gml.Web.Api/Core/Handlers/ProfileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Gml.Web.Api.Domains.Exceptions;
using Gml.Web.Api.Domains.System;
using Gml.Web.Api.Dto.Messages;
using Gml.Web.Api.Dto.Player;
using Gml.Web.Api.Dto.Profile;
using GmlCore.Interfaces;
using GmlCore.Interfaces.Enums;
Expand All @@ -27,7 +28,36 @@ public static async Task<IResult> GetProfiles(
IMapper mapper,
IGmlManager gmlManager)
{
var profiles = await gmlManager.Profiles.GetProfiles();
IEnumerable<IGameProfile> profiles = [];

if (context.User.IsInRole("Player"))
{
var userName = context.User.Identity?.Name;

if (string.IsNullOrEmpty(userName))
{
return Results.BadRequest(ResponseMessage.Create("Не удалось идентифицировать пользователя",
HttpStatusCode.BadRequest));
}

var user = await gmlManager.Users.GetUserByName(userName);

if (user is null)
{
return Results.BadRequest(ResponseMessage.Create("Не удалось идентифицировать пользователя",
HttpStatusCode.BadRequest));
}

profiles = (await gmlManager.Profiles.GetProfiles())
.Where(c =>
c is { IsEnabled: true, UserWhiteListGuid.Count: 0 } ||
c.UserWhiteListGuid.Any(g => g.Equals(user.Uuid)));

}else if (context.User.IsInRole("Admin"))
{
profiles = await gmlManager.Profiles.GetProfiles();
}

var gameProfiles = profiles as IGameProfile[] ?? profiles.ToArray();

var dtoProfiles = mapper.Map<ProfileReadDto[]>(profiles);
Expand Down Expand Up @@ -270,11 +300,14 @@ public static async Task<IResult> GetProfileInfo(

var user = await gmlManager.Users.GetUserByName(createInfoDto.UserName);

if (user is null || user.AccessToken != token)
if (user is null || user.AccessToken != token || user.IsBanned)
{
return Results.StatusCode(StatusCodes.Status403Forbidden);
}

if (profile.UserWhiteListGuid.Count != 0 && !profile.UserWhiteListGuid.Any(c => c.Equals(user.Uuid, StringComparison.OrdinalIgnoreCase)))
return Results.Forbid();

user.Manager = gmlManager;

var profileInfo = await gmlManager.Profiles.GetProfileInfo(profile.Name, new StartupOptions
Expand Down Expand Up @@ -341,11 +374,15 @@ public static async Task<IResult> GetProfileDetails(
MinimumRamMb = createInfoDto.RamSize,
OsName = osName,
OsArch = createInfoDto.OsArchitecture
},user);
}, user);

var whiteListPlayers = await gmlManager.Users.GetUsers(profile.UserWhiteListGuid);

var profileDto = mapper.Map<ProfileReadInfoDto>(profileInfo);

profileDto.Background = $"{context.Request.Scheme}://{context.Request.Host}/api/v1/file/{profile.BackgroundImageKey}";
profileDto.IsEnabled = profile.IsEnabled;
profileDto.UsersWhiteList = mapper.Map<List<PlayerReadDto>>(whiteListPlayers);

return Results.Ok(ResponseMessage.Create(profileDto, string.Empty, HttpStatusCode.OK));
}
Expand Down Expand Up @@ -384,4 +421,63 @@ public static async Task<IResult> RemoveProfile(

return Results.Ok(ResponseMessage.Create(message, HttpStatusCode.OK));
}

[Authorize]
public static async Task<IResult> AddPlayerToWhiteList(
IGmlManager gmlManager,
IMapper mapper,
string profileName,
string userUuid)
{
var profile = await gmlManager.Profiles.GetProfile(profileName);

if (profile is null)
return Results.NotFound(ResponseMessage.Create($"Профиль \"{profileName}\" не найден",
HttpStatusCode.NotFound));

var user = await gmlManager.Users.GetUserByUuid(userUuid);

if (user is null)
return Results.NotFound(ResponseMessage.Create($"Пользователь с UUID: \"{userUuid}\" не найден",
HttpStatusCode.NotFound));

if (profile.UserWhiteListGuid.Any(c => c.Equals(userUuid)))
return Results.BadRequest(ResponseMessage.Create($"Пользователь с UUID: \"{userUuid}\" уже находится белом списке пользователей профиля",
HttpStatusCode.BadRequest));

profile.UserWhiteListGuid.Add(user.Uuid);
await gmlManager.Profiles.SaveProfiles();

var mappedUser = mapper.Map<PlayerReadDto>(user);

return Results.Ok(ResponseMessage.Create(mappedUser, "Пользователь успешно добавлен в белый список профиля", HttpStatusCode.OK));
}

[Authorize]
public static async Task<IResult> RemovePlayerFromWhiteList(
IGmlManager gmlManager,
string profileName,
string userUuid)
{
var profile = await gmlManager.Profiles.GetProfile(profileName);

if (profile is null)
return Results.NotFound(ResponseMessage.Create($"Профиль \"{profileName}\" не найден",
HttpStatusCode.NotFound));

var user = await gmlManager.Users.GetUserByUuid(userUuid);

if (user is null)
return Results.NotFound(ResponseMessage.Create($"Пользователь с UUID: \"{userUuid}\" не найден",
HttpStatusCode.NotFound));

if (!profile.UserWhiteListGuid.Any(c=> c.Equals(userUuid)))
return Results.BadRequest(ResponseMessage.Create($"Пользователь с UUID: \"{userUuid}\" не найден в белом списке пользователей профиля",
HttpStatusCode.BadRequest));

profile.UserWhiteListGuid.Remove(user.Uuid);
await gmlManager.Profiles.SaveProfiles();

return Results.Ok(ResponseMessage.Create("Пользователь успешно удален из белого списка профиля", HttpStatusCode.OK));
}
}
11 changes: 11 additions & 0 deletions src/Gml.Web.Api/Core/Handlers/TextureIntegrationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,15 @@ public static async Task<IResult> GetUserCloak(IGmlManager gmlManager, string te

return Results.Stream(await gmlManager.Users.GetCloak(user));
}

public static async Task<IResult> GetUserHead(IGmlManager gmlManager, string userUuid)
{
var user = await gmlManager.Users.GetUserByUuid(userUuid);

if (user is null)
return Results.NotFound(ResponseMessage.Create($"Пользователь с UUID: \"{userUuid}\" не найден",
HttpStatusCode.NotFound));

return Results.Stream(await gmlManager.Users.GetHead(user).ConfigureAwait(false));
}
}
Loading

0 comments on commit 2ecc468

Please sign in to comment.