Beispiel #1
0
        public async Task ParseBytes()
        {
            var bytes = new byte[]
            {
                0x01, 0x0b, 0x00, 0x28, 0x21, 0x12, 0xa4, 0x42, 0x3d, 0x29,
                0x0c, 0x81, 0x83, 0x69, 0xa5, 0x48, 0x36, 0xfa, 0x52, 0x6c,
                0x80, 0x22, 0x00, 0x04, 0x4e, 0x6f, 0x6e, 0x65, 0x00, 0x08,
                0x00, 0x14, 0xf9, 0xa4, 0x0f, 0x85, 0x74, 0xa1, 0x45, 0x91,
                0xb9, 0x71, 0x12, 0x09, 0x53, 0x15, 0x61, 0xc6, 0x4e, 0xec,
                0x82, 0x07, 0x80, 0x28, 0x00, 0x04, 0xd1, 0xf8, 0x6c, 0xd8,
            };

            using (var stream = new MemoryStream(bytes))
            {
                var response = await StunMessage.ParseAsync(stream);

                Assert.Equal(
                    new byte[]
                {
                    0x3d, 0x29, 0x0c, 0x81, 0x83, 0x69, 0xa5, 0x48, 0x36, 0xfa,
                    0x52, 0x6c,
                },
                    response.TransactionId);
                Assert.IsType <ConnectionBindSuccessResponse>(response);
            }
        }
Beispiel #2
0
        public async Task ParseBytes()
        {
            var bytes = new byte[]
            {
                0x01, 0x14, 0x00, 0x2c, 0x21, 0x12, 0xa4, 0x42, 0x0c, 0x85,
                0x2c, 0x24, 0x1a, 0x87, 0x02, 0xc9, 0xa8, 0x2c, 0x0b, 0xad,
                0x00, 0x09, 0x00, 0x18, 0x00, 0x00, 0x04, 0x25, 0x49, 0x6e,
                0x76, 0x61, 0x6c, 0x69, 0x64, 0x20, 0x61, 0x6c, 0x6c, 0x6f,
                0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x00, 0x80, 0x22,
                0x00, 0x04, 0x4e, 0x6f, 0x6e, 0x65, 0x80, 0x28, 0x00, 0x04,
                0x88, 0x34, 0xf9, 0x1e,
            };

            using (var stream = new MemoryStream(bytes))
            {
                var response = (RefreshErrorResponse)await StunMessage.ParseAsync(stream);

                Assert.Equal(
                    new byte[]
                {
                    0x0c, 0x85, 0x2c, 0x24, 0x1a, 0x87, 0x02, 0xc9, 0xa8, 0x2c,
                    0x0b, 0xad,
                },
                    response.TransactionId);
                Assert.Equal(437, response.ErrorCode);
            }
        }
        public async Task ParseBytes()
        {
            var bytes = new byte[]
            {
                0x01, 0x08, 0x00, 0x28, 0x21, 0x12, 0xa4, 0x42, 0xf8, 0x57,
                0xe3, 0x50, 0x4c, 0x8f, 0xd3, 0x9d, 0xb8, 0xca, 0x69, 0x83,
                0x80, 0x22, 0x00, 0x04, 0x4e, 0x6f, 0x6e, 0x65, 0x00, 0x08,
                0x00, 0x14, 0xee, 0x56, 0x3a, 0x74, 0x03, 0x16, 0x80, 0xc4,
                0xe5, 0x2f, 0xfa, 0x37, 0x1e, 0x30, 0x42, 0xa6, 0x45, 0x15,
                0x8e, 0x84, 0x80, 0x28, 0x00, 0x04, 0xa0, 0x39, 0xc6, 0x25,
            };

            using (var stream = new MemoryStream(bytes))
            {
                var response = await StunMessage.ParseAsync(stream);

                Assert.Equal(
                    new byte[]
                {
                    0xf8, 0x57, 0xe3, 0x50, 0x4c, 0x8f, 0xd3, 0x9d, 0xb8, 0xca,
                    0x69, 0x83,
                },
                    response.TransactionId);
                Assert.IsType <CreatePermissionSuccessResponse>(response);
            }
        }
        public async Task ParseBytes()
        {
            var bytes = new byte[]
            {
                0x01, 0x04, 0x00, 0x30, 0x21, 0x12, 0xa4, 0x42, 0xaf, 0x9d,
                0x6b, 0xa5, 0x71, 0x17, 0xd7, 0xf2, 0x0a, 0x3a, 0x94, 0x7b,
                0x00, 0x0d, 0x00, 0x04, 0x00, 0x00, 0x03, 0x09, 0x80, 0x22,
                0x00, 0x1a, 0x43, 0x6f, 0x74, 0x75, 0x72, 0x6e, 0x2d, 0x34,
                0x2e, 0x35, 0x2e, 0x30, 0x2e, 0x38, 0x20, 0x27, 0x64, 0x61,
                0x6e, 0x20, 0x45, 0x69, 0x64, 0x65, 0x72, 0x27, 0x74, 0x75,
                0x80, 0x28, 0x00, 0x04, 0xa9, 0x72, 0xb1, 0xa3,
            };

            using (var stream = new MemoryStream(bytes))
            {
                var response =
                    (RefreshSuccessResponse)await StunMessage.ParseAsync(stream);

                Assert.Equal(
                    new byte[]
                {
                    0xaf, 0x9d, 0x6b, 0xa5, 0x71, 0x17, 0xd7, 0xf2, 0x0a, 0x3a,
                    0x94, 0x7b,
                },
                    response.TransactionId);
                Assert.Equal(777, response.Lifetime);
            }
        }
