示例#1
0
        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);
        }
示例#2
0
        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);
        }
示例#3
0
 bool CheckLogin(StreamWrapper clientStream)
 {
     if (loggedIn == null)
     {
         SingleResponse.Send(clientStream, ServerMessageType.AuthenticationError,
                             "You aren't logged in");
         return(false);
     }
     return(true);
 }
示例#4
0
        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);
            }
        }
示例#5
0
        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);
            }
        }
示例#6
0
        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);
        }
示例#7
0
        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);
            }
        }