コード例 #1
0
        public Microsoft.ServiceBus.Channels.IConnection Connect(Uri uri, TimeSpan timeout)
        {
            EventTraceActivity eventTraceActivity = new EventTraceActivity();
            TimeoutHelper      timeoutHelper      = new TimeoutHelper(timeout);
            WebStream          webStream          = (new WebStream(uri, "connection", this.useHttpsMode, eventTraceActivity, uri)).Open();

            Microsoft.ServiceBus.Channels.IConnection webStreamConnection = new WebStreamConnection(uri, this.bufferSize, eventTraceActivity, webStream, uri);
            webStreamConnection = SecureSocketUtil.InitiateSecureClientUpgradeIfNeeded(webStreamConnection, null, this.socketSecurityRole, uri.Host, timeoutHelper.RemainingTime());
            SocketMessageHelper socketMessageHelper = new SocketMessageHelper();
            Message             message             = Message.CreateMessage(socketMessageHelper.MessageVersion, "RelayedConnect", new ConnectMessage(uri));

            TrackingIdHeader.TryAddOrUpdate(message.Headers, eventTraceActivity.ActivityId.ToString());
            if (this.tokenProvider != null)
            {
                string        absoluteUri = RelayedHttpUtility.ConvertToHttpUri(uri).AbsoluteUri;
                SecurityToken token       = this.tokenProvider.GetToken(absoluteUri, "Send", false, timeoutHelper.RemainingTime());
                message.Headers.Add(new RelayTokenHeader(token));
            }
            socketMessageHelper.SendMessage(webStreamConnection, message, timeoutHelper.RemainingTime());
            Message message1 = socketMessageHelper.ReceiveMessage(webStreamConnection, timeoutHelper.RemainingTime());

            if (message1.IsFault)
            {
                throw ErrorUtility.ConvertToError(MessageFault.CreateFault(message1, 65536));
            }
            return(webStreamConnection);
        }
コード例 #2
0
ファイル: WebStream.cs プロジェクト: YunLi1988/ServiceBusFake
        public static bool IsSupportingScheme(Uri factoryEndpointUri, out Exception exception)
        {
            bool flag;

            exception = null;
            try
            {
                using (WebStream webStream = new WebStream(factoryEndpointUri, "connection", new EventTraceActivity()))
                {
                    flag = webStream.Connect();
                }
            }
            catch (WebException webException1)
            {
                WebException webException = webException1;
                string       str          = WebStream.CloseResponseInWebException(webException);
                exception = new CommunicationException(string.Concat(webException.Message, str), webException);
                flag      = false;
            }
            catch (Exception exception2)
            {
                Exception exception1 = exception2;
                if (Fx.IsFatal(exception1))
                {
                    throw;
                }
                exception = exception1;
                flag      = false;
            }
            return(flag);
        }
コード例 #3
0
 public ConnectAsyncResult(WebStreamOnewayConnectionInitiator initiator, Uri address, TimeSpan timeout, AsyncCallback callback, object state) : base(timeout, callback, state)
 {
     this.initiator       = initiator;
     this.address         = address;
     this.asyncWaitHandle = new BeginEndAsyncWaitHandle();
     this.activity        = new EventTraceActivity();
     this.webStream       = new WebStream(this.address, this.initiator.webStreamRole, this.initiator.useHttpsMode, this.activity, this.address);
 }
コード例 #4
0
 private static NetworkDetector.ConnectivityStatus CheckHttpsWebStreamConnectivity(Uri baseAddress, out Exception exception)
 {
     NetworkDetector.ConnectivityStatus connectivityStatu = NetworkDetector.ConnectivityStatus.Unavailable;
     exception = null;
     if (WebStream.IsSupportingScheme(ServiceBusUriHelper.CreateServiceUri("https", baseAddress.Authority, "/"), out exception))
     {
         connectivityStatu = NetworkDetector.ConnectivityStatus.Available;
     }
     NetworkDetector.LogResult(baseAddress, "https WebStream", connectivityStatu);
     return(connectivityStatu);
 }
