public String listUsers() { List <dynamic> users_ = new List <dynamic>(), groups_ = new List <dynamic>(); foreach (User item in ServerDispatcher.get().ServerSockets) { if (item.ID == user.ID) { continue; } users_.Add(new { id = item.ID, alias = item.Alias, status = item.status, PKEY = Convert.ToBase64String(item.PKEY) }); } foreach (Group group in ServerDispatcher.get().Rooms) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.ImportCspBlob(user.PKEY); byte[] encryptedKey = rsa.Encrypt(group.des.Key, false); byte[] encryptedIV = rsa.Encrypt(group.des.IV, false); groups_.Add(new { id = group.ID, alias = group.Alias, leader = group.leader, subscribers = group.Subscribers.Select(x => x.ID).ToList(), PKEY = Convert.ToBase64String(group.rsa.ExportCspBlob(false)), key = Convert.ToBase64String(encryptedKey), iv = Convert.ToBase64String(encryptedIV) }); } dynamic handshake = new { users = users_, rooms = groups_, command = DispatcherCodes.CONNECT }; string str = format(handshake); return(str); }
public void alertQuit() { var payload = new { user = user.ID, command = DispatcherCodes.QUIT }; List <Socket> sockets = ServerDispatcher.get().ServerSockets.Select(s => s.Socket).ToList(); Socket.Select(null, sockets, null, 1000); foreach (Socket sock in sockets) { send(format(payload), sock); sendDone.WaitOne(); } }
static void iniciaGrupos() { ServerDispatcher.get().Rooms.Add(new Group() { ID = "group#00", Alias = "Política", leader = null, des = new DESCryptoServiceProvider(), rsa = new RSACryptoServiceProvider(), }); ServerDispatcher.get().Rooms.Add(new Group() { ID = "group#01", Alias = "Assuntos Gerais", des = new DESCryptoServiceProvider(), rsa = new RSACryptoServiceProvider(), leader = null }); }
public void alertNewUser() { var payload = new { alias = user.Alias, id = user.ID, status = user.status, PKEY = Convert.ToBase64String(user.PKEY), command = DispatcherCodes.NEW_USER, groups = ServerDispatcher.get().Rooms.Where(v => v.Subscribers.Exists(x => x.ID == user.ID)).Select(x => x.ID).ToList() }; List <User> users = ServerDispatcher.get().ServerSockets.Where(s => s.ID != user.ID).ToList(); foreach (User u in users) { send(format(payload), u.Socket); sendDone.WaitOne(); } }
public void handleRequest(StateObject obj) { dynamic data = obj.data; switch ((int)data["command"]) { case (int)DispatcherCodes.CONNECT: user.ID = data["id"]; user.Alias = data["alias"]; user.status = UserStatus.DISPONIVEL; user.PKEY = Convert.FromBase64String(data["PKEY"]); ServerDispatcher.get().Rooms.Find(x => x.ID == "group#00").Subscribers.Add(user); ServerDispatcher.get().Rooms.Find(x => x.ID == "group#01").Subscribers.Add(user); send(listUsers()); sendDone.WaitOne(); alertNewUser(); break; case (int)DispatcherCodes.DISCONNECT: break; case (int)DispatcherCodes.SEND: if ((bool)data["isGroup"]) { Group group = ServerDispatcher.get().Rooms.Find(s => s.ID == data["id"]); List <Socket> sockets = new List <Socket>(ServerDispatcher.get().ServerSockets.Select(s => s.Socket)); foreach (Socket item in sockets) { dynamic message = new { message = data["message"], id = group.ID, sender = user.ID, command = DispatcherCodes.SEND, }; send(serializer.Serialize(message), item); sendDone.WaitOne(); } } else { User sendto = ServerDispatcher.get().ServerSockets.Find(x => x.ID == data["id"]); dynamic message = new { id = user.ID, sender = sendto.ID, command = DispatcherCodes.SEND, message = data["message"], key = data["key"], iv = data["iv"] }; send(serializer.Serialize(message), sendto.Socket); sendDone.WaitOne(); send(serializer.Serialize(message), client); sendDone.WaitOne(); } break; } }
public void releaseClient() { client.Disconnect(false); ServerDispatcher.get().ServerSockets.Remove(user); }