using System; using System.Collections.Generic; using System.Net.Http; using System.Security.Cryptography; using System.Text; using System.Text.Json; using System.Threading.Tasks; namespace AuthEngine; public class KeyAuthApp { public string Name { get; } public string OwnerId { get; } public string Secret { get; } public string Version { get; } public string ApiUrl { get; } public string SessionId { get; private set; } = ""; public ResponseData Response { get; private set; } = new(); public UserData User { get; private set; } = new(); public AppData AppInfo { get; private set; } = new(); private readonly HttpClient _http = new() { Timeout = TimeSpan.FromSeconds(15) }; public KeyAuthApp(string name, string ownerId, string secret, string version, string apiUrl) { Name = name.Trim(); OwnerId = ownerId.Trim(); Secret = secret.Trim(); Version = version.Trim(); ApiUrl = apiUrl.Trim(); } private string GetHwid() { try { var raw = Environment.MachineName + "_" + Environment.UserName + "_" + Environment.ProcessorCount; using var sha = SHA256.Create(); var hash = sha.ComputeHash(Encoding.UTF8.GetBytes(raw)); return Convert.ToHexString(hash).ToLowerInvariant()[..32]; } catch { return "HWID-" + Environment.MachineName; } } private async Task PostAsync(Dictionary values) { try { var content = new FormUrlEncodedContent(values); var res = await _http.PostAsync(ApiUrl, content); var json = await res.Content.ReadAsStringAsync(); return JsonDocument.Parse(json); } catch (Exception ex) { Response = new ResponseData { Success = false, Message = "Network request failed: " + ex.Message }; return null; } } public void Init() => InitAsync().GetAwaiter().GetResult(); public async Task InitAsync() { var doc = await PostAsync(new() { ["type"] = "init", ["name"] = Name, ["ownerid"] = OwnerId, ["secret"] = Secret, ["ver"] = Version }); if (doc == null) return; var root = doc.RootElement; Response = new ResponseData { Success = root.GetProperty("success").GetBoolean(), Message = root.TryGetProperty("message", out var m) ? m.GetString() ?? "" : "" }; if (Response.Success) { if (root.TryGetProperty("sessionid", out var sess)) { SessionId = sess.GetString() ?? ""; } if (root.TryGetProperty("appinfo", out var app)) { AppInfo.NumUsers = app.TryGetProperty("numUsers", out var nu) ? nu.GetString() ?? "0" : "0"; AppInfo.NumKeys = app.TryGetProperty("numKeys", out var nk) ? nk.GetString() ?? "0" : "0"; AppInfo.Version = app.TryGetProperty("version", out var v) ? v.GetString() ?? "" : ""; AppInfo.DownloadLink = app.TryGetProperty("downloadLink", out var dl) ? dl.GetString() ?? "" : ""; } } } public void License(string key) => LicenseAsync(key).GetAwaiter().GetResult(); public async Task LicenseAsync(string key) { var doc = await PostAsync(new() { ["type"] = "license", ["key"] = key.Trim(), ["hwid"] = GetHwid(), ["sessionid"] = SessionId, ["name"] = Name, ["ownerid"] = OwnerId }); ParseAuthResponse(doc); } public void Login(string username, string password) => LoginAsync(username, password).GetAwaiter().GetResult(); public async Task LoginAsync(string username, string password) { var doc = await PostAsync(new() { ["type"] = "login", ["username"] = username.Trim(), ["pass"] = password, ["hwid"] = GetHwid(), ["sessionid"] = SessionId, ["name"] = Name, ["ownerid"] = OwnerId }); ParseAuthResponse(doc); } public void Register(string username, string password, string licenseKey) => RegisterAsync(username, password, licenseKey).GetAwaiter().GetResult(); public async Task RegisterAsync(string username, string password, string licenseKey) { var doc = await PostAsync(new() { ["type"] = "register", ["username"] = username.Trim(), ["pass"] = password, ["key"] = licenseKey.Trim(), ["hwid"] = GetHwid(), ["sessionid"] = SessionId, ["name"] = Name, ["ownerid"] = OwnerId }); ParseAuthResponse(doc); } public void Upgrade(string username, string licenseKey) => UpgradeAsync(username, licenseKey).GetAwaiter().GetResult(); public async Task UpgradeAsync(string username, string licenseKey) { var doc = await PostAsync(new() { ["type"] = "upgrade", ["username"] = username.Trim(), ["key"] = licenseKey.Trim(), ["sessionid"] = SessionId, ["name"] = Name, ["ownerid"] = OwnerId }); if (doc == null) return; var root = doc.RootElement; Response = new ResponseData { Success = root.GetProperty("success").GetBoolean(), Message = root.TryGetProperty("message", out var m) ? m.GetString() ?? "" : "" }; } public string GetVar(string varId) { var doc = PostAsync(new() { ["type"] = "var", ["varid"] = varId.Trim(), ["sessionid"] = SessionId, ["name"] = Name, ["ownerid"] = OwnerId }).GetAwaiter().GetResult(); if (doc == null) return ""; if (doc.RootElement.GetProperty("success").GetBoolean()) { return doc.RootElement.TryGetProperty("message", out var msg) ? msg.GetString() ?? "" : ""; } return ""; } public byte[]? DownloadFile(string fileId) { var doc = PostAsync(new() { ["type"] = "file", ["fileid"] = fileId.Trim(), ["sessionid"] = SessionId, ["name"] = Name, ["ownerid"] = OwnerId }).GetAwaiter().GetResult(); if (doc == null) return null; if (doc.RootElement.GetProperty("success").GetBoolean() && doc.RootElement.TryGetProperty("contents", out var hexElem)) { string hex = hexElem.GetString() ?? ""; return Convert.FromHexString(hex); } return null; } public bool CheckBlacklist() { var doc = PostAsync(new() { ["type"] = "checkblacklist", ["hwid"] = GetHwid(), ["sessionid"] = SessionId, ["name"] = Name, ["ownerid"] = OwnerId }).GetAwaiter().GetResult(); if (doc == null) return false; return doc.RootElement.GetProperty("success").GetBoolean(); } public void Log(string message) { _ = PostAsync(new() { ["type"] = "log", ["message"] = message, ["sessionid"] = SessionId, ["name"] = Name, ["ownerid"] = OwnerId }); } private void ParseAuthResponse(JsonDocument? doc) { if (doc == null) return; var root = doc.RootElement; Response = new ResponseData { Success = root.GetProperty("success").GetBoolean(), Message = root.TryGetProperty("message", out var m) ? m.GetString() ?? "" : "" }; if (Response.Success && root.TryGetProperty("info", out var info)) { User.Username = info.TryGetProperty("username", out var u) ? u.GetString() ?? "" : ""; User.Hwid = info.TryGetProperty("hwid", out var h) ? h.GetString() ?? "" : ""; User.Ip = info.TryGetProperty("ip", out var ip) ? ip.GetString() ?? "" : ""; User.CreateDate = info.TryGetProperty("createdate", out var cd) ? cd.GetString() ?? "" : ""; User.LastLogin = info.TryGetProperty("lastlogin", out var ll) ? ll.GetString() ?? "" : ""; User.Subscriptions.Clear(); if (info.TryGetProperty("subscriptions", out var subs) && subs.ValueKind == JsonValueKind.Array) { foreach (var item in subs.EnumerateArray()) { var sName = item.TryGetProperty("subscription", out var sn) ? sn.GetString() ?? "default" : "default"; var sKey = item.TryGetProperty("key", out var sk) ? sk.GetString() ?? "" : ""; var sExp = item.TryGetProperty("expiry", out var se) ? se.GetString() ?? "0" : "0"; var sLeft = item.TryGetProperty("timeleft", out var sl) ? sl.ToString() ?? "0" : "0"; long expUnix = long.TryParse(sExp, out var expVal) ? expVal : 0; DateTime expDate = expUnix > 0 ? DateTimeOffset.FromUnixTimeSeconds(expUnix).LocalDateTime : DateTime.MinValue; User.Subscriptions.Add(new UserSubscription { Subscription = sName, Key = sKey, ExpiryUnix = expUnix, ExpiryDate = expDate, TimeLeftSeconds = long.TryParse(sLeft, out var leftVal) ? leftVal : 0 }); } } } } } public class ResponseData { public bool Success { get; set; } public string Message { get; set; } = ""; } public class AppData { public string NumUsers { get; set; } = "0"; public string NumKeys { get; set; } = "0"; public string Version { get; set; } = ""; public string DownloadLink { get; set; } = ""; } public class UserData { public string Username { get; set; } = ""; public string Hwid { get; set; } = ""; public string Ip { get; set; } = ""; public string CreateDate { get; set; } = ""; public string LastLogin { get; set; } = ""; public List Subscriptions { get; set; } = new(); } public class UserSubscription { public string Subscription { get; set; } = ""; public string Key { get; set; } = ""; public long ExpiryUnix { get; set; } public DateTime ExpiryDate { get; set; } public long TimeLeftSeconds { get; set; } }