Implement LBP1 tags, stricter resource checking, and more (#463)

* Add LBP1 tags, more strict resource checking, and more.

* Fix unit tests

* Add more length checking to dependency parser

* Online editor problems

* Fix tests pt 2

* Self code review and fixed digest bugs

* Don't add content length if it was already set

* Fix status endpoint

* Fix review bug and simplify review serialization

* Fix a typo in review serialization

* Remove duplicated code and fix search

* Remove duplicate database call
This commit is contained in:
Josh 2022-08-31 20:38:58 -05:00 committed by GitHub
commit d640c000aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 735 additions and 209 deletions

View file

@ -7,7 +7,6 @@ using LBPUnion.ProjectLighthouse.Match.Rooms;
using LBPUnion.ProjectLighthouse.PlayerData;
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
using LBPUnion.ProjectLighthouse.Tickets;
using LBPUnion.ProjectLighthouse.Types;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@ -122,6 +121,8 @@ public class LoginController : ControllerBase
// and so we don't pick the same token up when logging in later.
token.Used = true;
user.LastLogin = TimeHelper.TimestampMillis;
await this.database.SaveChangesAsync();
// Create a new room on LBP2/3/Vita

View file

@ -0,0 +1,42 @@
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.PlayerData;
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Controllers;
[ApiController]
[Route("LITTLEBIGPLANETPS3_XML/goodbye")]
[Produces("text/xml")]
public class LogoutController : ControllerBase
{
private readonly Database database;
public LogoutController(Database database)
{
this.database = database;
}
[HttpPost]
public async Task<IActionResult> OnPost()
{
GameToken? token = await this.database.GameTokenFromRequest(this.Request);
if (token == null) return this.StatusCode(403, "");
User? user = await this.database.Users.Where(u => u.UserId == token.UserId).FirstOrDefaultAsync();
if (user == null) return this.StatusCode(403, "");
user.LastLogout = TimeHelper.TimestampMillis;
this.database.GameTokens.RemoveWhere(t => t.TokenId == token.TokenId);
this.database.LastContacts.RemoveWhere(c => c.UserId == token.UserId);
await this.database.SaveChangesAsync();
return this.Ok();
}
}

View file

@ -1,7 +1,7 @@
#nullable enable
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Levels;
using LBPUnion.ProjectLighthouse.PlayerData;
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@ -19,12 +19,17 @@ public class EnterLevelController : ControllerBase
this.database = database;
}
[HttpPost("play/user/{slotId}")]
public async Task<IActionResult> PlayLevel(int slotId)
[HttpPost("play/{slotType}/{slotId:int}")]
public async Task<IActionResult> PlayLevel(string slotType, int slotId)
{
GameToken? token = await this.database.GameTokenFromRequest(this.Request);
if (token == null) return this.StatusCode(403, "");
if (SlotHelper.IsTypeInvalid(slotType)) return this.BadRequest();
// don't count plays for developer slots
if (slotType == "developer") return this.Ok();
Slot? slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == slotId);
if (slot == null) return this.StatusCode(403, "");
@ -84,16 +89,20 @@ public class EnterLevelController : ControllerBase
}
// Only used in LBP1
[HttpGet("enterLevel/{id:int}")]
public async Task<IActionResult> EnterLevel(int id)
[HttpPost("enterLevel/{slotType}/{slotId:int}")]
public async Task<IActionResult> EnterLevel(string slotType, int slotId)
{
GameToken? token = await this.database.GameTokenFromRequest(this.Request);
if (token == null) return this.StatusCode(403, "");
Slot? slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == id);
if (SlotHelper.IsTypeInvalid(slotType)) return this.BadRequest();
if (slotType == "developer") return this.Ok();
Slot? slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == slotId);
if (slot == null) return this.NotFound();
IQueryable<VisitedLevel> visited = this.database.VisitedLevels.Where(s => s.SlotId == id && s.UserId == token.UserId);
IQueryable<VisitedLevel> visited = this.database.VisitedLevels.Where(s => s.SlotId == slotId && s.UserId == token.UserId);
VisitedLevel? v;
if (!visited.Any())
{
@ -101,7 +110,7 @@ public class EnterLevelController : ControllerBase
v = new VisitedLevel
{
SlotId = id,
SlotId = slotId,
UserId = token.UserId,
};
this.database.VisitedLevels.Add(v);

View file

@ -228,6 +228,20 @@ public class PhotosController : ControllerBase
this.database.PhotoSubjects.RemoveWhere(p => p.PhotoSubjectId == subjectId);
}
HashSet<string> photoResources = new(){photo.LargeHash, photo.SmallHash, photo.MediumHash, photo.PlanHash,};
foreach (string hash in photoResources)
{
if (System.IO.File.Exists(Path.Combine("png", $"{hash}.png")))
{
System.IO.File.Delete(Path.Combine("png", $"{hash}.png"));
}
if (System.IO.File.Exists(Path.Combine("r", hash)))
{
System.IO.File.Delete(Path.Combine("r", hash));
}
}
this.database.Photos.Remove(photo);
await this.database.SaveChangesAsync();
return this.Ok();

