示例#1
0
 public string Login(ICommandPacket packet)
 {
     Load();
     if (packet.IsInvalidArguments(2))
     {
         socket.SendPacket(packet);
         return string.Empty;
     }
     var login = packet.Args.First().Trim().ToLower();
     if (dictAuth.ContainsKey(login) &&
         dictAuth[login] == packet.Args.Last().Trim())
     {
         socket.SendPacket(packet);
         Console.WriteLine($"[{Logger.Time()}] User {login} logined");
         return login;
     }
     packet.ErrorInfo = "Wrong login or password";
     socket.SendPacket(packet);
     return string.Empty;
 }
示例#2
0
        public void Help(HelpPacket helpPacket)
        {
            Session.SendPacket(Session.Character.GenerateSay("-------------Help command-------------", Shared.SayColorType.Purple));
            List <Type> classes = helpPacket.GetType().Assembly.GetTypes().Where(t => typeof(ICommandPacket).IsAssignableFrom(t) && t.GetCustomAttribute <PacketHeaderAttribute>()?.Authority <= Session.Account.Authority).OrderBy(x => x.Name).ToList();

            foreach (Type type in classes)
            {
                ICommandPacket classInstance = type.CreateInstance <ICommandPacket>();
                MethodInfo     method        = type.GetMethod("Help");
                if (method == null)
                {
                    continue;
                }

                string message = method.Invoke(classInstance, null).ToString();
                if (!string.IsNullOrEmpty(message))
                {
                    Session.SendPacket(Session.Character.GenerateSay(message, Shared.SayColorType.Green));
                }
            }
        }
示例#3
0
        public string Registration(ICommandPacket packet)
        {
            Load();
            string login = "";
            if (packet.IsInvalidArguments(2))
            { }
            else
            {
                login = packet.Args.First().Trim().ToLower();
                if (dictAuth.ContainsKey(login))
                packet.ErrorInfo = $"Name '{login}' is busy";
            }

            socket.SendPacket(packet);

            if (packet.Error != 0)
                return string.Empty;
            dictAuth.Add(login, packet.Args.Last());
            Console.WriteLine($"[{Logger.Time()}] User {login} registration end");
            Dump();
            return packet.Args.First();
        }
        private bool StepAuth()
        {
            var auth = new BaseAuth(socket);

            try
            {
                do
                {
                    packet = socket.RecivePacket(CommandType.Auth);
                    switch (packet.Command)
                    {
                        case CommandType.Login:
                            userName = auth.Login(packet);
                            break;
                        case CommandType.Registration:
                            userName = auth.Registration(packet);
                            break;
                        default:
                            return true;
                    }

                } while (userName == Empty);
            }
            catch (SocketException)
            {
                Console.WriteLine($"[{Logger.Time()}] Connection lost");
                return true;
            }
            return false;
        }
        public void Run(object arg = null)
        {
            socket = arg as AdvancedSocket;
            if (socket == null) return;
            if (StepAuth()) return;
            provider = new FolderProvider(userName, 
                ((IPEndPoint)socket.socket.RemoteEndPoint).Address.ToString());


            while (true)
                try
                {
                    packet = socket.RecivePacket(CommandType.WorkerCommands);
                    switch (packet.Command)
                    {
                        case CommandType.Add:
                            Add();
                            break;
                        case CommandType.Clone:
                            Clone();
                            break;
                        case CommandType.Commit:
                            Commit();
                            break;
                        case CommandType.Update:
                            Update();
                            break;
                        case CommandType.Revert:
                            Revert();
                            break;
                        case CommandType.Log:
                            Log();
                            break;
                        case CommandType.GoodBy:
                            Console.WriteLine($"[{Logger.Time()}] client say goodby");
                            return;
                        default:
                            return;
                    }
                }
                catch (SocketException)
                {
                    packet.ErrorInfo = "Connection lost";
                    return;
                }
                catch (GitHubException e)
                {
                    packet.ErrorInfo = e.Message;
                }
                catch (Exception e)
                {
                    packet.ErrorInfo = "Unknown server exception";
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    try{socket.SendPacket(packet);}
                    catch (Exception){/*ignored*/}
                    if(packet.Command != CommandType.Log)
                        Console.WriteLine($"[{Logger.Time()}][{userName}] {packet.Command} {Join(" ",packet.Args)} {packet.ErrorInfo}");
                }
            
        }
示例#6
0
 public void SendPacket(ICommandPacket packet)
 {
     var bytes = packet.Bytes;
     Send(BitConverter.GetBytes(bytes.Length));
     Send(packet.Bytes);
 }
示例#7
0
 public ICommandPacket SendAndRecivePacket(ICommandPacket packet)
 {
     SendPacket(packet);
     return RecivePacket();
 }