Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 47 additions & 8 deletions src/c#/GeneralUpdate.Core/Configuration/Environments.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,63 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace GeneralUpdate.Core.Configuration;

/// <summary>
/// Secure IPC environment variable provider.
/// AES-encrypted temp files in a dedicated subdirectory, auto-deleted after read.
/// </summary>
public static class Environments
{
// Fixed key/IV derived from a constant — not crypto-grade, but sufficient for
// ephemeral IPC where the file lives < 1 second and is in a per-user directory.
private static readonly byte[] _aesKey = SHA256.Create()
.ComputeHash(Encoding.UTF8.GetBytes("GeneralUpdate.IPC.EnvironmentProvider.v1"));
private static readonly byte[] _aesIV = new byte[16] { 0x47, 0x55, 0x50, 0x44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

private static string IpcDir
{
get
{
var dir = Path.Combine(Path.GetTempPath(), "GeneralUpdate", "ipc");
Directory.CreateDirectory(dir);
return dir;
}
}

public static void SetEnvironmentVariable(string key, string value)
{
var filePath = Path.Combine(Path.GetTempPath(), $"{key}.txt");
File.WriteAllText(filePath, value);
var filePath = Path.Combine(IpcDir, $"{key}.enc");
var plainBytes = Encoding.UTF8.GetBytes(value);
using var aes = Aes.Create();
aes.Key = _aesKey;
aes.IV = _aesIV;
using var encryptor = aes.CreateEncryptor();
var encrypted = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
File.WriteAllBytes(filePath, encrypted);
}

public static string GetEnvironmentVariable(string key)
{
var filePath = Path.Combine(Path.GetTempPath(), $"{key}.txt");
var filePath = Path.Combine(Path.GetTempPath(), "GeneralUpdate", "ipc", $"{key}.enc");
if (!File.Exists(filePath))
return string.Empty;

var content = File.ReadAllText(filePath);
File.SetAttributes(filePath, FileAttributes.Normal);
File.Delete(filePath);
return content;
try
{
var encrypted = File.ReadAllBytes(filePath);
using var aes = Aes.Create();
aes.Key = _aesKey;
aes.IV = _aesIV;
using var decryptor = aes.CreateDecryptor();
var plainBytes = decryptor.TransformFinalBlock(encrypted, 0, encrypted.Length);
return Encoding.UTF8.GetString(plainBytes);
}
finally
{
try { File.Delete(filePath); } catch { /* best-effort cleanup */ }
}
}
}
}
19 changes: 2 additions & 17 deletions src/c#/GeneralUpdate.Core/Event/EventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,13 @@ namespace GeneralUpdate.Core.Event
{
public class EventManager : IDisposable
{
private static readonly object _lockObj = new();
private static EventManager _instance;
private static readonly Lazy<EventManager> _lazy = new(() => new EventManager());
private Dictionary<Type, Delegate> _dicDelegates = new();
private bool _disposed = false;

private EventManager() { }

public static EventManager Instance
{
get
{
if (_instance == null)
{
lock (_lockObj)
{
if (_instance == null)
_instance = new EventManager();
}
}
return _instance;
}
}
public static EventManager Instance => _lazy.Value;

public void AddListener<TEventArgs>(Action<object, TEventArgs> listener) where TEventArgs : EventArgs
{
Expand Down