Beispiel #1
0
        public async Task InterceptAsync(HotelEndPoint endpoint)
        {
            _isIntercepting = true;
            int interceptCount = 0;

            while (!IsConnected && _isIntercepting)
            {
                try
                {
                    Local = await HNode.AcceptAsync(ListenPort).ConfigureAwait(false);

                    if (!_isIntercepting)
                    {
                        break;
                    }

                    if (++interceptCount == SocketSkip)
                    {
                        interceptCount = 0;
                        continue;
                    }

                    byte[] buffer = await Local.PeekAsync(6).ConfigureAwait(false);

                    if (!_isIntercepting)
                    {
                        break;
                    }

                    if (buffer.Length == 0)
                    {
                        interceptCount--;
                        continue;
                    }

                    Remote = new HNode();
                    if (BigEndian.ToUInt16(buffer, 4) != 4000)
                    {
                        buffer = await Local.ReceiveAsync(512).ConfigureAwait(false);

                        if (!_isIntercepting)
                        {
                            break;
                        }

                        if (Encoding.UTF8.GetString(buffer) == CROSS_DOMAIN_POLICY_REQUEST)
                        {
                            await Local.SendAsync(Encoding.UTF8.GetBytes(CROSS_DOMAIN_POLICY_RESPONSE)).ConfigureAwait(false);
                        }
                        else
                        {
                            throw new Exception("Expected cross-domain policy request.");
                        }
                        continue;
                    }

                    var args = new ConnectedEventArgs(endpoint);
                    OnConnected(args);

                    endpoint = (args.HotelServer ?? endpoint);
                    if (args.IsFakingPolicyRequest)
                    {
                        using (var tempRemote = await HNode.ConnectNewAsync(endpoint).ConfigureAwait(false))
                        {
                            await tempRemote.SendAsync(Encoding.UTF8.GetBytes(CROSS_DOMAIN_POLICY_REQUEST)).ConfigureAwait(false);
                        }
                    }

                    if (!await Remote.ConnectAsync(endpoint).ConfigureAwait(false))
                    {
                        break;
                    }
                    IsConnected = true;

                    _inSteps  = 0;
                    _outSteps = 0;
                    Task interceptOutgoingTask = InterceptOutgoingAsync();
                    Task interceptIncomingTask = InterceptIncomingAsync();
                }
                finally
                {
                    if (!IsConnected)
                    {
                        Local?.Dispose();
                        Remote?.Dispose();
                    }
                }
            }
            HNode.StopListeners(ListenPort);
            _isIntercepting = false;
        }
Beispiel #2
0
 protected virtual void OnConnected(ConnectedEventArgs e)
 {
     Connected?.Invoke(this, e);
 }