View file

@ -85,6 +85,12 @@ public class ResourcesController : ControllerBase
return this.Conflict();
}
if (!FileHelper.AreDependenciesSafe(file))
{
Logger.Warn($"File has unsafe dependencies (hash: {hash}, type: {file.FileType}", LogArea.Resources);
return this.Conflict();
}
string calculatedHash = file.Hash;
if (calculatedHash != hash)
{

View file

@ -1,14 +1,24 @@
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Levels;
using LBPUnion.ProjectLighthouse.PlayerData;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Controllers.Slots;
[ApiController]
[Route("LITTLEBIGPLANETPS3_XML/tags")]
[Route("LITTLEBIGPLANETPS3_XML")]
[Produces("text/plain")]
public class LevelTagsController : ControllerBase
{
[HttpGet]
private readonly Database database;
public LevelTagsController(Database database)
{
this.database = database;
}
[HttpGet("tags")]
public IActionResult Get()
{
string[] tags = Enum.GetNames(typeof(LevelTags));
@ -22,4 +32,32 @@ public class LevelTagsController : ControllerBase
return this.Ok(string.Join(",", tags));
}
[HttpPost("tag/{slotType}/{id:int}")]
public async Task<IActionResult> PostTag([FromForm] string t, [FromRoute] string slotType, [FromRoute] int id)
{
GameToken? token = await this.database.GameTokenFromRequest(this.Request);
if (token == null) return this.StatusCode(403, "");
Slot? slot = await this.database.Slots.Where(s => s.SlotId == id).FirstOrDefaultAsync();
if (slot == null) return this.BadRequest();
if (!LabelHelper.IsValidTag(t)) return this.BadRequest();
if (token.UserId == slot.CreatorId) return this.BadRequest();
if (slot.GameVersion != GameVersion.LittleBigPlanet1) return this.BadRequest();
if (slotType != "user") return this.BadRequest();
RatedLevel? rating = await this.database.RatedLevels.FirstOrDefaultAsync(r => r.UserId == token.UserId && r.SlotId == slot.SlotId);
if (rating == null) return this.BadRequest();
rating.TagLBP1 = t;
await this.database.SaveChangesAsync();
return this.Ok();
}
}

View file

@ -146,6 +146,8 @@ public class PublishController : ControllerBase
slot.GameVersion = slotVersion;
if (slotVersion == GameVersion.Unknown) slot.GameVersion = gameToken.GameVersion;
slot.AuthorLabels = LabelHelper.RemoveInvalidLabels(slot.AuthorLabels);
// Republish logic
if (slot.SlotId != 0)
{

View file

@ -5,10 +5,8 @@ using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Levels;
using LBPUnion.ProjectLighthouse.PlayerData;
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
using LBPUnion.ProjectLighthouse.PlayerData.Reviews;
using LBPUnion.ProjectLighthouse.Serialization;
using LBPUnion.ProjectLighthouse.Types;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@ -27,7 +25,7 @@ public class ReviewController : ControllerBase
}
// LBP1 rating
[HttpPost("rate/user/{slotId}")]
[HttpPost("rate/user/{slotId:int}")]
public async Task<IActionResult> Rate(int slotId, [FromQuery] int rating)
{
GameToken? token = await this.database.GameTokenFromRequest(this.Request);
@ -44,6 +42,7 @@ public class ReviewController : ControllerBase
SlotId = slotId,
UserId = token.UserId,
Rating = 0,
TagLBP1 = "",
};
this.database.RatedLevels.Add(ratedLevel);
}
@ -62,7 +61,7 @@ public class ReviewController : ControllerBase
GameToken? token = await this.database.GameTokenFromRequest(this.Request);
if (token == null) return this.StatusCode(403, "");
Slot? slot = await this.database.Slots.Include(s => s.Creator).Include(s => s.Location).FirstOrDefaultAsync(s => s.SlotId == slotId);
Slot? slot = await this.database.Slots.Include(s => s.Location).FirstOrDefaultAsync(s => s.SlotId == slotId);
if (slot == null) return this.StatusCode(403, "");
RatedLevel? ratedLevel = await this.database.RatedLevels.FirstOrDefaultAsync(r => r.SlotId == slotId && r.UserId == token.UserId);
@ -73,6 +72,7 @@ public class ReviewController : ControllerBase
SlotId = slotId,
UserId = token.UserId,
RatingLBP1 = 0,
TagLBP1 = "",
};
this.database.RatedLevels.Add(ratedLevel);
}
@ -113,7 +113,8 @@ public class ReviewController : ControllerBase
this.database.Reviews.Add(review);
}
review.Thumb = newReview.Thumb;
review.LabelCollection = newReview.LabelCollection;
review.LabelCollection = LabelHelper.RemoveInvalidLabels(newReview.LabelCollection);
review.Text = newReview.Text;
review.Deleted = false;
review.Timestamp = TimeHelper.UnixTimeMilliseconds();
@ -127,6 +128,7 @@ public class ReviewController : ControllerBase
SlotId = slotId,
UserId = token.UserId,
RatingLBP1 = 0,
TagLBP1 = "",
};
this.database.RatedLevels.Add(ratedLevel);
}
@ -170,7 +172,7 @@ public class ReviewController : ControllerBase
if (review == null) return current;
RatedReview? yourThumb = this.database.RatedReviews.FirstOrDefault(r => r.ReviewId == review.ReviewId && r.UserId == token.UserId);
return current + review.Serialize(null, yourThumb);
return current + review.Serialize(yourThumb);
}
);
string response = LbpSerializer.TaggedStringElement
@ -183,7 +185,7 @@ public class ReviewController : ControllerBase
"hint_start", pageStart + pageSize
},
{
"hint", reviewList.LastOrDefault()!.Timestamp // not sure
"hint", reviewList.LastOrDefault()?.Timestamp ?? 0
},
}
);
@ -222,7 +224,7 @@ public class ReviewController : ControllerBase
if (review == null) return current;
RatedReview? ratedReview = this.database.RatedReviews.FirstOrDefault(r => r.ReviewId == review.ReviewId && r.UserId == token.UserId);
return current + review.Serialize(null, ratedReview);
return current + review.Serialize(ratedReview);
}
);
@ -236,7 +238,7 @@ public class ReviewController : ControllerBase
"hint_start", pageStart
},
{
"hint", reviewList.LastOrDefault()!.Timestamp // Seems to be the timestamp of oldest
"hint", reviewList.LastOrDefault()?.Timestamp ?? 0 // Seems to be the timestamp of oldest
},
}
);

