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