Beispiel #5
0
        public async Task ParseBytes()
        {
            var bytes = new byte[]
            {
                0x01, 0x03, 0x00, 0x48, 0x21, 0x12, 0xa4, 0x42, 0xf5, 0xdb,
                0xe7, 0xc1, 0x2a, 0x74, 0xbe, 0xf9, 0x8b, 0x16, 0x56, 0x3e,
                0x00, 0x16, 0x00, 0x08, 0x00, 0x01, 0xc1, 0x9e, 0x17, 0x53,
                0x9b, 0x96, 0x00, 0x20, 0x00, 0x08, 0x00, 0x01, 0xaa, 0xaa,
                0xf2, 0xa2, 0x25, 0x18, 0x00, 0x0d, 0x00, 0x04, 0x00, 0x00,
                0x03, 0x09, 0x80, 0x22, 0x00, 0x04, 0x4e, 0x6f, 0x6e, 0x65,
                0x00, 0x08, 0x00, 0x14, 0x2b, 0x24, 0xd2, 0x75, 0xb9, 0x91,
                0x34, 0xd6, 0x48, 0x51, 0xc2, 0x15, 0x17, 0xf5, 0x65, 0x90,
                0x98, 0xfa, 0x9f, 0x7d, 0x80, 0x28, 0x00, 0x04, 0x6c, 0xcc,
                0x0c, 0x46,
            };

            using (var stream = new MemoryStream(bytes))
            {
                var response =
                    (AllocateSuccessResponse)await StunMessage.ParseAsync(stream);

                Assert.Equal(
                    new byte[]
                {
                    0xf5, 0xdb, 0xe7, 0xc1, 0x2a, 0x74, 0xbe, 0xf9, 0x8b, 0x16,
                    0x56, 0x3e,
                },
                    response.TransactionId);
                Assert.Equal(
                    new IPEndPoint(IPAddress.Parse("54.65.63.212"), 57484),
                    response.RelayedEndPoint);
            }
        }
Beispiel #6
0
        private async Task ProcessMessage(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested || _control.Connected)
            {
                NetworkStream stream = _control.GetStream();

                try
                {
                    StunMessage message;
                    try
                    {
                        message = await StunMessage.ParseAsync(stream, cancellationToken);

                        _logger.Debug("Stun Message is: {message}", message);
                    }
                    catch (TurnClientException e)
                    {
                        _logger.Error(e, "Failed to parse StunMessage. {e}", e);
                        ClearResponses();
                        break;
                    }

                    if (message is ConnectionAttempt attempt)
                    {
                        await _connectionAttempts.EnqueueAsync(attempt, cancellationToken);
                    }
                    else if (_responses.TryGetValue(
                                 message.TransactionId,
                                 out TaskCompletionSource <StunMessage> tcs))
                    {
                        // tcs may be already canceled.
                        tcs.TrySetResult(message);
                    }
                }
                catch (Exception e)
                {
                    _logger.Error(
                        e,
                        $"An unexpected exception occurred during {nameof(ProcessMessage)}(): {e}"
                        );
                }
            }

            _logger.Debug($"{nameof(ProcessMessage)} is ended. Connected: {_control.Connected}");
        }
        public async Task ParseBytes()
        {
            var bytes = new byte[]
            {
                0x01, 0x01, 0x00, 0x0c, 0x21, 0x12, 0xa4, 0x42, 0xb9, 0x5e,
                0x6c, 0xcd, 0xa3, 0x84, 0x74, 0xfb, 0x43, 0xe9, 0xae, 0xb1,
                0x00, 0x20, 0x00, 0x08, 0x00, 0x01, 0xf2, 0x6f, 0xf2, 0xa2,
                0x25, 0x18,
            };

            using (var stream = new MemoryStream(bytes))
            {
                var response =
                    (BindingSuccessResponse)await StunMessage.ParseAsync(stream);

                Assert.Equal(
                    new IPEndPoint(IPAddress.Parse("211.176.129.90"), 54141),
                    response.MappedAddress);
            }
        }
