void GetIP(ServerMessage request) { if (!CheckLogin(clientStream)) { return; } var other = ((UsernameRequest)request).Username; if (!s.Db.HasContact(loggedIn, other)) { SingleResponse.Send(clientStream, ServerMessageType.NotPermitted, "You and " + other + " don't share a contact"); return; } var ips = s.IpManager.GetIPs(other); var list = new List <ClientAddress>(); for (int i = 0; i < ips.Count; i++) { ushort port; if (s.HolePunchServer.GetSourcePort(ips[i], sessionKey, out port)) { list.Add(new ClientAddress(ips[i], port)); } } if (list.Count <= 0) { SingleResponse.Send(clientStream, ServerMessageType.Fail, other + " isn't logged in"); return; } SendIPs.Send(clientStream, list); }
void Login(ServerMessage request) { if (loggedIn != null) { SingleResponse.Send(clientStream, ServerMessageType.Fail, "You're already logged in"); return; } var login = (LoginRequest)request; if (!s.Db.AuthenticateUser(login.Username, login.Password)) { SingleResponse.Send(clientStream, ServerMessageType.AuthenticationError, "The username or password provided was incorrect"); return; } loggedIn = login.Username; currentPermissions = s.Db.GetPermissions(loggedIn); var maxIPcount = s.Permissions.MaxConcurrentIPs(currentPermissions); if (!s.IpManager.AddIP(loggedIn, ip, maxIPcount)) { SingleResponse.Send(clientStream, ServerMessageType.NotPermitted, "You're logged into too many devices. The most your account level allows is " + maxIPcount); return; } sessionKey = s.HolePunchServer.Add(ip); LoginSuccess.Send(clientStream, sessionKey); }
bool CheckLogin(StreamWrapper clientStream) { if (loggedIn == null) { SingleResponse.Send(clientStream, ServerMessageType.AuthenticationError, "You aren't logged in"); return(false); } return(true); }
void GetPublicKey(ServerMessage request) { var req = (UsernameRequest)request; var key = s.Db.GetPublicKey(req.Username); if (key == null) { SingleResponse.Send(clientStream, ServerMessageType.Fail, "No key information found"); } else { IncomingKey.Send(clientStream, key); } }
protected void HandleMessage(ServerMessage message) { MessageHandler handle; lock (handlers) handle = handlers[(byte)message.Type]; if (handle == null) { SingleResponse.Send(clientStream, ServerMessageType.Fail, "Unknown command"); } else { handle(this, message); } }
void SendContact(ServerMessage request) { if (!CheckLogin(clientStream)) { return; } var req = (ContactRequest)request; if (!s.Db.SendContact(loggedIn, req.Username, req.Message)) { SingleResponse.Send(clientStream, ServerMessageType.Fail, "The contact already exists"); return; } ServerMessage.Success(clientStream); }
void GetPrivateKey(ServerMessage request) { if (!CheckLogin(clientStream)) { return; } var req = (UsernameRequest)request; var key = s.Db.GetPrivateKey(loggedIn, req.Username); if (key == null) { SingleResponse.Send(clientStream, ServerMessageType.Fail, "No key information found"); } else { IncomingKey.Send(clientStream, key); } }