public static void Log(ApiLogMessage message) { if (message.Level >= displayLogLevel) { switch (message.Level) { case ApiLogLevel.Error: case ApiLogLevel.Critical: Console.ForegroundColor = ConsoleColor.Red; break; case ApiLogLevel.Warning: Console.ForegroundColor = ConsoleColor.Yellow; break; case ApiLogLevel.Debug: case ApiLogLevel.Info: Console.ForegroundColor = ConsoleColor.White; break; } Console.WriteLine($"{Enum.GetName(typeof(ApiLogLevel), message.Level).ToUpper()}: {message.Message}"); if (logStream != null) { logWriter?.WriteLine($"{Enum.GetName(typeof(ApiLogLevel), message.Level).ToUpper()}: {message.Message}"); logWriter?.Flush(); } Console.ForegroundColor = ConsoleColor.White; } }
private void ReplacePassword(ApiLogMessage msg, Regex regx) { var requestJson = msg.RequestJson; var requestDataStr = msg.RequestDataStr; requestDataStr = HttpUtility.UrlDecode(requestDataStr); requestDataStr = Base64Utils.DecodeBase64String(requestDataStr); requestJson = regx.Replace(requestJson, new MatchEvaluator(Match)); requestDataStr = regx.Replace(requestDataStr, new MatchEvaluator(Match)); requestDataStr = Base64Utils.EncodeBase64String(requestDataStr); requestDataStr = HttpUtility.UrlEncode(requestDataStr); msg.RequestJson = requestJson; msg.RequestDataStr = requestDataStr; }
public BaseResponse Index([FromUri] RequestData model) { var watcher = new Stopwatch(); watcher.Start(); var response = new BaseResponse(); BaseResponse exResponse = null; var requestId = string.Empty; var requestDataJson = string.Empty; var userDataJson = string.Empty; var logMsg = new ApiLogMessage(); var bizCode = string.Empty; var urlEncodedUserData = string.Empty; var urlEncodedRequestData = string.Empty; var parmUserData = string.Empty; var parmRequestData = string.Empty; BaseRequest baseRequest = null; Merchant merchant = null; try { if (model.IsNull() || model.Cmd.IsNullOrWhiteSpace()) { return(BaseResponse.Create(ApiEnum.ResponseCode.处理失败, "无效请求", null, 0)); } bizCode = ProcessorUtil.GetBizCode(model.Cmd); if (bizCode.IsNullOrWhiteSpace()) { return(BaseResponse.Create(ApiEnum.ResponseCode.无效交易类型, "无效交易类型", null, 0)); } baseRequest = ProcessorUtil.GetRequest(bizCode, model.ToJson()); if (baseRequest == null) { return(BaseResponse.Create(ApiEnum.ResponseCode.处理失败, "无效请求", null, 0)); } //验证参数 var errMsg = ""; if (!ModelVerify(baseRequest, out errMsg)) { response = BaseResponse.Create(ApiEnum.ResponseCode.参数不正确, errMsg, null, 0); return(response); } //商户校验 if (!MerchantVerify(baseRequest, out merchant, out errMsg)) { response = BaseResponse.Create(ApiEnum.ResponseCode.处理失败, errMsg, null, 0); return(response); } //验证签名 if (!VerifySign(baseRequest, merchant)) { response = BaseResponse.Create(ApiEnum.ResponseCode.无效调用凭证, "签名不正确", null, 0); return(response); } var processor = this.factory.Create(bizCode); response = processor.Process(baseRequest); } catch (Exception ex) { log.Error(ex); response = BaseResponse.Create(ApiEnum.ResponseCode.系统内部错误, "不好意思,程序开小差,正在重启" + ex.ToString(), 0); exResponse = BaseResponse.Create(ApiEnum.ResponseCode.系统内部错误, ex.ToString(), 0); logMsg.IsError = true; } finally { //WriteRequestInfo(userData, requestData, requestId, bizCode); watcher.Stop(); var duration = watcher.Elapsed.TotalMilliseconds; var logStr = string.Empty; logStr += string.Format("【请求报文】RequestId:{0}", requestId) + Environment.NewLine; logStr += string.Format("UserData:{0}", urlEncodedUserData) + Environment.NewLine; logStr += string.Format("RequestData:{0}", urlEncodedRequestData) + Environment.NewLine; logStr += string.Format("【响应报文】{0}", response.ToJson()); logStr += string.Format("【耗时】{0}毫秒", duration); log.Info(logStr.ToString()); logMsg.UserDataStr = urlEncodedUserData; logMsg.RequestDataStr = urlEncodedRequestData; logMsg.RequestId = requestId; logMsg.LogTime = DateTime.Now; logMsg.RequestJson = requestDataJson; logMsg.Response = exResponse.IsNull() ? response.ToJson() : exResponse.ToJson(); logMsg.Duration = duration; if (AppConfig.LogType == LogType.MQ) { try { this.bus.Publish(logMsg); } catch (Exception ex) { log.Error("写入MQ失败,RequestId:{0}\r\n{1}".Fmt("", ex.ToString())); } } } return(response); }
/// <summary> /// 过滤密码明文 /// </summary> /// <param name="msg"></param> private void FilterPassword(ApiLogMessage msg) { try { switch (msg.Cmd) { case "My.SetLoginPwd": Regex slpRegex = new Regex(@"(?<=""Password""\s*?:\s*?"")(.*?)(?="")", RegexOptions.IgnoreCase); ReplacePassword(msg, slpRegex); break; case "My.UpdateLoginPwd": Regex ulpRegex = new Regex(@"(?<=Password""\s*?:\s*?"")(.*?)(?="")", RegexOptions.IgnoreCase); ReplacePassword(msg, ulpRegex); break; case "My.UpdatePayPwd": Regex uppRegex = new Regex(@"(?<=Password""\s*?:\s*?"")(.*?)(?="")", RegexOptions.IgnoreCase); ReplacePassword(msg, uppRegex); break; case "My.PwdLogin": Regex plRegex = new Regex(@"(?<=""Password""\s*?:\s*?"")(.*?)(?="")", RegexOptions.IgnoreCase); ReplacePassword(msg, plRegex); break; case "My.SetPayPwd": Regex sppRegex = new Regex(@"(?<=""Password""\s*?:\s*?"")(.*?)(?="")", RegexOptions.IgnoreCase); ReplacePassword(msg, sppRegex); break; case "Invest.PayOrder": Regex payOrderRegex = new Regex(@"(?<=""payCode""\s*?:\s*?"")(.*?)(?="")", RegexOptions.IgnoreCase); ReplacePassword(msg, payOrderRegex); break; case "Mall.Pay": Regex payRegex = new Regex(@"(?<=""pwd""\s*?:\s*?"")(.*?)(?="")", RegexOptions.IgnoreCase); ReplacePassword(msg, payRegex); break; case "My.Register": Regex registerRegex = new Regex(@"(?<=""password""\s*?:\s*?"")(.*?)(?="")", RegexOptions.IgnoreCase); ReplacePassword(msg, registerRegex); break; case "My.CheckPayPwd": Regex checkPayPwdRegex = new Regex(@"(?<=""PayPass""\s*?:\s*?"")(.*?)(?="")", RegexOptions.IgnoreCase); ReplacePassword(msg, checkPayPwdRegex); break; case "My.WithdrawRP": Regex withdrawRPRegex = new Regex(@"(?<=""PayPwd""\s*?:\s*?"")(.*?)(?="")", RegexOptions.IgnoreCase); ReplacePassword(msg, withdrawRPRegex); break; case "CreditEx.ConfirmRepay": Regex confirmRepayRegex = new Regex(@"(?<=""pwd""\s*?:\s*?"")(.*?)(?="")", RegexOptions.IgnoreCase); ReplacePassword(msg, confirmRepayRegex); break; case "VehicleInsurance.PayOrder": Regex vpayOrderRegex = new Regex(@"(?<=""payCode""\s*?:\s*?"")(.*?)(?="")", RegexOptions.IgnoreCase); ReplacePassword(msg, vpayOrderRegex); break; } } catch (Exception ex) { log.Error("过滤密码明文失败", ex); } }