Files
king 06af036f86 feat(project): PC tray notifier for mentions and assignments
Windows tray app (desktop/notifier, C# .NET Framework 4.8 WinForms,
single exe, no installer/admin) that shows the same big alert popup as
the web even when the browser is in the background: layered topmost
window that does not steal focus, blinking border, open/close actions,
sound, autostart toggle, DPAPI-encrypted token.

Server side (project_db migration 023):
- notifier_links / notifier_tokens (SHA-256 hashes only)
- device link flow: link/start -> browser approve at /project/notifier
  -> link/poll issues token once
- Bearer API for alerts/read/logout reusing _alerts_payload; token
  re-checks current user record and project permission each call
- /project/notifier page (approve, connected PCs, revoke, download)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 10:21:58 +09:00

135 lines
5.2 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Web.Script.Serialization;
using Microsoft.Win32;
using System.Windows.Forms;
namespace DbxNotifier
{
/// <summary>
/// 설정 — %APPDATA%\DBXNotifier\settings.json. 연결 토큰은 Windows DPAPI(현재 사용자)로 암호화해 저장하므로
/// 파일을 다른 PC·다른 사용자로 복사해도 쓸 수 없다.
/// </summary>
internal sealed class Settings
{
public const string DefaultServer = "https://dbx.no1king.freeddns.org";
public string Server = DefaultServer;
public string Token = ""; // 원문은 메모리에서만
public long LastSeen = -1; // 이 번호까지는 본 알림(닫기 누른 것). -1 = 처음(지금 최신을 기준으로)
public bool Sound = true;
public string UserName = "";
private static readonly byte[] Entropy = Encoding.UTF8.GetBytes("DBXNotifier.v1");
// DBX_NOTIFIER_DIR — 테스트용 설정 폴더(없으면 %APPDATA%\DBXNotifier)
private static string Dir =>
Environment.GetEnvironmentVariable("DBX_NOTIFIER_DIR") is string d && d.Length > 0
? d
: Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "DBXNotifier");
private static string FilePath => Path.Combine(Dir, "settings.json");
public bool Connected => Token.Length > 0;
public static Settings Load()
{
var s = new Settings();
try
{
if (!File.Exists(FilePath)) return s;
var d = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(File.ReadAllText(FilePath, Encoding.UTF8));
if (d == null) return s;
if (d.TryGetValue("server", out var v) && v is string server && server.Length > 0) s.Server = server;
if (d.TryGetValue("last_seen", out v) && v != null) s.LastSeen = Convert.ToInt64(v);
if (d.TryGetValue("sound", out v) && v is bool sound) s.Sound = sound;
if (d.TryGetValue("user_name", out v) && v is string name) s.UserName = name;
if (d.TryGetValue("token", out v) && v is string enc && enc.Length > 0)
{
byte[] raw = ProtectedData.Unprotect(Convert.FromBase64String(enc), Entropy, DataProtectionScope.CurrentUser);
s.Token = Encoding.UTF8.GetString(raw);
}
}
catch
{
// 깨진 설정은 처음 상태로 — 다시 연결하면 된다.
}
return s;
}
public void Save()
{
try
{
Directory.CreateDirectory(Dir);
string enc = Token.Length == 0
? ""
: Convert.ToBase64String(ProtectedData.Protect(Encoding.UTF8.GetBytes(Token), Entropy, DataProtectionScope.CurrentUser));
var d = new Dictionary<string, object>
{
["server"] = Server,
["last_seen"] = LastSeen,
["sound"] = Sound,
["user_name"] = UserName,
["token"] = enc,
};
string tmp = FilePath + ".tmp";
File.WriteAllText(tmp, new JavaScriptSerializer().Serialize(d), new UTF8Encoding(false));
if (File.Exists(FilePath)) File.Replace(tmp, FilePath, null);
else File.Move(tmp, FilePath);
}
catch
{
// 저장 실패는 다음 저장 때 다시
}
}
}
/// <summary>Windows 시작 시 실행 — 현재 사용자 Run 키(관리자 권한 불필요). 사용자가 켜고 끈다.</summary>
internal static class AutoStart
{
private const string RunKey = @"Software\Microsoft\Windows\CurrentVersion\Run";
private const string Name = "DBXNotifier";
private static string Command => "\"" + Application.ExecutablePath + "\" --autostart";
public static bool Enabled
{
get
{
using (var k = Registry.CurrentUser.OpenSubKey(RunKey))
return k?.GetValue(Name) is string;
}
}
public static void Set(bool on)
{
try
{
using (var k = Registry.CurrentUser.CreateSubKey(RunKey))
{
if (on) k.SetValue(Name, Command);
else k.DeleteValue(Name, false);
}
}
catch
{
// 정책으로 막힌 PC — 메뉴 체크 표시만 그대로
}
}
/// <summary>exe 를 다른 폴더로 옮겼으면 등록된 경로를 지금 위치로.</summary>
public static void RefreshPath()
{
using (var k = Registry.CurrentUser.OpenSubKey(RunKey))
{
if (k?.GetValue(Name) is string cur && !string.Equals(cur, Command, StringComparison.OrdinalIgnoreCase))
Set(true);
}
}
}
}