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