tgstation-server  4.4.0
The /tg/station 13 server suite
BridgeController.cs
Go to the documentation of this file.
1 using Microsoft.AspNetCore.Mvc;
2 using Microsoft.Extensions.Hosting;
3 using Microsoft.Extensions.Logging;
4 using Newtonsoft.Json;
5 using Serilog.Context;
6 using System;
7 using System.Net;
8 using System.Net.Mime;
9 using System.Threading;
10 using System.Threading.Tasks;
13 
14 namespace Tgstation.Server.Host.Controllers
15 {
19  [Route("Bridge")]
20  [Produces(MediaTypeNames.Application.Json)]
22  {
26  static long requestsProcessed;
27 
32 
36  readonly ILogger<BridgeController> logger;
37 
44  public BridgeController(IBridgeDispatcher bridgeDispatcher, IHostApplicationLifetime applicationLifetime, ILogger<BridgeController> logger)
45  {
46  this.bridgeDispatcher = bridgeDispatcher ?? throw new ArgumentNullException(nameof(bridgeDispatcher));
47  if (applicationLifetime == null)
48  throw new ArgumentNullException(nameof(applicationLifetime));
49 
50  this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
51 
52  applicationLifetime.ApplicationStopped.Register(() => requestsProcessed = 0);
53  }
54 
61  [HttpGet]
62  public async Task<IActionResult> Process([FromQuery]string data, CancellationToken cancellationToken)
63  {
64  // Nothing to see here
65  if (!IPAddress.IsLoopback(Request.HttpContext.Connection.RemoteIpAddress))
66  return NotFound();
67 
68  using (LogContext.PushProperty("Bridge", Interlocked.Increment(ref requestsProcessed)))
69  {
70  BridgeParameters request;
71  try
72  {
73  request = JsonConvert.DeserializeObject<BridgeParameters>(data, DMApiConstants.SerializerSettings);
74  }
75  catch
76  {
77  logger.LogWarning("Error deserializing bridge request: {0}", data);
78  return BadRequest();
79  }
80 
81  logger.LogTrace("Bridge Request: {0}", data);
82 
83  var response = await bridgeDispatcher.ProcessBridgeRequest(request, cancellationToken).ConfigureAwait(false);
84  if (response == null)
85  Forbid();
86 
87  var responseJson = JsonConvert.SerializeObject(response, DMApiConstants.SerializerSettings);
88  logger.LogTrace("Bridge Response: {0}", responseJson);
89  return Content(responseJson, MediaTypeNames.Application.Json);
90  }
91  }
92  }
93 }
async Task< IActionResult > Process([FromQuery]string data, CancellationToken cancellationToken)
Processes a bridge request.
BridgeController(IBridgeDispatcher bridgeDispatcher, IHostApplicationLifetime applicationLifetime, ILogger< BridgeController > logger)
Initializes a new instance of the BridgeController .
static long requestsProcessed
Static counter for the number of requests processed.
Constants used for communication with the DMAPI
readonly IBridgeDispatcher bridgeDispatcher
The IBridgeDispatcher for the BridgeController
static readonly JsonSerializerSettings SerializerSettings
JsonSerializerSettings for use when communicating with the DMAPI.
Controller for recieving DMAPI requests from DreamDaemon.
readonly ILogger< BridgeController > logger
The ILogger for the BridgeController