diff --git a/src/NSmartProxy.Data/DTOs/UserStatusDTO.cs b/src/NSmartProxy.Data/DTOs/UserStatusDTO.cs new file mode 100644 index 0000000..4034752 --- /dev/null +++ b/src/NSmartProxy.Data/DTOs/UserStatusDTO.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace NSmartProxy.Data.DTOs +{ + public class UserStatusDTO + { + public int offlineUsersCount; + public int onlineUsersCount; + public int banUsersCount; + } +} diff --git a/src/NSmartProxy.Infrastructure/Shared/IDbOperator.cs b/src/NSmartProxy.Infrastructure/Shared/IDbOperator.cs index 5fffdcf..50c2f80 100644 --- a/src/NSmartProxy.Infrastructure/Shared/IDbOperator.cs +++ b/src/NSmartProxy.Infrastructure/Shared/IDbOperator.cs @@ -18,5 +18,6 @@ public interface IDbOperator : IDisposable long GetLength(); void Close(); bool Exist(string key); + int GetCount(); } } \ No newline at end of file diff --git a/src/NSmartProxy/Database/LiteDbOperator.cs b/src/NSmartProxy/Database/LiteDbOperator.cs index 45936c3..aa9883c 100644 --- a/src/NSmartProxy/Database/LiteDbOperator.cs +++ b/src/NSmartProxy/Database/LiteDbOperator.cs @@ -81,6 +81,10 @@ public void UpdateByName(string userName, string newUserName, string value) // keyCache.TryRemove(userName, out _); // liteCollection.Update(new KV(userName, value)); //} + public int GetCount() + { + return liteCollection.Count(); + } public List Select(int startIndex, int length) { diff --git a/src/NSmartProxy/Database/NSmartDb.cs b/src/NSmartProxy/Database/NSmartDb.cs index 4e6a23f..d2b3224 100644 --- a/src/NSmartProxy/Database/NSmartDb.cs +++ b/src/NSmartProxy/Database/NSmartDb.cs @@ -160,6 +160,11 @@ public bool Exist(string key) return hashf.Exist(String2Bytes(key)); } + public int GetCount() + { + throw new NotImplementedException(); + } + public bool Exist(long key) { return hashf.Exist(BitConverter.GetBytes(key)); diff --git a/src/NSmartProxy/Extension/HttpServerStaticFiles/dashboard.js b/src/NSmartProxy/Extension/HttpServerStaticFiles/dashboard.js index 34c3859..31b7292 100644 --- a/src/NSmartProxy/Extension/HttpServerStaticFiles/dashboard.js +++ b/src/NSmartProxy/Extension/HttpServerStaticFiles/dashboard.js @@ -36,12 +36,20 @@ function updatedServerStatus(data) { - + myChart2.data.datasets[0].data[0] = data.totalReceivedBytes; myChart2.data.datasets[0].data[1] = data.totalSentBytes; //myChart.data.datasets[0].data[2] = co; myChart2.update(); - myChart2.lab + } + + function updatedUserStatus(data) { + + myChart3.data.datasets[0].data[0] = data.onlineUsersCount; + myChart3.data.datasets[0].data[1] = data.offlineUsersCount; + myChart3.data.datasets[0].data[2] = data.banUsersCount; + //myChart.data.datasets[0].data[2] = co; + myChart3.update(); } function getClientsInfo() { @@ -58,12 +66,21 @@ $.get(apiUrl, function (res) { var data = res.Data; - //var serverStatus = $.parseJSON(data); updatedServerStatus(data); } ); } + function getUserStatus() { + var apiUrl = basepath + "GetUserStatus"; + $.get(apiUrl, + function (res) { + var data = res.Data; + updatedUserStatus(data); + } + ); + } + //'use strict' // Graphs var ctx1 = document.getElementById('myChart'); @@ -97,6 +114,7 @@ getLogFileTable(10); getClientsInfo(); getServerStatus(); + getUserStatus(); var myChart2 = new Chart(ctx2, { @@ -124,13 +142,13 @@ { type: 'doughnut', data: { - labels: ['活跃用户', '离线用户'/*, 'Yellow', 'Green', 'Purple', 'Orange'*/], + labels: ['活跃用户', '离线用户', '黑名单用户'], datasets: [ { label: '活跃用户', - data: [12, 19], + data: [0, 0], backgroundColor: [ - "rgb(255, 99, 132)", "rgb(54, 162, 235)", "rgb(255, 205, 86)" + "rgb(75, 192, 192)", "rgb(255, 99, 132)", "rgb(201, 203, 207)" ] } ] @@ -143,27 +161,13 @@ } }); - - //$("#myChart").click( - // function (evt) { - // var url = "连接管理"; - // //alert(url); - // } - //); - - //$("#myChart2").click( - // function (evt) { - // var url = " "; - // //alert(url); - // } - //); - - //$("#myChart3").click( - // function (evt) { - // var url = "用户管理"; - // //alert(url); - // } - //); + //red: 'rgb(255, 99, 132)', + // orange: 'rgb(255, 159, 64)', + // yellow: 'rgb(255, 205, 86)', + // green: 'rgb(75, 192, 192)', + // blue: 'rgb(54, 162, 235)', + // purple: 'rgb(153, 102, 255)', + // grey: 'rgb(201, 203, 207)' //定时更新数据 if (window.intevalId) { window.clearInterval(window.intevalId); @@ -173,16 +177,7 @@ getClientsInfo(); getServerStatus(); getLogFileTable(10); - //myChart2.data.datasets.pop(); - //更新数据 - //myChart2.data.datasets[0].data[1] += 3; - //myChart2.data.datasets[1] = 10; - //myChart2.data.datasets.push({ - //label: label, - // backgroundColor: color, - // data: [12, 19] - //}); - //myChart2.update(); + getUserStatus(); }, 5000 ); }()); diff --git a/src/NSmartProxy/Extension/HttpServer_APIs.cs b/src/NSmartProxy/Extension/HttpServer_APIs.cs index f5c38bf..8880e31 100644 --- a/src/NSmartProxy/Extension/HttpServer_APIs.cs +++ b/src/NSmartProxy/Extension/HttpServer_APIs.cs @@ -36,6 +36,25 @@ public ServerStatusDTO GetServerStatus() return dto; } + [Secure] + [API] + public UserStatusDTO GetUserStatus() + { + //Dbop.Close(); + int totalCount = Dbop.GetCount(); + var banCount = ServerContext.ServerConfig.BoundConfig.UsersBanlist.Count; + var onlineCount = ServerContext.Clients.Count(); + var restCount = totalCount - banCount - onlineCount; + + UserStatusDTO dto = new UserStatusDTO + { + onlineUsersCount = onlineCount, + offlineUsersCount = restCount, + banUsersCount = banCount + }; + return dto; + } + #endregion #region log @@ -686,31 +705,12 @@ public string GenerateCA(string hosts) return fileName; } - - //[FileUpload] - //[Secure] - //public string UploadCA(FileInfo fileInfo, int port) - //{ - // string baseLogPath = "./ca"; - // string targetPath = baseLogPath + "/" + port + ".pfx"; - // DirectoryInfo dir = new DirectoryInfo(baseLogPath); - // if (!dir.Exists) - // { - // dir.Create(); - // } - - // File.Move(fileInfo.FullName, targetPath); - - // return "success"; - //} - [FileUpload] [Secure] public string UploadTempFile(FileInfo fileInfo) { string baseLogPath = "./temp"; string targetPath = baseLogPath + "/" + fileInfo.Name; - ///string targetPath = baseLogPath + "/" + port + ".pfx"; DirectoryInfo dir = new DirectoryInfo(baseLogPath); if (!dir.Exists) { diff --git a/src/NSmartProxy/Server.cs b/src/NSmartProxy/Server.cs index 0a03aa1..9077dfd 100644 --- a/src/NSmartProxy/Server.cs +++ b/src/NSmartProxy/Server.cs @@ -354,17 +354,6 @@ private async Task ProcessConsumeRequestAsync(int consumerPort, string clientApp } finally { - //if (consumerClient.Client.Connected) - //{ - // consumerClient.Client.LingerState = new LingerOption(false, 0); - //} - - //if (s2pClient.Client.Connected) - //{ - // s2pClient.Client.LingerState = new LingerOption(false, 0); - //} - - consumerClient.Close(); s2pClient.Close(); transfering.Cancel(); @@ -714,23 +703,21 @@ await fromStream.ReadAsync(buffer, 0, buffer.Length, ct).ConfigureAwait(false)) { if (ioe is IOException) { return; } //Suppress this exception. throw; - //Server.Logger.Info(ioe.Message); } - - } - //Server.Logger.Debug($"{clientApp}对客户端传输关闭。"); } private void CloseClient(TcpClient client) { - Logger.Debug("invalid request,Closing client:" + client.Client.RemoteEndPoint.ToString()); - client.Close(); - Logger.Debug("Closed client:" + client.Client.RemoteEndPoint.ToString()); + if (client.Connected) + { + Logger.Debug("invalid request,Closing client:" + client.Client.RemoteEndPoint.ToString()); + client.Close(); + Logger.Debug("Closed client:" + client.Client.RemoteEndPoint.ToString()); + } } - #endregion #region http