[230] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Net;
|
---|
[250] | 4 | using System.Reflection;
|
---|
[325] | 5 | using AllocsFixes.NetConnections.Servers.Web.API;
|
---|
[230] | 6 |
|
---|
[325] | 7 | namespace AllocsFixes.NetConnections.Servers.Web.Handlers {
|
---|
[244] | 8 | public class ApiHandler : PathHandler {
|
---|
[325] | 9 | private readonly Dictionary<string, WebAPI> apis = new Dictionary<string, WebAPI> ();
|
---|
| 10 | private readonly string staticPart;
|
---|
[230] | 11 |
|
---|
[251] | 12 | public ApiHandler (string staticPart, string moduleName = null) : base (moduleName) {
|
---|
[230] | 13 | this.staticPart = staticPart;
|
---|
[250] | 14 |
|
---|
| 15 | foreach (Type t in Assembly.GetExecutingAssembly ().GetTypes ()) {
|
---|
[325] | 16 | if (!t.IsAbstract && t.IsSubclassOf (typeof (WebAPI))) {
|
---|
[251] | 17 | ConstructorInfo ctor = t.GetConstructor (new Type [0]);
|
---|
[250] | 18 | if (ctor != null) {
|
---|
[325] | 19 | WebAPI apiInstance = (WebAPI) ctor.Invoke (new object [0]);
|
---|
[250] | 20 | addApi (t.Name.ToLower (), apiInstance);
|
---|
| 21 | }
|
---|
| 22 | }
|
---|
[325] | 23 | }
|
---|
| 24 |
|
---|
| 25 | // Add dummy types
|
---|
| 26 | Type dummy_t = typeof (Null);
|
---|
| 27 | ConstructorInfo dummy_ctor = dummy_t.GetConstructor (new Type [0]);
|
---|
| 28 | if (dummy_ctor != null) {
|
---|
| 29 | WebAPI dummy_apiInstance = (WebAPI) dummy_ctor.Invoke (new object[0]);
|
---|
| 30 |
|
---|
| 31 | // Permissions that don't map to a real API
|
---|
| 32 | addApi ("viewallclaims", dummy_apiInstance);
|
---|
| 33 | addApi ("viewallplayers", dummy_apiInstance);
|
---|
| 34 | }
|
---|
[230] | 35 | }
|
---|
| 36 |
|
---|
[244] | 37 | private void addApi (string _apiName, WebAPI _api) {
|
---|
| 38 | apis.Add (_apiName, _api);
|
---|
[279] | 39 | WebPermissions.Instance.AddKnownModule ("webapi." + _apiName, _api.DefaultPermissionLevel ());
|
---|
[244] | 40 | }
|
---|
| 41 |
|
---|
[325] | 42 | public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user,
|
---|
| 43 | int permissionLevel) {
|
---|
[230] | 44 | string apiName = req.Url.AbsolutePath.Remove (0, staticPart.Length);
|
---|
[244] | 45 | if (!AuthorizeForCommand (apiName, user, permissionLevel)) {
|
---|
[325] | 46 | resp.StatusCode = (int) HttpStatusCode.Forbidden;
|
---|
[244] | 47 | if (user != null) {
|
---|
[247] | 48 | //Log.Out ("ApiHandler: user '{0}' not allowed to execute '{1}'", user.SteamID, apiName);
|
---|
[244] | 49 | }
|
---|
[325] | 50 |
|
---|
[244] | 51 | return;
|
---|
[325] | 52 | }
|
---|
| 53 |
|
---|
| 54 | foreach (KeyValuePair<string, WebAPI> kvp in apis) {
|
---|
| 55 | if (apiName.StartsWith (kvp.Key)) {
|
---|
| 56 | try {
|
---|
| 57 | kvp.Value.HandleRequest (req, resp, user, permissionLevel);
|
---|
| 58 | return;
|
---|
| 59 | } catch (Exception e) {
|
---|
| 60 | Log.Error ("Error in ApiHandler.HandleRequest(): Handler {0} threw an exception:", kvp.Key);
|
---|
| 61 | Log.Exception (e);
|
---|
| 62 | resp.StatusCode = (int) HttpStatusCode.InternalServerError;
|
---|
| 63 | return;
|
---|
[230] | 64 | }
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
[325] | 67 |
|
---|
[230] | 68 | Log.Out ("Error in ApiHandler.HandleRequest(): No handler found for API \"" + apiName + "\"");
|
---|
[325] | 69 | resp.StatusCode = (int) HttpStatusCode.NotFound;
|
---|
[230] | 70 | }
|
---|
| 71 |
|
---|
[244] | 72 | private bool AuthorizeForCommand (string apiName, WebConnection user, int permissionLevel) {
|
---|
| 73 | return WebPermissions.Instance.ModuleAllowedWithLevel ("webapi." + apiName, permissionLevel);
|
---|
[230] | 74 | }
|
---|
| 75 | }
|
---|
[325] | 76 | }
|
---|