Last active
June 25, 2021 03:56
-
-
Save tocalai/3b6c2a04d3a432a61bff97f8bc513383 to your computer and use it in GitHub Desktop.
Mock class to demonstrate monitoring http request in.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// supposed we have class to monitor request | |
public class RequestMonitor { | |
private readonly Context _context; | |
public RequestMonitor (Context context) { | |
_context = context; // DI the context of Http request | |
} | |
public void OnRequested () { | |
// retrieved http request information | |
var hostName = _context.GetHttpContext ().Request.Host.Value; | |
var hostAddress = _context.GetHttpContext ().Request.HttpContext.Connection.RemoteIpAddress; | |
var keys = _context.GetHttpContext ().Request.Headers.Keys; | |
var host = _context.GetHttpContext ().Request.Headers["Host"]; | |
var userAgent = _context.GetHttpContext ().Request.Headers["User-Agent"]; | |
var realIP = _context.GetHttpContext ().Request.Headers["X-Real-IP"]; | |
var forwardeds = _context.GetHttpContext ().Request.Headers["X-Forwarded-For"]; | |
var connectedInfo = new Dictionary<string, string> () { { "HostAddress", hostAddress.ToString () }, { "Host", host }, { "UserAgent", userAgent }, { "Real-IP", realIP }, { "Forward-For", forwardeds }, | |
}; | |
// simulate logs via console write | |
Console.WriteLine ("----------------------------------------------->"); | |
Console.WriteLine ($"{DateTime.UtcNow:yyyy/MM/dd HH:mm:ss}, [Info]: {JsonConvert.SerializeObject(connectedInfo)}"); | |
Console.WriteLine ("<-----------------------------------------------"); | |
// place further action logic for the request | |
// check if belongs to black list.. | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment