Пример #1
0
        public Client(Socket socket, Server server)
        {
            ClientState = new ClientState(socket);
            Server      = server;
            GameLog.InfoFormat("Connection {0} from {1}:{2}", ConnectionId,
                               ((IPEndPoint)Socket.RemoteEndPoint).Address.ToString(),
                               ((IPEndPoint)Socket.RemoteEndPoint).Port);

            if (server is Lobby)
            {
                EncryptionKey = Game.Config.ApiEndpoints.EncryptionEndpoint != null?GlobalConnectionManifest.RequestEncryptionKey(Game.Config.ApiEndpoints.EncryptionEndpoint.Url, ((IPEndPoint)socket.RemoteEndPoint).Address) : Encoding.ASCII.GetBytes("UrkcnItnI");

                GameLog.InfoFormat($"EncryptionKey is {Encoding.ASCII.GetString(EncryptionKey)}");

                var valid = Game.Config.ApiEndpoints.ValidationEndpoint != null?GlobalConnectionManifest.ValidateEncryptionKey(Game.Config.ApiEndpoints.ValidationEndpoint.Url, new ServerToken { Ip = ((IPEndPoint)socket.RemoteEndPoint).Address.ToString(), Seed = EncryptionKey }) : true;

                if (!valid)
                {
                    GameLog.ErrorFormat("Invalid key from {IP}", ((IPEndPoint)Socket.RemoteEndPoint).Address.ToString());
                    socket.Disconnect(true);
                }
            }

            EncryptionKeyTable = new byte[1024];
            _lastReceived      = DateTime.Now.Ticks;

            GlobalConnectionManifest.RegisterClient(this);

            ConnectedSince = DateTime.Now.Ticks;
        }
Пример #2
0
        public void ReadCallback(IAsyncResult ar)
        {
            ClientState  state = (ClientState)ar.AsyncState;
            Client       client;
            SocketError  errorCode = SocketError.SocketError;
            int          bytesRead = 0;
            ClientPacket receivedPacket;

            GameLog.Debug($"SocketConnected: {state.WorkSocket.Connected}, IAsyncResult: Completed: {ar.IsCompleted}, CompletedSynchronously: {ar.CompletedSynchronously}, queue size: {state.Buffer.Length}");
            GameLog.Debug("Running read callback");

            if (!GlobalConnectionManifest.ConnectedClients.TryGetValue(state.Id, out client))
            {
                // Is this a redirect?
                Redirect redirect;
                if (!GlobalConnectionManifest.TryGetRedirect(state.Id, out redirect))
                {
                    GameLog.ErrorFormat("Receive: data from unknown client (id {0}, closing connection", state.Id);
                    state.WorkSocket.Close();
                    state.WorkSocket.Dispose();
                    return;
                }
                client             = redirect.Client;
                client.ClientState = state;
                if (client.EncryptionKey == null)
                {
                    client.EncryptionKey = redirect.EncryptionKey;
                }
                GlobalConnectionManifest.RegisterClient(client);
            }

            try
            {
                bytesRead = state.WorkSocket.EndReceive(ar, out errorCode);
                if (bytesRead == 0 || errorCode != SocketError.Success)
                {
                    GameLog.Error($"bytesRead: {bytesRead}, errorCode: {errorCode}");
                    client.Disconnect();
                }
            }
            catch (Exception e)
            {
                GameLog.Fatal($"EndReceive Error:  {e.Message}");
                client.Disconnect();
            }

            try
            {
                // TODO: improve / refactor
                while (client.ClientState.TryGetPacket(out receivedPacket))
                {
                    client.Enqueue(receivedPacket);
                }
            }
            catch (Exception e)
            {
                GameLog.Error($"ReadCallback error: {e.Message}");
            }
            ContinueReceiving(state, client);
        }
Пример #3
0
        public virtual void AcceptConnection(IAsyncResult ar)
        {
            // TODO: @norrismiv async callbacks+inheritance? and/or can these callbacks suck less?
            AllDone.Set();
            if (!Active)
            {
                return;
            }
            Socket handler;
            Socket clientSocket;

            try
            {
                clientSocket = (Socket)ar.AsyncState;
                handler      = clientSocket.EndAccept(ar);
            }
            catch (ObjectDisposedException e)
            {
                GameLog.Error($"Disposed socket {e.Message}");
                return;
            }
            Client client = new Client(handler, this);

            Clients.TryAdd(handler.Handle, client);
            GlobalConnectionManifest.RegisterClient(client);

            if (this is Lobby)
            {
                var x7E = new ServerPacket(0x7E);
                x7E.WriteByte(0x1B);
                x7E.WriteString("CONNECTED SERVER\n");
                client.Enqueue(x7E);
                GameLog.DebugFormat("Lobby: AcceptConnection occuring");
                GameLog.DebugFormat("Lobby: cid is {0}", client.ConnectionId);
            }
            else if (this is Login)
            {
                GameLog.DebugFormat("Login: AcceptConnection occuring");
                GameLog.DebugFormat("Login: cid is {0}", client.ConnectionId);
            }
            else if (this is World)
            {
                GameLog.DebugFormat("World: AcceptConnection occuring");
                GameLog.DebugFormat("World: cid is {0}", client.ConnectionId);
            }
            try
            {
                handler.BeginReceive(client.ClientState.Buffer, 0, client.ClientState.Buffer.Length, 0,
                                     new AsyncCallback(ReadCallback), client.ClientState);
                GameLog.DebugFormat("AcceptConnection returning");
                clientSocket.BeginAccept(new AsyncCallback(AcceptConnection), clientSocket);
            }
            catch (SocketException e)
            {
                Game.ReportException(e);
                handler.Close();
            }
        }
