zhaolei
2020-11-20 4a2e5b9a21940f11757be37d99f0944e240e908b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using Bootstrap.DataAccess;
using Prow.Cache;
using Prow.Web;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Net;
 
namespace Microsoft.AspNetCore.Http
{
    /// <summary>
    /// HttpContextExtensions 扩展方法
    /// </summary>
    public static class HttpContextExtensions
    {
        /// <summary>
        /// 保存访问日志方法
        /// </summary>
        public static void SaveOnlineUser(this HttpContext context, string url)
        {
            var onlineUserSvr = context.RequestServices.GetRequiredService<IOnlineUsers>();
            var locator = context.RequestServices.GetRequiredService<IIPLocatorProvider>();
            var proxy = new Func<AutoExpireCacheEntry<OnlineUser>, Action?, AutoExpireCacheEntry<OnlineUser>>((c, action) =>
            {
                var v = c.Value;
                v.LastAccessTime = DateTime.Now;
                v.Method = context.Request.Method;
                v.RequestUrl = url;
                v.AddRequestUrl(url);
                action?.Invoke();
                TraceHelper.Save(context, v);
                return c;
            });
            onlineUserSvr.AddOrUpdate(context.Connection.Id ?? "", key =>
            {
                var agent = context.Request.Headers["User-Agent"];
                var userAgent = string.IsNullOrEmpty(agent) ? null : new UserAgent(agent);
                var v = new OnlineUser
                {
                    UserAgent = agent,
                    ConnectionId = key,
                    Ip = context.Connection.RemoteIpAddress.ToIPv4String(),
                    Browser = userAgent == null ? "Unknown" : $"{userAgent.Browser?.Name} {userAgent.Browser?.Version}",
                    OS = userAgent == null ? "Unknown" : $"{userAgent.OS?.Name} {userAgent.OS?.Version}",
                    FirstAccessTime = DateTime.Now,
                    Referer = context.Request.Headers["Referer"]
                };
                v.Location = locator?.Locate(v.Ip) ?? "";
                return proxy(new AutoExpireCacheEntry<OnlineUser>(v, 1000 * 60, __ => onlineUserSvr.TryRemove(key, out _)), null);
            }, (key, v) => proxy(v, () => v.Reset()));
        }
    }
}