diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bb5cefb..8006fcf8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -80,6 +80,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * `Utils.MaxChests()` is now `Utils.HasWorldReachedMaxChests()`. (@hakusaro) * `Utils.GetIPv4Address()` is now `Utils.GetIPv4AddressFromHostname()`. (@hakusaro) * Fixed the disappearing problem when placing tile entities. (@mistzzt) +* Removed the stat tracking system. (@hakusaro) ## TShock 4.3.25 * Fixed a critical exploit in the Terraria protocol that could cause massive unpreventable world corruption as well as a number of other problems. Thanks to @bartico6 for reporting. Fixed by the efforts of @QuiCM, @hakusaro, and tips in the right directioon from @bartico6. diff --git a/TShockAPI/StatTracker.cs b/TShockAPI/StatTracker.cs deleted file mode 100644 index deecfe8c..00000000 --- a/TShockAPI/StatTracker.cs +++ /dev/null @@ -1,314 +0,0 @@ -/* -TShock, a server mod for Terraria -Copyright (C) 2011-2018 Pryaxis & TShock Contributors - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - -using System; -using System.Net; -using System.Threading; -using System.Web; -using TerrariaApi.Server; -using System.Diagnostics; -using System.Linq; -using System.Runtime.InteropServices; -using System.Net.Http; -using System.Threading.Tasks; -using TShockAPI.Extensions; -using System.IO; -using System.Text.RegularExpressions; - -namespace TShockAPI -{ - /// - /// StatTracker class for tracking various server metrics - /// - public class StatTracker - { - /// - /// Calls the GetPhysicallyInstalledSystemMemory Windows API function, returning the amount of RAM installed on the host PC - /// - /// The amount of memory (in kilobytes) present on the host PC - /// - [DllImport("kernel32.dll")] - [return: MarshalAs(UnmanagedType.Bool)] - static extern bool GetPhysicallyInstalledSystemMemory(out long totalMemInKb); - - /// - /// A provider token set on the command line. Used by the stats server to group providers - /// - public string ProviderToken = ""; - /// - /// Whether or not to opt out of stat tracking - /// - public bool OptOut = false; - - private PluginItem[] plugins; - - private long totalMem = 0; - private string serverId = ""; - private HttpClient _client; - private const string TrackerUrl = "http://stats.tshock.co/submit/{0}"; - - /// - /// Creates a new instance of - /// - public StatTracker() - { - } - - /// - /// Starts the stat tracker - /// - public void Start() - { - if (OptOut) - { - //If opting out, return and do not start stat tracking - return; - } - - //HttpClient with a 5 second timeout - _client = new HttpClient() - { - Timeout = new TimeSpan(0, 0, 5) - }; - serverId = Guid.NewGuid().ToString(); - - Thread t = new Thread(async () => - { - do - { - //Wait 5 minutes - await Task.Delay(1000 * 60 * 5); - //Then update again - await SendUpdateAsync(); - } while (true); - }) - { - Name = "TShock Stat Tracker Thread", - IsBackground = true - }; - t.Start(); - } - - private async Task SendUpdateAsync() - { - JsonData data = PrepareJsonData(); - var serialized = Newtonsoft.Json.JsonConvert.SerializeObject(data); - var encoded = HttpUtility.UrlEncode(serialized); - var uri = string.Format(TrackerUrl, encoded); - try - { - HttpResponseMessage msg = await _client.GetAsync(uri); - if (msg.StatusCode != HttpStatusCode.OK) - { - string reason = msg.ReasonPhrase; - if (string.IsNullOrWhiteSpace(reason)) - { - reason = "none"; - } - throw new WebException("Stats server did not respond with an OK. " - + $"Server message: [error {msg.StatusCode}] {reason}"); - } - } - catch (Exception ex) - { - string msg = ex.BuildExceptionString(); - - //Give the console a brief - Console.ForegroundColor = ConsoleColor.Yellow; - Console.WriteLine($"StatTracker warning: {msg}"); - Console.ForegroundColor = ConsoleColor.Gray; - - //And log the full exception - TShock.Log.Warn($"StatTracker warning: {ex.ToString()}"); - TShock.Log.ConsoleError("Retrying in 5 minutes."); - } - } - - private JsonData PrepareJsonData() - { - return new JsonData() - { - port = Terraria.Netplay.ListenPort, - currentPlayers = TShock.Utils.GetActivePlayerCount(), - maxPlayers = TShock.Config.MaxSlots, - systemRam = GetTotalSystemRam(ServerApi.RunningMono), - version = TShock.VersionNum.ToString(), - terrariaVersion = Terraria.Main.versionNumber2, - providerId = ProviderToken, - serverId = serverId, - mono = ServerApi.RunningMono, - ignorePluginVersion = ServerApi.IgnoreVersion, - loadedPlugins = GetLoadedPlugins() - }; - } - - private PluginItem[] GetLoadedPlugins() - { - if (plugins != null) - { - return plugins; //Return early - } - - plugins = new PluginItem[ServerApi.Plugins.Count]; //Initialize with enough room to store the ammount of plugins loaded. - for (var i = 0; i < ServerApi.Plugins.Count; i++) - { - var pluginItem = new PluginItem(); - var apiAttribute = (ApiVersionAttribute)ServerApi.Plugins[i].Plugin.GetType().GetCustomAttributes(typeof(ApiVersionAttribute), false).FirstOrDefault(); - //The current implementation of loading plugins doesn't allow for a plugin to be loaded without an ApiVersion, the UNKNOWN is there incase a change is made to allow it - pluginItem.apiVersion = apiAttribute?.ApiVersion.ToString() ?? "UNKNOWN"; - pluginItem.name = ServerApi.Plugins[i].Plugin.Name; - pluginItem.version = ServerApi.Plugins[i].Plugin.Version.ToString(); - plugins[i] = pluginItem; - } - return plugins; - } - - /// - /// Returns the amount of free RAM, in megabytes. - /// - /// Whether or not this program is being executed in a Mono runtime - /// Free RAM memory amount, in megabytes - public long GetFreeSystemRam(bool mono) - { - if (mono) - { - //Temporary in case mono won't work - if (File.Exists("/proc/meminfo")) - { - var l = File.ReadAllLines("/proc/meminfo"); - foreach (string s in l) - { - if (s.StartsWith("MemFree:")) - { - var m = Regex.Match(s, "MemFree:(\\s*)(\\d*) kB"); - if (m.Success) - { - long val; - if (long.TryParse(m.Groups[2].Value, out val)) - { - return val / 1024; - } - } - } - } - } - return -1; - } - else - { - var pc = new PerformanceCounter("Memory", "Available MBytes"); - return pc.RawValue; - } - } - - /// - /// Returns the total amount of installed RAM, in gigabytes. - /// - /// Whether or not this program is being executed in a Mono runtime - /// Total RAM memory amount, in gigabytes - public long GetTotalSystemRam(bool isMono) - { - if (totalMem != 0) - { - return totalMem; //Return early - } - - if (isMono) //Set totalMem so it can be returned later - { - var pc = new PerformanceCounter("Mono Memory", "Total Physical Memory"); - totalMem = (pc.RawValue / 1024 / 1024 / 1024); - } - else - { - GetPhysicallyInstalledSystemMemory(out totalMem); - totalMem = (totalMem / 1024 / 1024); // Super hardcore maths to convert to Gb from Kb - } - - return totalMem; - } - } - /// - /// Holding information regarding loaded plugins - /// - public struct PluginItem - { - /// - /// Plugin name - /// - public string name; - /// - /// Assembly version - /// - public string version; - /// - /// Api version or UNKNOWN if attribute is missing, which is currently impossible - /// - public string apiVersion; - } - - /// - /// Contains JSON-Serializable information about a server - /// - public struct JsonData - { - /// - /// The port the server is running on - /// - public int port; - /// - /// The number of players currently on the server - /// - public int currentPlayers; - /// - /// The maximum number of player slots available on the server - /// - public int maxPlayers; - /// - /// The amount of RAM installed on the server's host PC - /// - public long systemRam; - /// - /// The TShock version being used by the server - /// - public string version; - /// - /// Whether or not server was started with --ignoreversion - /// - public bool ignorePluginVersion; - /// - /// List of loaded plugins and version name: - /// - public PluginItem[] loadedPlugins; - /// - /// The Terraria version supported by the server - /// - public string terrariaVersion; - /// - /// The provider ID set for the server - /// - public string providerId; - /// - /// The server ID set for the server - /// - public string serverId; - /// - /// Whether or not the server is running with Mono - /// - public bool mono; - } -} diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index d309bd07..b851e720 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -116,8 +116,6 @@ namespace TShockAPI public static RestManager RestManager; /// Utils - Static reference to the utilities class, which contains a variety of utility functions. public static Utils Utils = Utils.Instance; - /// StatTracker - Static reference to the stat tracker, which sends some server metrics every 5 minutes. - public static StatTracker StatTracker = new StatTracker(); /// UpdateManager - Static reference to the update checker, which checks for updates and notifies server admins of updates. public static UpdateManager UpdateManager; /// Log - Static reference to the log system, which outputs to either SQL or a text file, depending on user config. @@ -328,12 +326,6 @@ namespace TShockAPI Log.ConsoleInfo("TShock {0} ({1}) now running.", Version, VersionCodename); - var systemRam = StatTracker.GetFreeSystemRam(ServerApi.RunningMono); - if (systemRam > -1 && systemRam < 2048) - { - Log.ConsoleError("This machine has less than 2 gigabytes of RAM free. Be advised that it might not be enough to run TShock."); - } - ServerApi.Hooks.GamePostInitialize.Register(this, OnPostInit); ServerApi.Hooks.GameUpdate.Register(this, OnUpdate); ServerApi.Hooks.GameHardmodeTileUpdate.Register(this, OnHardUpdate); @@ -757,13 +749,11 @@ namespace TShockAPI } }) - .AddFlag("--provider-token", (token) => StatTracker.ProviderToken = token) //Flags without arguments .AddFlag("-logclear", () => LogClear = true) .AddFlag("-autoshutdown", () => Main.instance.EnableAutoShutdown()) - .AddFlag("-dump", () => Utils.Dump()) - .AddFlag("--stats-optout", () => StatTracker.OptOut = true); + .AddFlag("-dump", () => Utils.Dump()); CliParser.ParseFromSource(parms); } @@ -896,7 +886,6 @@ namespace TShockAPI } UpdateManager = new UpdateManager(); - StatTracker.Start(); } /// LastCheck - Used to keep track of the last check for basically all time based checks. diff --git a/TShockAPI/TShockAPI.csproj b/TShockAPI/TShockAPI.csproj index b7bf6f8e..230443c0 100644 --- a/TShockAPI/TShockAPI.csproj +++ b/TShockAPI/TShockAPI.csproj @@ -144,7 +144,6 @@ -