Add new REST endpoint:

- /v2/server/rawcmd

Takes parameters:
 - cmd

Commands have to start with /, some commands aren't written properly, and may not return any more output than a status code.
This commit is contained in:
Lucas Nicodemus 2012-01-02 22:14:30 -07:00
parent f04b9d7a6d
commit b8e4dffd74
4 changed files with 74 additions and 27 deletions

View file

@ -40,8 +40,8 @@ namespace TShockAPI
private int[] Compressed = new int[52];
#if DEBUG_NET
Command dump;
Command flush;
Command dump;
Command flush;
#endif
public PacketBufferer()
@ -51,10 +51,10 @@ namespace TShockAPI
buffers[i] = new PacketBuffer();
#if DEBUG_NET
dump = new Command("superadmin", Dump, "netdump");
flush = new Command("superadmin", Flush, "netflush");
Commands.ChatCommands.Add(dump);
Commands.ChatCommands.Add(flush);
dump = new Command("superadmin", Dump, "netdump");
flush = new Command("superadmin", Flush, "netflush");
Commands.ChatCommands.Add(dump);
Commands.ChatCommands.Add(flush);
#endif
NetHooks.SendBytes += ServerHooks_SendBytes;
@ -78,8 +78,8 @@ namespace TShockAPI
if (disposing)
{
#if DEBUG_NET
Commands.ChatCommands.Remove(dump);
Commands.ChatCommands.Remove(flush);
Commands.ChatCommands.Remove(dump);
Commands.ChatCommands.Remove(flush);
#endif
NetHooks.SendBytes -= ServerHooks_SendBytes;
ServerHooks.SocketReset -= ServerHooks_SocketReset;
@ -94,7 +94,7 @@ namespace TShockAPI
for (int i = 1; i < Bytes.Length; i++)
{
sb.AppendLine("{0,-25}{1,-25}{2,-25}{3}".SFormat(Enum.GetName(typeof (PacketTypes), i) + ":", Packets[i], Bytes[i],
Compressed[i]));
Compressed[i]));
}
File.WriteAllText(Path.Combine(TShock.SavePath, "dmp.txt"), sb.ToString());
}
@ -167,12 +167,12 @@ namespace TShockAPI
lock (buffers[socket.whoAmI])
{
#if DEBUG_NET
int size = (count - offset);
var pt = buffer[offset + 4];
int size = (count - offset);
var pt = buffer[offset + 4];
Packets[pt]++;
Bytes[pt] += size;
Compressed[pt] += Compress(buffer, offset, count);
Packets[pt]++;
Bytes[pt] += size;
Compressed[pt] += Compress(buffer, offset, count);
#endif
using (var ms = new MemoryStream(buffer, offset, count))
{
@ -213,17 +213,17 @@ namespace TShockAPI
}
#if DEBUG_NET
static int Compress(byte[] buffer, int offset, int count)
{
using (var ms = new MemoryStream())
{
using (var gzip = new GZipStream(ms, CompressionMode.Compress, true))
{
gzip.Write(buffer, offset, count);
}
return (int)ms.Length;
}
}
static int Compress(byte[] buffer, int offset, int count)
{
using (var ms = new MemoryStream())
{
using (var gzip = new GZipStream(ms, CompressionMode.Compress, true))
{
gzip.Write(buffer, offset, count);
}
return (int)ms.Length;
}
}
#endif
}

View file

@ -48,5 +48,5 @@ using System.Runtime.InteropServices;
// Build Number
// MMdd of the build
[assembly: AssemblyVersion("3.4.2.0101")]
[assembly: AssemblyFileVersion("3.4.2.0101")]
[assembly: AssemblyVersion("3.4.2.0102")]
[assembly: AssemblyFileVersion("3.4.2.0102")]

View file

@ -64,6 +64,7 @@ namespace TShockAPI
Rest.Register(new RestCommand("/v2/server/broadcast", Broadcast) { RequiresToken = true});
Rest.Register(new RestCommand("/v2/server/off", Off) {RequiresToken = true});
Rest.Register(new RestCommand("/v2/server/rawcmd", ServerCommand) {RequiresToken = true});
#region Deprecated Endpoints
Rest.Register(new RestCommand("/bans/read/{user}/info", BanInfo) { RequiresToken = true });
@ -82,6 +83,22 @@ namespace TShockAPI
#region RestServerMethods
private object ServerCommand(RestVerbs verbs, IParameterCollection parameters)
{
if (parameters["cmd"] != null && parameters["cmd"].Trim() != "")
{
TSRESTPlayer tr = new TSRESTPlayer();
RestObject ro = new RestObject("200");
Commands.HandleCommand(tr, parameters["cmd"]);
foreach (string s in tr.GetCommandOutput())
{
ro.Add("response", s);
}
return ro;
}
return new RestObject("500")["response"] = "Invalid cmd parameter passed to REST. Cowardly not running a blank command.";
}
private object Off(RestVerbs verbs, IParameterCollection parameters)
{
bool confirm;

View file

@ -405,6 +405,36 @@ namespace TShockAPI
}
}
public class TSRestPlayer : TSServerPlayer
{
internal List<string> CommandReturn = new List<string>();
public TSRestPlayer()
{
Group = new SuperAdminGroup();
}
public override void SendMessage(string msg)
{
SendMessage(msg, 0, 255, 0);
}
public override void SendMessage(string msg, Color color)
{
SendMessage(msg, color.R, color.G, color.B);
}
public override void SendMessage(string msg, byte red, byte green, byte blue)
{
CommandReturn.Add(msg);
}
public List<string> GetCommandOutput()
{
return CommandReturn;
}
}
public class TSServerPlayer : TSPlayer
{
public TSServerPlayer()