diff --git a/TShockAPI/ConfigFile.cs b/TShockAPI/ConfigFile.cs index 3faf935f..ed142ff0 100644 --- a/TShockAPI/ConfigFile.cs +++ b/TShockAPI/ConfigFile.cs @@ -187,6 +187,12 @@ namespace TShockAPI [Description("This will announce a player's location on join")] public bool EnableGeoIP = false; + [Description("This will turn on a token requirement for the /status API endpoint.")] + public bool EnableTokenEndpointAuthentication = false; + + [Description("This is used when the API endpoint /status is queried.")] + public string ServerNickname = "TShock Server"; + public static ConfigFile Read(string path) { if (!File.Exists(path)) diff --git a/TShockAPI/Properties/AssemblyInfo.cs b/TShockAPI/Properties/AssemblyInfo.cs index 13fd0a20..f2f36cc1 100644 --- a/TShockAPI/Properties/AssemblyInfo.cs +++ b/TShockAPI/Properties/AssemblyInfo.cs @@ -36,5 +36,5 @@ using System.Runtime.InteropServices; // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("3.3.0.0904")] -[assembly: AssemblyFileVersion("3.3.0.0904")] +[assembly: AssemblyVersion("3.3.0.0905")] +[assembly: AssemblyFileVersion("3.3.0.0905")] diff --git a/TShockAPI/Rest.cs b/TShockAPI/Rest.cs index 21b21924..02d290b2 100644 --- a/TShockAPI/Rest.cs +++ b/TShockAPI/Rest.cs @@ -44,10 +44,12 @@ namespace TShockAPI Ip = ip; Port = port; Start(); + Console.WriteLine("TShock REST API loaded on " + ip + ":" + port + "."); } public virtual void Stop() { listener.Stop(); + Console.WriteLine("TShock REST API disabled."); } public void Register(string path, RestCommandD callback) diff --git a/TShockAPI/RestManager.cs b/TShockAPI/RestManager.cs index 2ce4abd0..608c35ee 100644 --- a/TShockAPI/RestManager.cs +++ b/TShockAPI/RestManager.cs @@ -16,14 +16,47 @@ namespace TShockAPI { public void RegisterRestfulCommands() { - Rest.Register(new RestCommand("/HelloWorld/name/{username}", usertest)); - //Rest.Register(new RestCommand("/wizard/{username}", wizard)); + Rest.Register(new RestCommand("/status", Status)); + //RegisterExamples(); } #region RestMethods + object Status(RestVerbs verbs, IParameterCollection parameters, RequestEventArgs request) + { + var ReturnBlock = new Dictionary(); + if (TShock.Config.EnableTokenEndpointAuthentication) + { + ReturnBlock.Add("status", "403"); + ReturnBlock.Add("error", "Server settings require a token for this API call."); + return ReturnBlock; + } + ReturnBlock.Add("status", "200"); + ReturnBlock.Add("name", TShock.Config.ServerNickname); + ReturnBlock.Add("port", Convert.ToString(TShock.Config.ServerPort)); + ReturnBlock.Add("playercount", Convert.ToString(TShock.Players.Count())); + string CurrentPlayers = ""; + foreach (TSPlayer tplayer in TShock.Players) + { + CurrentPlayers += tplayer.Name + ", "; + } + + ReturnBlock.Add("players", CurrentPlayers); + return ReturnBlock; + } + + #endregion + + #region RestExampleMethods + + public void RegisterExamples() + { + Rest.Register(new RestCommand("/HelloWorld/name/{username}", UserTest)); + Rest.Register(new RestCommand("/wizard/{username}", Wizard)); + } + //The Wizard example, for demonstrating the response convention: - object wizard(RestVerbs verbs, IParameterCollection parameters, RequestEventArgs request) + object Wizard(RestVerbs verbs, IParameterCollection parameters, RequestEventArgs request) { var returnBack = new Dictionary(); returnBack.Add("status", "200"); //Keep this in everything, 200 = ok, etc. Standard http status codes. @@ -33,7 +66,7 @@ namespace TShockAPI { } //http://127.0.0.1:8080/HelloWorld/name/{username}?type=status - object usertest(RestVerbs verbs, IParameterCollection parameters, RequestEventArgs request) + object UserTest(RestVerbs verbs, IParameterCollection parameters, RequestEventArgs request) { var ret = new Dictionary(); var type = parameters["type"];