Sqlite added
BanManager now uses sqlite (imcomplete)
This commit is contained in:
parent
91abde27d2
commit
c644fcd45c
4 changed files with 84 additions and 86 deletions
|
|
@ -18,118 +18,92 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using Community.CsharpSqlite.SQLiteClient;
|
||||||
|
|
||||||
namespace TShockAPI
|
namespace TShockAPI
|
||||||
{
|
{
|
||||||
public class BanManager
|
public class BanManager
|
||||||
{
|
{
|
||||||
private DateTime LastLoad;
|
private DateTime LastLoad;
|
||||||
private string Path;
|
private IDbConnection database;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// IP - Name - Reason
|
/// IP - Name - Reason
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private List<Ban> Bans = new List<Ban>();
|
private List<Ban> Bans = new List<Ban>();
|
||||||
|
|
||||||
public BanManager(string path)
|
public BanManager(IDbConnection db)
|
||||||
{
|
{
|
||||||
Path = path;
|
database = db;
|
||||||
|
|
||||||
|
using (var com = database.CreateCommand())
|
||||||
|
{
|
||||||
|
com.CommandText =
|
||||||
|
"CREATE TABLE IF NOT EXISTS \"Bans\" (\"IP\" VARCHAR(15) NOT NULL UNIQUE , \"Name\" VARCHAR(32) NOT NULL , \"Reason\" VARCHAR(255) NOT NULL );";
|
||||||
|
com.ExecuteNonQuery();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Ban GetBanByIp(string ip)
|
public Ban GetBanByIp(string ip)
|
||||||
{
|
{
|
||||||
EnsureChanges();
|
using (var com = database.CreateCommand())
|
||||||
foreach (var ban in Bans)
|
|
||||||
{
|
{
|
||||||
if (ban.IP.Equals(ip))
|
com.CommandText = "SELECT * FROM Bans WHERE IP=@ip";
|
||||||
return ban;
|
AddParameter(com, "@ip", ip);
|
||||||
|
using (var reader = com.ExecuteReader())
|
||||||
|
{
|
||||||
|
if (reader.Read())
|
||||||
|
return new Ban((string)reader["IP"], (string)reader["Name"], (string)reader["Reason"]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
IDbDataParameter AddParameter(IDbCommand command, string name, object data)
|
||||||
|
{
|
||||||
|
var parm = command.CreateParameter();
|
||||||
|
parm.ParameterName = name;
|
||||||
|
parm.Value = data;
|
||||||
|
command.Parameters.Add(parm);
|
||||||
|
return parm;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public Ban GetBanByName(string name, bool casesensitive = true)
|
public Ban GetBanByName(string name, bool casesensitive = true)
|
||||||
{
|
{
|
||||||
EnsureChanges();
|
using (var com = database.CreateCommand())
|
||||||
foreach (var ban in Bans)
|
|
||||||
{
|
{
|
||||||
if (ban.Name.Equals(name,
|
var namecol = casesensitive ? "Name" : "UPPER(Name)";
|
||||||
casesensitive
|
if (!casesensitive)
|
||||||
? StringComparison.Ordinal
|
name = name.ToUpper();
|
||||||
: StringComparison.InvariantCultureIgnoreCase))
|
com.CommandText = "SELECT *, COUNT(*) FROM Bans WHERE " + namecol + "=@name LIMIT 5";
|
||||||
return ban;
|
AddParameter(com, "@name", name);
|
||||||
|
using (var reader = com.ExecuteReader())
|
||||||
|
{
|
||||||
|
if (reader.Read())
|
||||||
|
return new Ban((string)reader["IP"], (string)reader["Name"], (string)reader["Reason"]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddBan(string ip, string name = "", string reason = "")
|
public void AddBan(string ip, string name = "", string reason = "")
|
||||||
{
|
{
|
||||||
if (GetBanByIp(ip) != null)
|
using (var com = database.CreateCommand())
|
||||||
return;
|
{
|
||||||
Bans.Add(new Ban(ip, name, reason));
|
com.CommandText = "INSERT INTO Bans (IP, Name, Reason) VALUES (@ip, @name, @reason)";
|
||||||
SaveBans();
|
AddParameter(com, "@ip", ip);
|
||||||
|
AddParameter(com, "@name", name);
|
||||||
|
AddParameter(com, "@reason", reason);
|
||||||
|
com.ExecuteNonQuery();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveBan(Ban ban)
|
public void RemoveBan(Ban ban)
|
||||||
{
|
{
|
||||||
Bans.Remove(ban);
|
|
||||||
SaveBans();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Reloads the file if it was changed
|
|
||||||
/// </summary>
|
|
||||||
public void EnsureChanges()
|
|
||||||
{
|
|
||||||
if (File.Exists(Path))
|
|
||||||
{
|
|
||||||
if (new FileInfo(Path).LastWriteTime > LastLoad)
|
|
||||||
LoadBans();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Bans.Clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Removes | from the string
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="str"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public string MakeSafe(string str)
|
|
||||||
{
|
|
||||||
return str.Replace("|", " ");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void LoadBans()
|
|
||||||
{
|
|
||||||
Bans.Clear();
|
|
||||||
|
|
||||||
if (!File.Exists(Path))
|
|
||||||
return;
|
|
||||||
|
|
||||||
LastLoad = new FileInfo(Path).LastWriteTime;
|
|
||||||
|
|
||||||
foreach (var line in File.ReadAllLines(Path))
|
|
||||||
{
|
|
||||||
var bansp = line.Split('|');
|
|
||||||
if (bansp.Length != 3)
|
|
||||||
continue;
|
|
||||||
Bans.Add(new Ban(bansp[0], bansp[1], bansp[2]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SaveBans()
|
|
||||||
{
|
|
||||||
var output = new StringBuilder();
|
|
||||||
foreach (var ban in Bans)
|
|
||||||
{
|
|
||||||
output.AppendFormat("{0}|{1}|{2}\r\n", MakeSafe(ban.IP), MakeSafe(ban.Name), MakeSafe(ban.Reason));
|
|
||||||
}
|
|
||||||
|
|
||||||
File.WriteAllText(Path, output.ToString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -67,6 +67,11 @@ namespace TShockAPI
|
||||||
|
|
||||||
public int MaximumLoginAttempts = 3;
|
public int MaximumLoginAttempts = 3;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Valid types are "sqlite" and "mysql"
|
||||||
|
/// </summary>
|
||||||
|
public string StorageType = "sqlite";
|
||||||
|
|
||||||
public static ConfigFile Read(string path)
|
public static ConfigFile Read(string path)
|
||||||
{
|
{
|
||||||
if (!File.Exists(path))
|
if (!File.Exists(path))
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using Community.CsharpSqlite.SQLiteClient;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Content;
|
using Microsoft.Xna.Framework.Content;
|
||||||
using Terraria;
|
using Terraria;
|
||||||
|
|
@ -46,7 +47,7 @@ namespace TShockAPI
|
||||||
|
|
||||||
public static ConfigFile Config { get; set; }
|
public static ConfigFile Config { get; set; }
|
||||||
|
|
||||||
public static IDbConnection Sql;
|
public static IDbConnection DB;
|
||||||
|
|
||||||
public override Version Version
|
public override Version Version
|
||||||
{
|
{
|
||||||
|
|
@ -77,21 +78,38 @@ namespace TShockAPI
|
||||||
|
|
||||||
public override void Initialize()
|
public override void Initialize()
|
||||||
{
|
{
|
||||||
HandleCommandLine(Environment.GetCommandLineArgs());
|
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
||||||
|
|
||||||
ConfigFile.ConfigRead += OnConfigRead;
|
ConfigFile.ConfigRead += OnConfigRead;
|
||||||
|
|
||||||
Bans = new BanManager(FileTools.BansPath);
|
|
||||||
Backups = new BackupManager(Path.Combine(SavePath, "backups"));
|
|
||||||
|
|
||||||
FileTools.SetupConfig();
|
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
Log.Initialize(Path.Combine(SavePath, "log.txt"), LogLevel.All, false);
|
Log.Initialize(Path.Combine(SavePath, "log.txt"), LogLevel.All, false);
|
||||||
#else
|
#else
|
||||||
Log.Initialize(Path.Combine(SavePath, "log.txt"), LogLevel.All & ~LogLevel.Debug, false);
|
Log.Initialize(Path.Combine(SavePath, "log.txt"), LogLevel.All & ~LogLevel.Debug, false);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
HandleCommandLine(Environment.GetCommandLineArgs());
|
||||||
|
|
||||||
|
Backups = new BackupManager(Path.Combine(SavePath, "backups"));
|
||||||
|
|
||||||
|
FileTools.SetupConfig();
|
||||||
|
|
||||||
|
if (Config.StorageType.ToLower() == "sqlite")
|
||||||
|
{
|
||||||
|
string sql = Path.Combine(SavePath, "tshock.sqlite");
|
||||||
|
DB = new SqliteConnection(string.Format("uri=file://{0},Version=3", sql));
|
||||||
|
DB.Open();
|
||||||
|
}
|
||||||
|
else if (Config.StorageType.ToLower() == "mysql")
|
||||||
|
{
|
||||||
|
throw new NotSupportedException("Mysql is not yet supported");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new Exception("Invalid storage type");
|
||||||
|
}
|
||||||
|
|
||||||
|
Bans = new BanManager(DB);
|
||||||
|
|
||||||
Log.ConsoleInfo(string.Format("TShock Version {0} ({1}) now running.", Version, VersionCodename));
|
Log.ConsoleInfo(string.Format("TShock Version {0} ({1}) now running.", Version, VersionCodename));
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -104,9 +122,7 @@ namespace TShockAPI
|
||||||
NetHooks.GetData += GetData;
|
NetHooks.GetData += GetData;
|
||||||
NetHooks.GreetPlayer += OnGreetPlayer;
|
NetHooks.GreetPlayer += OnGreetPlayer;
|
||||||
NpcHooks.StrikeNpc += NpcHooks_OnStrikeNpc;
|
NpcHooks.StrikeNpc += NpcHooks_OnStrikeNpc;
|
||||||
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
|
||||||
|
|
||||||
Bans.LoadBans();
|
|
||||||
GetDataHandlers.InitGetDataHandler();
|
GetDataHandlers.InitGetDataHandler();
|
||||||
Commands.InitCommands();
|
Commands.InitCommands();
|
||||||
RegionManager.ReadAllSettings();
|
RegionManager.ReadAllSettings();
|
||||||
|
|
@ -119,7 +135,7 @@ namespace TShockAPI
|
||||||
|
|
||||||
public override void DeInitialize()
|
public override void DeInitialize()
|
||||||
{
|
{
|
||||||
Bans.SaveBans();
|
DB.Close();
|
||||||
GameHooks.PostInitialize -= OnPostInit;
|
GameHooks.PostInitialize -= OnPostInit;
|
||||||
GameHooks.Update -= OnUpdate;
|
GameHooks.Update -= OnUpdate;
|
||||||
ServerHooks.Join -= OnJoin;
|
ServerHooks.Join -= OnJoin;
|
||||||
|
|
@ -274,7 +290,7 @@ namespace TShockAPI
|
||||||
|
|
||||||
Players[ply] = player;
|
Players[ply] = player;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnLeave(int ply)
|
private void OnLeave(int ply)
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,9 @@
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Reference Include="Community.CsharpSqlite.SQLiteClient">
|
||||||
|
<HintPath>SqlBins\Community.CsharpSqlite.SQLiteClient.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
<Reference Include="Microsoft.Xna.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86" />
|
<Reference Include="Microsoft.Xna.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86" />
|
||||||
<Reference Include="Microsoft.Xna.Framework.Game, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86" />
|
<Reference Include="Microsoft.Xna.Framework.Game, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86" />
|
||||||
<Reference Include="Microsoft.Xna.Framework.Graphics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86" />
|
<Reference Include="Microsoft.Xna.Framework.Graphics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86" />
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue