Пример #1
0
        private static string GetTagValue(string text, string tag, int offset)
        {
            string funcName = "GetTagValue";
            int    num      = text.IndexOf("<" + tag, offset);

            if (num > 0)
            {
                int num2 = num + tag.Length + 1;
                num2 = text.IndexOf(">", num);
                if (num2 > 0)
                {
                    num2++;
                    int num3 = text.IndexOf("</" + tag, num2);
                    if (num3 > num2)
                    {
                        return(text.Substring(num2, num3 - num2));
                    }
                    WinRMHelper.TraceDebugTagNotFound(funcName, text, "</" + tag, num2);
                }
                else
                {
                    WinRMHelper.TraceDebugTagNotFound(funcName, text, ">", num);
                }
            }
            else
            {
                WinRMHelper.TraceDebugTagNotFound(funcName, text, "<" + tag, offset);
            }
            return(string.Empty);
        }
        // Token: 0x060006F3 RID: 1779 RVA: 0x00028E60 File Offset: 0x00027060
        protected override void ExposeExceptionToClientResponse(Exception ex)
        {
            if (!WinRMHelper.FriendlyErrorEnabled.Value)
            {
                base.ExposeExceptionToClientResponse(ex);
                return;
            }
            if (ex is WebException)
            {
                WebException ex2 = (WebException)ex;
                if (WinRMHelper.IsPingRequest(ex2))
                {
                    if (ExTraceGlobals.VerboseTracer.IsTraceEnabled(1))
                    {
                        ExTraceGlobals.VerboseTracer.TraceDebug <int>((long)this.GetHashCode(), "[RemotePowerShellProxyRequestHandler::ExposeExceptionToClientResponse]: Context={0}, Ping found.", base.TraceContext);
                    }
                    RequestDetailsLoggerBase <RequestDetailsLogger> .SafeSetLogger(base.Logger, 21, "Ping");

                    base.ClientResponse.Headers["X-RemotePS-Ping"] = "Ping";
                    return;
                }
                if (WinRMHelper.CouldBePingRequest(ex2))
                {
                    if (ExTraceGlobals.VerboseTracer.IsTraceEnabled(1))
                    {
                        ExTraceGlobals.VerboseTracer.TraceDebug <int>((long)this.GetHashCode(), "[RemotePowerShellProxyRequestHandler::ExposeExceptionToClientResponse]: Context={0}, Could be Ping request.", base.TraceContext);
                    }
                    base.ClientResponse.Headers["X-RemotePS-Ping"] = "Possible-Ping";
                    return;
                }
                if (ex2.Status != WebExceptionStatus.ProtocolError)
                {
                    WinRMInfo.SetFailureCategoryInfo(base.ClientResponse.Headers, 3, ex2.Status.ToString());
                }
                if (ex2.Response != null)
                {
                    string text = ex2.Response.Headers["X-BasicAuthToOAuthConversionDiagnostics"];
                    if (!string.IsNullOrWhiteSpace(text))
                    {
                        base.ClientResponse.Headers["X-BasicAuthToOAuthConversionDiagnostics"] = text + " ";
                    }
                }
            }
            if (ex is HttpProxyException && !string.IsNullOrWhiteSpace(ex.Message) && !WinRMHelper.DiagnosticsInfoHasBeenWritten(base.ClientResponse.Headers))
            {
                WinRMInfo.SetFailureCategoryInfo(base.ClientResponse.Headers, 3, ex.GetType().Name);
                string diagnosticsInfo = WinRMHelper.GetDiagnosticsInfo(base.HttpContext);
                if (ExTraceGlobals.VerboseTracer.IsTraceEnabled(1))
                {
                    ExTraceGlobals.VerboseTracer.TraceDebug <int, string>((long)this.GetHashCode(), "[RemotePowerShellProxyRequestHandler::ExposeExceptionToClientResponse]: Context={0}, Write Message {1} to client response.", base.TraceContext, ex.Message);
                }
                WinRMHelper.SetDiagnosticsInfoWrittenFlag(base.ClientResponse.Headers);
                RequestDetailsLoggerBase <RequestDetailsLogger> .SafeAppendGenericInfo(base.Logger, "FriendlyError", "ExposeException");

                base.ClientResponse.Write(diagnosticsInfo + ex.Message);
                return;
            }
            base.ExposeExceptionToClientResponse(ex);
        }
