Skip to content

Commit af8a84c

Browse files
committed
Added a config option to override player IPs using their token request IP.
1 parent ff15993 commit af8a84c

4 files changed

Lines changed: 72 additions & 9 deletions

File tree

LabExtended/API/ExPlayer.cs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,8 @@ public static IEnumerable<ExPlayer> Get(RoleTypeId role)
465465
=> Get(n => n.Role.Type == role);
466466
#endregion
467467

468+
private string? cInfoRejectionReason;
469+
468470
internal StringBuilder? infoBuilder;
469471
internal string? infoProperty;
470472

@@ -1158,10 +1160,10 @@ public bool HasPermission(string permission)
11581160
if (string.IsNullOrEmpty(permission))
11591161
return false;
11601162

1161-
if (!IsOnlineAndVerified)
1163+
if (ReferenceHub == null)
11621164
return false;
11631165

1164-
if (ApiLoader.ApiConfig.HostPermissions && IsHost)
1166+
if (ApiLoader.ApiConfig.HostPermissions && (ReferenceHub.isLocalPlayer || (host?.ReferenceHub != null && this == host)))
11651167
return true;
11661168

11671169
permission = permission
@@ -1171,12 +1173,12 @@ public bool HasPermission(string permission)
11711173
if (!ApiLoader.ApiConfig.RegexPermissions)
11721174
return PermissionsManager.HasPermissions(this, permission);
11731175

1174-
if (HasPermission($"-{permission}"))
1175-
return false;
1176-
11771176
var permissions = PermissionsManager.GetPermissions(this);
11781177

11791178
if (permissions.Length < 1)
1179+
return ApiLoader.ApiConfig.OverridePermissions;
1180+
1181+
if (permissions.Contains($"-{permission}"))
11801182
return false;
11811183

11821184
var pattern = Regex.Escape(permission)
@@ -1193,6 +1195,7 @@ public bool HasPermission(string permission)
11931195
pattern = "^" + pattern;
11941196

11951197
var regex = new Regex(pattern, RegexOptions.Compiled);
1198+
var negative = false;
11961199

11971200
for (var x = 0; x < permissions.Length; x++)
11981201
{
@@ -1201,14 +1204,17 @@ public bool HasPermission(string permission)
12011204
if (string.IsNullOrEmpty(ownedPermission))
12021205
continue;
12031206

1207+
if (ownedPermission == $"-{permission}")
1208+
negative = true;
1209+
12041210
if (ownedPermission == permission)
12051211
return true;
12061212

12071213
if (regex.IsMatch(ownedPermission))
1208-
return true;
1214+
return !negative;
12091215
}
12101216

1211-
return false;
1217+
return ApiLoader.ApiConfig.OverridePermissions;
12121218
}
12131219

12141220
/// <summary>
@@ -1598,7 +1604,10 @@ private void RefreshCustomInfo()
15981604
|| !HasEnabledCustomInfo
15991605
|| !IsVerified
16001606
|| ReferenceHub == null)
1607+
{
1608+
cInfoRejectionReason = null;
16011609
return;
1610+
}
16021611

16031612
infoBuilder.Clear();
16041613

@@ -1611,24 +1620,37 @@ private void RefreshCustomInfo()
16111620
ExPlayerEvents.OnRefreshingCustomInfo(this, infoBuilder);
16121621

16131622
if (infoBuilder.Length == 0)
1623+
{
1624+
cInfoRejectionReason = null;
16141625
return;
1626+
}
16151627

16161628
while (infoBuilder[infoBuilder.Length - 1] == '\n')
16171629
infoBuilder.Remove(infoBuilder.Length - 1, 1);
16181630

16191631
var customInfo = infoBuilder.ToString();
16201632

16211633
if (NetworkBehaviour.SyncVarEqual(customInfo, ref ReferenceHub.nicknameSync._customPlayerInfoString))
1634+
{
1635+
cInfoRejectionReason = null;
16221636
return;
1637+
}
16231638