View file

@ -4,7 +4,6 @@ using System.Xml.Serialization;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Levels;
using LBPUnion.ProjectLighthouse.PlayerData;
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
using LBPUnion.ProjectLighthouse.Serialization;
using Microsoft.AspNetCore.Mvc;
@ -39,6 +38,12 @@ public class ScoreController : ControllerBase
Score? score = (Score?)serializer.Deserialize(new StringReader(bodyString));
if (score == null) return this.BadRequest();
if (score.PlayerIds.Length == 0) return this.BadRequest();
if (score.Points < 0) return this.BadRequest();
if (score.Type is > 4 or < 1) return this.BadRequest();
SanitizationHelper.SanitizeStringsInClass(score);
if (slotType == "developer") id = await SlotHelper.GetPlaceholderSlotId(this.database, id, SlotType.Developer);
@ -60,6 +65,9 @@ public class ScoreController : ControllerBase
case GameVersion.LittleBigPlanet3:
slot.PlaysLBP3Complete++;
break;
case GameVersion.LittleBigPlanetPSP: break;
case GameVersion.Unknown: break;
default: throw new ArgumentOutOfRangeException();
}
// Submit scores from all players in lobby
@ -110,7 +118,6 @@ public class ScoreController : ControllerBase
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
public async Task<IActionResult> TopScores(string slotType, int slotId, int type, [FromQuery] int pageStart = -1, [FromQuery] int pageSize = 5)
{
// Get username
GameToken? token = await this.database.GameTokenFromRequest(this.Request);
if (token == null) return this.StatusCode(403, "");

View file

@ -1,4 +1,5 @@
#nullable enable
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Levels;
using LBPUnion.ProjectLighthouse.PlayerData;
using LBPUnion.ProjectLighthouse.Serialization;
@ -20,7 +21,7 @@ public class SearchController : ControllerBase
[HttpGet("searchLBP3")]
public Task<IActionResult> SearchSlotsLBP3([FromQuery] int pageSize, [FromQuery] int pageStart, [FromQuery] string textFilter)
=> SearchSlots(textFilter, pageSize, pageStart, "results");
=> this.SearchSlots(textFilter, pageSize, pageStart, "results");
[HttpGet("search")]
public async Task<IActionResult> SearchSlots(
@ -41,8 +42,8 @@ public class SearchController : ControllerBase
string[] keywords = query.Split(" ");
IQueryable<Slot> dbQuery = this.database.Slots.Include(s => s.Creator)
.Include(s => s.Location)
IQueryable<Slot> dbQuery = this.database.Slots.ByGameVersion(gameToken.GameVersion, false, true)
.Where(s => s.Type == SlotType.User)
.OrderBy(s => !s.TeamPick)
.ThenByDescending(s => s.FirstUploaded)
.Where(s => s.SlotId >= 0); // dumb query to conv into IQueryable

View file

@ -24,6 +24,19 @@ public class SlotsController : ControllerBase
this.database = database;
}
private string GenerateSlotsResponse(string slotAggregate, int start, int total) =>
LbpSerializer.TaggedStringElement("slots",
slotAggregate,
new Dictionary<string, object>
{
{
"hint_start", start
},
{
"total", total
},
});
[HttpGet("slots/by")]
public async Task<IActionResult> SlotsBy([FromQuery] string u, [FromQuery] int pageStart, [FromQuery] int pageSize)
{
@ -46,24 +59,9 @@ public class SlotsController : ControllerBase
string.Empty,
(current, slot) => current + slot.Serialize(token.GameVersion)
);
return this.Ok
(
LbpSerializer.TaggedStringElement
(
"slots",
response,
new Dictionary<string, object>
{
{
"hint_start", pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots)
},
{
"total", await this.database.Slots.CountAsync(s => s.CreatorId == targetUserId)
},
}
)
);
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
int total = await this.database.Slots.CountAsync(s => s.CreatorId == targetUserId);
return this.Ok(this.GenerateSlotsResponse(response, start, total));
}
[HttpGet("slotList")]
@ -162,10 +160,9 @@ public class SlotsController : ControllerBase
[FromQuery] int? page = null
)
{
int _pageStart = pageStart;
if (page != null) _pageStart = (int)page * 30;
if (page != null) pageStart = (int)page * 30;
// bit of a better placeholder until we can track average user interaction with /stream endpoint
return await this.ThumbsSlots(_pageStart, Math.Min(pageSize, 30), gameFilterType, players, move, "thisWeek");
return await this.ThumbsSlots(pageStart, Math.Min(pageSize, 30), gameFilterType, players, move, "thisWeek");
}
[HttpGet("slots")]
@ -184,24 +181,96 @@ public class SlotsController : ControllerBase
.Take(Math.Min(pageSize, 30));
string response = Enumerable.Aggregate(slots, string.Empty, (current, slot) => current + slot.Serialize(gameVersion));
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
int total = await StatisticsHelper.SlotCount();
return this.Ok(this.GenerateSlotsResponse(response, start, total));
}
return this.Ok
(
LbpSerializer.TaggedStringElement
(
"slots",
response,
new Dictionary<string, object>
{
{
"hint_start", pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots)
},
{
"total", await StatisticsHelper.SlotCount()
},
}
)
);
[HttpGet("slots/like/{slotType}/{slotId:int}")]
public async Task<IActionResult> SimilarSlots([FromRoute] string slotType, [FromRoute] int slotId, [FromQuery] int pageStart, [FromQuery] int pageSize)
{
GameToken? token = await this.database.GameTokenFromRequest(this.Request);
if (token == null) return this.StatusCode(403, "");
if (pageSize <= 0) return this.BadRequest();
if (slotType != "user") return this.BadRequest();
GameVersion gameVersion = token.GameVersion;
Slot? targetSlot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == slotId);
if (targetSlot == null) return this.BadRequest();
string[] tags = targetSlot.LevelTags;
List<int> slotIdsWithTag = this.database.RatedLevels
.Where(r => r.TagLBP1.Length > 0)
.Where(r => tags.Contains(r.TagLBP1))
.Select(r => r.SlotId)
.ToList();
IQueryable<Slot> slots = this.database.Slots.ByGameVersion(gameVersion, false, true)
.Where(s => slotIdsWithTag.Contains(s.SlotId))
.OrderByDescending(s => s.PlaysLBP1)
.Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30));
string response = Enumerable.Aggregate(slots, string.Empty, (current, slot) => current + slot.Serialize(gameVersion));
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
int total = slotIdsWithTag.Count;
return this.Ok(this.GenerateSlotsResponse(response, start, total));
}
[HttpGet("slots/highestRated")]
public async Task<IActionResult> HighestRatedSlots([FromQuery] int pageStart, [FromQuery] int pageSize)
{
GameToken? token = await this.database.GameTokenFromRequest(this.Request);
if (token == null) return this.StatusCode(403, "");
if (pageSize <= 0) return this.BadRequest();
GameVersion gameVersion = token.GameVersion;
IEnumerable<Slot> slots = this.database.Slots.ByGameVersion(gameVersion, false, true)
.AsEnumerable()
.OrderByDescending(s => s.RatingLBP1)
.Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30));
string response = Enumerable.Aggregate(slots, string.Empty, (current, slot) => current + slot.Serialize(gameVersion));
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
int total = await StatisticsHelper.SlotCount();
return this.Ok(this.GenerateSlotsResponse(response, start, total));
}
[HttpGet("slots/tag")]
public async Task<IActionResult> SimilarSlots([FromQuery] string tag, [FromQuery] int pageStart, [FromQuery] int pageSize)
{
GameToken? token = await this.database.GameTokenFromRequest(this.Request);
if (token == null) return this.StatusCode(403, "");
if (pageSize <= 0) return this.BadRequest();
GameVersion gameVersion = token.GameVersion;
List<int> slotIdsWithTag = await this.database.RatedLevels.Where(r => r.TagLBP1.Length > 0)
.Where(r => r.TagLBP1 == tag)
.Select(s => s.SlotId)
.ToListAsync();
IQueryable<Slot> slots = this.database.Slots.ByGameVersion(gameVersion, false, true)
.Where(s => slotIdsWithTag.Contains(s.SlotId))
.OrderByDescending(s => s.PlaysLBP1)
.Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30));
string response = Enumerable.Aggregate(slots, string.Empty, (current, slot) => current + slot.Serialize(gameVersion));
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
int total = slotIdsWithTag.Count;
return this.Ok(this.GenerateSlotsResponse(response, start, total));
}
[HttpGet("slots/mmpicks")]
@ -220,24 +289,10 @@ public class SlotsController : ControllerBase
.Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 30));
string response = Enumerable.Aggregate(slots, string.Empty, (current, slot) => current + slot.Serialize(gameVersion));
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
int total = await StatisticsHelper.TeamPickCount();
return this.Ok
(
LbpSerializer.TaggedStringElement
(
"slots",
response,
new Dictionary<string, object>
{
{
"hint_start", pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots)
},
{
"total", await StatisticsHelper.TeamPickCount()
},
}
)
);
return this.Ok(this.GenerateSlotsResponse(response, start, total));
}
[HttpGet("slots/lbp2luckydip")]
@ -253,24 +308,10 @@ public class SlotsController : ControllerBase
IEnumerable<Slot> slots = this.database.Slots.ByGameVersion(gameVersion, false, true).OrderBy(_ => EF.Functions.Random()).Take(Math.Min(pageSize, 30));
string response = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize(gameVersion));
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
int total = await StatisticsHelper.SlotCount();
return this.Ok
(
LbpSerializer.TaggedStringElement
(
"slots",
response,
new Dictionary<string, object>
{
{
"hint_start", pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots)
},
{
"total", await StatisticsHelper.SlotCount()
},
}
)
);
return this.Ok(this.GenerateSlotsResponse(response, start, total));
}
[HttpGet("slots/thumbs")]
@ -299,24 +340,10 @@ public class SlotsController : ControllerBase
.Take(Math.Min(pageSize, 30));
string response = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize(token.GameVersion));
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
int total = await StatisticsHelper.SlotCount();
return this.Ok
(
LbpSerializer.TaggedStringElement
(
"slots",
response,
new Dictionary<string, object>
{
{
"hint_start", pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots)
},
{
"total", await StatisticsHelper.SlotCount()
},
}
)
);
return this.Ok(this.GenerateSlotsResponse(response, start, total));
}
[HttpGet("slots/mostUniquePlays")]
@ -359,24 +386,10 @@ public class SlotsController : ControllerBase
.Take(Math.Min(pageSize, 30));
string response = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize(token.GameVersion));
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
int total = await StatisticsHelper.SlotCount();
return this.Ok
(
LbpSerializer.TaggedStringElement
(
"slots",
response,
new Dictionary<string, object>
{
{
"hint_start", pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots)
},
{
"total", await StatisticsHelper.SlotCount()
},
}
)
);
return this.Ok(this.GenerateSlotsResponse(response, start, total));
}
[HttpGet("slots/mostHearted")]
@ -405,24 +418,10 @@ public class SlotsController : ControllerBase
.Take(Math.Min(pageSize, 30));
string response = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize(token.GameVersion));
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
int total = await StatisticsHelper.SlotCount();
return this.Ok
(
LbpSerializer.TaggedStringElement
(
"slots",
response,
new Dictionary<string, object>
{
{
"hint_start", pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots)
},
{
"total", await StatisticsHelper.SlotCount()
},
}
)
);
return this.Ok(this.GenerateSlotsResponse(response, start, total));
}
// /slots/busiest?pageStart=1&pageSize=30&gameFilterType=both&players=1&move=true
@ -475,18 +474,10 @@ public class SlotsController : ControllerBase
}
string response = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize(token.GameVersion));
int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots);
int total = playersBySlotId.Count;
return this.Ok(LbpSerializer.TaggedStringElement("slots",
response,
new Dictionary<string, object>
{
{
"hint_start", pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots)
},
{
"total", playersBySlotId.Count
},
}));
return this.Ok(this.GenerateSlotsResponse(response, start, total));
}

