Skip to content
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@ packages/*

# Private key files #
scripts/ssh_private_key

.idea/
15 changes: 11 additions & 4 deletions TShockAPI/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1742,13 +1742,20 @@ void BanDetails()

private static void Whitelist(CommandArgs args)
{
if (args.Parameters.Count == 1)
if (args.Parameters is [{ } ip])
{
using (var tw = new StreamWriter(FileTools.WhitelistPath, true))
if (TShock.Whitelist.AddToWhitelist(ip))
{
args.Player.SendSuccessMessage(GetString($"Added {ip} to the whitelist."));
}
else
{
tw.WriteLine(args.Parameters[0]);
args.Player.SendErrorMessage(GetString($"Failed to add {ip} to the whitelist. Perhaps it is already whitelisted?"));
}
args.Player.SendSuccessMessage(GetString($"Added {args.Parameters[0]} to the whitelist."));
}
else
{
args.Player.SendErrorMessage(GetString($"Invalid Whitelist syntax. Refer to {Specifier}whitelist help for details on how to use the {Specifier}whitelist command"));
}
}

Expand Down
42 changes: 3 additions & 39 deletions TShockAPI/FileTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ internal static string MotdPath
/// <summary>
/// Path to the file containing the whitelist.
/// </summary>
internal static string WhitelistPath
{
get { return Path.Combine(TShock.SavePath, "whitelist.txt"); }
}
internal static string WhitelistPath => Path.Combine(TShock.SavePath, "whitelist.txt");

/// <summary>
/// Path to the file containing the config.
Expand Down Expand Up @@ -104,8 +101,8 @@ public static void SetupConfig()

CreateIfNot(RulesPath, "Respect the admins!\nDon't use TNT!");
CreateIfNot(MotdPath, MotdFormat);
CreateIfNot(WhitelistPath);

CreateIfNot(WhitelistPath, Whitelist.DefaultWhitelistContent);
bool writeConfig = true; // Default to true if the file doesn't exist
if (File.Exists(ConfigPath))
{
Expand Down Expand Up @@ -140,39 +137,6 @@ public static void SetupConfig()
}
}

/// <summary>
/// Tells if a user is on the whitelist
/// </summary>
/// <param name="ip">string ip of the user</param>
/// <returns>true/false</returns>
public static bool OnWhitelist(string ip)
{
if (!TShock.Config.Settings.EnableWhitelist)
{
return true;
}
CreateIfNot(WhitelistPath, "127.0.0.1");
using (var tr = new StreamReader(WhitelistPath))
{
string whitelist = tr.ReadToEnd();
ip = TShock.Utils.GetRealIP(ip);
bool contains = whitelist.Contains(ip);
if (!contains)
{
foreach (var line in whitelist.Split(Environment.NewLine.ToCharArray()))
{
if (string.IsNullOrWhiteSpace(line))
continue;
contains = TShock.Utils.GetIPv4AddressFromHostname(line).Equals(ip);
if (contains)
return true;
}
return false;
}
return true;
}
}

/// <summary>
/// Looks for a 'Settings' token in the json object. If one is not found, returns a new json object with all tokens of the previous object added
/// as children to a root 'Settings' token
Expand Down
9 changes: 8 additions & 1 deletion TShockAPI/TShock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ public class TShock : TerrariaPlugin
public static ILog Log;
/// <summary>instance - Static reference to the TerrariaPlugin instance.</summary>
public static TerrariaPlugin instance;

/// <summary>
/// Whitelist - Static reference to the whitelist system, which allows whitelisting of IP addresses and networks.
/// </summary>
public static Whitelist Whitelist { get; set; }

/// <summary>
/// Static reference to a <see cref="CommandLineParser"/> used for simple command-line parsing
/// </summary>
Expand Down Expand Up @@ -354,6 +360,7 @@ public override void Initialize()
Bouncer = new Bouncer();
RegionSystem = new RegionHandler(Regions);
ItemBans = new ItemBans(this, DB);
Whitelist = new(FileTools.WhitelistPath);

var geoippath = "GeoIP.dat";
if (Config.Settings.EnableGeoIP && File.Exists(geoippath))
Expand Down Expand Up @@ -1317,7 +1324,7 @@ private void OnConnect(ConnectEventArgs args)
return;
}

if (!FileTools.OnWhitelist(player.IP))
if (!Whitelist.IsWhitelisted(player.IP))
{
player.Kick(Config.Settings.WhitelistKickReason, true, true, null, false);
args.Handled = true;
Expand Down
3 changes: 2 additions & 1 deletion TShockAPI/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
TShock, a server mod for Terraria
Copyright (C) 2011-2019 Pryaxis & TShock Contributors

Expand Down Expand Up @@ -608,6 +608,7 @@ public void Reload()
TShock.ProjectileBans.UpdateBans();
TShock.TileBans.UpdateBans();
TShock.Bans.UpdateBans();
TShock.Whitelist.ReloadFromFile();
}

/// <summary>
Expand Down
Loading
Loading