using Luski.net.Enums; using Luski.net.Interfaces; using Luski.net.JsonTypes.BaseTypes; using System.Collections.Generic; using System; using System.Text.Json.Serialization; using System.Linq; using System.Net.Http; using System.Threading.Tasks; using Luski.net.JsonTypes.WSS; using System.Text.Json.Serialization.Metadata; using System.Threading; using JacobTechEncryption; using JacobTechEncryption.Enums; using Luski.net.Enums.Main; using Luski.net.JsonTypes; namespace Luski.net.Structures.Main; public class MainSocketChannel : IncomingHTTP { [JsonInclude] [JsonPropertyName("id")] public long Id { get; internal set; } = default!; [JsonInclude] [JsonPropertyName("title")] public string Title { get; internal set; } = default!; [JsonInclude] [JsonPropertyName("description")] public string Description { get; internal set; } = default!; [JsonInclude] [JsonPropertyName("key")] public string Key { get; internal set; } = default!; [JsonPropertyName("type")] [JsonInclude] public ChannelType Type { get; internal set; } = default!; public MainServer Server { get; internal set; } = default!; [JsonPropertyName("members")] [JsonInclude] public long[] MemberIdList { get; internal set; } = default!; [JsonIgnore] public IReadOnlyList Members { get { if (MemberIdList is null || MemberIdList.Length == 0) return Array.Empty().ToList().AsReadOnly(); if (_members is null || !_members.Any()) { _members = new(); foreach (long member in MemberIdList) { if (member != Server.User.Id) _members.Add(Server.GetUser(member, CancellationToken.None).Result); else _members.Add(Server.User); } } return _members.AsReadOnly(); } } [JsonIgnore] private List _members = new(); public Task SendKeysToUsers(CancellationToken CancellationToken) { if (Key is null) { StartKeyProcessAsync(CancellationToken).Wait(); return Task.CompletedTask; } int num = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 50) * 2.0)); if (num == 0) num = 1; string lkey = Server.EncryptionHandler.GetChannelKey(Id); Parallel.ForEach(Members, new ParallelOptions() { MaxDegreeOfParallelism = num }, i => { if (i.Id != Server.User.Id) { long key = i.GetUserKeys(CancellationToken).Result.First().Id; if (true) { WSSKeyExchange send = new() { to = i.Id, channel = Id, key = Convert.ToBase64String(Encryption.RSA.Encrypt(lkey, key.ToString(), EncoderType.UTF8)) }; Server.SendServerOld(send, WSSKeyExchangeContext.Default.WSSKeyExchange); } } }); return Task.CompletedTask; } internal Task StartKeyProcessAsync(CancellationToken CancellationToken) { //TODO code key exchange /* ClientEncryption.GenerateNewKeys(out string Public, out string Private); Key = Public; HttpResponseMessage b; using (HttpClient web = new()) { web.DefaultRequestHeaders.Add("token", Luski.net.Server.Token); b = web.PostAsync($"https://{Server.Domain}/{Server.ApiVersion}/SocketChannel/SetKey/{Id}", new StringContent(Key), CancellationToken).Result; } int num = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * Luski.net.Server.Percent) * 2.0)); if (num == 0) num = 1; Encryption.File.Channels.AddKey(Id, Private); Parallel.ForEach(Members, new ParallelOptions() { MaxDegreeOfParallelism = num }, i => { if (i.Id != Luski.net.Server._user?.Id) { string key = i.GetUserKey(CancellationToken, Server).Result; if (!string.IsNullOrEmpty(key)) { WSSKeyExchange send = new() { to = i.Id, channel = Id, key = Convert.ToBase64String(Encryption.Encrypt(Private, key)) }; Luski.net.Server.SendServer(send, WSSKeyExchangeContext.Default.WSSKeyExchange); } } });*/ return Task.CompletedTask; } } [JsonSerializable(typeof(MainSocketChannel))] [JsonSourceGenerationOptions( GenerationMode = JsonSourceGenerationMode.Default, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, WriteIndented = false)] internal partial class MainSocketChannelContext : JsonSerializerContext { }