コード例 #5
0
ファイル: WebStream.cs プロジェクト: YunLi1988/ServiceBusFake
        public WebStream Open()
        {
            int       num;
            Exception communicationException = null;
            int       num1 = 5;

            do
            {
                int num2 = num1 - 1;
                try
                {
                    MessagingClientEtwProvider.Provider.WebStreamConnecting(base.Activity, this.factoryEndpointUri.AbsoluteUri, this.sbUri.AbsoluteUri, num2);
                    if (this.Connect())
                    {
                        MessagingClientEtwProvider.Provider.WebStreamConnectCompleted(base.Activity, this.factoryEndpointUri.AbsoluteUri, this.sbUri.AbsoluteUri, num2);
                        return(this);
                    }
                }
                catch (WebException webException1)
                {
                    WebException webException = webException1;
                    if (Fx.IsFatal(webException))
                    {
                        throw;
                    }
                    string str = WebStream.CloseResponseInWebException(webException);
                    communicationException = new CommunicationException(string.Concat(webException.Message, str), webException);
                }
                catch (Exception exception1)
                {
                    Exception exception = exception1;
                    if (Fx.IsFatal(exception))
                    {
                        throw;
                    }
                    communicationException = exception;
                }
                string str1 = (communicationException != null ? communicationException.ToString() : "Failed to connect. No exception thrown.");
                MessagingClientEtwProvider.Provider.WebStreamConnectFailed(base.Activity, this.factoryEndpointUri.AbsoluteUri, this.sbUri.AbsoluteUri, num2, str1);
                this.Reset();
                this.disposed = false;
                num           = num1 - 1;
                num1          = num;
            }while (num > 0);
            this.disposed = true;
            throw Fx.Exception.AsError(new CommunicationException(SRClient.HTTPConnectivityMode, communicationException), null);
        }
