using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Security.Authentication; using System.Text; using System.Text.Json; using System.Threading; using JacobTechEncryption; using JacobTechEncryption.Enums; using Luski.net.Enums; using Luski.net.JsonTypes; using Luski.net.JsonTypes.WSS; using Luski.net.Structures.Main; using Luski.net.Structures.Public; using WebSocketSharp; namespace Luski.net; public partial class MainServer { public void Login(string Email, string Password, System.Threading.CancellationToken CancellationToken) { Both(Email, Password, CancellationToken); } public void CreateAccount(string Email, string Password, string Username, string PFP, System.Threading.CancellationToken CancellationToken) { Both(Email, Password, CancellationToken, Username, PFP); } private void Both(string Email, string Password, System.Threading.CancellationToken CancellationToken, string? Username = null, string? pfp = null) { if (!ClientEncryption.Generating) { ClientEncryption.GenerateKeys(); } while (!ClientEncryption.Generated) { } login = true; Login json; List> heads = new() { new("key", ClientEncryption.MyPublicKey), new("username", Convert.ToBase64String(Encryption.RSA.Encrypt(Email, ClientEncryption.MyPublicKey, EncoderType.UTF8))), new("password", ClientEncryption.RemotePasswordEncrypt(Encoding.UTF8.GetBytes(Password))) }; if (File.Exists("LastPassVer.txt") && int.TryParse(File.ReadAllText("LastPassVer.txt"), out int lpv) && lpv < ClientEncryption.PasswordVersion && lpv >= 0) { heads.Add(new("old_password", ClientEncryption.RemotePasswordEncrypt(Encoding.UTF8.GetBytes(Password), lpv))); heads.Add(new("old_version", lpv.ToString())); } if (pfp is not null) { heads.Add(new("username", Username)); json = SendServer( "CreateAccount", pfp, LoginContext.Default.Login, CancellationToken, heads.ToArray()).Result; } else { json = GetFromServer( "Login", LoginContext.Default.Login, CancellationToken, heads.ToArray()).Result; } if (json.Error is not null) throw new Exception($"Luski appears to be down at the current moment: {json.ErrorMessage}"); if (ClientEncryption.ofkey is null || ClientEncryption.outofkey is null) throw new Exception("Something went wrong generating the offline keys"); login = false; if (json is not null && json.Error is null) { ServerOut = new WebSocket($"wss://{Domain}/WSS/{ApiVersion}"); ServerOut.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls13 | SslProtocols.Tls12; ServerOut.OnMessage += DataFromServer; ServerOut.WaitTime = new TimeSpan(0, 0, 5); ServerOut.OnError += ServerOut_OnError; ServerOut.Connect(); SendServer(new WSSLogin() { Token = json.Token! }, WSSLoginContext.Default.WSSLogin); while (Token is null && Error is null) { } if (Error is not null) { throw new Exception(Error); } if (Token is null) throw new Exception("Server did not send a token"); CanRequest = true; long id = long.Parse(Encoding.UTF8.GetString(Convert.FromBase64String( Token.Split('.')[0] ))); User = GetUser(id, MainSocketAppUserContext.Default.MainSocketAppUser, CancellationToken) .Result; if (User is null || User.Error is not null) { string error = "User was null"; if (User is not null && User.Error is not null) error = $"{User.Error}: {User.ErrorMessage}"; throw new Exception($"Something went wrong getting your user infermation\n{error}"); } Console.WriteLine("User got"); Console.WriteLine("Insert"); //User.Email = Email; _ = UpdateStatus(UserStatus.Online, CancellationToken); Console.WriteLine("stat"); try { ClientEncryption.pw = Email.ToLower() + Password; _ = ClientEncryption.File.GetOfflineKey(); } catch { try { ClientEncryption.pw = Email + Password; var temp222 = ClientEncryption.File.LuskiDataFile.GetDefualtDataFile(); ClientEncryption.pw = Email.ToLower() + Password; if (temp222 is not null) temp222.Save("key.lsk", ClientEncryption.pw); } catch { Token = null; Error = null; ServerOut.Close(); throw new Exception("The key file you have is getting the wrong pasword. Type your Email in the same way you creaated your account to fix this error."); } } Console.WriteLine("req offline"); OfflineData offlinedata = GetFromServer("Keys/GetOfflineData", OfflineDataContext.Default.OfflineData, CancellationToken).Result; if (offlinedata.Data is not null && offlinedata.Data.Any()) Console.WriteLine(offlinedata.Data); if (offlinedata is not null && offlinedata.Error is null && offlinedata.Data is not null && offlinedata.Data.Length > 0) { foreach (string keyex in offlinedata.Data) { Console.WriteLine(keyex); KeyExchange? okd = JsonSerializer.Deserialize(keyex); Console.WriteLine(okd); if (okd is not null && !string.IsNullOrEmpty(okd.key)) { Console.WriteLine(okd.channel); Console.WriteLine(okd.key); ClientEncryption.File.Channels.AddKey(okd.channel, Encoding.UTF8.GetString(Encryption.RSA.Decrypt(Convert.FromBase64String(okd.key), ClientEncryption.File.GetOfflineKey()))); } } } Console.WriteLine("lpv"); System.IO.File.WriteAllText("LastPassVer.txt", ClientEncryption.PasswordVersion.ToString()); ClientEncryption.File.SetOfflineKey(ClientEncryption.ofkey); using HttpClient setkey = new(); setkey.DefaultRequestHeaders.Add("token", Token); _ = setkey.PostAsync($"https://{Domain}/{ApiVersion}/Keys/SetOfflineKey", new StringContent(ClientEncryption.outofkey)).Result; //_ = User.Channels; foreach (var ch in chans) { _ = ch.Members; } ClientEncryption.outofkey = null; ClientEncryption.ofkey = null; } else throw new Exception(json?.ErrorMessage); } }