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.Services;
using Artemis.Core.Services.Interfaces;
using Artemis.Storage.Repositories.Interfaces;
using Ninject.Activation;
using Ninject.Extensions.Conventions;
using Ninject.Modules;
using Serilog;
@ -26,14 +30,14 @@ namespace Artemis.Core.Ninject
.Configure(c => c.InSingletonScope());
});
// Bind all protected services as singletons TODO: Protect 'em
// Bind all protected services as singletons
Kernel.Bind(x =>
{
x.FromThisAssembly()
.SelectAllClasses()
.InheritedFrom<IProtectedArtemisService>()
.BindAllInterfaces()
.Configure(c => c.InSingletonScope());
.Configure(c => c.When(HasAccessToProtectedService).InSingletonScope());
});
// Bind all repositories as singletons
@ -45,9 +49,14 @@ namespace Artemis.Core.Ninject
.BindAllInterfaces()
.Configure(c => c.InSingletonScope());
});
Kernel.Bind<PluginSettings>().ToProvider<PluginSettingsProvider>();
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.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace Artemis.Plugins.Modules.General
{
public static class ColorHelpers
{
private static readonly Random _rand = new Random();
private static readonly Random Rand = new Random();
/// <summary>
/// Comes up with a 'pure' psuedo-random color
/// </summary>
/// <returns>The color</returns>
public static Color GetRandomRainbowColor()
public static int GetRandomHue()
{
var colors = new List<int>();
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;
return Rand.Next(0, 360);
}
public static Color ShiftColor(Color c, int shiftAmount)
public static Color ColorFromHSV(double hue, double saturation, double value)
{
int newRed = c.R;
int newGreen = c.G;
int newBlue = c.B;
var hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
var f = hue / 60 - Math.Floor(hue / 60);
// Red to purple
if (c.R == 255 && c.B < 255 && c.G == 0)
newBlue = newBlue + shiftAmount;
// Purple to blue
else if (c.B == 255 && c.R > 0)
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;
value = value * 255;
var v = Convert.ToInt32(value);
var p = Convert.ToInt32(value * (1 - saturation));
var q = Convert.ToInt32(value * (1 - f * saturation));
var t = Convert.ToInt32(value * (1 - (1 - f) * saturation));
newRed = BringIntInColorRange(newRed);
newGreen = BringIntInColorRange(newGreen);
newBlue = BringIntInColorRange(newBlue);
return Color.FromArgb(c.A, newRed, newGreen, newBlue);
}
private static int BringIntInColorRange(int i)
{
return Math.Min(255, Math.Max(0, i));
if (hi == 0)
return Color.FromArgb(255, v, t, p);
if (hi == 1)
return Color.FromArgb(255, q, v, p);
if (hi == 2)
return Color.FromArgb(255, p, v, t);
if (hi == 3)
return Color.FromArgb(255, p, q, v);
if (hi == 4)
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.Plugins.Abstract;
using Artemis.Core.Plugins.Models;
using Artemis.Core.Services;
using Artemis.Plugins.Modules.General.ViewModels;
using Stylet;
@ -14,17 +13,20 @@ namespace Artemis.Plugins.Modules.General
{
private readonly PluginSettings _settings;
public GeneralModule(PluginInfo pluginInfo, PluginSettings settings, ISettingsService settingsService) : base(pluginInfo)
public GeneralModule(PluginInfo pluginInfo, PluginSettings settings) : base(pluginInfo)
{
_settings = settings;
DisplayName = "General";
ExpandsMainDataModel = true;
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()
{
@ -36,20 +38,21 @@ namespace Artemis.Plugins.Modules.General
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];
Colors[i] = ColorHelpers.ShiftColor(color, (int) (deltaTime * 200));
Hues[i]++;
if (Hues[i] > 360)
Hues[i] = 0;
}
}
public override void Render(double deltaTime, Surface surface, Graphics graphics)
{
// Per-device coloring, slower
// RenderPerDevice(surface, graphics);
RenderPerDevice(surface, graphics);
// Per-LED coloring, slowest
RenderPerLed(surface, graphics);
// RenderPerLed(surface, graphics);
}
public void RenderFullSurface(Surface surface, Graphics graphics)
@ -61,14 +64,7 @@ namespace Artemis.Plugins.Modules.General
var index = 0;
foreach (var device in surface.Devices)
{
var color = Colors[index];
if (color.A == 0)
{
color = ColorHelpers.GetRandomRainbowColor();
Colors[index] = color;
}
graphics.FillRectangle(new SolidBrush(color), device.RenderRectangle);
graphics.FillRectangle(new SolidBrush(ColorHelpers.ColorFromHSV(Hues[index], 1, 1)), device.RenderRectangle);
index++;
}
}
@ -78,14 +74,7 @@ namespace Artemis.Plugins.Modules.General
var index = 0;
foreach (var led in surface.Devices.SelectMany(d => d.Leds))
{
var color = Colors[index];
if (color.A == 0)
{
color = ColorHelpers.GetRandomRainbowColor();
Colors[index] = color;
}
graphics.FillRectangle(new SolidBrush(color), led.AbsoluteRenderRectangle);
graphics.FillRectangle(new SolidBrush(ColorHelpers.ColorFromHSV(Hues[index], 1, 1)), led.AbsoluteRenderRectangle);
index++;
}
}