Friends have been copied to the main server.
This commit is contained in:
JacobTech 2023-07-10 14:19:03 -04:00
parent c3bb39b21b
commit ea39d93cf5
9 changed files with 113 additions and 36 deletions

View File

@ -16,12 +16,8 @@ public class API
{
IEnumerable<PublicServer> isl = InternalServers.Where(a => (a.Domain == Domain && a.ApiVersion == Version));
if (isl.Any()) return isl.First();
PublicServer s = new()
PublicServer s = new(Domain, Version)
{
Domain = Domain,
ApiVersion = Version,
Storage = new(Domain),
EncryptionHandler = new(Domain),
ServerType = ServerType.Public
};
InternalServers.Add(s);
@ -30,12 +26,8 @@ public class API
public MainServer GetMainServer(string Domain, string Version = "v1")
{
MainServer = new()
MainServer = new(Domain, Version)
{
Domain = Domain,
ApiVersion = Version,
Storage = new(Domain),
EncryptionHandler = new(Domain),
ServerType = ServerType.Main
};
return MainServer;

View File

@ -47,8 +47,7 @@ public partial class MainServer
MainSocketMessage? m = JsonSerializer.Deserialize<MainSocketMessage>(e.Data);
if (m is not null)
{
m.decrypt(Storage.GetResourceKey(StorageDirectory.ChannelKeys, m.ChannelID.ToString(),
EncryptionHandler.Hash), CancellationToken.None);
m.decrypt(EncryptionHandler.GetChannelKey(m.ChannelID), CancellationToken.None);
_ = MessageReceived.Invoke(m);
}
}
@ -97,15 +96,12 @@ public partial class MainServer
KeyExchange? KE = JsonSerializer.Deserialize<KeyExchange>(e.Data);
if (KE is not null)
{
Storage.SetResourceKey(
StorageDirectory.ChannelKeys,
KE.channel.ToString(),
EncryptionHandler.Hash,
EncryptionHandler.SetChannelKey(
KE.channel,
Encoding.UTF8.GetString(
Encryption.RSA.Decrypt(
Convert.FromBase64String(KE.key),
EncryptionHandler.myPrivateKey
)
EncryptionHandler.myPrivateKey)
)
);
}

View File

@ -1,12 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
using System.Threading.Tasks;
using JacobTechEncryption;
using Luski.net.Enums;
using Luski.net.Enums.Main;
using Luski.net.Interfaces;
using Luski.net.JsonTypes;
using Luski.net.JsonTypes.BaseTypes;
using Luski.net.JsonTypes.HTTP;
using Luski.net.Structures.Main;
@ -16,8 +20,76 @@ namespace Luski.net;
public partial class MainServer : Server
{
internal MainServer(string Domain, string API_Version):
base(Domain, API_Version)
{
}
public MainSocketAppUser User { get; internal set; } = default!;
public async Task<MainSocketRemoteUser> SendFriendResult(long user, bool answer, CancellationToken CancellationToken)
{
FriendRequestResult json = await SendServer("FriendRequestResult",
new FriendRequestResultOut()
{
Id = user,
Result = answer
},
FriendRequestResultOutContext.Default.FriendRequestResultOut,
FriendRequestResultContext.Default.FriendRequestResult,
CancellationToken);
if (json is not null && json.Error is null && json.ErrorMessage is null && answer && json.Channel is not null)
{
MainSocketDMChannel chan = await GetChannel((long)json.Channel, MainSocketDMChannelContext.Default.MainSocketDMChannel, CancellationToken);
_ = chan.StartKeyProcessAsync(CancellationToken);
chans.Add(chan);
}
else
{
throw new Exception(json?.Error.ToString());
}
return GetUser(user, MainSocketRemoteUserContext.Default.MainSocketRemoteUser, CancellationToken).Result;
}
public async Task<MainSocketRemoteUser> SendFriendRequest(long code, CancellationToken CancellationToken)
{
string ccode = Convert.ToBase64String(Encryption.Hashing.SHA256(Encoding.Unicode.GetBytes(code.ToString())));
FriendRequestResult? json = await SendServer("FriendRequest", new FriendRequest() { code = ccode}, FriendRequestContext.Default.FriendRequest, FriendRequestResultContext.Default.FriendRequestResult, CancellationToken);
if (json.StatusCode != HttpStatusCode.Accepted)
{
if (json is not null && json.Error is not null)
{
switch ((ErrorCode)(int)json.Error)
{
case ErrorCode.InvalidToken:
throw new Exception("Your current token is no longer valid");
case ErrorCode.ServerError:
throw new Exception($"Error from server: {json.ErrorMessage}");
case ErrorCode.InvalidPostData:
throw new Exception("The post data dent to the server is not the correct format. This may be because you app is couropt or you are using the wron API version");
case ErrorCode.Forbidden:
throw new Exception("You already have an outgoing request or the persone is not real");
}
}
if (json is not null && json.Channel is not null)
{
MainSocketDMChannel chan = await GetChannel((long)json.Channel, MainSocketDMChannelContext.Default.MainSocketDMChannel, CancellationToken);
_ = chan.StartKeyProcessAsync(CancellationToken);
chans.Add(chan);
}
}
MainSocketRemoteUser b = await GetUser(code, MainSocketRemoteUserContext.Default.MainSocketRemoteUser, CancellationToken);
if (json.Channel is not null)
b.FriendStatus = FriendStatus.Friends;
else
b.FriendStatus = FriendStatus.PendingOut;
return b;
}
public async Task<TChannel> GetChannel<TChannel>(long Channel, CancellationToken CancellationToken) where TChannel : MainSocketChannel, new()
{
TChannel Return = new();

View File

@ -2,5 +2,8 @@ namespace Luski.net;
public class PublicServer : Server
{
internal PublicServer(string Domain, string API_Version):
base(Domain, API_Version)
{
}
}

View File

@ -14,8 +14,8 @@ namespace Luski.net;
public partial class Server
{
public ServerType ServerType { get; internal set; } = ServerType.Public;
public string Domain { get; internal set; } = default!;
public string ApiVersion { get; internal set; } = "v1";
public string Domain { get; } = default!;
public string ApiVersion { get; } = "v1";
internal WebSocket? ServerOut;
internal string? Token = null, Error = null, gen = null;
internal bool CanRequest = false, login = false;

View File

@ -17,12 +17,16 @@ namespace Luski.net;
public partial class Server
{
internal Server()
internal Server(string Domain, string API_Version)
{
this.Domain = Domain;
this.ApiVersion = API_Version;
Storage = new(Domain);
EncryptionHandler = new(Domain, API_Version, Storage);
}
public ServerEncryption EncryptionHandler { get; internal set; } = null!;
public ServerStorage Storage { get; internal set; } = null!;
public ServerEncryption EncryptionHandler { get; }
public ServerStorage Storage { get; }
public async Task<byte[]> GetAvatar(CancellationToken CancellationToken)
{

View File

@ -1,6 +1,7 @@
using System;
using System.Security.Cryptography;
using JacobTechEncryption;
using Luski.net.Enums;
namespace Luski.net;
@ -9,11 +10,24 @@ public class ServerEncryption
internal bool Generating, Generated;
internal string ServerPublicKey = "", MyPublicKey = "", myPrivateKey = "", OfflinePrivateKey = "", OfflinePublicKey = "";
internal byte[] Hash = default!;
internal ServerEncryption(string Domain)
internal ServerEncryption(string Domain, string API_Version, ServerStorage Storage)
{
this.Storage = Storage;
//TODO Get server p key
}
public string GetChannelKey(long Channel)
{
return Storage.GetResourceKey(StorageDirectory.ChannelKeys, Channel.ToString(), Hash);
}
public void SetChannelKey(long Channel, string Key)
{
Storage.SetResourceKey(StorageDirectory.ChannelKeys, Channel.ToString(), Hash, Key);
}
private ServerStorage Storage { get; }
internal int PasswordVersion = 0;
internal byte[] LocalPasswordEncrypt(byte[] Password) => LocalPasswordEncrypt(Password, PasswordVersion);
internal string RemotePasswordEncrypt(byte[] Password) => RemotePasswordEncrypt(Password, PasswordVersion);

View File

@ -70,8 +70,7 @@ public class MainSocketChannel : IncomingHTTP
}
int num = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 50) * 2.0));
if (num == 0) num = 1;
string lkey = Server.Storage.GetResourceKey(StorageDirectory.ChannelKeys, Id.ToString(),
Server.EncryptionHandler.Hash);
string lkey = Server.EncryptionHandler.GetChannelKey(Id);
Parallel.ForEach(Members, new ParallelOptions()
{
MaxDegreeOfParallelism = num

View File

@ -56,8 +56,7 @@ public class MainSocketTextChannel : MainSocketChannel
int num = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 5) * 2.0));
if (num == 0) num = 1;
string key = Server.Storage.GetResourceKey(StorageDirectory.ChannelKeys, Id.ToString(),
Server.EncryptionHandler.Hash);
string key = Server.EncryptionHandler.GetChannelKey(Id);
if (data is null) throw new Exception("Invalid data from server");
if (data.Messages is null) data.Messages = Array.Empty<MainSocketMessage>();
Parallel.ForEach(data.Messages, new ParallelOptions()
@ -103,8 +102,7 @@ public class MainSocketTextChannel : MainSocketChannel
{
int num = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 5) * 2.0));
if (num == 0) num = 1;
string key = Server.Storage.GetResourceKey(StorageDirectory.ChannelKeys, Id.ToString(),
Server.EncryptionHandler.Hash);
string key = Server.EncryptionHandler.GetChannelKey(Id);
if (data.Messages is null) data.Messages = Array.Empty<MainSocketMessage>();
Parallel.ForEach(data.Messages, new ParallelOptions()
{
@ -139,8 +137,7 @@ public class MainSocketTextChannel : MainSocketChannel
public async Task<Task> SendMessage(string Message, CancellationToken CancellationToken, params File?[] Files)
{
string key = Server.Storage.GetResourceKey(StorageDirectory.ChannelKeys, Id.ToString(),
Server.EncryptionHandler.Hash);
string key = Server.EncryptionHandler.GetChannelKey(Id);
JsonTypes.HTTP.Message m = new()
{
Context = Convert.ToBase64String(Encryption.RSA.Encrypt(Message, key, EncoderType.UTF8)),