130 lines
5.5 KiB
C#
Executable File
130 lines
5.5 KiB
C#
Executable File
using Luski.net.Enums;
|
|
using Luski.net.JsonTypes;
|
|
using Luski.net.JsonTypes.BaseTypes;
|
|
using Luski.net.JsonTypes.WSS;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using WebSocketSharp;
|
|
using File = System.IO.File;
|
|
|
|
namespace Luski.net;
|
|
|
|
public sealed partial class Server
|
|
{
|
|
internal Server(string Email, string Password, Branch branch = Branch.Master, string? Username = null, string? pfp = null)
|
|
{
|
|
if (!Encryption.Generating)
|
|
{
|
|
Encryption.GenerateKeys();
|
|
}
|
|
while (!Encryption.Generated) { }
|
|
InternalDomain = $"api.{branch}.luski.JacobTech.com";
|
|
Branch = branch;
|
|
login = true;
|
|
Login json;
|
|
List<KeyValuePair<string, string?>> heads = new()
|
|
{
|
|
new("key", Encryption.MyPublicKey),
|
|
new("email", Convert.ToBase64String(Encryption.Encrypt(Email))),
|
|
new("password", Encryption.RemotePasswordEncrypt(Encryption.Encoder.GetBytes(Password)))
|
|
};
|
|
if (File.Exists("LastPassVer.txt") && int.TryParse(File.ReadAllText("LastPassVer.txt"), out int lpv) && lpv < Encryption.PasswordVersion && lpv >= 0)
|
|
{
|
|
heads.Add(new("old_password", Encryption.RemotePasswordEncrypt(Encryption.Encoder.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,
|
|
heads.ToArray()).Result;
|
|
}
|
|
else
|
|
{
|
|
json = GetFromServer(
|
|
"Login",
|
|
LoginContext.Default.Login,
|
|
heads.ToArray()).Result;
|
|
}
|
|
|
|
if (json.Error is not null) throw new Exception($"Luski appears to be down at the current moment: {json.ErrorMessage}");
|
|
if (Encryption.ofkey is null || Encryption.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://{InternalDomain}/WSS/{API_Ver}");
|
|
ServerOut.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls13;
|
|
ServerOut.OnMessage += DataFromServer;
|
|
ServerOut.WaitTime = new TimeSpan(0, 0, 5);
|
|
ServerOut.OnError += ServerOut_OnError;
|
|
ServerOut.Connect();
|
|
string Infermation = $"{{\"token\": \"{json.Token}\"}}";
|
|
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;
|
|
_user = SocketUserBase.GetUser(long.Parse(Encoding.UTF8.GetString(Convert.FromBase64String(Token.Split('.')[0]))), SocketAppUserContext.Default.SocketAppUser).Result;
|
|
if (_user is null || _user.Error is not null) throw new Exception("Something went wrong getting your user infermation");
|
|
_ = _user.Channels;
|
|
foreach (var ch in chans)
|
|
{
|
|
_ = ch.Members;
|
|
}
|
|
_user.Email = Email;
|
|
_ = UpdateStatus(UserStatus.Online);
|
|
|
|
try
|
|
{
|
|
Encryption.pw = Email.ToLower() + Password;
|
|
_ = Encryption.File.GetOfflineKey();
|
|
}
|
|
catch
|
|
{
|
|
try
|
|
{
|
|
Encryption.pw = Email + Password;
|
|
var temp222 = Encryption.File.LuskiDataFile.GetDefualtDataFile();
|
|
Encryption.pw = Email.ToLower() + Password;
|
|
if (temp222 is not null) temp222.Save(GetKeyFilePath, Encryption.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.");
|
|
}
|
|
}
|
|
OfflineKeyData offlinedata = GetFromServer("Keys/GetOfflineData", OfflineKeyDataContext.Default.OfflineKeyData).Result;
|
|
if (string.IsNullOrEmpty(Encryption.File.GetOfflineKey())) Encryption.File.SetOfflineKey(Encryption.ofkey);
|
|
if (offlinedata is not null && offlinedata.Error is null && offlinedata.keys is not null)
|
|
{
|
|
foreach (KeyExchange key in offlinedata.keys)
|
|
{
|
|
Encryption.File.Channels.AddKey(key.channel, Encryption.Encoder.GetString(Encryption.Decrypt(Convert.FromBase64String(key.key), Encryption.File.GetOfflineKey())));
|
|
}
|
|
}
|
|
System.IO.File.WriteAllText("LastPassVer.txt", Encryption.PasswordVersion.ToString());
|
|
Encryption.File.SetOfflineKey(Encryption.ofkey);
|
|
using HttpClient setkey = new();
|
|
setkey.DefaultRequestHeaders.Add("token", Token);
|
|
_ = setkey.PostAsync($"https://{InternalDomain}/{API_Ver}/Keys/SetOfflineKey", new StringContent(Encryption.outofkey)).Result;
|
|
Encryption.outofkey = null;
|
|
Encryption.ofkey = null;
|
|
}
|
|
else throw new Exception(json?.ErrorMessage);
|
|
}
|
|
}
|