// Read an authentication message and return a valid ClientCon if successful. private ClientCon Authenticate(TcpClient tcpClient, BinaryWriter writer, BinaryReader reader) { var pubkey = reader.ReadString(); var authToken = reader.ReadLenBytes(); var signature = reader.ReadLenBytes(); try { // verify signature if (!EncryptionService.VerifySigned(authToken, signature, pubkey)) { return new ClientCon(); } // extract nick from token var authReader = new BinaryReader(new MemoryStream(authToken)); var nick = authReader.ReadString(); lock (clients) { // check to see if logging in same as someone else foreach (var other in clients) { if (other.Pubkey == pubkey && other.Name != nick) { Console.WriteLine(nick + " (" + tcpClient.Client.RemoteEndPoint + "): Cannot log in with the same publickey as " + other.Name); return new ClientCon(); } } } switch (CheckDatabase(nick, pubkey)) { case 0: return new ClientCon(tcpClient, writer, nick, pubkey); case 1: return new ClientCon(tcpClient, writer, nick, pubkey); case 2: // user connecting with public key that does not match the username return new ClientCon(); default: // something else happened return new ClientCon(); } } catch (Exception e) { Console.WriteLine(e); return new ClientCon(); } }
// Process (send to dest) a message from a client private void ProcessMsg(BinaryReader reader, ClientCon me) { var sendToKey = reader.ReadString(); var message = reader.ReadLenBytes(); lock (clients) { var found = false; foreach (var other in clients) { if (other.Pubkey == sendToKey) { found = true; other.Writer.Write("msgrecv"); other.Writer.Write(me.Name); other.Writer.WriteLenBytes(message); } } if (!found) { me.Writer.Write("usernotfound"); me.Writer.Write(sendToKey); } } }
// Process the `recv` message - name, encrypted message pair private void ProcessMsgRecv(BinaryReader reader) { var name = reader.ReadString(); var message = reader.ReadLenBytes(); var decrypt = encryption.Decrypt(message); AddIncomingMessage(name, decrypt); }