コード例 #6
0
            protected override IEnumerator <IteratorAsyncResult <WebStreamRelayedConnectionListener.ConnectAsyncResult> .AsyncStep> GetAsyncSteps()
            {
                Uri uri;

                uri = (!this.relayedConnectionListener.useHttpsMode ? ServiceBusUriHelper.CreateServiceUri(Uri.UriSchemeHttp, this.Request.HttpEndpoint.ToString(), "/") : ServiceBusUriHelper.CreateServiceUri(Uri.UriSchemeHttps, this.Request.HttpsEndpoint.ToString(), "/"));
                WebStream webStream = (new WebStream(uri, "connection", this.relayedConnectionListener.useHttpsMode, this.Activity, this.relayedConnectionListener.uri)).Open();

                this.connection = new WebStreamConnection(uri, this.relayedConnectionListener.bufferSize, this.Activity, webStream, this.relayedConnectionListener.uri);
                WebStreamRelayedConnectionListener.ConnectAsyncResult connectAsyncResult = this;
                IteratorAsyncResult <WebStreamRelayedConnectionListener.ConnectAsyncResult> .BeginCall beginCall = (WebStreamRelayedConnectionListener.ConnectAsyncResult thisRef, TimeSpan t, AsyncCallback c, object s) => SecureSocketUtil.BeginInitiateSecureClientUpgradeIfNeeded(thisRef.connection, null, thisRef.relayedConnectionListener.socketSecurityMode, thisRef.relayedConnectionListener.uri.Host, t, c, s);
                yield return(connectAsyncResult.CallAsync(beginCall, (WebStreamRelayedConnectionListener.ConnectAsyncResult thisRef, IAsyncResult r) => thisRef.connection = SecureSocketUtil.EndInitiateSecureClientUpgradeIfNeeded(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Transfer));

                this.message            = Message.CreateMessage(this.relayedConnectionListener.messageHelper.MessageVersion, "RelayedAccept", new AcceptMessage(this.Request.Id));
                this.message.Headers.To = EndpointAddress.AnonymousUri;
                WebStreamRelayedConnectionListener.ConnectAsyncResult connectAsyncResult1 = this;
                IteratorAsyncResult <WebStreamRelayedConnectionListener.ConnectAsyncResult> .BeginCall beginCall1 = (WebStreamRelayedConnectionListener.ConnectAsyncResult thisRef, TimeSpan t, AsyncCallback c, object s) => thisRef.relayedConnectionListener.messageHelper.BeginSendMessage(thisRef.connection, thisRef.message, t, c, s);
                yield return(connectAsyncResult1.CallAsync(beginCall1, (WebStreamRelayedConnectionListener.ConnectAsyncResult thisRef, IAsyncResult r) => thisRef.relayedConnectionListener.messageHelper.EndSendMessage(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Transfer));

                IOThreadScheduler.ScheduleCallbackNoFlow(new Action <object>(this.relayedConnectionListener.BeginConnectCallback), this.connection);
            }
コード例 #7
0
 public WebStreamConnection(Uri factoryEndpointUri, int asyncReadBufferSize, EventTraceActivity activity, WebStream webStream, Uri sbUri) : base(webStream, asyncReadBufferSize, activity)
 {
     this.factoryEndpointUri = factoryEndpointUri;
     this.sbUri = sbUri;
 }
コード例 #8
0
ファイル: WebStream.cs プロジェクト: YunLi1988/ServiceBusFake
        private PumpStream CreateUpStreamRequest(Uri endpointLocation)
        {
            PumpStream pumpStream;

            try
            {
                this.PreauthorizeUpStreamRequestIfNeeded(endpointLocation, this.proxyAuthMode);
                this.upstreamRequest        = (HttpWebRequest)WebRequest.Create(endpointLocation);
                this.upstreamRequest.Method = "POST";
                this.upstreamRequest.ConnectionGroupName          = this.connectionGroupId;
                this.upstreamRequest.SendChunked                  = true;
                this.upstreamRequest.Timeout                      = this.timeoutForUpDownRequestInMiliSecond;
                this.upstreamRequest.ServicePoint.ConnectionLimit = 2048;
                if (endpointLocation.Scheme == Uri.UriSchemeHttps && this.upstreamRequest.ServicePoint.ProtocolVersion != HttpVersion.Version11)
                {
                    try
                    {
                        typeof(ServicePoint).GetProperty("HttpBehaviour", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(this.upstreamRequest.ServicePoint, (byte)0, null);
                    }
                    catch (Exception exception1)
                    {
                        Exception exception = exception1;
                        if (!Fx.IsFatal(exception))
                        {
                            this.upstreamRequest = null;
                            throw Fx.Exception.AsError(new CommunicationException(SRClient.UpstreamConnection, exception), null);
                        }
                        throw;
                    }
                }
                this.upstreamRequest.ServicePoint.UseNagleAlgorithm = false;
                this.upstreamRequest.ServicePoint.Expect100Continue = false;
                this.upstreamRequest.ContentType = "application/octet-stream";
                this.upstreamRequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
                this.upstreamRequest.AllowWriteStreamBuffering = false;
                this.upstreamRequest.Headers.Add("X-WSCONNECT", this.webStreamRole);
                this.upstreamRequest.Headers.Add("X-PROCESS-AT", "http://schemas.microsoft.com/netservices/2009/05/servicebus/connect/roles/relay");
                if (this.upstreamRequest.Proxy != null)
                {
                    this.ConfigureProxy(this.upstreamRequest, this.proxyAuthMode);
                }
                this.ConfigurePreauthorizationRequest(this.upstreamRequest, this.proxyAuthMode);
                this.upstreamRequest.ServicePoint.UseNagleAlgorithm = false;
                this.upstreamRequest.ServicePoint.Expect100Continue = false;
                this.upstreamRequest.ServicePoint.ConnectionLimit   = 2048;
                this.sessionServicePoint = this.upstreamRequest.ServicePoint;
                Stream requestStream = this.upstreamRequest.GetRequestStream();
                ThrottledPipeStream throttledPipeStream  = new ThrottledPipeStream(25, WebStream.NaglingDelay);
                ThrottledPipeStream throttledPipeStream1 = throttledPipeStream;
                Stream            stream            = requestStream;
                FramingOutputPump framingOutputPump = new FramingOutputPump(new BufferRead(throttledPipeStream1.Read), new BufferWrite(stream.Write), 5000, base.Activity, this.factoryEndpointUri);
                PumpStream        writePumpStream   = new WritePumpStream(throttledPipeStream, requestStream, framingOutputPump)
                {
                    PumpCompletedEvent = new Action(this.OnWriteStreamCompleted)
                };
                writePumpStream.BeginRunPump();
                pumpStream = writePumpStream;
            }
            catch (Exception exception3)
            {
                Exception exception2 = exception3;
                if (!Fx.IsFatal(exception2))
                {
                    string str = WebStream.CloseResponseInWebException(exception2);
                    throw Fx.Exception.AsError(new CommunicationException(string.Concat(SRClient.UpstreamConnection, str), exception2), null);
                }
                throw;
            }
            return(pumpStream);
        }
コード例 #9
0
ファイル: WebStream.cs プロジェクト: YunLi1988/ServiceBusFake
        private void CreateSession(out Uri endpointLocation1, out Uri endpointLocation2)
        {
            WebStream.ProxyAuthMode[] proxyAuthModeArray = new WebStream.ProxyAuthMode[] { WebStream.ProxyAuthMode.None, WebStream.ProxyAuthMode.Kerberos, WebStream.ProxyAuthMode.Ntlm, WebStream.ProxyAuthMode.Other };
            this.proxyAuthMode = WebStream.ProxyAuthMode.None;
            string empty = string.Empty;

            WebStream.ProxyAuthMode[] proxyAuthModeArray1 = proxyAuthModeArray;
            int num = 0;

Label1:
            while (num < (int)proxyAuthModeArray1.Length)
            {
                WebStream.ProxyAuthMode proxyAuthMode = proxyAuthModeArray1[num];
                switch (proxyAuthMode)
                {
                case WebStream.ProxyAuthMode.Kerberos:
                {
                    if (empty.Contains("KERBEROS"))
                    {
                        break;
                    }
                    goto Label0;
                }

                case WebStream.ProxyAuthMode.Ntlm:
                {
                    if (!empty.Contains("NTLM"))
                    {
                        goto Label0;
                    }
                    else
                    {
                        break;
                    }
                }
                }
                try
                {
                    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(this.factoryEndpointUri);
                    httpWebRequest.KeepAlive   = false;
                    httpWebRequest.Method      = "POST";
                    httpWebRequest.ContentType = "text/plain";
                    httpWebRequest.Headers.Add("X-WSCREATE", this.webStreamRole);
                    httpWebRequest.Headers.Add("X-PROCESS-AT", "http://schemas.microsoft.com/netservices/2009/05/servicebus/connect/roles/relay");
                    if (httpWebRequest.Proxy != null)
                    {
                        this.ConfigureProxy(httpWebRequest, proxyAuthMode);
                    }
                    httpWebRequest.ServicePoint.ConnectionLimit = 2048;
                    using (Stream requestStream = httpWebRequest.GetRequestStream())
                    {
                        using (StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.UTF8))
                        {
                            streamWriter.Write(string.Empty);
                            streamWriter.Flush();
                        }
                    }
                    HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();
                    if (response.StatusCode != HttpStatusCode.Created || response.Headers["X-WSENDPT1"] == null || response.Headers["X-WSENDPT2"] == null)
                    {
                        throw Fx.Exception.AsWarning(new CommunicationException(SRClient.FaultyEndpointResponse), null);
                    }
                    if (!Uri.TryCreate(response.Headers["X-WSENDPT1"], UriKind.Absolute, out endpointLocation1))
                    {
                        throw Fx.Exception.AsWarning(new CommunicationException(SRClient.URIEndpoint), null);
                    }
                    if (!Uri.TryCreate(response.Headers["X-WSENDPT2"], UriKind.Absolute, out endpointLocation2))
                    {
                        throw Fx.Exception.AsWarning(new CommunicationException(SRClient.URIEndpoint), null);
                    }
                    response.Close();
                    this.proxyAuthMode = proxyAuthMode;
                }
                catch (WebException webException1)
                {
                    WebException    webException    = webException1;
                    HttpWebResponse httpWebResponse = webException.Response as HttpWebResponse;
                    if (httpWebResponse == null || httpWebResponse.StatusCode != HttpStatusCode.ProxyAuthenticationRequired)
                    {
                        string str = WebStream.CloseResponseInWebException(webException);
                        throw Fx.Exception.AsWarning(new CommunicationException(string.Concat(SRClient.FactoryEndpoint, str), webException), null);
                    }
                    empty = httpWebResponse.Headers["Proxy-Authenticate"].ToUpperInvariant();
                    goto Label0;
                }
                catch (Exception exception1)
                {
                    Exception exception = exception1;
                    if (!Fx.IsFatal(exception))
                    {
                        throw Fx.Exception.AsError(new CommunicationException(SRClient.FactoryEndpoint, exception), null);
                    }
                    throw;
                }
                return;
            }
            ExceptionTrace exceptionTrace   = Fx.Exception;
            CultureInfo    invariantCulture = CultureInfo.InvariantCulture;

            object[] objArray = new object[] { empty };
            throw exceptionTrace.AsError(new CommunicationException(string.Format(invariantCulture, "Failed to authenticate with proxy supporting modes '{0}'.", objArray)), null);
Label0:
            num++;
            goto Label1;
        }
コード例 #10
0
 private static NetworkDetector.ConnectivityStatus CheckHttpWebStreamConnectivity(Uri baseAddress, out Exception exception)
 {
     NetworkDetector.ConnectivityStatus connectivityStatu = NetworkDetector.ConnectivityStatus.Unavailable;
     exception = null;
     if (!RelayEnvironment.GetEnvironmentVariable("RELAYFORCEHTTPS", false) && WebStream.IsSupportingScheme(ServiceBusUriHelper.CreateServiceUri("http", baseAddress.Authority, "/"), out exception))
     {
         connectivityStatu = NetworkDetector.ConnectivityStatus.Available;
     }
     NetworkDetector.LogResult(baseAddress, "Http WebStream", connectivityStatu);
     return(connectivityStatu);
 }