yo, i only need to do login (and printing stuff) and im done!

This commit is contained in:
Rusty Striker 2025-01-01 19:42:20 +02:00
parent 145e77e437
commit 04a4f7ece8
Signed by: RustyStriker
GPG key ID: 87E4D691632DFF15
5 changed files with 124 additions and 54 deletions

View file

@ -37,6 +37,26 @@ public class Data
}
}
public bool AddMessage(string Phone, byte[] Message)
{
if (Keys.ContainsKey(Phone))
{
if (Messages.TryGetValue(Phone, out var value))
{
value.Enqueue(Message);
}
else
{
Messages[Phone] = new Queue<byte[]>([Message]);
}
return true;
}
else
{
return false;
}
}
public bool PeekMessages(string Phone)
{
return Messages.TryGetValue(Phone, out Queue<byte[]>? value) && value.TryPeek(out var _);

View file

@ -200,22 +200,22 @@ public class Program
switch ((RequestType)msg[1])
{
case RequestType.GetMessages:
byte[] msgsLens = new byte[16]; // 128 bits
byte[] msgsLens = Enumerable.Repeat<byte>(0, 16).ToArray(); // 128 bits
// get 15 messages, last byte will indicate if there are more
List<byte[]> msgs = Data.GetMessages(clientPhone, 15) ?? [];
List<byte[]> msgs = Data.GetMessages(clientPhone, 7) ?? [];
Write(id, $"Got {msgs.Count} messages");
byte[] msgsBytes = new byte[msgs.Select(m => m.Length).Sum()];
int msgsbytesIndex = 0;
for (int i = 0; i < msgsLens.Length - 1; i += 1)
for (int i = 0; i < msgs.Count; i += 1)
{
// it is expected that all messages will be less than 255 bytes, hence a single byte to
// denote length is sufficient, but a simple update to the protocol can allow up to 7 messages
// per request (instead of 15), and use an ushort (u16) instead
msgsLens[i] = (byte)(msgs.Count > i ? msgs[i].Length : 0);
if (i < msgs.Count)
{
// copy the message to the msgsBytes array
Array.Copy(msgs[i], 0, msgsBytes, msgsbytesIndex, msgs[i].Length);
}
// messages are encrypted blocks of (currently) 1024 RSA keys, so it would be 256 bytes
// meaning we need a short at least (technically we need 9 bytes, but using a full short will allow for
// bigger key sizes without much hassle, until a certain length)
msgsLens[2 * i] = (byte)(msgs[i].Length >> 8);
msgsLens[(2 * i) + 1] = (byte)msgs[i].Length;
// copy the message to the msgsBytes array
Array.Copy(msgs[i], 0, msgsBytes, msgsbytesIndex, msgs[i].Length);
msgsbytesIndex += msgs[i].Length;
}
msgsLens[15] = Data.PeekMessages(clientPhone) ? (byte)1 : (byte)0;
// only need to encrypt the lengths of the messages, as the messages themselves are encrypted
@ -242,7 +242,14 @@ public class Program
case RequestType.SendMessage:
string recv = Utils.BytesToNumber(msg[3..11]);
int msgLen = BitConverter.ToInt32(msg, 11);
if (msgLen != (len - MSG_LEN))
{
Write(id, $"Got message to {recv} of length {len - MSG_LEN} but expected {msgLen}");
}
byte[] clientMsg = buffer[MSG_LEN..(msgLen + MSG_LEN)];
// simply add the clientMsg to the "Data"
bool added = Data.AddMessage(recv, clientMsg);
Write(id, $"Added message to {recv} of length {msgLen}: {added}");
break;
default:
msg = sk.EncryptCfb(Encoding.UTF8.GetBytes("INVALID REQUEST"), sk.IV, PaddingMode.PKCS7);