Esempio n. 1
0
        private void ClientConnected(object sender, ClientAcceptEventArgs e)
        {
            var clientConnection = e.AcceptedConnection;
            var serverSocket     = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            serverSocket.Connect(this.txtOtherServer.Text, (int)this.numRealGSPort.Value);
            var socketConnection = SocketConnection.Create(serverSocket);
            var encryptor        = new PipelinedXor32Encryptor(new PipelinedSimpleModulusEncryptor(socketConnection.Output, PipelinedSimpleModulusEncryptor.DefaultClientKey).Writer);
            var decryptor        = new PipelinedSimpleModulusDecryptor(socketConnection.Input, PipelinedSimpleModulusDecryptor.DefaultClientKey)
            {
                AcceptWrongBlockChecksum = true
            };
            var serverConnection = new Connection(socketConnection, decryptor, encryptor);
            var proxy            = new Proxy(clientConnection, serverConnection, this.InvokeByProxy);

            this.InvokeByProxy(new Action(() =>
            {
                this.proxiedConnections.Add(proxy);
                if (this.proxiedConnections.Count == 1)
                {
                    this.connectedClientsListBox.SelectedItem = proxy;
                    this.ConnectionSelected(this, EventArgs.Empty);
                }
            }));
        }
Esempio n. 2
0
        /// <summary>
        /// Defines the entry point of the application.
        /// </summary>
        /// <param name="args">The arguments. The target address can be specified as an argument in the format [IP]:[Port].</param>
        internal static async Task Main(string[] args)
        {
            var address          = args.Length > 0 ? args[0] : "127.0.0.1:55901";
            var socketConnection = await SocketConnection.ConnectAsync(IPEndPoint.Parse(address));

            var encryptor  = new PipelinedXor32Encryptor(new PipelinedSimpleModulusEncryptor(socketConnection.Output, PipelinedSimpleModulusEncryptor.DefaultClientKey).Writer);
            var decryptor  = new PipelinedSimpleModulusDecryptor(socketConnection.Input, PipelinedSimpleModulusDecryptor.DefaultClientKey);
            var connection = new Connection(socketConnection, decryptor, encryptor);

            _ = new TestClient(connection);

            await connection.BeginReceive();

            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
        }
        /// <summary>
        /// Tests the encryption-decryption cycle for the packet from server to client. The specified packet must be the same after the packet has passed this cycle.
        /// Packets from server to client are never encrypted by Xor32, so these encryptor/decryptors are not involved here.
        /// </summary>
        /// <param name="packet">The packet.</param>
        /// <returns>The task.</returns>
        private async Task EncryptDecryptFromServerToClient(byte[] packet)
        {
            // this pipe connects the encryptor with the decryptor. You can imagine this as the server-to-client pipe of a network socket, for example.
            var pipe = new Pipe();

            var encryptor = new PipelinedSimpleModulusEncryptor(pipe.Writer);
            var decryptor = new PipelinedSimpleModulusDecryptor(pipe.Reader, PipelinedSimpleModulusDecryptor.DefaultClientKey);

            encryptor.Writer.Write(packet);
            await encryptor.Writer.FlushAsync().ConfigureAwait(false);

            var readResult = await decryptor.Reader.ReadAsync().ConfigureAwait(false);

            var result = readResult.Buffer.ToArray();

            Assert.That(result, Is.EquivalentTo(packet));
        }