Beispiel #8
0
        public async Task ParseBytes()
        {
            var bytes = new byte[]
            {
                0x01, 0x13, 0x00, 0x48, 0x21, 0x12, 0xa4, 0x42, 0xee, 0x29,
                0xdd, 0x2d, 0x7a, 0xe9, 0x9c, 0xf4, 0x00, 0x82, 0xf2, 0x5e,
                0x00, 0x09, 0x00, 0x10, 0x00, 0x00, 0x04, 0x01, 0x55, 0x6e,
                0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x69, 0x7a, 0x65, 0x64,
                0x00, 0x15, 0x00, 0x10, 0x37, 0x35, 0x64, 0x34, 0x35, 0x34,
                0x31, 0x39, 0x63, 0x33, 0x39, 0x33, 0x34, 0x33, 0x66, 0x65,
                0x00, 0x14, 0x00, 0x0a, 0x74, 0x77, 0x69, 0x6c, 0x69, 0x6f,
                0x2e, 0x63, 0x6f, 0x6d, 0x03, 0x6d, 0x80, 0x22, 0x00, 0x04,
                0x4e, 0x6f, 0x6e, 0x65, 0x80, 0x28, 0x00, 0x04, 0x6e, 0x1e,
                0x55, 0x49,
            };

            using (var stream = new MemoryStream(bytes))
            {
                var response =
                    (AllocateErrorResponse)await StunMessage.ParseAsync(stream);

                Assert.Equal(
                    new byte[]
                {
                    0xee, 0x29, 0xdd, 0x2d, 0x7a, 0xe9, 0x9c, 0xf4, 0x00, 0x82,
                    0xf2, 0x5e,
                },
                    response.TransactionId);
                Assert.Equal(401, response.ErrorCode);
                Assert.Equal("twilio.com", response.Realm);
                Assert.Equal(
                    new byte[]
                {
                    0x37, 0x35, 0x64, 0x34, 0x35, 0x34,
                    0x31, 0x39, 0x63, 0x33, 0x39, 0x33,
                    0x34, 0x33, 0x66, 0x65,
                },
                    response.Nonce
                    );
            }
        }
Beispiel #9
0
        public async Task ParseBytes()
        {
            var bytes = new byte[]
            {
                0x00, 0x1c, 0x00, 0x24, 0x21, 0x12, 0xa4, 0x42, 0x8e, 0x3b,
                0x82, 0x73, 0xda, 0x6f, 0xc3, 0xa9, 0x1e, 0x88, 0x8f, 0x0b,
                0x00, 0x2a, 0x00, 0x04, 0x21, 0x5a, 0x86, 0x01, 0x00, 0x12,
                0x00, 0x08, 0x00, 0x01, 0x60, 0xc6, 0x17, 0x53, 0x9b, 0x96,
                0x80, 0x22, 0x00, 0x04, 0x4e, 0x6f, 0x6e, 0x65, 0x80, 0x28,
                0x00, 0x04, 0x58, 0x07, 0xfb, 0x8e,
            };

            using (var stream = new MemoryStream(bytes))
            {
                var response =
                    (ConnectionAttempt)await StunMessage.ParseAsync(stream);

                Assert.Equal(
                    new byte[] { 0x21, 0x5a, 0x86, 0x01 },
                    response.ConnectionId);
            }
        }
Beispiel #10
0
        public async Task <NetworkStream> AcceptRelayedStreamAsync(
            CancellationToken cancellationToken = default(CancellationToken))
        {
            while (true)
            {
                ConnectionAttempt attempt =
                    await _connectionAttempts.DequeueAsync(cancellationToken);

                byte[] id            = attempt.ConnectionId;
                var    bindRequest   = new ConnectionBindRequest(id);
                var    relayedClient = new TcpClient(_host, _port);
                _relayedClients.Add(relayedClient);
                NetworkStream relayedStream = relayedClient.GetStream();

                try
                {
                    await SendMessageAsync(relayedStream, bindRequest, cancellationToken);

                    StunMessage bindResponse =
                        await StunMessage.ParseAsync(relayedStream, cancellationToken);

                    if (bindResponse is ConnectionBindSuccessResponse)
                    {
                        return(relayedStream);
                    }

                    throw new TurnClientException("ConnectionBind failed.", bindResponse);
                }
                catch (IOException e)
                {
                    _logger.Warning(
                        e,
                        "The connection seems to disconnect before parsing; ignored."
                        );
                }
            }
        }
Beispiel #11
0
        public async Task <bool> IsConnectable(
            CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                using var client = new TcpClient();
#pragma warning disable PC001 // API not supported on all platforms
                client.Connect(_host, _port);
#pragma warning restore PC001 // API not supported on all platforms
                NetworkStream stream = client.GetStream();

                var request = new BindingRequest();
                var asBytes = request.Encode(this);
                await stream.WriteAsync(asBytes, 0, asBytes.Length, cancellationToken);

                await StunMessage.ParseAsync(stream, cancellationToken);

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }