| 1 | using System;
 | 
|---|
| 2 | using System.Net;
 | 
|---|
| 3 | using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;
 | 
|---|
| 4 | using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
 | 
|---|
| 5 | 
 | 
|---|
| 6 | namespace AllocsFixes.NetConnections.Servers.Web.API {
 | 
|---|
| 7 |         public class ExecuteConsoleCommand : WebAPI {
 | 
|---|
| 8 |                 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
 | 
|---|
| 9 |                         int _permissionLevel) {
 | 
|---|
| 10 |                         if (string.IsNullOrEmpty (_req.QueryString ["command"])) {
 | 
|---|
| 11 |                                 _resp.StatusCode = (int) HttpStatusCode.BadRequest;
 | 
|---|
| 12 |                                 Web.SetResponseTextContent (_resp, "No command given");
 | 
|---|
| 13 |                                 return;
 | 
|---|
| 14 |                         }
 | 
|---|
| 15 | 
 | 
|---|
| 16 |                         WebCommandResult.ResultType responseType =
 | 
|---|
| 17 |                                 _req.QueryString ["raw"] != null
 | 
|---|
| 18 |                                         ? WebCommandResult.ResultType.Raw
 | 
|---|
| 19 |                                         : (_req.QueryString ["simple"] != null
 | 
|---|
| 20 |                                                 ? WebCommandResult.ResultType.ResultOnly
 | 
|---|
| 21 |                                                 : WebCommandResult.ResultType.Full);
 | 
|---|
| 22 | 
 | 
|---|
| 23 |                         string commandline = _req.QueryString ["command"];
 | 
|---|
| 24 |                         string commandPart = commandline.Split (' ') [0];
 | 
|---|
| 25 |                         string argumentsPart = commandline.Substring (Math.Min (commandline.Length, commandPart.Length + 1));
 | 
|---|
| 26 | 
 | 
|---|
| 27 |                         IConsoleCommand command = SdtdConsole.Instance.GetCommand (commandline);
 | 
|---|
| 28 | 
 | 
|---|
| 29 |                         if (command == null) {
 | 
|---|
| 30 |                                 _resp.StatusCode = (int) HttpStatusCode.NotFound;
 | 
|---|
| 31 |                                 Web.SetResponseTextContent (_resp, "Unknown command");
 | 
|---|
| 32 |                                 return;
 | 
|---|
| 33 |                         }
 | 
|---|
| 34 | 
 | 
|---|
| 35 |                         int commandPermissionLevel = GameManager.Instance.adminTools.GetCommandPermissionLevel (command.GetCommands ());
 | 
|---|
| 36 | 
 | 
|---|
| 37 |                         if (_permissionLevel > commandPermissionLevel) {
 | 
|---|
| 38 |                                 _resp.StatusCode = (int) HttpStatusCode.Forbidden;
 | 
|---|
| 39 |                                 Web.SetResponseTextContent (_resp, "You are not allowed to execute this command");
 | 
|---|
| 40 |                                 return;
 | 
|---|
| 41 |                         }
 | 
|---|
| 42 | 
 | 
|---|
| 43 |                         _resp.SendChunked = true;
 | 
|---|
| 44 |                         WebCommandResult wcr = new WebCommandResult (commandPart, argumentsPart, responseType, _resp);
 | 
|---|
| 45 |                         SdtdConsole.Instance.ExecuteAsync (commandline, wcr);
 | 
|---|
| 46 |                 }
 | 
|---|
| 47 | 
 | 
|---|
| 48 |                 public override int DefaultPermissionLevel () {
 | 
|---|
| 49 |                         return 2000;
 | 
|---|
| 50 |                 }
 | 
|---|
| 51 |         }
 | 
|---|
| 52 | }
 | 
|---|