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