Friends.
Friends have been copied to the main server.
This commit is contained in:
parent
c3bb39b21b
commit
ea39d93cf5
@ -16,12 +16,8 @@ public class API
|
|||||||
{
|
{
|
||||||
IEnumerable<PublicServer> isl = InternalServers.Where(a => (a.Domain == Domain && a.ApiVersion == Version));
|
IEnumerable<PublicServer> isl = InternalServers.Where(a => (a.Domain == Domain && a.ApiVersion == Version));
|
||||||
if (isl.Any()) return isl.First();
|
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
|
ServerType = ServerType.Public
|
||||||
};
|
};
|
||||||
InternalServers.Add(s);
|
InternalServers.Add(s);
|
||||||
@ -30,12 +26,8 @@ public class API
|
|||||||
|
|
||||||
public MainServer GetMainServer(string Domain, string Version = "v1")
|
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
|
ServerType = ServerType.Main
|
||||||
};
|
};
|
||||||
return MainServer;
|
return MainServer;
|
||||||
|
@ -47,8 +47,7 @@ public partial class MainServer
|
|||||||
MainSocketMessage? m = JsonSerializer.Deserialize<MainSocketMessage>(e.Data);
|
MainSocketMessage? m = JsonSerializer.Deserialize<MainSocketMessage>(e.Data);
|
||||||
if (m is not null)
|
if (m is not null)
|
||||||
{
|
{
|
||||||
m.decrypt(Storage.GetResourceKey(StorageDirectory.ChannelKeys, m.ChannelID.ToString(),
|
m.decrypt(EncryptionHandler.GetChannelKey(m.ChannelID), CancellationToken.None);
|
||||||
EncryptionHandler.Hash), CancellationToken.None);
|
|
||||||
_ = MessageReceived.Invoke(m);
|
_ = MessageReceived.Invoke(m);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -97,15 +96,12 @@ public partial class MainServer
|
|||||||
KeyExchange? KE = JsonSerializer.Deserialize<KeyExchange>(e.Data);
|
KeyExchange? KE = JsonSerializer.Deserialize<KeyExchange>(e.Data);
|
||||||
if (KE is not null)
|
if (KE is not null)
|
||||||
{
|
{
|
||||||
Storage.SetResourceKey(
|
EncryptionHandler.SetChannelKey(
|
||||||
StorageDirectory.ChannelKeys,
|
KE.channel,
|
||||||
KE.channel.ToString(),
|
|
||||||
EncryptionHandler.Hash,
|
|
||||||
Encoding.UTF8.GetString(
|
Encoding.UTF8.GetString(
|
||||||
Encryption.RSA.Decrypt(
|
Encryption.RSA.Decrypt(
|
||||||
Convert.FromBase64String(KE.key),
|
Convert.FromBase64String(KE.key),
|
||||||
EncryptionHandler.myPrivateKey
|
EncryptionHandler.myPrivateKey)
|
||||||
)
|
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,16 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Text;
|
||||||
using System.Text.Json.Serialization.Metadata;
|
using System.Text.Json.Serialization.Metadata;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using JacobTechEncryption;
|
||||||
using Luski.net.Enums;
|
using Luski.net.Enums;
|
||||||
using Luski.net.Enums.Main;
|
using Luski.net.Enums.Main;
|
||||||
using Luski.net.Interfaces;
|
using Luski.net.Interfaces;
|
||||||
|
using Luski.net.JsonTypes;
|
||||||
using Luski.net.JsonTypes.BaseTypes;
|
using Luski.net.JsonTypes.BaseTypes;
|
||||||
using Luski.net.JsonTypes.HTTP;
|
using Luski.net.JsonTypes.HTTP;
|
||||||
using Luski.net.Structures.Main;
|
using Luski.net.Structures.Main;
|
||||||
@ -16,8 +20,76 @@ namespace Luski.net;
|
|||||||
|
|
||||||
public partial class MainServer : Server
|
public partial class MainServer : Server
|
||||||
{
|
{
|
||||||
|
internal MainServer(string Domain, string API_Version):
|
||||||
|
base(Domain, API_Version)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
public MainSocketAppUser User { get; internal set; } = default!;
|
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()
|
public async Task<TChannel> GetChannel<TChannel>(long Channel, CancellationToken CancellationToken) where TChannel : MainSocketChannel, new()
|
||||||
{
|
{
|
||||||
TChannel Return = new();
|
TChannel Return = new();
|
||||||
|
@ -2,5 +2,8 @@ namespace Luski.net;
|
|||||||
|
|
||||||
public class PublicServer : Server
|
public class PublicServer : Server
|
||||||
{
|
{
|
||||||
|
internal PublicServer(string Domain, string API_Version):
|
||||||
|
base(Domain, API_Version)
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
@ -14,8 +14,8 @@ namespace Luski.net;
|
|||||||
public partial class Server
|
public partial class Server
|
||||||
{
|
{
|
||||||
public ServerType ServerType { get; internal set; } = ServerType.Public;
|
public ServerType ServerType { get; internal set; } = ServerType.Public;
|
||||||
public string Domain { get; internal set; } = default!;
|
public string Domain { get; } = default!;
|
||||||
public string ApiVersion { get; internal set; } = "v1";
|
public string ApiVersion { get; } = "v1";
|
||||||
internal WebSocket? ServerOut;
|
internal WebSocket? ServerOut;
|
||||||
internal string? Token = null, Error = null, gen = null;
|
internal string? Token = null, Error = null, gen = null;
|
||||||
internal bool CanRequest = false, login = false;
|
internal bool CanRequest = false, login = false;
|
||||||
|
@ -17,12 +17,16 @@ namespace Luski.net;
|
|||||||
|
|
||||||
public partial class Server
|
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 ServerEncryption EncryptionHandler { get; }
|
||||||
public ServerStorage Storage { get; internal set; } = null!;
|
public ServerStorage Storage { get; }
|
||||||
|
|
||||||
public async Task<byte[]> GetAvatar(CancellationToken CancellationToken)
|
public async Task<byte[]> GetAvatar(CancellationToken CancellationToken)
|
||||||
{
|
{
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using JacobTechEncryption;
|
using JacobTechEncryption;
|
||||||
|
using Luski.net.Enums;
|
||||||
|
|
||||||
namespace Luski.net;
|
namespace Luski.net;
|
||||||
|
|
||||||
@ -9,11 +10,24 @@ public class ServerEncryption
|
|||||||
internal bool Generating, Generated;
|
internal bool Generating, Generated;
|
||||||
internal string ServerPublicKey = "", MyPublicKey = "", myPrivateKey = "", OfflinePrivateKey = "", OfflinePublicKey = "";
|
internal string ServerPublicKey = "", MyPublicKey = "", myPrivateKey = "", OfflinePrivateKey = "", OfflinePublicKey = "";
|
||||||
internal byte[] Hash = default!;
|
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
|
//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 int PasswordVersion = 0;
|
||||||
internal byte[] LocalPasswordEncrypt(byte[] Password) => LocalPasswordEncrypt(Password, PasswordVersion);
|
internal byte[] LocalPasswordEncrypt(byte[] Password) => LocalPasswordEncrypt(Password, PasswordVersion);
|
||||||
internal string RemotePasswordEncrypt(byte[] Password) => RemotePasswordEncrypt(Password, PasswordVersion);
|
internal string RemotePasswordEncrypt(byte[] Password) => RemotePasswordEncrypt(Password, PasswordVersion);
|
||||||
|
@ -70,8 +70,7 @@ public class MainSocketChannel : IncomingHTTP
|
|||||||
}
|
}
|
||||||
int num = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 50) * 2.0));
|
int num = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 50) * 2.0));
|
||||||
if (num == 0) num = 1;
|
if (num == 0) num = 1;
|
||||||
string lkey = Server.Storage.GetResourceKey(StorageDirectory.ChannelKeys, Id.ToString(),
|
string lkey = Server.EncryptionHandler.GetChannelKey(Id);
|
||||||
Server.EncryptionHandler.Hash);
|
|
||||||
Parallel.ForEach(Members, new ParallelOptions()
|
Parallel.ForEach(Members, new ParallelOptions()
|
||||||
{
|
{
|
||||||
MaxDegreeOfParallelism = num
|
MaxDegreeOfParallelism = num
|
||||||
|
@ -56,8 +56,7 @@ public class MainSocketTextChannel : MainSocketChannel
|
|||||||
int num = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 5) * 2.0));
|
int num = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 5) * 2.0));
|
||||||
if (num == 0) num = 1;
|
if (num == 0) num = 1;
|
||||||
|
|
||||||
string key = Server.Storage.GetResourceKey(StorageDirectory.ChannelKeys, Id.ToString(),
|
string key = Server.EncryptionHandler.GetChannelKey(Id);
|
||||||
Server.EncryptionHandler.Hash);
|
|
||||||
if (data is null) throw new Exception("Invalid data from server");
|
if (data is null) throw new Exception("Invalid data from server");
|
||||||
if (data.Messages is null) data.Messages = Array.Empty<MainSocketMessage>();
|
if (data.Messages is null) data.Messages = Array.Empty<MainSocketMessage>();
|
||||||
Parallel.ForEach(data.Messages, new ParallelOptions()
|
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));
|
int num = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 5) * 2.0));
|
||||||
if (num == 0) num = 1;
|
if (num == 0) num = 1;
|
||||||
string key = Server.Storage.GetResourceKey(StorageDirectory.ChannelKeys, Id.ToString(),
|
string key = Server.EncryptionHandler.GetChannelKey(Id);
|
||||||
Server.EncryptionHandler.Hash);
|
|
||||||
if (data.Messages is null) data.Messages = Array.Empty<MainSocketMessage>();
|
if (data.Messages is null) data.Messages = Array.Empty<MainSocketMessage>();
|
||||||
Parallel.ForEach(data.Messages, new ParallelOptions()
|
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)
|
public async Task<Task> SendMessage(string Message, CancellationToken CancellationToken, params File?[] Files)
|
||||||
{
|
{
|
||||||
string key = Server.Storage.GetResourceKey(StorageDirectory.ChannelKeys, Id.ToString(),
|
string key = Server.EncryptionHandler.GetChannelKey(Id);
|
||||||
Server.EncryptionHandler.Hash);
|
|
||||||
JsonTypes.HTTP.Message m = new()
|
JsonTypes.HTTP.Message m = new()
|
||||||
{
|
{
|
||||||
Context = Convert.ToBase64String(Encryption.RSA.Encrypt(Message, key, EncoderType.UTF8)),
|
Context = Convert.ToBase64String(Encryption.RSA.Encrypt(Message, key, EncoderType.UTF8)),
|
||||||
|
Loading…
Reference in New Issue
Block a user