source: binary-improvements/MapRendering/Web/API/ExecuteConsoleCommand.cs@ 322

Last change on this file since 322 was 306, checked in by alloc, 7 years ago

Fixes update A16.2

File size: 1.8 KB
Line 
1using AllocsFixes.JSON;
2using AllocsFixes.PersistentData;
3using System;
4using System.Collections.Generic;
5using System.Net;
6
7namespace AllocsFixes.NetConnections.Servers.Web.API
8{
9 public class ExecuteConsoleCommand : WebAPI
10 {
11 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel) {
12 if (string.IsNullOrEmpty (req.QueryString ["command"])) {
13 resp.StatusCode = (int)HttpStatusCode.BadRequest;
14 Web.SetResponseTextContent (resp, "No command given");
15 return;
16 }
17
18 WebCommandResult.ResultType responseType =
19 req.QueryString ["raw"] != null ? WebCommandResult.ResultType.Raw :
20 (req.QueryString ["simple"] != null ? 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.NotImplemented;
31 Web.SetResponseTextContent (resp, "Unknown command");
32 return;
33 }
34
35 AdminToolsCommandPermissions atcp = GameManager.Instance.adminTools.GetAdminToolsCommandPermission (command.GetCommands ());
36
37 if (permissionLevel > atcp.PermissionLevel) {
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}
53
Note: See TracBrowser for help on using the repository browser.