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 { /// /// 설정 — %APPDATA%\DBXNotifier\settings.json. 연결 토큰은 Windows DPAPI(현재 사용자)로 암호화해 저장하므로 /// 파일을 다른 PC·다른 사용자로 복사해도 쓸 수 없다. /// 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>(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 { ["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 { // 저장 실패는 다음 저장 때 다시 } } } /// Windows 시작 시 실행 — 현재 사용자 Run 키(관리자 권한 불필요). 사용자가 켜고 끈다. 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 — 메뉴 체크 표시만 그대로 } } /// exe 를 다른 폴더로 옮겼으면 등록된 경로를 지금 위치로. 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); } } } }