Пример #3
0
 // Token: 0x06000473 RID: 1139 RVA: 0x00018DCC File Offset: 0x00016FCC
 internal static bool CouldBePingRequest(WebException ex)
 {
     if (ex.Status != WebExceptionStatus.ProtocolError)
     {
         return(false);
     }
     if (ex.Response == null)
     {
         return(false);
     }
     if (WinRMHelper.IsPingRequest(ex))
     {
         if (ExTraceGlobals.VerboseTracer.IsTraceEnabled(1))
         {
             ExTraceGlobals.VerboseTracer.TraceDebug <string>(0L, "[WinRMHelper::CouldBePingRequest] ex.Response.Headers[WinRMInfo.PingHeaderKey] = {0}.", ex.Response.Headers["X-RemotePS-Ping"]);
         }
         return(false);
     }
     return(ex.Response is HttpWebResponse && ((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.InternalServerError);
 }
        // Token: 0x06000380 RID: 896 RVA: 0x00013F04 File Offset: 0x00012104
        private bool TryGetUpdatedBufferToSend(ArraySegment <byte> buffer, out byte[] updatedBuffer, out string failureHint)
        {
            HttpContext context = base.RequestContext.HttpContext;

            updatedBuffer = null;
            failureHint   = null;
            if (context.Response == null)
            {
                failureHint = "Response Null";
                return(false);
            }
            int statusCode = context.Response.StatusCode;

            if (statusCode < 400)
            {
                if (ExTraceGlobals.VerboseTracer.IsTraceEnabled(1))
                {
                    ExTraceGlobals.VerboseTracer.TraceDebug <int, int>((long)this.GetHashCode(), "[RpsOutDataResponseStreamProxy::TryGetUpdatedBufferToSend] Context={0}, StatusCode={1}.", base.RequestContext.TraceContext, statusCode);
                }
                return(false);
            }
            if (WinRMHelper.DiagnosticsInfoHasBeenWritten(context.Response.Headers))
            {
                if (ExTraceGlobals.VerboseTracer.IsTraceEnabled(1))
                {
                    ExTraceGlobals.VerboseTracer.TraceDebug <int>((long)this.GetHashCode(), "[RpsOutDataResponseStreamProxy::TryGetUpdatedBufferToSend] Context={0}, diagnostics info has been written.", base.RequestContext.TraceContext);
                }
                failureHint = "BeenWritten";
                return(false);
            }
            if (WinRMHelper.TryInsertDiagnosticsInfo(buffer, () => WinRMHelper.GetDiagnosticsInfo(context), out updatedBuffer, out failureHint, delegate(string winRMFaultMessage)
            {
                RequestDetailsLoggerBase <RequestDetailsLogger> .SafeAppendGenericInfo(this.RequestContext.Logger, "WinRMFaultMessage", winRMFaultMessage);
                WinRMInfo.SetWSManFailureCategory(context.Response.Headers, winRMFaultMessage);
            }))
            {
                WinRMHelper.SetDiagnosticsInfoWrittenFlag(context.Response.Headers);
                return(true);
            }
            return(false);
        }
        private void OnEndRequest(object sender, EventArgs e)
        {
            HttpContext  httpContext = HttpContext.Current;
            HttpResponse response    = httpContext.Response;

            if (response == null)
            {
                return;
            }
            RequestDetailsLogger current = RequestDetailsLoggerBase <RequestDetailsLogger> .GetCurrent(httpContext);

            RequestDetailsLoggerBase <RequestDetailsLogger> .SafeAppendGenericInfo(current, "OnEndRequest.Start.ContentType", response.ContentType);

            if (response.Headers["X-RemotePS-RevisedAction"] != null)
            {
                RequestDetailsLoggerBase <RequestDetailsLogger> .SafeSetLogger(current, HttpProxyMetadata.ProtocolAction, response.Headers["X-RemotePS-RevisedAction"]);
            }
            bool flag = httpContext.Items.Contains("X-HeaderPreSent") && (bool)httpContext.Items["X-HeaderPreSent"];

            if (flag)
            {
                RequestDetailsLoggerBase <RequestDetailsLogger> .SafeAppendGenericInfo(current, "FriendlyError", "Skip-HeaderPreSent");

                return;
            }
            try
            {
                int statusCode = response.StatusCode;
                int num;
                if (WinRMHelper.TryConvertStatusCode(statusCode, out num))
                {
                    ExTraceGlobals.VerboseTracer.TraceDebug <int, int>((long)this.GetHashCode(), "[RpsFriendlyErrorModule::OnEndRequest]: Convert status code from {0} to {1}.", statusCode, num);
                    response.StatusCode = num;
                    RequestDetailsLoggerBase <RequestDetailsLogger> .SafeSetLogger(current, ServiceCommonMetadata.HttpStatus, statusCode);
                }
                if (statusCode >= 400 && !"Ping".Equals(response.Headers["X-RemotePS-Ping"], StringComparison.OrdinalIgnoreCase) && !"Possible-Ping".Equals(response.Headers["X-RemotePS-Ping"], StringComparison.OrdinalIgnoreCase))
                {
                    response.ContentType = "application/soap+xml;charset=UTF-8";
                    if (!WinRMHelper.DiagnosticsInfoHasBeenWritten(response.Headers))
                    {
                        string diagnosticsInfo = WinRMHelper.GetDiagnosticsInfo(httpContext);
                        ExTraceGlobals.VerboseTracer.TraceDebug <int, string>((long)this.GetHashCode(), "[RpsFriendlyErrorModule::OnEndRequest]: Original Status Code: {0}, Append diagnostics info: {1}.", statusCode, diagnosticsInfo);
                        if (statusCode == 401)
                        {
                            response.Output.Write(diagnosticsInfo + HttpProxyStrings.ErrorAccessDenied);
                        }
                        else
                        {
                            response.Output.Write(diagnosticsInfo);
                        }
                        RequestDetailsLoggerBase <RequestDetailsLogger> .SafeAppendGenericInfo(current, "FriendlyError", "HttpModule");
                    }
                }
            }
            catch (Exception ex)
            {
                ExTraceGlobals.VerboseTracer.TraceError <Exception>((long)this.GetHashCode(), "[RpsFriendlyErrorModule::OnEndRequest]: Exception = {0}", ex);
                RequestDetailsLoggerBase <RequestDetailsLogger> .SafeAppendGenericError(current, "RpsFriendlyErrorModule.OnEndRequest", ex.Message);
            }
            finally
            {
                RequestDetailsLoggerBase <RequestDetailsLogger> .SafeAppendGenericInfo(current, "OnEndRequest.End.ContentType", response.ContentType);
            }
        }
Пример #6
0
        internal static bool TryInsertDiagnosticsInfo(ArraySegment <byte> buffer, Func <string> getDiagnosticInfo, out byte[] updatedBuffer, out string failureHint, Action <string> logging = null)
        {
            failureHint   = null;
            updatedBuffer = null;
            if (buffer.Count == 0)
            {
                ExTraceGlobals.VerboseTracer.TraceDebug(0L, "[WinRMHelper::TryInsertDiagnosticsInfo] buffer == null || buffer.Count = 0.");
                failureHint = "buffer Null/Empty";
                return(false);
            }
            string @string = Encoding.UTF8.GetString(buffer.Array, 0, 15);

            if (string.IsNullOrEmpty(@string))
            {
                ExTraceGlobals.VerboseTracer.TraceDebug <string>(0L, "[WinRMHelper::TryInsertDiagnosticsInfo] EnvelopString is null/empty.", @string);
                failureHint = "EnvelopString Null/Empty";
                return(false);
            }
            if ([email protected]("<s:Envelope", StringComparison.OrdinalIgnoreCase))
            {
                ExTraceGlobals.VerboseTracer.TraceDebug <string>(0L, "[WinRMHelper::TryInsertDiagnosticsInfo] EnvelopString = {0}, Not start with <s:Envelop.", @string);
                failureHint = "No s:Envelop";
                return(false);
            }
            string text = Encoding.UTF8.GetString(buffer.Array, 0, buffer.Count);

            if (string.IsNullOrEmpty(text))
            {
                ExTraceGlobals.VerboseTracer.TraceDebug <string>(0L, "[WinRMHelper::TryInsertDiagnosticsInfo] Output is null/empty.", text);
                failureHint = "Output Null/Empty";
                return(false);
            }
            int num = text.IndexOf("<f:Message>");

            if (num < 0)
            {
                ExTraceGlobals.VerboseTracer.TraceDebug <string>(0L, "[WinRMHelper::TryInsertDiagnosticsInfo] Output = {0}, faultMsgIndex < 0.", text);
                failureHint = "No f:Message";
                return(false);
            }
            int length = text.Length;

            ExTraceGlobals.VerboseTracer.TraceDebug <string, int>(0L, "[WinRMHelper::TryInsertDiagnosticsInfo] Output(faultMsgIndex + 100) = {0}, faultMsgIndex = {1}.", text.Substring(num, (num + 100 < length) ? 100 : (length - num)), num);
            string text2 = "f:Message";
            int    num2  = num + "<f:Message>".Length;

            if (text.IndexOf("<f:ProviderFault", num2, "<f:ProviderFault".Length) >= 0)
            {
                text2 = "<f:ProviderFault";
                text2 = text2.TrimStart(new char[]
                {
                    '<'
                });
                num2 = text.IndexOf('>', num2) + 1;
                ExTraceGlobals.VerboseTracer.TraceDebug <string, int>(0L, "[WinRMHelper::TryInsertDiagnosticsInfo] Output(positionToInsert + 100) = {0}, positionToInsert = {1}.", text.Substring(num, (num2 + 100 < length) ? 100 : (length - num2)), num2);
            }
            if (logging != null)
            {
                int num3 = text.IndexOf("<s:Fault");
                if (num3 > 0)
                {
                    int num4 = text.IndexOf("<s:Code", num3);
                    if (num4 > 0)
                    {
                        string tagValue = WinRMHelper.GetTagValue(text, "s:Value", num4);
                        string text3    = string.Empty;
                        int    num5     = text.IndexOf("<s:Subcode", num4);
                        if (num5 > 0)
                        {
                            text3 = WinRMHelper.GetTagValue(text, "s:Value", num5);
                        }
                        else
                        {
                            WinRMHelper.TraceDebugTagNotFound("TryInsertDiagnosticsInfo", text, "<s:Subcode", 0);
                        }
                        string tagValue2 = WinRMHelper.GetTagValue(text, text2, num4);
                        logging(string.Concat(new string[]
                        {
                            tagValue,
                            "/",
                            text3,
                            "/",
                            tagValue2
                        }));
                    }
                    else
                    {
                        WinRMHelper.TraceDebugTagNotFound("TryInsertDiagnosticsInf", text, "<s:Code", 0);
                    }
                }
                else
                {
                    WinRMHelper.TraceDebugTagNotFound("TryInsertDiagnosticsInfo", text, "<s:Fault", 0);
                }
            }
            string value = getDiagnosticInfo();

            text          = text.Insert(num2, value);
            updatedBuffer = Encoding.UTF8.GetBytes(text);
            return(true);
        }