From 136239a8388e5b3de549fe4d6bdea2fd80127c17 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Fri, 29 Mar 2019 18:18:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8A=9F=E8=83=BD=EF=BC=9A?= =?UTF-8?q?=E5=9C=B0=E7=90=86=E4=BF=A1=E6=81=AF=E6=94=AF=E6=8C=81=E5=9B=BD?= =?UTF-8?q?=E9=99=85=E5=9F=8E=E5=B8=82=20closed=20#IUU35?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #Issue https://gitee.com/LongbowEnterprise/dashboard/issues?id=IUU35 --- Bootstrap.Admin/BootstrapAdmin.db | Bin 90112 -> 90112 bytes .../OnlineUsers/DefaultOnlineUsers.cs | 105 ++++++++++++-- Bootstrap.Admin/appsettings.Development.json | 2 +- Bootstrap.Admin/appsettings.json | 3 +- Bootstrap.DataAccess/Dict.cs | 11 +- Bootstrap.DataAccess/Helper/DictHelper.cs | 8 +- DatabaseScripts/InitData.sql | 4 +- .../MongoDB/BootstrapAdmin.Dicts.json | 20 ++- DatabaseScripts/MySQL/initData.sql | 4 +- DatabaseScripts/Postgresql/initData.sql | 4 +- DatabaseScripts/SQLite/InitData.sql | 4 +- UnitTest/Bootstrap.DataAccess/DictsTest.cs | 133 +++++++++++++++++- UnitTest/DB/UnitTest.db | Bin 90112 -> 90112 bytes UnitTest/TestHelper.cs | 4 +- 14 files changed, 276 insertions(+), 26 deletions(-) diff --git a/Bootstrap.Admin/BootstrapAdmin.db b/Bootstrap.Admin/BootstrapAdmin.db index 4971ced0a372c2c745c82a3d33c8f0681ac9f80d..578cfcf602adb6845d6472f255eaf184794af18c 100644 GIT binary patch delta 168 zcmZoTz}j$tb%HeG^ocUgjMFzJEIH4t!lf{o{en9am%?U2feW1Vi7M<2s`8EE9M3oJ ze!hGDi*@^+?_K9rnvv=m5L{N2QBqQ1rLUh@kXfvk1r*dv&eP8<03yHCa{KJmO4~G3 zb3+586vHITw8WGY<1~{*!(<~%19PJkgJc6E12eVE0$X`yHU?EyeiR#??V0j?$*$*{ KH_yM7*a`roOG6F- delta 37 tcmZoTz}j$tb%HeGw23m#jMFwIEIH4t%+)-Z{et^uK><^)&CR!JTLB8o4q*TQ diff --git a/Bootstrap.Admin/OnlineUsers/DefaultOnlineUsers.cs b/Bootstrap.Admin/OnlineUsers/DefaultOnlineUsers.cs index bc6c2a21..78beefda 100644 --- a/Bootstrap.Admin/OnlineUsers/DefaultOnlineUsers.cs +++ b/Bootstrap.Admin/OnlineUsers/DefaultOnlineUsers.cs @@ -1,11 +1,11 @@ -using Bootstrap.DataAccess; -using Longbow.Configuration; +using Bootstrap.DataAccess; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Net.Http; +using System.Threading.Tasks; namespace Bootstrap.Admin { @@ -15,6 +15,7 @@ namespace Bootstrap.Admin internal class DefaultOnlineUsers : IOnlineUsers { private ConcurrentDictionary _onlineUsers = new ConcurrentDictionary(); + private ConcurrentDictionary _ipLocator = new ConcurrentDictionary(); private HttpClient _client; private IEnumerable _local = new string[] { "::1", "127.0.0.1" }; /// @@ -59,28 +60,112 @@ namespace Bootstrap.Admin /// public string RetrieveLocaleByIp(string ip = null) { - if (DictHelper.RetrieveLocaleIP() == 0 || ip.IsNullOrEmpty() || _local.Any(p => p == ip)) return "本地连接"; + if (string.IsNullOrEmpty(DictHelper.RetrieveLocaleIPSvr()) || ip.IsNullOrEmpty() || _local.Any(p => p == ip)) return "本地连接"; - var url = ConfigurationManager.AppSettings["IPSvrUrl"]; - var task = _client.GetAsJsonAsync($"{url}{ip}"); - task.Wait(); - return task.Result.status == "0" ? string.Join(" ", task.Result.address.SpanSplit("|").Skip(1).Take(2)) : "XX XX"; + return _ipLocator.GetOrAdd(ip, key => + { + var ipSvr = DictHelper.RetrieveLocaleIPSvr(); + var url = $"{DictHelper.RetrieveLocaleIPSvrUrl(ipSvr)}{ip}"; + var task = ipSvr == "BaiDuIPSvr" ? RetrieveLocator(url) : RetrieveLocator(url); + task.Wait(); + return task.Result; + }); + } + + private async Task RetrieveLocator(string url) + { + var result = await _client.GetAsJsonAsync(url); + return result.ToString(); } /// /// /// - private class IPLocator + private class BaiDuIPLocator { /// /// 详细地址信息 /// - public string address { get; set; } + public string Address { get; set; } /// /// 结果状态返回码 /// - public string status { get; set; } + public string Status { get; set; } + + /// + /// + /// + /// + public override string ToString() + { + return Status == "0" ? string.Join(" ", Address.SpanSplit("|").Skip(1).Take(2)) : "XX XX"; + } + } + + private class JuheIPLocator + { + /// + /// + /// + public string ResultCode { get; set; } + + /// + /// + /// + public string Reason { get; set; } + + /// + /// + /// + public JuheIPLocatorResult Result { get; set; } + + /// + /// + /// + /// + public int Error_Code { get; set; } + + /// + /// + /// + /// + public override string ToString() + { + return Error_Code != 0 ? "XX XX" : Result.ToString(); + } + } + + private class JuheIPLocatorResult + { + /// + /// + /// + public string Country { get; set; } + + /// + /// + /// + public string Province { get; set; } + + /// + /// + /// + public string City { get; set; } + + /// + /// + /// + public string Isp { get; set; } + + /// + /// + /// + /// + public override string ToString() + { + return Country != "中国" ? $"{Country} {Province} {Isp}" : $"{Province} {City} {Isp}"; + } } } } diff --git a/Bootstrap.Admin/appsettings.Development.json b/Bootstrap.Admin/appsettings.Development.json index 1df0bca0..11b48fe9 100644 --- a/Bootstrap.Admin/appsettings.Development.json +++ b/Bootstrap.Admin/appsettings.Development.json @@ -1,4 +1,4 @@ -{ +{ "Logging": { "IncludeScopes": false, "LogLevel": { diff --git a/Bootstrap.Admin/appsettings.json b/Bootstrap.Admin/appsettings.json index f7b986ad..2b044d11 100644 --- a/Bootstrap.Admin/appsettings.json +++ b/Bootstrap.Admin/appsettings.json @@ -1,4 +1,4 @@ -{ +{ "Logging": { "IncludeScopes": false, "LogLevel": { @@ -49,7 +49,6 @@ } } ], - "IPSvrUrl": "http://api.map.baidu.com/location/ip?ak=6lvVPMDlm2gjLpU0aiqPsHXi2OiwGQRj&ip=", "SwaggerPathBase": "/BA", "AllowOrigins": "http://localhost,http://argo.zylweb.cn", "KeyPath": "..\\..\\keys", diff --git a/Bootstrap.DataAccess/Dict.cs b/Bootstrap.DataAccess/Dict.cs index d0e35263..ae20f704 100644 --- a/Bootstrap.DataAccess/Dict.cs +++ b/Bootstrap.DataAccess/Dict.cs @@ -153,10 +153,17 @@ namespace Bootstrap.DataAccess public int RetrieveCookieExpiresPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "系统设置" && d.Name == "Cookie保留时长" && d.Define == 0)?.Code, 7); /// - /// 获得 项目是否获取登录地点 默认为false + /// 获得 IP地理位置 /// /// - public int RetrieveLocaleIP() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "系统设置" && d.Name == "获取IP地点" && d.Define == 0)?.Code, 0); + public string RetrieveLocaleIPSvr() => DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "系统设置" && d.Name == "IP地理位置接口" && d.Define == 0)?.Code; + + /// + /// 获得 项目是否获取登录地点 默认为false + /// + /// 服务提供名称 + /// + public string RetrieveLocaleIPSvrUrl(string ipSvr) => DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "系统设置" && d.Name == ipSvr && d.Define == 0)?.Code; /// /// 获得 访问日志保留时长 默认为1个月 diff --git a/Bootstrap.DataAccess/Helper/DictHelper.cs b/Bootstrap.DataAccess/Helper/DictHelper.cs index 8f692837..d68fabaa 100644 --- a/Bootstrap.DataAccess/Helper/DictHelper.cs +++ b/Bootstrap.DataAccess/Helper/DictHelper.cs @@ -164,7 +164,13 @@ namespace Bootstrap.DataAccess /// /// /// - public static int RetrieveLocaleIP() => DbContextManager.Create().RetrieveLocaleIP(); + public static string RetrieveLocaleIPSvr() => DbContextManager.Create().RetrieveLocaleIPSvr(); + + /// + /// + /// + /// + public static string RetrieveLocaleIPSvrUrl(string ipSvr) => DbContextManager.Create().RetrieveLocaleIPSvrUrl(ipSvr); /// /// 访问日志保留时长 默认一个月 diff --git a/DatabaseScripts/InitData.sql b/DatabaseScripts/InitData.sql index 703ecb3f..cf866f40 100644 --- a/DatabaseScripts/InitData.sql +++ b/DatabaseScripts/InitData.sql @@ -40,7 +40,9 @@ INSERT [dbo].[Dicts] ([Category], [Name], [Code], [Define]) VALUES (N'系统设 -- 时长单位 天 INSERT [dbo].[Dicts] ([Category], [Name], [Code], [Define]) VALUES (N'系统设置', N'Cookie保留时长', '7', 0) -INSERT [dbo].[Dicts] ([Category], [Name], [Code], [Define]) VALUES (N'系统设置', N'获取IP地点', '0', 0) +INSERT [dbo].[Dicts] ([Category], [Name], [Code], [Define]) VALUES (N'系统设置', N'IP地理位置接口', 'JuheIPSvr', 0) +INSERT [dbo].[Dicts] ([Category], [Name], [Code], [Define]) VALUES (N'系统设置', N'BaiDuIPSvr', 'http://api.map.baidu.com/location/ip?ak=6lvVPMDlm2gjLpU0aiqPsHXi2OiwGQRj&ip=', 0) +INSERT [dbo].[Dicts] ([Category], [Name], [Code], [Define]) VALUES (N'系统设置', N'JuheIPSvr', 'http://apis.juhe.cn/ip/ipNew?key=f57102d1b9fadd3f4a1c29072d0c0206&ip=', 0) INSERT [dbo].[Dicts] ([Category], [Name], [Code], [Define]) VALUES (N'系统设置', N'演示系统', '0', 0) DELETE FROM Navigations diff --git a/DatabaseScripts/MongoDB/BootstrapAdmin.Dicts.json b/DatabaseScripts/MongoDB/BootstrapAdmin.Dicts.json index ed575e5e..50d3b39a 100644 --- a/DatabaseScripts/MongoDB/BootstrapAdmin.Dicts.json +++ b/DatabaseScripts/MongoDB/BootstrapAdmin.Dicts.json @@ -310,19 +310,33 @@ { "_id": ObjectId("5bd6c73d5fa31256f77e4a44"), "Category": "系统设置", - "Name": "获取IP地点", - "Code": "0", + "Name": "IP地理位置接口", + "Code": "JuheIPSvr", "Define": NumberInt(0) }, { "_id": ObjectId("5bd6c73d5fa31256f77e4a45"), "Category": "系统设置", + "Name": "BaiDuIPSvr", + "Code": "http://api.map.baidu.com/location/ip?ak=6lvVPMDlm2gjLpU0aiqPsHXi2OiwGQRj&ip=", + "Define": NumberInt(0) + }, + { + "_id": ObjectId("5bd6c73d5fa31256f77e4a46"), + "Category": "系统设置", + "Name": "JuheIPSvr", + "Code": "http://apis.juhe.cn/ip/ipNew?key=f57102d1b9fadd3f4a1c29072d0c0206&ip=", + "Define": NumberInt(0) + }, + { + "_id": ObjectId("5bd6c73d5fa31256f77e4a47"), + "Category": "系统设置", "Name": "访问日志保留时长", "Code": "1", "Define": NumberInt(0) }, { - "_id": ObjectId("5bd6c73d5fa31256f77e4a46"), + "_id": ObjectId("5bd6c73d5fa31256f77e4a48"), "Category": "系统设置", "Name": "演示系统", "Code": "0", diff --git a/DatabaseScripts/MySQL/initData.sql b/DatabaseScripts/MySQL/initData.sql index 25e7c815..7732ede9 100644 --- a/DatabaseScripts/MySQL/initData.sql +++ b/DatabaseScripts/MySQL/initData.sql @@ -37,7 +37,9 @@ INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', '访问 -- 时长单位 天 INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', 'Cookie保留时长', '7', 0); -INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', '获取IP地点', '0', 0); +INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', 'IP地理位置接口', 'JuheIPSvr', 0); +INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', 'BaiDuIPSvr', 'http://api.map.baidu.com/location/ip?ak=6lvVPMDlm2gjLpU0aiqPsHXi2OiwGQRj&ip=', 0); +INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', 'JuheIPSvr', 'http://apis.juhe.cn/ip/ipNew?key=f57102d1b9fadd3f4a1c29072d0c0206&ip=', 0); INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', '演示系统', '0', 0); DELETE FROM Navigations; diff --git a/DatabaseScripts/Postgresql/initData.sql b/DatabaseScripts/Postgresql/initData.sql index 71744a87..9086e3de 100644 --- a/DatabaseScripts/Postgresql/initData.sql +++ b/DatabaseScripts/Postgresql/initData.sql @@ -37,7 +37,9 @@ INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', '访问 -- 时长单位 天 INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', 'Cookie保留时长', '7', 0); -INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', '获取IP地点', '0', 0); +INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', 'IP地理位置接口', 'JuheIPSvr', 0); +INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', 'BaiDuIPSvr', 'http://api.map.baidu.com/location/ip?ak=6lvVPMDlm2gjLpU0aiqPsHXi2OiwGQRj&ip=', 0); +INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', 'JuheIPSvr', 'http://apis.juhe.cn/ip/ipNew?key=f57102d1b9fadd3f4a1c29072d0c0206&ip=', 0); INSERT INTO Dicts (Category, Name, Code, Define) VALUES ('系统设置', '演示系统', '0', 0); DELETE FROM Navigations; diff --git a/DatabaseScripts/SQLite/InitData.sql b/DatabaseScripts/SQLite/InitData.sql index fd9998dd..72c9cf61 100644 --- a/DatabaseScripts/SQLite/InitData.sql +++ b/DatabaseScripts/SQLite/InitData.sql @@ -35,7 +35,9 @@ INSERT INTO [Dicts] ([Category], [Name], [Code], [Define]) VALUES ('系统设置 -- 时长单位 天 INSERT INTO [Dicts] ([Category], [Name], [Code], [Define]) VALUES ('系统设置', 'Cookie保留时长', '7', 0); -INSERT INTO [Dicts] ([Category], [Name], [Code], [Define]) VALUES ('系统设置', '获取IP地点', '0', 0); +INSERT INTO [Dicts] ([Category], [Name], [Code], [Define]) VALUES ('系统设置', 'IP地理位置接口', 'JuheIPSvr', 0); +INSERT INTO [Dicts] ([Category], [Name], [Code], [Define]) VALUES ('系统设置', 'BaiDuIPSvr', 'http://api.map.baidu.com/location/ip?ak=6lvVPMDlm2gjLpU0aiqPsHXi2OiwGQRj&ip=', 0); +INSERT INTO [Dicts] ([Category], [Name], [Code], [Define]) VALUES ('系统设置', 'JuheIPSvr', 'http://apis.juhe.cn/ip/ipNew?key=f57102d1b9fadd3f4a1c29072d0c0206&ip=', 0); INSERT INTO [Dicts] ([Category], [Name], [Code], [Define]) VALUES ('系统设置', '演示系统', '0', 0); DELETE FROM Navigations; diff --git a/UnitTest/Bootstrap.DataAccess/DictsTest.cs b/UnitTest/Bootstrap.DataAccess/DictsTest.cs index b5d82cc6..e287a132 100644 --- a/UnitTest/Bootstrap.DataAccess/DictsTest.cs +++ b/UnitTest/Bootstrap.DataAccess/DictsTest.cs @@ -1,4 +1,6 @@ +using System; using System.Linq; +using System.Net.Http; using Xunit; namespace Bootstrap.DataAccess @@ -129,7 +131,44 @@ namespace Bootstrap.DataAccess public void RetrieveLocaleIP_Ok() { var dict = new Dict(); - Assert.Equal(0, dict.RetrieveLocaleIP()); + var ipSvr = dict.RetrieveLocaleIPSvr(); + Assert.Equal("JuheIPSvr", ipSvr); + + var ipUri = dict.RetrieveLocaleIPSvrUrl(ipSvr); + Assert.NotNull(ipUri); + } + + [Fact] + public async void BaiduIPSvr_Ok() + { + var dict = new Dict(); + var ipUri = dict.RetrieveLocaleIPSvrUrl("BaiDuIPSvr"); + + var client = HttpClientFactory.Create(); + + // 日本东京 + var locator = await client.GetAsJsonAsync($"{ipUri}207.148.111.94"); + Assert.NotEqual("0", locator.Status); + + // 四川成都 + locator = await client.GetAsJsonAsync($"{ipUri}182.148.123.196"); + Assert.Equal("0", locator.Status); + } + + [Fact] + public async void JuheIPSvr_Ok() + { + var dict = new Dict(); + var ipUri = dict.RetrieveLocaleIPSvrUrl("JuheIPSvr"); + + // 日本东京 + var client = HttpClientFactory.Create(); + var locator = await client.GetAsJsonAsync($"{ipUri}207.148.111.94"); + Assert.Equal(0, locator.Error_Code); + + // 四川成都 + locator = await client.GetAsJsonAsync($"{ipUri}182.148.123.196"); + Assert.Equal(0, locator.Error_Code); } [Fact] @@ -138,5 +177,97 @@ namespace Bootstrap.DataAccess var dict = new Dict(); Assert.Equal(1, dict.RetrieveAccessLogPeriod()); } + + #region Private Class For Test + /// + /// + /// + private class BaiDuIPLocator + { + /// + /// 详细地址信息 + /// + public string Address { get; set; } + + /// + /// 结果状态返回码 + /// + public string Status { get; set; } + + /// + /// + /// + /// + public override string ToString() + { + return Status == "0" ? string.Join(" ", Address.SpanSplit("|").Skip(1).Take(2)) : "XX XX"; + } + } + + private class JuheIPLocator + { + /// + /// + /// + public string ResultCode { get; set; } + + /// + /// + /// + public string Reason { get; set; } + + /// + /// + /// + public JuheIPLocatorResult Result { get; set; } + + /// + /// + /// + /// + public int Error_Code { get; set; } + + /// + /// + /// + /// + public override string ToString() + { + return Error_Code != 0 ? "XX XX" : Result.ToString(); + } + } + + private class JuheIPLocatorResult + { + /// + /// + /// + public string Country { get; set; } + + /// + /// + /// + public string Province { get; set; } + + /// + /// + /// + public string City { get; set; } + + /// + /// + /// + public string Isp { get; set; } + + /// + /// + /// + /// + public override string ToString() + { + return Country != "中国" ? $"{Country} {Province} {Isp}" : $"{Province} {City} {Isp}"; + } + } + #endregion } } diff --git a/UnitTest/DB/UnitTest.db b/UnitTest/DB/UnitTest.db index a0df0183814932bb2469f0ccfe9e156ef22590a1..10e6d098f59b2db8d2cec786299c2e0a88a0af37 100644 GIT binary patch delta 302 zcmZoTz}j$tb%Hdb=0q81M$L^0OU^T^akflmzu?ZsrNH@~vt_fOfB|QHrW!kgs$!!m z$Mem*pYNXkV%@&yd)GN7X1bJm1_YNCWt5Z@Sn2C07G&z>CKl)=C1$3S>Lusr>gVJq zCzfR9=jmq_*e7P&n&p&*1^Bw;Z`Ib zsG7>7*i`Qs@N~|G=M&nV?Ck|w{H$;3)BeR?r5UMU+Y?oQj+1W`N6`Y8MYye4FAJze zFB#+-Ao5Esx6e+kv`sTLH#9IxF-)>dOH4^IPBTd~Og6GKFgHpuNH#DsFatV@(ROq5 Ht=d)qrT2I( delta 37 tcmZoTz}j$tb%Hdb+C&*=MzxIzOU^SZb2U$9zu>-EP{5RHbMvj*Rsi&D4S@gv diff --git a/UnitTest/TestHelper.cs b/UnitTest/TestHelper.cs index 086ad459..c6035ed1 100644 --- a/UnitTest/TestHelper.cs +++ b/UnitTest/TestHelper.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; @@ -60,7 +60,7 @@ namespace UnitTest { var dbPath = RetrievePath($"UnitTest{Path.DirectorySeparatorChar}DB{Path.DirectorySeparatorChar}UnitTest.db"); var dbFile = Path.Combine(AppContext.BaseDirectory, "UnitTest.db"); - if (!File.Exists(dbFile)) File.Copy(dbPath, dbFile); + File.Copy(dbPath, dbFile, true); builder.ConfigureAppConfiguration(app => app.AddInMemoryCollection(new KeyValuePair[] { new KeyValuePair("DB:0:Enabled", "false"),