| 1 | using System;
|
|---|
| 2 | using System.Collections.Generic;
|
|---|
| 3 | using System.Net;
|
|---|
| 4 | using System.Reflection;
|
|---|
| 5 | using AllocsFixes.NetConnections.Servers.Web.API;
|
|---|
| 6 | using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;
|
|---|
| 7 | using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
|
|---|
| 8 |
|
|---|
| 9 | namespace AllocsFixes.NetConnections.Servers.Web.Handlers {
|
|---|
| 10 | public class ApiHandler : AbsHandler {
|
|---|
| 11 | private readonly Dictionary<string, WebAPI> apis = new CaseInsensitiveStringDictionary<WebAPI> ();
|
|---|
| 12 |
|
|---|
| 13 | public ApiHandler () : base (null) {
|
|---|
| 14 |
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | public override void SetBasePathAndParent (Web _parent, string _relativePath) {
|
|---|
| 18 | base.SetBasePathAndParent (_parent, _relativePath);
|
|---|
| 19 |
|
|---|
| 20 | Type[] apiWithParentCtorTypes = { typeof (Web) };
|
|---|
| 21 | Object[] apiWithParentCtorArgs = { _parent };
|
|---|
| 22 |
|
|---|
| 23 | Type[] apiEmptyCtorTypes = { };
|
|---|
| 24 | Object[] apiEmptyCtorArgs = { };
|
|---|
| 25 |
|
|---|
| 26 | foreach (Type t in Assembly.GetExecutingAssembly ().GetTypes ()) {
|
|---|
| 27 | if (!t.IsAbstract && t.IsSubclassOf (typeof (WebAPI))) {
|
|---|
| 28 | ConstructorInfo ctor = t.GetConstructor (apiWithParentCtorTypes);
|
|---|
| 29 | if (ctor != null) {
|
|---|
| 30 | WebAPI apiInstance = (WebAPI) ctor.Invoke (apiWithParentCtorArgs);
|
|---|
| 31 | addApi (apiInstance);
|
|---|
| 32 | continue;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | ctor = t.GetConstructor (apiEmptyCtorTypes);
|
|---|
| 36 | if (ctor != null) {
|
|---|
| 37 | WebAPI apiInstance = (WebAPI) ctor.Invoke (apiEmptyCtorArgs);
|
|---|
| 38 | addApi (apiInstance);
|
|---|
| 39 | }
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | // Permissions that don't map to a real API
|
|---|
| 44 | addApi (new Null ("viewallclaims"));
|
|---|
| 45 | addApi (new Null ("viewallplayers"));
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | private void addApi (WebAPI _api) {
|
|---|
| 49 | apis.Add (_api.Name, _api);
|
|---|
| 50 | WebPermissions.Instance.AddKnownModule ("webapi." + _api.Name, _api.DefaultPermissionLevel ());
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | #if ENABLE_PROFILER
|
|---|
| 54 | private static readonly UnityEngine.Profiling.CustomSampler apiHandlerSampler = UnityEngine.Profiling.CustomSampler.Create ("API_Handler");
|
|---|
| 55 | #endif
|
|---|
| 56 |
|
|---|
| 57 | public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con,
|
|---|
| 58 | int _permissionLevel) {
|
|---|
| 59 | string apiName = _requestPath.Remove (0, urlBasePath.Length);
|
|---|
| 60 |
|
|---|
| 61 | if (!apis.TryGetValue (apiName, out WebAPI api)) {
|
|---|
| 62 | Log.Out ($"Error in {nameof(ApiHandler)}.HandleRequest(): No handler found for API \"{apiName}\"");
|
|---|
| 63 | _resp.StatusCode = (int) HttpStatusCode.NotFound;
|
|---|
| 64 | return;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | if (!AuthorizeForApi (apiName, _permissionLevel)) {
|
|---|
| 68 | _resp.StatusCode = (int) HttpStatusCode.Forbidden;
|
|---|
| 69 | if (_con != null) {
|
|---|
| 70 | //Log.Out ($"{nameof(ApiHandler)}: user '{user.SteamID}' not allowed to execute '{apiName}'");
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | return;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | try {
|
|---|
| 77 | #if ENABLE_PROFILER
|
|---|
| 78 | apiHandlerSampler.Begin ();
|
|---|
| 79 | #endif
|
|---|
| 80 | api.HandleRequest (_req, _resp, _con, _permissionLevel);
|
|---|
| 81 | #if ENABLE_PROFILER
|
|---|
| 82 | apiHandlerSampler.End ();
|
|---|
| 83 | #endif
|
|---|
| 84 | } catch (Exception e) {
|
|---|
| 85 | Log.Error ($"Error in {nameof(ApiHandler)}.HandleRequest(): Handler {api.Name} threw an exception:");
|
|---|
| 86 | Log.Exception (e);
|
|---|
| 87 | _resp.StatusCode = (int) HttpStatusCode.InternalServerError;
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | private bool AuthorizeForApi (string _apiName, int _permissionLevel) {
|
|---|
| 92 | return WebPermissions.Instance.ModuleAllowedWithLevel ("webapi." + _apiName, _permissionLevel);
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|