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