From 7cfc73ea4d0e59c9b886c23288d96d771daad8d9 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Sat, 4 Feb 2012 20:09:18 -0700 Subject: [PATCH 1/6] Added -rest-token, which will allow command line creation of a REST token. --- TShockAPI/Rest/SecureRest.cs | 4 ++++ TShockAPI/TShock.cs | 15 ++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/TShockAPI/Rest/SecureRest.cs b/TShockAPI/Rest/SecureRest.cs index bf41e2cd..976d6aae 100644 --- a/TShockAPI/Rest/SecureRest.cs +++ b/TShockAPI/Rest/SecureRest.cs @@ -43,6 +43,10 @@ namespace Rests Register(new RestCommand("/token/create/{username}/{password}", NewToken) {RequiresToken = false}); Register(new RestCommand("/v2/token/create/{password}", NewTokenV2) { RequiresToken = false }); Register(new RestCommand("/token/destroy/{token}", DestroyToken) {RequiresToken = true}); + foreach (KeyValuePair t in TShockAPI.TShock.RESTStartupTokens) + { + Tokens.Add(t.Key, t.Value); + } } private object DestroyToken(RestVerbs verbs, IParameterCollection parameters) diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index fc880bce..2eefb04f 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -64,6 +64,10 @@ namespace TShockAPI public static RestManager RestManager; public static Utils Utils = new Utils(); public static StatTracker StatTracker = new StatTracker(); + /// + /// Used for implementing REST Tokens prior to the REST system starting up. + /// + public static Dictionary RESTStartupTokens = new Dictionary(); /// /// Called after TShock is initialized. Useful for plugins that needs hooks before tshock but also depend on tshock being loaded. @@ -128,7 +132,7 @@ namespace TShockAPI ConfigFile.ConfigRead += OnConfigRead; FileTools.SetupConfig(); - HandleCommandLine_Port(Environment.GetCommandLineArgs()); + HandleCommandLinePostConfigLoad(Environment.GetCommandLineArgs()); if (Config.StorageType.ToLower() == "sqlite") { @@ -173,7 +177,7 @@ namespace TShockAPI Itembans = new ItemManager(DB); RememberedPos = new RemeberedPosManager(DB); InventoryDB = new InventoryManager(DB); - RestApi = new SecureRest(Netplay.serverListenIP, 8080); + RestApi = new SecureRest(Netplay.serverListenIP, Config.RestApiPort); RestApi.Verify += RestApi_Verify; RestApi.Port = Config.RestApiPort; RestManager = new RestManager(RestApi); @@ -351,7 +355,7 @@ namespace TShockAPI } } - private void HandleCommandLine_Port(string[] parms) + private void HandleCommandLinePostConfigLoad(string[] parms) { for (int i = 0; i < parms.Length; i++) { @@ -363,6 +367,11 @@ namespace TShockAPI OverridePort = true; Log.ConsoleInfo("Port overridden by startup argument. Set to " + port); } + if (parms[i].ToLower() == "-rest-token") + { + string token = Convert.ToString(parms[++i]); + RESTStartupTokens.Add(token, "null"); + } } } From 70e8bb9759c01530dc672cc50d2f1e6e1fb4964e Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Sat, 4 Feb 2012 20:14:34 -0700 Subject: [PATCH 2/6] Added -rest-enabled, which will define if the REST API is enabled from command line. --- TShockAPI/TShock.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 2eefb04f..1e6326c5 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -372,6 +372,10 @@ namespace TShockAPI string token = Convert.ToString(parms[++i]); RESTStartupTokens.Add(token, "null"); } + if (parms[i].ToLower() == "-rest-enabled") + { + Config.RestApiEnabled = Convert.ToBoolean(parms[++i]); + } } } From 2d12cbe398ad8bb1e6a2a63e1915b121a3b8358a Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Sat, 4 Feb 2012 20:19:04 -0700 Subject: [PATCH 3/6] Added -rest-enabled and -rest-port command line parameters --- TShockAPI/TShock.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 1e6326c5..47dc044a 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -376,6 +376,14 @@ namespace TShockAPI { Config.RestApiEnabled = Convert.ToBoolean(parms[++i]); } + if (parms[i].ToLower() == "-rest-enabled") + { + Config.RestApiEnabled = Convert.ToBoolean(parms[++i]); + } + if (parms[i].ToLower() == "-rest-port") + { + Config.RestApiPort = Convert.ToInt32(parms[++i]); + } } } From c1f62040457c2b5d948a4556141c30aad2e885d7 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Sat, 4 Feb 2012 20:19:32 -0700 Subject: [PATCH 4/6] Remove duplicated -rest-enabled flags --- TShockAPI/TShock.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 47dc044a..669ecf63 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -376,10 +376,6 @@ namespace TShockAPI { Config.RestApiEnabled = Convert.ToBoolean(parms[++i]); } - if (parms[i].ToLower() == "-rest-enabled") - { - Config.RestApiEnabled = Convert.ToBoolean(parms[++i]); - } if (parms[i].ToLower() == "-rest-port") { Config.RestApiPort = Convert.ToInt32(parms[++i]); From ccee8f3ff4ab2bd959397ce2831cdf6bdad39e51 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Sat, 4 Feb 2012 21:22:55 -0700 Subject: [PATCH 5/6] Add some debug output so people know why REST is magically enabled. --- TShockAPI/Properties/AssemblyInfo.cs | 4 ++-- TShockAPI/TShock.cs | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/TShockAPI/Properties/AssemblyInfo.cs b/TShockAPI/Properties/AssemblyInfo.cs index 857627ed..ee585f2f 100644 --- a/TShockAPI/Properties/AssemblyInfo.cs +++ b/TShockAPI/Properties/AssemblyInfo.cs @@ -48,5 +48,5 @@ using System.Runtime.InteropServices; // Build Number // MMdd of the build -[assembly: AssemblyVersion("3.6.0.0126")] -[assembly: AssemblyFileVersion("3.6.0.0126")] +[assembly: AssemblyVersion("3.6.0.0204")] +[assembly: AssemblyFileVersion("3.6.0.0204")] diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 669ecf63..30bf13f6 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -371,14 +371,19 @@ namespace TShockAPI { string token = Convert.ToString(parms[++i]); RESTStartupTokens.Add(token, "null"); + Console.WriteLine("Startup parameter overrode REST token."); } if (parms[i].ToLower() == "-rest-enabled") { - Config.RestApiEnabled = Convert.ToBoolean(parms[++i]); + Config.RestApiEnabled = true; + Console.WriteLine("Startup parameter overrode REST enable."); + } if (parms[i].ToLower() == "-rest-port") { Config.RestApiPort = Convert.ToInt32(parms[++i]); + Console.WriteLine("Startup parameter overrode REST port."); + } } } From 9df87854151465fc235d0f2a082cbb3e1ff5de3d Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Sat, 4 Feb 2012 21:24:53 -0700 Subject: [PATCH 6/6] Undid change to -rest-enabled that would limit the functionality of the startup parameter. --- TShockAPI/TShock.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 30bf13f6..6c8e8054 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -375,7 +375,7 @@ namespace TShockAPI } if (parms[i].ToLower() == "-rest-enabled") { - Config.RestApiEnabled = true; + Config.RestApiEnabled = Convert.ToBoolean(parms[++i]); Console.WriteLine("Startup parameter overrode REST enable."); }