using System; using System.Collections.Generic; using System.IO; 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.Shared.PublicServers.V1.Enums; using Luski.Shared.PublicServers.V1.ServerToClient.HTTP; using WebSocketSharp; namespace Luski.net; public partial class MainServer { public void Login(string Username, string Password, CancellationToken CancellationToken) { Both(Username, Password, CancellationToken); } public void CreateAccount(string Username, string Password, string Displayname, string PFP, CancellationToken CancellationToken) { Both(Username, Password, CancellationToken, Displayname, PFP); } private void Both(string Username, string Password, CancellationToken CancellationToken, string? Displayname = null, string? pfp = null) { if (!EncryptionHandler.Generating) { EncryptionHandler.GenerateKeys(); } while (!EncryptionHandler.Generated) { } login = true; LoginSTC json; List> heads = new() { new("key", EncryptionHandler.MyPublicKey), new("email", Convert.ToBase64String(Encryption.RSA.Encrypt(Username, EncryptionHandler.ServerPublicKey, EncoderType.UTF16))), new("password", EncryptionHandler.RemotePasswordEncrypt(Encoding.Unicode.GetBytes(Password))) }; if (File.Exists("LastPassVer.txt") && int.TryParse(File.ReadAllText("LastPassVer.txt"), out int lpv) && lpv < EncryptionHandler.PasswordVersion && lpv >= 0) { heads.Add(new("old_password", EncryptionHandler.RemotePasswordEncrypt(Encoding.Unicode.GetBytes(Password), lpv))); heads.Add(new("old_version", lpv.ToString())); } if (pfp is not null) { heads.Add(new("username", Displayname)); json = SendServer( "CreateAccount", pfp, LoginSTCContext.Default.LoginSTC, CancellationToken, heads.ToArray()).Result; } else { json = GetFromServer( "Login", LoginSTCContext.Default.LoginSTC, 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 (EncryptionHandler.OfflinePrivateKey is null || EncryptionHandler.OfflinePublicKey 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($"{(Secure ? "wss" : "ws" )}://{Domain}/WSS/{ApiVersion}"); if (Secure) ServerOut.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls13 | SslProtocols.Tls12; ServerOut.OnMessage += DataFromServer; ServerOut.WaitTime = new TimeSpan(0, 0, 5); ServerOut.OnError += ServerOut_OnError; ServerOut.Connect(); SendServerOld(new WSSLogin() { Token = json.Token! }, WSSLoginContext.Default.WSSLogin); while (Token is null && Error is null) { Thread.Sleep(500); } 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}"); } _ = UpdateStatus(UserStatus.Online, CancellationToken); if (pfp is not null) { //post } else { } EncryptionHandler.Hash = EncryptionHandler.LocalPasswordEncrypt(Encoding.Unicode.GetBytes(Username.ToLower() + Password)); OfflineDataBlobSTC offlinedata = GetFromServer("Keys/GetOfflineData", OfflineDataBlobSTCContext.Default.OfflineDataBlobSTC, CancellationToken).Result; if (offlinedata is not null && offlinedata.Error is null && offlinedata.Data is not null && offlinedata.Data.Length > 0) { foreach (string keyex in offlinedata.Data) { KeyExchange? okd = JsonSerializer.Deserialize(keyex); if (okd is not null && !string.IsNullOrEmpty(okd.key)) { Storage.SetResourceKey( StorageDirectory.ChannelKeys, okd.channel.ToString(), EncryptionHandler.Hash, Encoding.Unicode.GetString( Encryption.RSA.Decrypt( Convert.FromBase64String(okd.key), Storage.GetResourceKeyRaw( StorageDirectory.ServerKeys, "pkey", EncryptionHandler.Hash ) ) ) ); } } } System.IO.File.WriteAllText("LastPassVer.txt", EncryptionHandler.PasswordVersion.ToString()); Storage.SetResourceKey(StorageDirectory.ServerKeys, "pkey", EncryptionHandler.Hash, EncryptionHandler.OfflinePrivateKey); using HttpClient setkey = new(); setkey.DefaultRequestHeaders.Add("token", Token); _ = setkey.PostAsync($"{(Secure ? "https" : "http" )}://{Domain}/{ApiVersion}/Keys/SetOfflineKey", new StringContent(EncryptionHandler.OfflinePublicKey)).Result; foreach (var ch in chans) { _ = ch.Members; } EncryptionHandler.OfflinePublicKey = null; EncryptionHandler.OfflinePrivateKey = null; } else throw new Exception(json?.ErrorMessage); } }