Esempio n. 1
0
        public static User Connect(string name, string password, AlebClient client)
        {
            if (!Validation.ValidateUsername(name))
            {
                return(null);
            }
            if (!Validation.ValidatePassword(password))
            {
                return(null);
            }

            User user = Pool.FirstOrDefault(i => i?.Name == name);

            if (user == null)
            {
                user = new User(name, password);
                Pool.Add(user);
            }
            else if (user.Password != password)
            {
                user = null;
            }

            if (user != null)
            {
                user.Client = client;
            }

            return(user);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            AlebClient.LogCommunication = true;

            string bind = Protocol.Localhost;

            if (args.Length == 2 && args[0] == "--bind")
            {
                bind = args[1];
            }
            else if (args.Length != 0)
            {
                Console.Error.WriteLine("Invalid arguments.");
                return;
            }

            TcpListener server = new TcpListener(
                IPAddress.TryParse(bind, out IPAddress ip)
                    ? ip
                    : Dns.GetHostEntry(bind).AddressList.First(i => i.AddressFamily == AddressFamily.InterNetwork),
                Protocol.Port
                );

            try {
                server.Start();
            } catch {
                Console.Error.WriteLine("Failed to start server! Is the port already in use?");
                return;
            }

            Console.WriteLine($"Aleb Server started at {server.LocalEndpoint}");

            while (true)
            {
                AlebClient client = new AlebClient(server.AcceptTcpClient());

                Console.WriteLine($"Connection from {client.Address}");

                client.MessageReceived += Login;
                client.Disconnected    += Disconnect;

                client.Run();

                client.Send("Version", Protocol.Version);
                client.Flush();
            }
        }
Esempio n. 3
0
        static void Login(AlebClient sender, Message msg)
        {
            if (msg.Command != "Login")
            {
                return;
            }

            User user = User.Connect(msg.Args[0], msg.Args[1], sender);

            sender.Send("LoginResult", user?.State.ToString() ?? "null");

            if (user != null)
            {
                sender.MessageReceived -= Login;
                sender.Disconnected    -= Disconnect;

                Console.WriteLine($"{user.Name} logged in from {sender.Address}");
            }

            sender.Flush();
        }
Esempio n. 4
0
        public static async Task <ConnectStatus> Connect(string host)
        {
            TcpClient tcp = null;

            try {
                tcp = new TcpClient();
                await tcp.ConnectAsync(host ?? Protocol.Localhost, Protocol.Port);
            } catch {
                tcp?.Dispose();
                return(ConnectStatus.Failed);
            }

            AlebClient.LogCommunication = true;

            Server = new AlebClient(tcp);

            Server.MessageReceived += Received;
            Server.Disconnected    += _ => Disconnected?.Invoke();

            Server.Name = "Server";

            Task <Message> VersionTask = Register("Version");

            Server.Run();

            await VersionTask;

            if (Convert.ToInt32((await VersionTask).Args[0]) != Protocol.Version)
            {
                Server.Dispose();
                Server = null;

                return(ConnectStatus.VersionMismatch);
            }

            return(ConnectStatus.Success);
        }
Esempio n. 5
0
 static void Disconnect(AlebClient sender)
 => Console.WriteLine($"{sender.Address} disconnected without logging in");