diff --git a/NetPanel.Bl/ExecuteBl.cs b/NetPanel.Bl/ExecuteBl.cs
index ae9662f..4ec918a 100644
--- a/NetPanel.Bl/ExecuteBl.cs
+++ b/NetPanel.Bl/ExecuteBl.cs
@@ -11,16 +11,17 @@ namespace NetPanel.Bl
public class ExecuteBl
{
- public static string Exec(string command)
+
+ public static string Exec(string fileName,string command)
{
if (OperatingSystem.IsWindows()) // 检查当前操作系统是否为 Windows
{
- return ExecuteCommandOnWindows(command);
+ return ExecuteCommandOnWindows(fileName, command);
}
else if (OperatingSystem.IsLinux()) // 检查当前操作系统是否为 Linux
{
- return ExecuteCommandOnLinux(command);
+ return ExecuteCommandOnLinux(fileName, command);
}
else
{
@@ -28,20 +29,21 @@ namespace NetPanel.Bl
}
}
+
///
/// win 执行命令
///
///
///
- private static string ExecuteCommandOnWindows(string command)
+ private static string ExecuteCommandOnWindows(string fileName ,string command)
{
using var process = new Process
{
StartInfo = new ProcessStartInfo
{
- FileName = "cmd.exe",
- Arguments = "/C " + command,
+ FileName = fileName,
+ Arguments = command,
RedirectStandardOutput = true,
RedirectStandardError = true,
//StandardErrorEncoding = Encoding.UTF8,
@@ -65,15 +67,15 @@ namespace NetPanel.Bl
/// linux 执行命令
///
///
- private static string ExecuteCommandOnLinux(string command)
+ private static string ExecuteCommandOnLinux(string fileName ,string command)
{
using var process = new Process
{
StartInfo = new ProcessStartInfo
{
- FileName = "/bin/bash",
- Arguments = "-c \"" + command + "\"",
+ FileName = fileName,
+ Arguments = command,
RedirectStandardOutput = true,
RedirectStandardError = true,
//StandardErrorEncoding = Encoding.UTF8,
diff --git a/NetPanel.Help/LogHeler.cs b/NetPanel.Help/LogHeler.cs
new file mode 100644
index 0000000..94763b4
--- /dev/null
+++ b/NetPanel.Help/LogHeler.cs
@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Xml.Linq;
+
+namespace NetPanel.Help
+{
+ public class LogHeler
+ {
+ public static void Set(string name, string str)
+ {
+ string sPath = AppContext.BaseDirectory + "/Log";
+ File.AppendAllText(sPath + "/"+ name + "_" + DateTime.Now.ToString("yyyyMMdd") + ".txt", "[" + DateTime.Now.ToString() + "]:" + str + "\r\n", Encoding.UTF8);
+ }
+
+ public static void SelErrt(string str)
+ {
+ Set("Errt",str);
+ }
+
+ }
+}
diff --git a/NetPanel/Controllers/HomeController.cs b/NetPanel/Controllers/HomeController.cs
index 1820995..14beb28 100644
--- a/NetPanel/Controllers/HomeController.cs
+++ b/NetPanel/Controllers/HomeController.cs
@@ -1,9 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
+using NetPanel.Bl;
using NetPanel.Entity;
+using NetPanel.Help;
using System;
using System.Collections.Generic;
using System.Diagnostics;
+using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
@@ -18,6 +21,83 @@ namespace NetPanel.Controllers
}
+ ///
+ /// 获取概况
+ ///
+ ///
+ public IActionResult GetOverview()
+ {
+ ReturnMsg returnMsg = new ReturnMsg();
+
+ try
+ {
+
+
+
+ #region 内存
+ string output = ExecuteBl.Exec("free", "-m");
+
+ // 解析输出行
+ string[] lines = output.Split('\n');
+ string[] memoryInfo = lines[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
+ // 获取已使用和总内存大小
+ float totalMemory = float.Parse(memoryInfo[1]);
+ float usedMemory = float.Parse(memoryInfo[2]);
+
+ LogHeler.SelErrt("[HomeController-GetOverview]" + output);
+
+ #endregion
+
+ #region 存储
+ output = ExecuteBl.Exec("df", "-h ./");
+ lines = output.Split('\n');
+ memoryInfo = lines[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
+
+ // 转换为GB单位
+ double totalSizeInGB = memoryInfo[1].Replace("G", "").ToDouble();
+ double availableFreeSpaceInGB = memoryInfo[2].Replace("G", "").ToDouble();
+
+ #endregion
+
+ #region Cpu使用率
+ output = ExecuteBl.Exec("/usr/bin/top", "-b -n 1");
+ string cpuLine = output.Split('\n')[2];
+ string[] cpuValues = cpuLine.Split(' ', StringSplitOptions.RemoveEmptyEntries);
+ double cpuUsage = double.Parse(cpuValues[1]);
+
+ #endregion
+
+ var data = new
+ {
+ //cpu
+ Cpu = new { Total = 100, Used = cpuUsage },
+ //内存
+ Ram = new { Total = totalMemory, Used = usedMemory },
+ Size = new { Total = totalSizeInGB, Used = availableFreeSpaceInGB },
+ };
+
+ //var data = new
+ //{
+ // //cpu
+ // Cpu = new { Total = 100, Used = new Random().Next(0, 100) },
+ // //内存
+ // Ram = new { Total = 1000, Used = new Random().Next(0, 1000) },
+ // //内存
+ // Size = new { Total = 2000, Used = new Random().Next(0, 2000) },
+ //};
+
+ returnMsg.Code = 1;
+ returnMsg.Obj = data;
+ }
+ catch (Exception es)
+ {
+ LogHeler.SelErrt("[HomeController-GetOverview]" + es.ToString());
+ }
+ return Ok(returnMsg);
+ }
+
+
+
public IActionResult Index()
{
diff --git a/NetPanel/Controllers/LoginController.cs b/NetPanel/Controllers/LoginController.cs
index 8d3f550..b75189f 100644
--- a/NetPanel/Controllers/LoginController.cs
+++ b/NetPanel/Controllers/LoginController.cs
@@ -57,6 +57,8 @@ namespace NetPanel.Controllers
enReturnMsg.Msg = "用户名,密码错误!";
return Json(enReturnMsg);
}
+ HttpContext.Session.SetString("user", JsonConvert.SerializeObject(userAdmin.UserName));
+
enReturnMsg.Code = 1;
enReturnMsg.Msg = "成功!";
return Json(enReturnMsg);
diff --git a/NetPanel/Filter/ActionFilter.cs b/NetPanel/Filter/ActionFilter.cs
new file mode 100644
index 0000000..93069df
--- /dev/null
+++ b/NetPanel/Filter/ActionFilter.cs
@@ -0,0 +1,54 @@
+using Microsoft.AspNetCore.Mvc.Filters;
+using NetPanel.Help;
+
+namespace NetPanel.Filter
+{
+ public class ActionFilter : IAuthorizationFilter
+ {
+
+ private readonly IConfiguration _configuration;
+
+ public ActionFilter(IConfiguration configuration)
+ {
+ _configuration = configuration;
+ }
+
+ public void OnAuthorization(AuthorizationFilterContext context)
+ {
+ //(1). 方法1
+ // string c= context.ActionDescriptor.RouteValues["area"].ToString();
+
+ // context.ActionDescriptor.RouteValues[“controller”].ToString();
+
+ // context.ActionDescriptor.RouteValues[“action”].ToString();
+
+ // (2). 方法2:
+
+ // context.RouteData.Values[“controller”].ToString();
+
+ // context.RouteData.Values[“action”].ToString();
+
+ // context.HttpContext.Connection.RemoteIpAddress
+
+
+ string isAdmin = _configuration.GetConnectionString("IsAdmin");
+ if (isAdmin == "1")
+ {
+ return;
+ }
+
+ //控制器名称
+ string controllerName = context.ActionDescriptor.RouteValues["controller"].ToString();
+ string action = context.ActionDescriptor.RouteValues["action"].ToString();
+ if (controllerName == "Login" || controllerName == "Api" || (controllerName == "Home" && action == "Error"))
+ {
+ return;
+ }
+ string sUser = context.HttpContext.Session.GetString("user");
+ if (sUser.IsNull())
+ {
+ context.Result = new Microsoft.AspNetCore.Mvc.RedirectResult("/Login/Login");
+ }
+ }
+ }
+}
diff --git a/NetPanel/Program.cs b/NetPanel/Program.cs
index 1ce65ca..a19f3d5 100644
--- a/NetPanel/Program.cs
+++ b/NetPanel/Program.cs
@@ -1,12 +1,21 @@
var builder = WebApplication.CreateBuilder(args);
+var services = builder.Services;
+
// Add services to the container.
-builder.Services.AddControllersWithViews();
-builder.Services.AddControllers().AddJsonOptions(options =>
+services.AddControllersWithViews();
+services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
-
+services.AddSession(options =>
+{
+ options.IdleTimeout = TimeSpan.FromDays(1);
+});
+services.AddControllersWithViews(o =>
+{
+ o.Filters.Add();
+});
var app = builder.Build();
@@ -17,11 +26,11 @@ if (!app.Environment.IsDevelopment())
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
-
+app.UseSession();
+app.UseCookiePolicy();
app.UseRouting();
app.UseAuthorization();
-
app.MapControllerRoute(
name: "default",
pattern: "{controller=Login}/{action=Login}/{id?}");
diff --git a/NetPanel/Views/Home/Index.cshtml b/NetPanel/Views/Home/Index.cshtml
index 33fdfaf..0e900b2 100644
--- a/NetPanel/Views/Home/Index.cshtml
+++ b/NetPanel/Views/Home/Index.cshtml
@@ -19,33 +19,23 @@
@*
-

-
*@
+
+ *@
@Html.Partial("Top")
@Html.Partial("Menu")
-
-
-
-
-
- |
-
- 加载中,请等待... |
-
-
-
-
+
+

+ 加载中,请等待...
+
-
-
@Html.Partial("UiJs")
+