1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Don't allow assemblies in the plugin folder to inject protected services

This commit is contained in:
Robert 2019-10-31 20:07:10 +01:00
parent 8777e8975f
commit ae41c6cac3
3 changed files with 49 additions and 81 deletions

View File

@ -1,8 +1,12 @@
using Artemis.Core.Exceptions; using System;
using System.IO;
using System.Linq;
using Artemis.Core.Exceptions;
using Artemis.Core.Plugins.Models; using Artemis.Core.Plugins.Models;
using Artemis.Core.Services; using Artemis.Core.Services;
using Artemis.Core.Services.Interfaces; using Artemis.Core.Services.Interfaces;
using Artemis.Storage.Repositories.Interfaces; using Artemis.Storage.Repositories.Interfaces;
using Ninject.Activation;
using Ninject.Extensions.Conventions; using Ninject.Extensions.Conventions;
using Ninject.Modules; using Ninject.Modules;
using Serilog; using Serilog;
@ -26,14 +30,14 @@ namespace Artemis.Core.Ninject
.Configure(c => c.InSingletonScope()); .Configure(c => c.InSingletonScope());
}); });
// Bind all protected services as singletons TODO: Protect 'em // Bind all protected services as singletons
Kernel.Bind(x => Kernel.Bind(x =>
{ {
x.FromThisAssembly() x.FromThisAssembly()
.SelectAllClasses() .SelectAllClasses()
.InheritedFrom<IProtectedArtemisService>() .InheritedFrom<IProtectedArtemisService>()
.BindAllInterfaces() .BindAllInterfaces()
.Configure(c => c.InSingletonScope()); .Configure(c => c.When(HasAccessToProtectedService).InSingletonScope());
}); });
// Bind all repositories as singletons // Bind all repositories as singletons
@ -49,5 +53,10 @@ namespace Artemis.Core.Ninject
Kernel.Bind<PluginSettings>().ToProvider<PluginSettingsProvider>(); Kernel.Bind<PluginSettings>().ToProvider<PluginSettingsProvider>();
Kernel.Bind<ILogger>().ToProvider<LoggerProvider>(); Kernel.Bind<ILogger>().ToProvider<LoggerProvider>();
} }
private bool HasAccessToProtectedService(IRequest r)
{
return r.ParentRequest != null && !r.ParentRequest.Service.Assembly.Location.StartsWith(Path.Combine(Constants.DataFolder, "plugins"));
}
} }
} }

View File

@ -1,69 +1,39 @@
using System; using System;
using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq;
namespace Artemis.Plugins.Modules.General namespace Artemis.Plugins.Modules.General
{ {
public static class ColorHelpers public static class ColorHelpers
{ {
private static readonly Random _rand = new Random(); private static readonly Random Rand = new Random();
/// <summary> public static int GetRandomHue()
/// Comes up with a 'pure' psuedo-random color
/// </summary>
/// <returns>The color</returns>
public static Color GetRandomRainbowColor()
{ {
var colors = new List<int>(); return Rand.Next(0, 360);
for (var i = 0; i < 3; i++)
colors.Add(_rand.Next(0, 256));
var highest = colors.Max();
var lowest = colors.Min();
colors[colors.FindIndex(c => c == highest)] = 255;
colors[colors.FindIndex(c => c == lowest)] = 0;
var returnColor = Color.FromArgb(255, colors[0], colors[1], colors[2]);
return returnColor;
} }
public static Color ShiftColor(Color c, int shiftAmount) public static Color ColorFromHSV(double hue, double saturation, double value)
{ {
int newRed = c.R; var hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
int newGreen = c.G; var f = hue / 60 - Math.Floor(hue / 60);
int newBlue = c.B;
// Red to purple value = value * 255;
if (c.R == 255 && c.B < 255 && c.G == 0) var v = Convert.ToInt32(value);
newBlue = newBlue + shiftAmount; var p = Convert.ToInt32(value * (1 - saturation));
// Purple to blue var q = Convert.ToInt32(value * (1 - f * saturation));
else if (c.B == 255 && c.R > 0) var t = Convert.ToInt32(value * (1 - (1 - f) * saturation));
newRed = newRed - shiftAmount;
// Blue to light-blue
else if (c.B == 255 && c.G < 255)
newGreen = newGreen + shiftAmount;
// Light-blue to green
else if (c.G == 255 && c.B > 0)
newBlue = newBlue - shiftAmount;
// Green to yellow
else if (c.G == 255 && c.R < 255)
newRed = newRed + shiftAmount;
// Yellow to red
else if (c.R == 255 && c.G > 0)
newGreen = newGreen - shiftAmount;
newRed = BringIntInColorRange(newRed); if (hi == 0)
newGreen = BringIntInColorRange(newGreen); return Color.FromArgb(255, v, t, p);
newBlue = BringIntInColorRange(newBlue); if (hi == 1)
return Color.FromArgb(255, q, v, p);
return Color.FromArgb(c.A, newRed, newGreen, newBlue); if (hi == 2)
} return Color.FromArgb(255, p, v, t);
if (hi == 3)
private static int BringIntInColorRange(int i) return Color.FromArgb(255, p, q, v);
{ if (hi == 4)
return Math.Min(255, Math.Max(0, i)); return Color.FromArgb(255, t, p, v);
return Color.FromArgb(255, v, p, q);
} }
} }
} }

