|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Diagnostics; |
| 6 | +using Newtonsoft.Json; |
| 7 | + |
| 8 | +namespace StackifyLib |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Encapsulate settings retrieval mechanism. Currently supports config file and environment variables. |
| 12 | + /// Could be expanded to include other type of configuration servers later. |
| 13 | + /// </summary> |
| 14 | + public class Config |
| 15 | + { |
| 16 | + public static void LoadSettings() |
| 17 | + { |
| 18 | + try |
| 19 | + { |
| 20 | + CaptureErrorPostdata = Get("Stackify.CaptureErrorPostdata", "") |
| 21 | + .Equals("true", StringComparison.CurrentCultureIgnoreCase); |
| 22 | + |
| 23 | + CaptureServerVariables = Get("Stackify.CaptureServerVariables", "") |
| 24 | + .Equals("true", StringComparison.CurrentCultureIgnoreCase); |
| 25 | + CaptureSessionVariables = Get("Stackify.CaptureSessionVariables", "") |
| 26 | + .Equals("true", StringComparison.CurrentCultureIgnoreCase); |
| 27 | + |
| 28 | + CaptureErrorHeaders = Get("Stackify.CaptureErrorHeaders", "") |
| 29 | + .Equals("true", StringComparison.CurrentCultureIgnoreCase); |
| 30 | + |
| 31 | + CaptureErrorCookies = Get("Stackify.CaptureErrorCookies", "") |
| 32 | + .Equals("true", StringComparison.CurrentCultureIgnoreCase); |
| 33 | + |
| 34 | + CaptureErrorHeadersWhitelist = Get("Stackify.CaptureErrorHeadersWhitelist", ""); |
| 35 | + |
| 36 | + if (!string.IsNullOrEmpty(CaptureErrorHeadersWhitelist)) |
| 37 | + { |
| 38 | + ErrorHeaderGoodKeys = CaptureErrorHeadersWhitelist.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList(); |
| 39 | + } |
| 40 | + |
| 41 | + CaptureErrorHeadersBlacklist = Get("Stackify.CaptureErrorHeadersBlacklist", ""); |
| 42 | + if (!string.IsNullOrEmpty(CaptureErrorHeadersBlacklist)) |
| 43 | + { |
| 44 | + ErrorHeaderBadKeys = CaptureErrorHeadersBlacklist.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList(); |
| 45 | + } |
| 46 | + |
| 47 | + CaptureErrorCookiesWhitelist = Get("Stackify.CaptureErrorCookiesWhitelist", ""); |
| 48 | + if (!string.IsNullOrEmpty(CaptureErrorCookiesWhitelist)) |
| 49 | + { |
| 50 | + ErrorCookiesGoodKeys = CaptureErrorCookiesWhitelist.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList(); |
| 51 | + } |
| 52 | + |
| 53 | + CaptureErrorCookiesBlacklist = Get("Stackify.CaptureErrorCookiesBlacklist", ""); |
| 54 | + if (!string.IsNullOrEmpty(CaptureErrorCookiesBlacklist)) |
| 55 | + { |
| 56 | + ErrorCookiesBadKeys = CaptureErrorCookiesBlacklist.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList(); |
| 57 | + } |
| 58 | + |
| 59 | + CaptureErrorSessionWhitelist = Get("Stackify.CaptureErrorSessionWhitelist", ""); |
| 60 | + if (!string.IsNullOrEmpty(CaptureErrorSessionWhitelist)) |
| 61 | + { |
| 62 | + ErrorSessionGoodKeys = CaptureErrorSessionWhitelist.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList(); |
| 63 | + } |
| 64 | + |
| 65 | + } |
| 66 | + catch (Exception ex) |
| 67 | + { |
| 68 | + Debug.WriteLine(ex.ToString()); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public static List<string> ErrorHeaderGoodKeys = new List<string>(); |
| 73 | + public static List<string> ErrorHeaderBadKeys = new List<string>(); |
| 74 | + public static List<string> ErrorCookiesGoodKeys = new List<string>(); |
| 75 | + public static List<string> ErrorCookiesBadKeys = new List<string>(); |
| 76 | + public static List<string> ErrorSessionGoodKeys = new List<string>(); |
| 77 | + |
| 78 | + public static bool CaptureSessionVariables { get; set; } = false; |
| 79 | + public static bool CaptureServerVariables { get; set; } = false; |
| 80 | + public static bool CaptureErrorPostdata { get; set; } = false; |
| 81 | + public static bool CaptureErrorHeaders { get; set; } = true; |
| 82 | + public static bool CaptureErrorCookies { get; set; } = false; |
| 83 | + |
| 84 | + public static string CaptureErrorSessionWhitelist { get; set; } = null; |
| 85 | + |
| 86 | + public static string CaptureErrorHeadersWhitelist { get; set; } = null; |
| 87 | + |
| 88 | + public static string CaptureErrorHeadersBlacklist { get; set; } = "cookie,authorization"; |
| 89 | + |
| 90 | + public static string CaptureErrorCookiesWhitelist { get; set; } = null; |
| 91 | + |
| 92 | + public static string CaptureErrorCookiesBlacklist { get; set; } = ".ASPXAUTH"; |
| 93 | + |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Attempts to fetch a setting value given the key. |
| 97 | + /// .NET configuration file will be used first, if the key is not found, environment variable will be used next. |
| 98 | + /// </summary> |
| 99 | + /// <param name="key">configuration key in config file or environment variable name.</param> |
| 100 | + /// <param name="defaultValue">If nothing is found, return optional defaultValue provided.</param> |
| 101 | + /// <returns>string value for the requested setting key.</returns> |
| 102 | + internal static string Get(string key, string defaultValue = null) |
| 103 | + { |
| 104 | + return defaultValue; |
| 105 | + //string v = null; |
| 106 | + //try |
| 107 | + //{ |
| 108 | + // if (key != null) |
| 109 | + // { |
| 110 | + // v = ConfigurationManager.AppSettings[key]; |
| 111 | + // if (string.IsNullOrEmpty(v)) |
| 112 | + // v = Environment.GetEnvironmentVariable(key); |
| 113 | + // } |
| 114 | + //} |
| 115 | + //finally |
| 116 | + //{ |
| 117 | + // if (v == null) |
| 118 | + // v = defaultValue; |
| 119 | + //} |
| 120 | + //return v; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments