2023-07-03 23:24:35 -04:00
using System ;
using System.Collections.Generic ;
2023-07-08 09:06:13 -04:00
using System.IO ;
2023-07-03 23:24:35 -04:00
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 ;
2024-03-20 23:18:34 -04:00
using Luski.Shared.PublicServers.V1.Enums ;
using Luski.Shared.PublicServers.V1.ServerToClient.HTTP ;
2023-07-03 23:24:35 -04:00
using WebSocketSharp ;
namespace Luski.net ;
2023-07-08 09:06:13 -04:00
public partial class MainServer
2023-07-03 23:24:35 -04:00
{
2023-07-10 07:35:05 -04:00
public void Login ( string Username , string Password , CancellationToken CancellationToken )
2023-07-03 23:24:35 -04:00
{
2023-07-10 07:35:05 -04:00
Both ( Username , Password , CancellationToken ) ;
2023-07-03 23:24:35 -04:00
}
2023-07-10 07:35:05 -04:00
public void CreateAccount ( string Username , string Password , string Displayname , string PFP , CancellationToken CancellationToken )
2023-07-03 23:24:35 -04:00
{
2023-07-10 07:35:05 -04:00
Both ( Username , Password , CancellationToken , Displayname , PFP ) ;
2023-07-03 23:24:35 -04:00
}
2023-07-10 07:35:05 -04:00
private void Both ( string Username , string Password , CancellationToken CancellationToken , string? Displayname = null , string? pfp = null )
2023-07-03 23:24:35 -04:00
{
2023-07-10 07:35:05 -04:00
if ( ! EncryptionHandler . Generating )
2023-07-03 23:24:35 -04:00
{
2023-07-10 07:35:05 -04:00
EncryptionHandler . GenerateKeys ( ) ;
2023-07-03 23:24:35 -04:00
}
2023-07-10 07:35:05 -04:00
while ( ! EncryptionHandler . Generated ) { }
2023-07-03 23:24:35 -04:00
login = true ;
2024-03-20 23:18:34 -04:00
LoginSTC json ;
2023-07-03 23:24:35 -04:00
List < KeyValuePair < string , string? > > heads = new ( )
{
2023-07-10 07:35:05 -04:00
new ( "key" , EncryptionHandler . MyPublicKey ) ,
2023-08-21 10:58:17 -04:00
new ( "email" , Convert . ToBase64String ( Encryption . RSA . Encrypt ( Username , EncryptionHandler . ServerPublicKey , EncoderType . UTF16 ) ) ) ,
new ( "password" , EncryptionHandler . RemotePasswordEncrypt ( Encoding . Unicode . GetBytes ( Password ) ) )
2023-07-03 23:24:35 -04:00
} ;
2023-07-10 07:35:05 -04:00
if ( File . Exists ( "LastPassVer.txt" ) & & int . TryParse ( File . ReadAllText ( "LastPassVer.txt" ) , out int lpv ) & & lpv < EncryptionHandler . PasswordVersion & & lpv > = 0 )
2023-07-03 23:24:35 -04:00
{
2023-08-21 10:58:17 -04:00
heads . Add ( new ( "old_password" , EncryptionHandler . RemotePasswordEncrypt ( Encoding . Unicode . GetBytes ( Password ) , lpv ) ) ) ;
2023-07-03 23:24:35 -04:00
heads . Add ( new ( "old_version" , lpv . ToString ( ) ) ) ;
}
if ( pfp is not null )
{
2023-07-10 07:35:05 -04:00
heads . Add ( new ( "username" , Displayname ) ) ;
2023-07-03 23:24:35 -04:00
json = SendServer (
"CreateAccount" ,
pfp ,
2024-03-20 23:18:34 -04:00
LoginSTCContext . Default . LoginSTC ,
2023-07-03 23:24:35 -04:00
CancellationToken ,
heads . ToArray ( ) ) . Result ;
}
else
{
json = GetFromServer (
"Login" ,
2024-03-20 23:18:34 -04:00
LoginSTCContext . Default . LoginSTC ,
2023-07-03 23:24:35 -04:00
CancellationToken ,
heads . ToArray ( ) ) . Result ;
}
if ( json . Error is not null ) throw new Exception ( $"Luski appears to be down at the current moment: {json.ErrorMessage}" ) ;
2023-07-10 07:35:05 -04:00
if ( EncryptionHandler . OfflinePrivateKey is null | | EncryptionHandler . OfflinePublicKey is null ) throw new Exception ( "Something went wrong generating the offline keys" ) ;
2023-07-03 23:24:35 -04:00
login = false ;
if ( json is not null & & json . Error is null )
{
2023-08-21 10:58:17 -04:00
ServerOut = new WebSocket ( $"{(Secure ? " wss " : " ws " )}://{Domain}/WSS/{ApiVersion}" ) ;
if ( Secure ) ServerOut . SslConfiguration . EnabledSslProtocols = SslProtocols . Tls13 | SslProtocols . Tls12 ;
2023-07-03 23:24:35 -04:00
ServerOut . OnMessage + = DataFromServer ;
ServerOut . WaitTime = new TimeSpan ( 0 , 0 , 5 ) ;
ServerOut . OnError + = ServerOut_OnError ;
ServerOut . Connect ( ) ;
2023-08-21 10:58:17 -04:00
SendServerOld ( new WSSLogin ( ) { Token = json . Token ! } , WSSLoginContext . Default . WSSLogin ) ;
2023-07-03 23:24:35 -04:00
while ( Token is null & & Error is null )
{
2023-08-21 10:58:17 -04:00
Thread . Sleep ( 500 ) ;
2023-07-03 23:24:35 -04:00
}
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 ]
) ) ) ;
2023-07-08 09:06:13 -04:00
User = GetUser ( id , MainSocketAppUserContext . Default . MainSocketAppUser ,
CancellationToken )
. Result ;
2023-07-03 23:24:35 -04:00
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 ) ;
2023-08-21 10:58:17 -04:00
if ( pfp is not null )
{
//post
}
else
{
}
EncryptionHandler . Hash = EncryptionHandler . LocalPasswordEncrypt ( Encoding . Unicode . GetBytes ( Username . ToLower ( ) + Password ) ) ;
2024-03-20 23:18:34 -04:00
OfflineDataBlobSTC offlinedata = GetFromServer ( "Keys/GetOfflineData" , OfflineDataBlobSTCContext . Default . OfflineDataBlobSTC , CancellationToken ) . Result ;
2023-07-03 23:24:35 -04:00
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 < KeyExchange > ( keyex ) ;
if ( okd is not null & & ! string . IsNullOrEmpty ( okd . key ) )
{
2023-07-10 07:35:05 -04:00
Storage . SetResourceKey (
StorageDirectory . ChannelKeys ,
okd . channel . ToString ( ) ,
EncryptionHandler . Hash ,
2023-08-21 10:58:17 -04:00
Encoding . Unicode . GetString (
2023-07-10 07:35:05 -04:00
Encryption . RSA . Decrypt (
Convert . FromBase64String ( okd . key ) ,
2023-08-21 10:58:17 -04:00
Storage . GetResourceKeyRaw (
2023-07-10 07:35:05 -04:00
StorageDirectory . ServerKeys ,
"pkey" ,
EncryptionHandler . Hash
)
)
)
) ;
2023-07-03 23:24:35 -04:00
}
}
}
2023-07-10 07:35:05 -04:00
System . IO . File . WriteAllText ( "LastPassVer.txt" , EncryptionHandler . PasswordVersion . ToString ( ) ) ;
Storage . SetResourceKey ( StorageDirectory . ServerKeys , "pkey" , EncryptionHandler . Hash , EncryptionHandler . OfflinePrivateKey ) ;
2023-07-03 23:24:35 -04:00
using HttpClient setkey = new ( ) ;
setkey . DefaultRequestHeaders . Add ( "token" , Token ) ;
2023-08-21 10:58:17 -04:00
_ = setkey . PostAsync ( $"{(Secure ? " https " : " http " )}://{Domain}/{ApiVersion}/Keys/SetOfflineKey" , new StringContent ( EncryptionHandler . OfflinePublicKey ) ) . Result ;
2023-07-03 23:24:35 -04:00
foreach ( var ch in chans )
{
_ = ch . Members ;
}
2023-07-10 07:35:05 -04:00
EncryptionHandler . OfflinePublicKey = null ;
EncryptionHandler . OfflinePrivateKey = null ;
2023-07-03 23:24:35 -04:00
}
else throw new Exception ( json ? . ErrorMessage ) ;
}
}