196 lines
8.4 KiB
C#
196 lines
8.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using JacobTechEncryption;
|
|
using JacobTechEncryption.Enums;
|
|
using Luski.net.Enums;
|
|
using Luski.net.JsonTypes.BaseTypes;
|
|
using Luski.net.JsonTypes.WSS;
|
|
using Luski.net.Structures;
|
|
using Luski.net.Structures.Public;
|
|
using Luski.Shared.PublicServers.V1.Enums;
|
|
using Luski.Shared.PublicServers.V1.ServerToClient.HTTP;
|
|
using Luski.Shared.PublicServers.V1.Shared;
|
|
using WebSocketSharp;
|
|
using File = System.IO.File;
|
|
using DataType = Luski.Shared.PublicServers.V1.Enums.DataType;
|
|
|
|
namespace Luski.net;
|
|
|
|
public partial class PublicServer
|
|
{
|
|
private void DataFromServer(object? sender, MessageEventArgs e)
|
|
{
|
|
if (e.IsPing) return;
|
|
if (PrintServerMessages)
|
|
{
|
|
Console.WriteLine(e.Data);
|
|
}
|
|
try
|
|
{
|
|
BetterIncomingWSS? data = JsonSerializer.Deserialize(e.Data, BetterIncomingWSSContext.Default.BetterIncomingWSS);
|
|
switch (data?.Type)
|
|
{
|
|
case DataType.Token:
|
|
WSSLogin n = JsonSerializer.Deserialize(e.Data, WSSLoginContext.Default.WSSLogin)!;
|
|
File.WriteAllText(Storage.GetStorageDirectory(StorageDirectory.StorageInfo) + "token",
|
|
n.SessionToken);
|
|
Token = n.Token;
|
|
break;
|
|
case DataType.StatusUpdate:
|
|
StatusEvent se = JsonSerializer.Deserialize(data.Data!.ToString()!, StatusEventContext.Default.StatusEvent)!;
|
|
SocketUser u;
|
|
if (se.User == User.Id) u = User;
|
|
else
|
|
{
|
|
u = GetUser<SocketUser>(se.User);
|
|
u.Status = se.After;
|
|
}
|
|
if (StatusUpdate is not null)
|
|
{
|
|
StatusUpdate.Invoke(se.Before, u);
|
|
}
|
|
break;
|
|
case DataType.Role:
|
|
RoleEvent re = JsonSerializer.Deserialize(data.Data!.ToString()!, RoleEventContext.Default.RoleEvent)!;
|
|
Role[] ra = roles.Where(s => s.ID == re.ID).ToArray();
|
|
Role r;
|
|
bool @new = false;
|
|
if (ra.Length > 0)
|
|
{
|
|
r = ra[0];
|
|
if (re.ServerPermissions is not null) r.ServerPermissions = re.ServerPermissions.Value;
|
|
if (re.Description is not null) r.Description = re.Description;
|
|
if (re.DisplayName is not null) r.DisplayName = re.DisplayName;
|
|
if (re.ColorType is not null) r.ColorType = re.ColorType.Value;
|
|
if (re.Color is not null)
|
|
{
|
|
if (r.ColorType == ColorType.Full)
|
|
{
|
|
Color nc = new(re.Color);
|
|
r.Colors = new []{nc};
|
|
}
|
|
else
|
|
{
|
|
List<Color> cols = new();
|
|
for (int i = 0; i < re.Color.Length - 7; i+=8)
|
|
{
|
|
cols.Add(new(re.Color[i..(i+8)]));
|
|
}
|
|
r.Colors = cols.ToArray();
|
|
}
|
|
}
|
|
if (re.Index is not null) r.Index = re.Index.Value;
|
|
}
|
|
else
|
|
{
|
|
@new = true;
|
|
Task<Role> rr = GetRole(re.ID);
|
|
rr.Wait();
|
|
r = rr.Result;
|
|
}
|
|
|
|
if (RoleEvent is not null) _ = RoleEvent.Invoke(@new, r);
|
|
break;
|
|
case DataType.RoleMember:
|
|
RoleMemberEvent rme = JsonSerializer.Deserialize(data.Data!.ToString()!, RoleMemberEventContext.Default.RoleMemberEvent)!;
|
|
break;
|
|
case DataType.RoleDelete:
|
|
break;
|
|
case DataType.MessageCreate:
|
|
MessageSTC smsg = JsonSerializer.Deserialize(data.Data!.ToString()!, MessageSTCContext.Default.MessageSTC)!;
|
|
List<long> fl = new();
|
|
List<SocketFile> sf = new();
|
|
foreach (ServerFileInfoSTC v in smsg.Files)
|
|
{
|
|
if (v.NameKey == 0)
|
|
{
|
|
if (string.IsNullOrEmpty(v.Name))
|
|
{
|
|
v.Name = "";
|
|
}
|
|
else v.Name = Encryption.Generic.Encoders[(int)v.NameEncoder]
|
|
.GetString(Convert.FromBase64String(v.Name));
|
|
}
|
|
else
|
|
{
|
|
LocalKeyInfo key = EncryptionHandler.GetKey(v.NameKey);
|
|
switch (key.EncryptionType)
|
|
{
|
|
case EncryptionType.RSA:
|
|
v.Name = Encryption.RSA.Decrypt(Convert.FromBase64String(v.Name), key.Key,
|
|
v.NameEncoder);
|
|
break;
|
|
default:
|
|
v.Name = Encryption.Generic.Encoders[(int)v.NameEncoder]
|
|
.GetString(Convert.FromBase64String(v.Name));
|
|
break;
|
|
}
|
|
}
|
|
fl.Add(v.ID);
|
|
sf.Add(new()
|
|
{
|
|
ID = v.ID,
|
|
Size = v.Size,
|
|
Name = v.Name,
|
|
Encoder = v.Encoder,
|
|
NameEncoder = v.NameEncoder,
|
|
Key = v.Key,
|
|
NameKey = v.NameKey,
|
|
Server = this
|
|
});
|
|
}
|
|
|
|
if (smsg.EncryptionKey == 0)
|
|
{
|
|
if (string.IsNullOrEmpty(smsg.Context))
|
|
{
|
|
smsg.Context = "";
|
|
}
|
|
else smsg.Context = Encryption.Generic.Encoders[(int)smsg.EncoderType]
|
|
.GetString(Convert.FromBase64String(smsg.Context));
|
|
}
|
|
else
|
|
{
|
|
LocalKeyInfo key = EncryptionHandler.GetKey(smsg.EncryptionKey);
|
|
switch (key.EncryptionType)
|
|
{
|
|
case EncryptionType.RSA:
|
|
smsg.Context = Encryption.RSA.Decrypt(Convert.FromBase64String(smsg.Context), key.Key,
|
|
smsg.EncoderType);
|
|
break;
|
|
default:
|
|
smsg.Context = Encryption.Generic.Encoders[(int)smsg.EncoderType]
|
|
.GetString(Convert.FromBase64String(smsg.Context));
|
|
break;
|
|
}
|
|
}
|
|
SocketMessage sm = new()
|
|
{
|
|
ID = smsg.ID,
|
|
AuthorID = smsg.AuthorID,
|
|
TimeStamp = smsg.Timestamp,
|
|
ChannelID = smsg.ChannelID,
|
|
Context = smsg.Context,
|
|
EncoderType = smsg.EncoderType,
|
|
EncryptionKey = smsg.EncryptionKey,
|
|
FileIDs = fl.ToArray(),
|
|
Server = this,
|
|
ProfileID = smsg.ProfileID,
|
|
_Files = sf
|
|
};
|
|
if (MessageReceived is not null) MessageReceived.Invoke(sm);
|
|
break;
|
|
default:
|
|
Console.WriteLine("Unknown");
|
|
break;
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Exception(exception);
|
|
}
|
|
}
|
|
} |