View File

@ -4,7 +4,6 @@ using System.Linq;
using Artemis.Core.Models.Surface; using Artemis.Core.Models.Surface;
using Artemis.Core.Plugins.Abstract; using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Plugins.Models; using Artemis.Core.Plugins.Models;
using Artemis.Core.Services;
using Artemis.Plugins.Modules.General.ViewModels; using Artemis.Plugins.Modules.General.ViewModels;
using Stylet; using Stylet;
@ -14,17 +13,20 @@ namespace Artemis.Plugins.Modules.General
{ {
private readonly PluginSettings _settings; private readonly PluginSettings _settings;
public GeneralModule(PluginInfo pluginInfo, PluginSettings settings, ISettingsService settingsService) : base(pluginInfo) public GeneralModule(PluginInfo pluginInfo, PluginSettings settings) : base(pluginInfo)
{ {
_settings = settings; _settings = settings;
DisplayName = "General"; DisplayName = "General";
ExpandsMainDataModel = true; ExpandsMainDataModel = true;
var testSetting = _settings.GetSetting("TestSetting", DateTime.Now); var testSetting = _settings.GetSetting("TestSetting", DateTime.Now);
Colors = new Color[1000];
Hues = new int[1000];
for (var i = 0; i < 1000; i++)
Hues[i] = ColorHelpers.GetRandomHue();
} }
public Color[] Colors { get; set; } public int[] Hues { get; set; }
public override void EnablePlugin() public override void EnablePlugin()
{ {
@ -36,20 +38,21 @@ namespace Artemis.Plugins.Modules.General
public override void Update(double deltaTime) public override void Update(double deltaTime)
{ {
for (var i = 0; i < Colors.Length; i++) for (var i = 0; i < Hues.Length; i++)
{ {
var color = Colors[i]; Hues[i]++;
Colors[i] = ColorHelpers.ShiftColor(color, (int) (deltaTime * 200)); if (Hues[i] > 360)
Hues[i] = 0;
} }
} }
public override void Render(double deltaTime, Surface surface, Graphics graphics) public override void Render(double deltaTime, Surface surface, Graphics graphics)
{ {
// Per-device coloring, slower // Per-device coloring, slower
// RenderPerDevice(surface, graphics); RenderPerDevice(surface, graphics);
// Per-LED coloring, slowest // Per-LED coloring, slowest
RenderPerLed(surface, graphics); // RenderPerLed(surface, graphics);
} }
public void RenderFullSurface(Surface surface, Graphics graphics) public void RenderFullSurface(Surface surface, Graphics graphics)
@ -61,14 +64,7 @@ namespace Artemis.Plugins.Modules.General
var index = 0; var index = 0;
foreach (var device in surface.Devices) foreach (var device in surface.Devices)
{ {
var color = Colors[index]; graphics.FillRectangle(new SolidBrush(ColorHelpers.ColorFromHSV(Hues[index], 1, 1)), device.RenderRectangle);
if (color.A == 0)
{
color = ColorHelpers.GetRandomRainbowColor();
Colors[index] = color;
}
graphics.FillRectangle(new SolidBrush(color), device.RenderRectangle);
index++; index++;
} }
} }
@ -78,14 +74,7 @@ namespace Artemis.Plugins.Modules.General
var index = 0; var index = 0;
foreach (var led in surface.Devices.SelectMany(d => d.Leds)) foreach (var led in surface.Devices.SelectMany(d => d.Leds))
{ {
var color = Colors[index]; graphics.FillRectangle(new SolidBrush(ColorHelpers.ColorFromHSV(Hues[index], 1, 1)), led.AbsoluteRenderRectangle);
if (color.A == 0)
{
color = ColorHelpers.GetRandomRainbowColor();
Colors[index] = color;
}
graphics.FillRectangle(new SolidBrush(color), led.AbsoluteRenderRectangle);
index++; index++;
} }
} }