16241639
if (!NicknameSync.ValidateCustomInfo(customInfo, out var rejectionText))
16251640
{
1641+
if (cInfoRejectionReason != null && cInfoRejectionReason == rejectionText)
1642+
return;
1643+
1644+
cInfoRejectionReason = rejectionText;
1645+
16261646
ApiLog.Warn("LabExtended", $"CustomInfo of &3{ToLogString()}&r was &1REJECTED&r! (&3{rejectionText}&r)");
16271647
return;
16281648
}
16291649

16301650
ReferenceHub.nicknameSync._customPlayerInfoString = customInfo;
16311651
ReferenceHub.nicknameSync.syncVarDirtyBits |= 2UL;
1652+
1653+
cInfoRejectionReason = null;
16321654
}
16331655

16341656
private void RefreshModifiers()

LabExtended/API/Settings/Entries/Buttons/SettingsButton.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ public TimeSpan? TimeSinceLastTrigger
7373
return TimeSpan.FromMilliseconds(Time.realtimeSinceStartup - LastTriggerTime.Value);
7474
}
7575
}
76+
77+
/// <summary>
78+
/// Gets or sets the text of the button.
79+
/// </summary>
80+
public string Text
81+
{
82+
get => Base.ButtonText;
83+
set => Base.ButtonText = value;
84+
}
7685

7786
/// <summary>
7887
/// Gets or sets the required duration, in seconds, that the button must be held to trigger successfully.

LabExtended/Core/Configs/ApiConfig.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,22 @@ public class ApiConfig
3333
/// </summary>
3434
[Description("Whether or not to use a regex pattern to match permissions.")]
3535
public bool RegexPermissions { get; set; } = true;
36-
36+
37+
/// <summary>
38+
/// Gets or sets a value indicating whether permission requirements are ignored.
39+
/// </summary>
40+
[Description("Whether or not to ignore permission requirements..")]
41+
public bool OverridePermissions { get; set; } = false;
42+
43+
/// <summary>
44+
/// Gets or sets a list of IPs that should be overriden by the player's token request IP.'
45+
/// </summary>
46+
[Description("Sets a list of IPs that should be overriden by the player's token request IP.")]
47+
public List<string> TokenIpOverride { get; set; } = new()
48+
{
49+
50+
};
51+
3752
/// <summary>
3853
/// Gets or sets the configuration settings for file storage.
3954
/// </summary>

LabExtended/Events/InternalEvents.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,24 @@ internal static void HandlePlayerVerified(ExPlayer player)
7474
player.PersistentStorage.Lifes++;
7575
}
7676
}
77-
77+
78+
if (player.Connection != null
79+
&& player.ConnectionToClient != null
80+
&& !string.IsNullOrWhiteSpace(player.ConnectionToClient.address)
81+
&& ApiLoader.ApiConfig?.TokenIpOverride?.Count > 0
82+
&& ApiLoader.ApiConfig.TokenIpOverride.Contains(player.ConnectionToClient.address)
83+
&& player.ReferenceHub.authManager != null
84+
&& player.ReferenceHub.authManager.AuthenticationResponse.AuthToken != null
85+
&& !string.IsNullOrEmpty(player.ReferenceHub.authManager.AuthenticationResponse.AuthToken.RequestIp))
86+
{
87+
player.ConnectionToClient.IpOverride =
88+
player.ReferenceHub.authManager.AuthenticationResponse.AuthToken.RequestIp;
89+
player.ReferenceHub.queryProcessor._ipAddress = player.ConnectionToClient.IpOverride;
90+
91+
ApiLog.Debug("LabExtended", $"Overriden IP of &1{player.UserId}&r to &3{player.ConnectionToClient.address}&r" +
92+
$" (&6{player.ConnectionToClient.OriginalIpAddress}&r)");
93+
}
94+
7895
ApiLog.Info("LabExtended",
7996
$"Player &3{player.Nickname}&r (&6{player.UserId}&r) &2joined&r from &3{player.IpAddress} ({player.CountryCode})&r!");
8097

0 commit comments

Comments
 (0)