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