Esempio n. 1
0
        private static void ClientLoop(ILogger packetLogger)
        {
            var diagnosticProvider = new InfusionDiagnosticPushStreamProvider(Configuration, Console);

            serverDiagnosticPushStream =
                new CompositeDiagnosticPushStream(new ConsoleDiagnosticPushStream(packetLogger, "proxy -> server"),
                                                  new InfusionBinaryDiagnosticPushStream(DiagnosticStreamDirection.ClientToServer, diagnosticProvider.GetStream));
            serverDiagnosticPullStream = new ConsoleDiagnosticPullStream(packetLogger, "server -> proxy");

            serverConnection = new ServerConnection(ServerConnectionStatus.Initial, serverDiagnosticPullStream,
                                                    serverDiagnosticPushStream);
            serverConnection.PacketReceived += ServerConnectionOnPacketReceived;

            clientConnection = new UltimaClientConnection(UltimaClientConnectionStatus.Initial,
                                                          new ConsoleDiagnosticPullStream(packetLogger, "client -> proxy"),
                                                          new CompositeDiagnosticPushStream(new ConsoleDiagnosticPushStream(packetLogger, "proxy -> client"),
                                                                                            new InfusionBinaryDiagnosticPushStream(DiagnosticStreamDirection.ServerToClient, diagnosticProvider.GetStream)));
            clientConnection.PacketReceived += ClientConnectionOnPacketReceived;

            diagnosticProvider.ClientConnection = clientConnection;
            diagnosticProvider.ServerConnection = serverConnection;

            Task.Run(() => ServerLoop());

            try
            {
                while (true)
                {
                    var client = listener.AcceptTcpClient();
                    ClientStream = client.GetStream();

                    int receivedLength;
                    var receiveBuffer = new byte[65535];

                    while ((receivedLength = ClientStream.Read(receiveBuffer, 0, receiveBuffer.Length)) > 0)
                    {
                        var memoryStream = new MemoryStream(receiveBuffer, 0, receivedLength, false);

                        clientConnection.ReceiveBatch(new MemoryStreamToPullStreamAdapter(memoryStream));
                    }

                    Thread.Yield();
                }
            }
            catch (IOException ioex) when(ioex.InnerException is SocketException socex && socex.SocketErrorCode == SocketError.ConnectionReset)
            {
                Console.Error("Connection to client lost.");
                throw;
            }
            catch (Exception ex)
            {
                Console.Error(serverDiagnosticPullStream.Flush());
                Console.Error(ex.ToString());
                throw;
            }
            finally
            {
                diagnosticProvider.Dispose();
            }
        }
Esempio n. 2
0
        public HeadlessClient(Legacy legacyApi, IConsole console, HeadlessStartConfig startConfig)
        {
            this.legacyApi             = legacyApi;
            this.console               = console;
            this.startConfig           = startConfig;
            packetRegistry             = PacketDefinitionRegistryFactory.CreateClassicClient(startConfig.ClientVersion);
            diagnosticProvider         = new InfusionDiagnosticPushStreamProvider(LogConfig, console);
            serverDiagnosticPushStream =
                new CompositeDiagnosticPushStream(new ConsoleDiagnosticPushStream(packetLogger, "headless -> server", packetRegistry),
                                                  new InfusionBinaryDiagnosticPushStream(DiagnosticStreamDirection.ClientToServer, diagnosticProvider.GetStream));
            serverDiagnosticPullStream = new ConsoleDiagnosticPullStream(packetLogger, "server -> headless", packetRegistry);

            serverConnection = new ServerConnection(ServerConnectionStatus.Initial, serverDiagnosticPullStream,
                                                    serverDiagnosticPushStream, packetRegistry, startConfig.Encryption);
            serverConnection.PacketReceived += ServerConnectionOnPacketReceived;
            ultimaServer = new UltimaServer(serverPacketHandler, SendToServer, packetRegistry);

            serverPacketHandler = new ServerPacketHandler(packetRegistry);
            clientPacketHandler = new ClientPacketHandler(packetRegistry);
            serverEndpoint      = startConfig.ServerEndPoint;

            ultimaClient = new UltimaClient(clientPacketHandler, SendToClient);

            serverPacketHandler.RegisterFilter(FilterServerPacket);
            serverPacketHandler.Subscribe(PacketDefinitions.GameServerList, HandleGameServerList);
            serverPacketHandler.Subscribe(PacketDefinitions.CharactersStartingLocations, HandleCharactersStartingLocationsPacket);
            serverPacketHandler.Subscribe(PacketDefinitions.EnableLockedClientFeatures, HandleEnableLockedClientFeatures);

            clientPacketHandler.Subscribe(PacketDefinitions.LoginRequest, HandleLoginRequest);
            clientPacketHandler.Subscribe(PacketDefinitions.GameServerLoginRequest, HandleGameServerLoginRequest);
            clientPacketHandler.Subscribe(PacketDefinitions.SelectServerRequest, HandleSelectServerRequest);
        }
