TShock icon indicating copy to clipboard operation
TShock copied to clipboard

Feature: Whitelist overhaul

Open SakuraIsayeki opened this issue 7 months ago • 13 comments

Overhauled the whitelist system to use a new dedicated service. This one is memory-based to optimise lookups, rather than accessing the file at each server join for WL comparison.

Added support for in-file comments, by using # prefix (considered invalid and ignored by parser). Could come in handy for those massive files, or just keeping track of "whose IP is this" cases. Added support for ranges using CIDR notation, to allow for entire subnets to be whitelisted. Particularly useful for LAN parties.

New default for the whitelist.txt file looks like this :

# Localhost
127.0.0.1
::1

# Uncomment to allow IPs within private ranges
# 10.0.0.0/8
# 172.16.0.0/12
# 192.168.0.0/16
# fe80::/10
# fd00::/8

Resolves #3091.

SakuraIsayeki avatar May 24 '25 22:05 SakuraIsayeki

@ACaiCat if you think the code is good, can you add a code review too?

hakusaro avatar May 25 '25 02:05 hakusaro

CleanShot 2025-05-25 at 12 00 39

hakusaro avatar May 25 '25 03:05 hakusaro

@ACaiCat if you think the code is good, can you add a code review too?

Maybe you should find someone more professional, but it's not me.

ACaiCat avatar May 25 '25 03:05 ACaiCat

Maybe you should find someone more professional, but it's not me.

You are professional

hakusaro avatar May 25 '25 03:05 hakusaro

Also, if the whitelist is blank, I think it would be fine to populate it with the example whitelist instead of leaving it blank. e.g., if whitelist is disabled, and is empty, add the default config to it.

hakusaro avatar May 25 '25 03:05 hakusaro

I tested this and after adding 127.0.0.1 to the whitelist and using /reload, I was not able to whitelist myself.

Also, Terraria doesn't support IPv6.

It seems can not be reloaded through /reload command (Need restart whole server)

ACaiCat avatar May 25 '25 03:05 ACaiCat

Other note:

  • There's a submodule update that actually reverts 57039ed002, probably unwanted.

  • The include of .idea folder?

Oops, indeed unwanted changes. Lemme fix that right away 😅

SakuraIsayeki avatar May 25 '25 15:05 SakuraIsayeki

I test it on my win server, /whitelist command can't work.I am sure that no other process was using whitelist.txt

Log:

2025-05-30 21:28:08 - Utils: INFO: Server execute: /whitelist 221.12.2.1。
2025-05-30 21:28:08 - Command: ERROR: System.IO.IOException: The process cannot access the file 'C:\Users\Administrator\Desktop\a\tshock\whitelist.txt' because it is being used by another process.
   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options) in System.Private.CoreLib.dll:token 0x60001ce+0xa7
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode) in System.Private.CoreLib.dll:token 0x60001cd+0x6
   at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode) in System.Private.CoreLib.dll:token 0x6008cfe+0x14
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode) in System.Private.CoreLib.dll:token 0x6008cd5+0xa
   at System.IO.FileInfo.OpenRead() in System.Private.CoreLib.dll:token 0x6008742+0x0
   at TShockAPI.Whitelist.AddLine(ReadOnlySpan`1 content) in /home/runner/work/TShock/TShock/TShockAPI/Whitelist.cs:line 227
   at TShockAPI.Whitelist.AddToWhitelist(IPAddress ip) in /home/runner/work/TShock/TShock/TShockAPI/Whitelist.cs:line 203
   at TShockAPI.Whitelist.AddToWhitelist(ReadOnlySpan`1 ip) in /home/runner/work/TShock/TShock/TShockAPI/Whitelist.cs:line 175
   at TShockAPI.Commands.Whitelist(CommandArgs args) in /home/runner/work/TShock/TShock/TShockAPI/Commands.cs:line 1747
   at TShockAPI.Command.Run(CommandArgs args) in /home/runner/work/TShock/TShock/TShockAPI/Commands.cs:line 158

ACaiCat avatar May 30 '25 13:05 ACaiCat

I test it on my win server, /whitelist command can't work.I am sure that no other process was using whitelist.txt

By any chance, would you have the repro steps for this one ? Because I can't repro the issue.

SakuraIsayeki avatar May 30 '25 13:05 SakuraIsayeki

By any chance, would you have the repro steps for this one ? Because I can't repro the issue.

Just try to add a ip through /whitelist 192.168.1.2

ACaiCat avatar May 30 '25 13:05 ACaiCat

@THEXN tested it too and got the same error. c34ee5cc3b502444c2953575df78f4d8

ACaiCat avatar May 30 '25 13:05 ACaiCat

Okay. Seems I derped handling the file streams. Now fixed, give it a go on last commit :)

SakuraIsayeki avatar May 30 '25 14:05 SakuraIsayeki

It works now. image

ACaiCat avatar May 30 '25 14:05 ACaiCat