Пример #4
0
 public Client(Socket socket, Server server)
 {
     ClientState = new ClientState(socket);
     Server      = server;
     Logger.InfoFormat("Connection {0} from {1}:{2}", ConnectionId,
                       ((IPEndPoint)Socket.RemoteEndPoint).Address.ToString(),
                       ((IPEndPoint)Socket.RemoteEndPoint).Port);
     EncryptionKey      = Encoding.ASCII.GetBytes("UrkcnItnI");
     EncryptionKeyTable = new byte[1024];
     _lastReceived      = DateTime.Now.Ticks;
     GlobalConnectionManifest.RegisterClient(this);
     ConnectedSince = DateTime.Now.Ticks;
 }
Пример #5
0
        public void ReadCallback(IAsyncResult ar)
        {
            ClientState state = (ClientState)ar.AsyncState;
            Client      client;
            SocketError errorCode;
            int         bytesRead = 0;

            if (this is Login)
            {
                Logger.InfoFormat("This is a login ReadCallback");
            }
            try
            {
                bytesRead = state.WorkSocket.EndReceive(ar, out errorCode);
                if (errorCode != SocketError.Success)
                {
                    bytesRead = 0;
                }
            }
            catch (SocketException)
            {
                state.WorkSocket.Close();
            }
            catch (ObjectDisposedException)
            {
                state.WorkSocket.Close();
            }

            if (!GlobalConnectionManifest.ConnectedClients.TryGetValue(state.Id, out client))
            {
                // Is this a redirect?
                Redirect redirect;
                if (!GlobalConnectionManifest.TryGetRedirect(state.Id, out redirect))
                {
                    Logger.ErrorFormat("Receive: data from unknown client (id {0}, closing connection", state.Id);
                    state.WorkSocket.Close();
                    state.WorkSocket.Dispose();
                    return;
                }
                client             = redirect.Client;
                client.ClientState = state;
                GlobalConnectionManifest.RegisterClient(client);
            }

            if (bytesRead > 0)
            {
                if (this is Login)
                {
                    Logger.InfoFormat("Dafuq bro");
                }
                var inboundBytes = state.ReceiveBufferTake(bytesRead).ToArray();
                if (inboundBytes[0] != 0xAA)
                {
                    Logger.DebugFormat("cid {0}: client is wat",
                                       client.ConnectionId);
                    state.ResetReceive();
                }
                else
                {
                    while (inboundBytes.Length > 3)
                    {
                        Logger.InfoFormat("Inside while loop");
                        var packetLength = ((int)inboundBytes[1] << 8) + (int)inboundBytes[2] + 3;
                        if (packetLength > inboundBytes.Length)
                        {
                            // We haven't received the entire packet yet; read more bytes
                            break;
                        }
                        else
                        {
                            // We've received an intact packet, pop it off
                            ClientPacket receivedPacket =
                                new ClientPacket(state.ReceiveBufferPop(packetLength).ToArray());
                            // Also remove it from our local buffer...this seems kinda gross to me
                            inboundBytes =
                                new List <byte>(inboundBytes).GetRange(packetLength,
                                                                       inboundBytes.Length - packetLength)
                                .ToArray();

                            if (receivedPacket.ShouldEncrypt)
                            {
                                receivedPacket.Decrypt(client);
                            }
                            if (receivedPacket.Opcode == 0x39 || receivedPacket.Opcode == 0x3A)
                            {
                                receivedPacket.DecryptDialog();
                            }
                            try
                            {
                                if (this is Lobby)
                                {
                                    Logger.InfoFormat("Lobby: 0x{0:X2}", receivedPacket.Opcode);
                                    var handler = (this as Lobby).PacketHandlers[receivedPacket.Opcode];
                                    handler.Invoke(client, receivedPacket);
                                    Logger.InfoFormat("Lobby packet done");
                                    client.UpdateLastReceived();
                                }
                                else if (this is Login)
                                {
                                    Logger.InfoFormat("Login: 0x{0:X2}", receivedPacket.Opcode);
                                    var handler = (this as Login).PacketHandlers[receivedPacket.Opcode];
                                    handler.Invoke(client, receivedPacket);
                                    Logger.InfoFormat("Login packet done");
                                    client.UpdateLastReceived();
                                }
                                else
                                {
                                    client.UpdateLastReceived(receivedPacket.Opcode != 0x45 &&
                                                              receivedPacket.Opcode != 0x75);
                                    Logger.InfoFormat("Queuing: 0x{0:X2}", receivedPacket.Opcode);
                                    World.MessageQueue.Add(new HybrasylClientMessage(receivedPacket,
                                                                                     client.ConnectionId));
                                    Logger.InfoFormat("World packet done");
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.ErrorFormat("EXCEPTION IN HANDLING: 0x{0:X2}: {1}", receivedPacket.Opcode, e);
                            }
                        }
                    }
                }
            }
            else
            {
                Logger.DebugFormat("cid {0}: client is disconnected or corrupt packets received",
                                   client.ConnectionId);
                client.Disconnect();
                return;
            }

            // Continue getting dem bytes
            try
            {
                state.WorkSocket.BeginReceive(state.Buffer, 0, state.Buffer.Length, 0,
                                              new AsyncCallback(this.ReadCallback), state);
                Logger.InfoFormat("Triggering receive callback");
            }
            catch (ObjectDisposedException)
            {
                //client.Disconnect();
                state.WorkSocket.Close();
            }
            catch (SocketException)
            {
                client.Disconnect();
            }
        }