Esempio n. 3
0
 public ServerConnection(ServerConnectionStatus status, IDiagnosticPullStream diagnosticPullStream,
                         IDiagnosticPushStream diagnosticPushStream, byte[] newGameKey, uint loginSeed, LoginEncryptionKey loginKey)
     : this(status, diagnosticPullStream, diagnosticPushStream, PacketDefinitionRegistryFactory.CreateClassicClient(), EncryptionSetup.Autodetect)
 {
     InitNewGameEncryption(newGameKey);
     this.loginStream = new LoginPushStream(loginSeed, loginKey);
 }
Esempio n. 4
0
 public UltimaClientConnection(UltimaClientConnectionStatus status, IDiagnosticPullStream diagnosticPullStream,
                               IDiagnosticPushStream diagnosticPushStream)
 {
     this.diagnosticPullStream = diagnosticPullStream;
     this.diagnosticPushStream = diagnosticPushStream;
     Status = status;
 }
Esempio n. 5
0
 public ServerConnection(ServerConnectionStatus status, IDiagnosticPullStream diagnosticPullStream,
                         IDiagnosticPushStream diagnosticPushStream)
 {
     this.Status = status;
     this.diagnosticPullStream = diagnosticPullStream;
     this.diagnosticPushStream = diagnosticPushStream;
     huffmanStream             = new HuffmanStream(receiveNewGameStream);
     preLoginStream            = new PullStreamToStreamAdapter(diagnosticPullStream);
 }
 public UltimaClientConnection(UltimaClientConnectionStatus status, IDiagnosticPullStream diagnosticPullStream,
     IDiagnosticPushStream diagnosticPushStream, PacketDefinitionRegistry packetRegistry,
     EncryptionSetup encryption, LoginEncryptionKey? configuedLoginEncryptionKey)
 {
     this.diagnosticPullStream = diagnosticPullStream;
     this.diagnosticPushStream = diagnosticPushStream;
     this.encryption = encryption;
     this.configuedLoginEncryptionKey = configuedLoginEncryptionKey;
     Status = status;
     packetLogParser = new PacketLogParser(packetRegistry);
     loginStream = new LoginPullStream();
     loginStream.BaseStream = diagnosticPullStream;
     receiveNewGameStream = new ClientNewGamePullStream();
     receiveNewGameStream.BaseStream = diagnosticPullStream;
     sendNewGameStream = new ClientNewGamePushStream();
 }
Esempio n. 7
0
        public ServerConnection(ServerConnectionStatus status, IDiagnosticPullStream diagnosticPullStream,
                                IDiagnosticPushStream diagnosticPushStream, PacketDefinitionRegistry packetRegistry, EncryptionSetup encryptionSetup)
        {
            this.Status = status;
            this.diagnosticPullStream = diagnosticPullStream;
            this.diagnosticPushStream = diagnosticPushStream;
            this.packetRegistry       = packetRegistry;
            this.encryptionSetup      = encryptionSetup;
            this.loginStream          = new LoginPushStream();

            this.receiveNewGameStream = new ServerNewGamePullStream();
            this.sendNewGameStream    = new ServerNewGamePushStream();

            huffmanStream  = new HuffmanStream(new PullStreamToStreamAdapter(receiveNewGameStream));
            preLoginStream = new PullStreamToStreamAdapter(diagnosticPullStream);
        }
Esempio n. 8
0
 public ServerConnection(ServerConnectionStatus status, IDiagnosticPullStream diagnosticPullStream,
                         IDiagnosticPushStream diagnosticPushStream)
     : this(status, diagnosticPullStream, diagnosticPushStream, PacketDefinitionRegistryFactory.CreateClassicClient(), EncryptionSetup.Autodetect)
 {
 }
