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