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