Esempio n. 9
0
        private void ClientLoop(ILogger packetLogger)
        {
            var diagnosticProvider = new InfusionDiagnosticPushStreamProvider(LogConfig, Console);

            serverDiagnosticPushStream =
                new CompositeDiagnosticPushStream(new ConsoleDiagnosticPushStream(packetLogger, "proxy -> server", packetRegistry),
                                                  new InfusionBinaryDiagnosticPushStream(DiagnosticStreamDirection.ClientToServer, diagnosticProvider.GetStream));
            serverDiagnosticPullStream = new ConsoleDiagnosticPullStream(packetLogger, "server -> proxy", packetRegistry);

            serverConnection = new ServerConnection(ServerConnectionStatus.Initial, serverDiagnosticPullStream,
                                                    serverDiagnosticPushStream, packetRegistry, proxyStartConfig.Encryption);
            serverConnection.PacketReceived += ServerConnectionOnPacketReceived;

            clientConnection = new UltimaClientConnection(UltimaClientConnectionStatus.Initial,
                                                          new ConsoleDiagnosticPullStream(packetLogger, "client -> proxy", packetRegistry),
                                                          new CompositeDiagnosticPushStream(new ConsoleDiagnosticPushStream(packetLogger, "proxy -> client", packetRegistry),
                                                                                            new InfusionBinaryDiagnosticPushStream(DiagnosticStreamDirection.ServerToClient, diagnosticProvider.GetStream)), packetRegistry,
                                                          proxyStartConfig.Encryption, proxyStartConfig.LoginEncryptionKey);
            clientConnection.PacketReceived           += ClientConnectionOnPacketReceived;
            clientConnection.NewGameEncryptionStarted += ClientConnectionOnNewGameEncryptionStarted;
            clientConnection.LoginEncryptionStarted   += ClientConnectionOnLoginEncryptionStarted;

            diagnosticProvider.ClientConnection = clientConnection;
            diagnosticProvider.ServerConnection = serverConnection;
            bool serverLoopStarted = false;

            try
            {
                while (true)
                {
                    var client = listener.AcceptTcpClient();
                    ClientStream = client.GetStream();

                    lock (serverStreamLock)
                    {
                        if (ServerStream == null)
                        {
                            ServerStream = ConnectToServer();
                        }
                    }

                    if (!serverLoopStarted)
                    {
                        Task.Run(() => ServerLoop());
                        serverLoopStarted = true;
                    }

                    int receivedLength;
                    var receiveBuffer = new byte[65535];

                    while ((receivedLength = ClientStream.Read(receiveBuffer, 0, receiveBuffer.Length)) > 0)
                    {
#if DUMP_RAW
                        Console.Info("client -> proxy");
                        Console.Info(receiveBuffer.Take(receivedLength).Select(x => x.ToString("X2")).Aggregate((l, r) => l + " " + r));
#endif

                        var memoryStream = new MemoryStream(receiveBuffer, 0, receivedLength, false);
                        clientConnection.ReceiveBatch(new MemoryStreamToPullStreamAdapter(memoryStream), receivedLength);
                    }

                    lock (serverStreamLock)
                    {
                        DisconnectFromServer();
                        ServerStream = ConnectToServer();
                    }

                    Thread.Yield();
                }
            }
            catch (IOException ioex) when(ioex.InnerException is SocketException socex && socex.SocketErrorCode == SocketError.ConnectionReset)
            {
                Console.Error("Connection to client lost. Please restart infusion.");
                Console.Debug(socex.ToString());
            }
            catch (Exception ex)
            {
                Console.Error("Connection to client lost. Please restart infusion.");
                Console.Debug(serverDiagnosticPullStream.Flush());
                Console.Debug(ex.ToString());
                throw;
            }
            finally
            {
                diagnosticProvider.Dispose();
            }
        }
Esempio n. 10
0
 private ServerConnection CreateEncryptedConnection(ServerConnectionStatus status,
                                                    IDiagnosticPullStream diagnosticPullStream, IDiagnosticPushStream diagnosticPushStream)
 {
     return(new ServerConnection(status, diagnosticPullStream,
                                 diagnosticPushStream, newGameKey: new byte[] { 127, 0, 0, 1 },
                                 loginSeed: 0xA9FE5050, loginKey: new LoginEncryptionKey(0x2cc3ed9d, 0xa374227f, 0)));
 }