Skip to content

Commit

Permalink
[RequestLib] Added mod network rate limiter. Fixed some metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
kafeijao committed Aug 15, 2023
1 parent 863a8d3 commit bfa86b6
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CVRSuperMario64/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
[assembly: MelonPlatformDomain(MelonPlatformDomainAttribute.CompatibleDomains.MONO)]
[assembly: MelonColor(255, 0, 255, 0)]
[assembly: MelonAuthorColor(255, 119, 77, 79)]
[assembly: MelonOptionalDependencies(AssemblyInfoParams.BTKUILibName)]
[assembly: MelonAdditionalDependencies(AssemblyInfoParams.BTKUILibName)]

namespace Kafe.CVRSuperMario64.Properties;
internal static class AssemblyInfoParams {
Expand Down
2 changes: 1 addition & 1 deletion Instances/Properties/CVRMG.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"loaderversion": "v0.6.1",
"modtype": "Mod",
"author": "kafeijao",
"description": "__**Features**__:\n- Rejoin the last instance when restarting the game.\n- When rejoining teleport to the previous location.\n- When starting the game, join on an online Instance of the Home world.\n- Keep the joined Instances History and allow to rejoin them.\n- Restart the game via button.\n\nYou can toggle the joining last instance, or/and starting in an online instance, rejoining last Instance takes priority. Also you can configure the online instance region and privacy type.\n\nCheck [README.md](https://github.com/kafeijao/Kafe_CVR_Mods/tree/master/Instances) for more in-depth info.\n\n",
"description": "__**Features**__:\n- Rejoin the last instance when restarting the game.\n- When rejoining teleport to the previous location.\n- When starting the game, join on an online Instance of the Home world.\n- Keep the joined Instances History and allow to rejoin them.\n- Restart the game via button.\n\nYou can toggle the joining last instance, or/and starting in an online instance, rejoining last Instance takes priority. Also you can configure the online instance region and privacy type.\n\nCheck [README.md](https://github.com/kafeijao/Kafe_CVR_Mods/tree/master/Instances) for more in-depth info.\n\n**Optional Requirements**\n- [ChatBox](https://api.cvrmg.com/v1/mods/download/168)",
"searchtags": [
"rejoin",
"history",
Expand Down
2 changes: 1 addition & 1 deletion LoginProfiles/Properties/CVRMG.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"loaderversion": "v0.6.1",
"modtype": "Mod",
"author": "kafeijao",
"description": "Allows to start CVR with different **credentials profiles** via steam Launch Options, or straight from the commandline.\n\n```--profile=profile_id```\n\nI normally use this to test sync stuff (where I need 2 clients). For this, you'd need to copy CVR files to another folder, and lunch together with `-skipsteam` launch option. In my case would be: `ChilloutVR.exe --profile=kafeijao -skipsteam`",
"description": "Allows to start CVR with different **credentials profiles** via steam Launch Options, or straight from the commandline.\n\n```--profile=profile_id```\n\nI normally use this to test sync stuff (where I need 2 clients). For this, you'd need to copy CVR files to another folder, and lunch together with `--no-steam` launch option. In my case would be: `ChilloutVR.exe --profile=kafeijao -skipsteam`",
"searchtags": [
"login",
"profile",
Expand Down
4 changes: 2 additions & 2 deletions OSC/Properties/CVRMG.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"loaderversion": "v0.6.1",
"modtype": "Mod",
"author": "kafeijao",
"description": "This mod enables interactions with ChilloutVR using OSC. It also focus on compatibility with existing applications. If something is not working create an [issue in github](https://github.com/kafeijao/Kafe_CVR_Mods/issues) and I will fix it asap.\n\n__**Avatar**__\n- Change into new avatars\n- Change/Read parameters\n__**Inputs**__\n- Control your avatar\n- Send actions to the game\n__**Props**__\n- Set/Read prop location\n- Set/Read synced params\n__**Tracking**__\n- Listen to tracking data (location/battery) from `Trackers`, `Controllers`, `HMD`, `Base Stations`, and `Play Space`.\n__**ChatBox**__\n- Send ChaBox messages and typing events\n\n__**New**__ [python library](https://github.com/kafeijao/cvr_osc_lib_py) to abstract this OSC API with examples!\n\nRefer to the [IN-DEPTH](https://github.com/kafeijao/Kafe_CVR_Mods/tree/master/OSC) documentation for more information.",
"description": "This mod enables interactions with ChilloutVR using OSC. It also focus on compatibility with existing applications. If something is not working create an [issue in github](https://github.com/kafeijao/Kafe_CVR_Mods/issues) and I will fix it asap.\n\n__**Avatar**__\n- Change into new avatars\n- Change/Read parameters\n__**Inputs**__\n- Control your avatar\n- Send actions to the game\n__**Props**__\n- Set/Read prop location\n- Set/Read synced params\n__**Tracking**__\n- Listen to tracking data (location/battery) from `Trackers`, `Controllers`, `HMD`, `Base Stations`, and `Play Space`.\n__**ChatBox**__\n- Send ChaBox messages and typing events\n\n__**New**__ [python library](https://github.com/kafeijao/cvr_osc_lib_py) to abstract this OSC API with examples!\n\nRefer to the [IN-DEPTH](https://github.com/kafeijao/Kafe_CVR_Mods/tree/master/OSC) documentation for more information.\n\n**Optional Requirements**\n- [ChatBox](https://api.cvrmg.com/v1/mods/download/168)",
"searchtags": [
"osc",
"input",
Expand All @@ -17,6 +17,6 @@
"requirements": [],
"downloadlink": "https://github.com/kafeijao/Kafe_CVR_Mods/releases/download/r56/OSC.dll",
"sourcelink": "https://github.com/kafeijao/Kafe_CVR_Mods/tree/master/OSC",
"changelog": "- Fixed Tracking endpoints\n- Cached entities for performance.\n- Fixed tracking ednpoints (again)",
"changelog": "- Fixed Tracking endpoints\n- Cached entities for performance.\n- Fixed tracking endpoints (again)",
"embedcolor": "16C60C"
}
41 changes: 41 additions & 0 deletions RequestLib/ModNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ private static void OnMessage(object _, MessageReceivedEventArgs e) {
// Ignore messages from non-friends
if (ModConfig.MeOnlyReceiveFromFriends.Value && !Friends.FriendsWith(senderGuid)) return;

// Process the rate limiter
if (RateLimiter.IsRateLimited(senderGuid)) return;

try {

// Read the version of the message
Expand Down Expand Up @@ -477,6 +480,43 @@ private static IEnumerator HandleInitialSync() {
_sendSyncRequestCoroutine = null;
}

private class RateLimiter {
private const int MaxMessages = 3;
private const int TimeWindowSeconds = 10;
public static readonly Dictionary<string, RateLimiter> UserRateLimits = new();
private DateTime LastMessageTime { get; set; } = DateTime.UtcNow;
private int MessageCount { get; set; } = 1;
private bool Notified { get; set; }

public static bool IsRateLimited(string senderGuid) {
if (UserRateLimits.TryGetValue(senderGuid, out var userState)) {
if ((DateTime.UtcNow - userState.LastMessageTime).TotalSeconds <= TimeWindowSeconds) {
// The user is above the rate limit
if (userState.MessageCount >= MaxMessages) {
if (userState.Notified) return true;
userState.Notified = true;
MelonLogger.Warning($"The player {CVRPlayerManager.Instance.TryGetPlayerName(senderGuid)} " +
$"send over {MaxMessages} messages within {TimeWindowSeconds} seconds. To " +
$"prevent crashing/lag it's going to be rate limited.");
return true;
}
userState.MessageCount++;
}
else {
// Reset their count if it's been more than the time window.
userState.MessageCount = 1;
userState.LastMessageTime = DateTime.UtcNow;
userState.Notified = false;
}
}
else {
// If the sender is not in the dictionary, add them.
UserRateLimits[senderGuid] = new RateLimiter();
}
return false;
}
}

[HarmonyPatch]
internal class HarmonyPatches {

Expand Down Expand Up @@ -507,6 +547,7 @@ public static void After_NetworkManager_ReceiveReconnectToken() {

// Clear previous info
API.RemotePlayerMods.Clear();
RateLimiter.UserRateLimits.Clear();

// Wait some time and send the sync request to everyone
_sendSyncRequestCoroutine = MelonCoroutines.Start(HandleInitialSync());
Expand Down
2 changes: 1 addition & 1 deletion RequestLib/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

namespace Kafe.RequestLib.Properties;
internal static class AssemblyInfoParams {
public const string Version = "0.0.7";
public const string Version = "0.0.8";
public const string Author = "kafeijao";
public const string BTKUILibName = "BTKUILib";
public const string BTKSAImmersiveHudName = "BTKSAImmersiveHud";
Expand Down
4 changes: 2 additions & 2 deletions RequestLib/Properties/CVRMG.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_id": 169,
"name": "RequestLib",
"modversion": "0.0.7",
"modversion": "0.0.8",
"gameversion": "2023r171p2",
"loaderversion": "v0.6.1",
"modtype": "Mod",
Expand All @@ -14,6 +14,6 @@
"requirements": ["[BTKUILib](https://api.cvrmg.com/v1/mods/download/113)"],
"downloadlink": "https://github.com/kafeijao/Kafe_CVR_Mods/releases/download/r56/RequestLib.dll",
"sourcelink": "https://github.com/kafeijao/Kafe_CVR_Mods/tree/master/RequestLib",
"changelog": "- Fixed optional requirements.",
"changelog": "- Added a mod network rate limiter.",
"embedcolor": "16C60C"
}
2 changes: 1 addition & 1 deletion TeleportRequest/Properties/CVRMG.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"loaderversion": "v0.6.1",
"modtype": "Mod",
"author": "kafeijao",
"description": "This mod allows you to request to teleport to other players. You're only able to request/accept if the world allows flight.\n\n__**Features**__\n- Request to teleport to a player.\n- Teleport yourself to the previous location before teleporting to a player.\n\n**Note:** This mod has optional integration with [ChatBox](https://api.cvrmg.com/v1/mods/download/168). It allows to write commands in the ChatBox to request to teleport, accept/decline, and go back.\n\nCheck [README.md](https://github.com/kafeijao/Kafe_CVR_Mods/tree/master/TeleportRequest) for more in-depth info.\n\n",
"description": "This mod allows you to request to teleport to other players. You're only able to request/accept if the world allows flight.\n\n__**Features**__\n- Request to teleport to a player.\n- Teleport yourself to the previous location before teleporting to a player.\n\n**Note:** This mod has optional integration with [ChatBox](https://api.cvrmg.com/v1/mods/download/168). It allows to write commands in the ChatBox to request to teleport, accept/decline, and go back.\n\nCheck [README.md](https://github.com/kafeijao/Kafe_CVR_Mods/tree/master/TeleportRequest) for more in-depth info.\n\n**Optional Requirements**\n- [ChatBox](https://api.cvrmg.com/v1/mods/download/168)",
"searchtags": [
"request",
"lib"
Expand Down

0 comments on commit bfa86b6

Please sign in to comment.