View file

@ -1,4 +1,3 @@
using System.IO.Compression;
using LBPUnion.ProjectLighthouse.Configuration;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Logging;
@ -76,17 +75,30 @@ public class GameServerStartup
// Client digest check.
if (!context.Request.Cookies.TryGetValue("MM_AUTH", out string? authCookie) || authCookie == null) authCookie = string.Empty;
string digestPath = context.Request.Path;
#if !DEBUG
const string url = "/LITTLEBIGPLANETPS3_XML";
string strippedPath = digestPath.Contains(url) ? digestPath[url.Length..] : "";
#endif
Stream body = context.Request.Body;
bool usedAlternateDigestKey = false;
if (computeDigests && digestPath.StartsWith("/LITTLEBIGPLANETPS3_XML"))
{
string clientRequestDigest = await CryptoHelper.ComputeDigest
(digestPath, authCookie, body, ServerConfiguration.Instance.DigestKey.PrimaryDigestKey);
// The game sets X-Digest-B on a resource upload instead of X-Digest-A
string digestHeaderKey = "X-Digest-A";
bool excludeBodyFromDigest = false;
if (digestPath.Contains("/upload/"))
{
digestHeaderKey = "X-Digest-B";
excludeBodyFromDigest = true;
}
// Check the digest we've just calculated against the X-Digest-A header if the game set the header. They should match.
if (context.Request.Headers.TryGetValue("X-Digest-A", out StringValues sentDigest))
string clientRequestDigest = await CryptoHelper.ComputeDigest
(digestPath, authCookie, body, ServerConfiguration.Instance.DigestKey.PrimaryDigestKey, excludeBodyFromDigest);
// Check the digest we've just calculated against the digest header if the game set the header. They should match.
if (context.Request.Headers.TryGetValue(digestHeaderKey, out StringValues sentDigest))
{
if (clientRequestDigest != sentDigest)
{
@ -97,7 +109,7 @@ public class GameServerStartup
body.Position = 0;
clientRequestDigest = await CryptoHelper.ComputeDigest
(digestPath, authCookie, body, ServerConfiguration.Instance.DigestKey.AlternateDigestKey);
(digestPath, authCookie, body, ServerConfiguration.Instance.DigestKey.AlternateDigestKey, excludeBodyFromDigest);
if (clientRequestDigest != sentDigest)
{
#if DEBUG
@ -108,11 +120,20 @@ public class GameServerStartup
#endif
// We still failed to validate. Abort the request.
context.Response.StatusCode = 403;
context.Abort();
return;
}
}
}
#if !DEBUG
// The game doesn't start sending digests until after the announcement so if it's not one of those requests
// and it doesn't include a digest we need to reject the request
else if (!ServerStatics.IsUnitTesting && !strippedPath.Equals("/login") && !strippedPath.Equals("/eula")
&& !strippedPath.Equals("/announce") && !strippedPath.Equals("/status"))
{
context.Response.StatusCode = 403;
return;
}
#endif
context.Response.Headers.Add("X-Digest-B", clientRequestDigest);
context.Request.Body.Position = 0;
@ -140,6 +161,10 @@ public class GameServerStartup
context.Response.Headers.Add("X-Digest-A", serverDigest);
}
// Add a content-length header if it isn't present to disable response chunking
if(!context.Response.Headers.ContainsKey("Content-Length"))
context.Response.Headers.Add("Content-Length", responseBuffer.Length.ToString());
// Copy the buffered response to the actual response stream.
responseBuffer.Position = 0;
await responseBuffer.CopyToAsync(oldResponseStream);
@ -175,4 +200,4 @@ public class GameServerStartup
app.UseEndpoints(endpoints => endpoints.MapControllers());
app.UseEndpoints(endpoints => endpoints.MapRazorPages());
}
}
}