diff --git a/CHANGELOG.md b/CHANGELOG.md index 3aa1a11..b6d5f01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ **2021-08-24** +* v1.4.1 : + * Added operation functions permission detection + * Added a prompt to the console for non-root users to run * v1.4.0 : * Adjusted the front-end page display style * Adjusted static file path structure diff --git a/Makefile b/Makefile index bac1fb7..c6244c6 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ # @Author: github.com/plutobell # @Creation: 2020-08-10 # @Last modification: 2021-08-24 -# @Version: 1.4.0 +# @Version: 1.4.1 PROGRAM = pi-dashboard-go OUTPUT = build diff --git a/README.md b/README.md index 4155679..2b8c385 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ chmod +x pi-dashboard-go **Pi Dashboard Go** can be configured via command line parameters: ```bash -Pi Dashboard Go version: v1.4.0 +Pi Dashboard Go version: v1.4.1 Project address: https://github.com/plutobell/pi-dashboard-go Usage: Pi Dashboard Go [-auth USR:PSW] [-disk Paths] [-help] diff --git a/config/config.go b/config/config.go index 8937b5a..ca23f32 100644 --- a/config/config.go +++ b/config/config.go @@ -3,17 +3,19 @@ // @Author: github.com/plutobell // @Creation: 2020-08-01 // @Last modification: 2021-08-24 -// @Version: 1.4.0 +// @Version: 1.4.1 package config +import "os/user" + const ( //PROJECT 项目地址 PROJECT string = "https://github.com/plutobell/pi-dashboard-go" //AUTHOR 作者信息 AUTHOR string = "github:plutobell" //VERSION 版本信息 - VERSION string = "1.4.0" + VERSION string = "1.4.1" //USERNAME 默认用户 USERNAME string = "pi" //PASSWORD 默认密码 @@ -43,4 +45,6 @@ var ( SessionName string // FileName 当前文件名 FileName string + // LinuxUserInfo 当前Linux用户信息 + LinuxUserInfo *user.User ) diff --git a/device/device.go b/device/device.go index 14cfdbd..3b63167 100644 --- a/device/device.go +++ b/device/device.go @@ -3,7 +3,7 @@ // @Author: github.com/plutobell // @Creation: 2020-08-01 // @Last modification: 2021-08-24 -// @Version: 1.4.0 +// @Version: 1.4.1 package device @@ -76,7 +76,6 @@ func Info() map[string]string { "uptime": "cat /proc/uptime | awk '{ print $1}'", "login_user_count": "who -q | awk 'NR==2{print $2}'", "load_average": "cat /proc/loadavg | awk '{print $1,$2,$3,$4}'", - "current_user": "whoami", "hostname": "cat /etc/hostname", "os": "uname -o", "system": "cat /etc/os-release | grep PRETTY_NAME=", @@ -123,6 +122,8 @@ func Info() map[string]string { device[k] = res } + device["current_user"] = config.LinuxUserInfo.Username + cpuTemperature, _ := strconv.Atoi(device["cpu_temperature"]) device["uptime"] = resolveTime(device["uptime"]) device["hostname"] = strings.Replace(strings.Replace(device["hostname"], " ", "", -1), "\n", "", -1) diff --git a/main.go b/main.go index 95805cf..a8d5088 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,7 @@ // @Author: github.com/plutobell // @Creation: 2020-08-01 // @Last modification: 2021-08-24 -// @Version: 1.4.0 +// @Version: 1.4.1 package main @@ -12,6 +12,7 @@ import ( "fmt" "log" "os" + "os/user" "path/filepath" "strconv" "strings" @@ -36,6 +37,7 @@ func init() { config.SessionName = "logged_in" config.FileName = filepath.Base(os.Args[0]) + config.LinuxUserInfo, _ = user.Current() flag.Usage = usage } diff --git a/server/assets/css/common.css b/server/assets/css/common.css index 4a5a39b..ccdca8c 100644 --- a/server/assets/css/common.css +++ b/server/assets/css/common.css @@ -4,7 +4,7 @@ @Author: github.com/plutobell @Creation: 2020-08-01 @Last modification: 2021-08-24 -@Version: 1.4.0 +@Version: 1.4.1 */ ::-webkit-scrollbar { diff --git a/server/assets/css/index.css b/server/assets/css/index.css index 5b533f0..878ecce 100644 --- a/server/assets/css/index.css +++ b/server/assets/css/index.css @@ -4,7 +4,7 @@ @Author: github.com/plutobell @Creation: 2020-08-01 @Last modification: 2021-08-24 -@Version: 1.4.0 +@Version: 1.4.1 */ #loading{ diff --git a/server/assets/css/login.css b/server/assets/css/login.css index 7a0bae4..55f65a8 100644 --- a/server/assets/css/login.css +++ b/server/assets/css/login.css @@ -4,7 +4,7 @@ @Author: github.com/plutobell @Creation: 2020-08-01 @Last modification: 2021-08-24 -@Version: 1.4.0 +@Version: 1.4.1 */ .box-radius { diff --git a/server/assets/js/common.js b/server/assets/js/common.js index 4676aae..e3a2abf 100644 --- a/server/assets/js/common.js +++ b/server/assets/js/common.js @@ -3,7 +3,7 @@ // @Author: github.com/plutobell // @Creation: 2020-08-01 // @Last modification: 2021-08-24 -// @Version: 1.4.0 +// @Version: 1.4.1 window.oncontextmenu=function(){return false;} window.onkeydown = window.onkeyup = window.onkeypress = function (event) { diff --git a/server/assets/js/index.js b/server/assets/js/index.js index 2d47618..d2c62d8 100644 --- a/server/assets/js/index.js +++ b/server/assets/js/index.js @@ -3,7 +3,7 @@ // @Author: github.com/plutobell // @Creation: 2020-08-01 // @Last modification: 2021-08-24 -// @Version: 1.4.0 +// @Version: 1.4.1 var new_version = "" var new_version_notes = "" @@ -523,18 +523,22 @@ $("#reboot").click(function(){ $.ajaxSetup(csrfAddToAjaxHeader()); $.post('/api/operation?action=reboot', function(data){ if (data.status == true) { - showModalBox("Reboot", "OK") + showModalBox("Reboot Device", "OK") $("#loading").show(); unScroll(); $("#reboot").css("pointer-events", "auto"); + } else if (data.status == "NotRootUser") { + showModalBox("Reboot Device", '"Reboot Device" requires running Pi Dashboard Go as root user') + unScroll(); + $("#reboot").css("pointer-events", "auto"); } else { - showModalBox("Reboot", "Fail"); + showModalBox("Reboot Device", "Unknown Error"); $("#loading").show(); unScroll(); $("#reboot").css("pointer-events", "auto"); } }).fail(function() { - showModalBox("Reboot", "Fail"); + showModalBox("Reboot Device", "Request Error"); $("#loading").show(); unScroll(); $("#reboot").css("pointer-events", "auto"); @@ -546,18 +550,22 @@ $("#shutdown").click(function(){ $.ajaxSetup(csrfAddToAjaxHeader()); $.post('/api/operation?action=shutdown', function(data){ if (data.status == true) { - showModalBox("Shutdown", "OK"); + showModalBox("Shutdown Device", "OK"); $("#loading").show(); unScroll(); $("#shutdown").css("pointer-events", "auto"); + } else if (data.status == "NotRootUser") { + showModalBox("Shutdown Device", '"Shutdown Device" requires running Pi Dashboard Go as root user') + unScroll(); + $("#shutdown").css("pointer-events", "auto"); } else { - showModalBox("Shutdown", "Fail"); + showModalBox("Shutdown Device", "Unknown Error"); $("#loading").show(); unScroll(); $("#shutdown").css("pointer-events", "auto"); } }).fail(function() { - showModalBox("Shutdown", "Fail"); + showModalBox("Shutdown Device", "Request Error"); $("#loading").show(); unScroll(); $("#shutdown").css("pointer-events", "auto"); @@ -573,14 +581,19 @@ $("#dropcaches").click(function(){ // $("#loading").show(); // unScroll(); $("#dropcaches").css("pointer-events", "auto"); + } else if (data.status == "NotRootUser") { + showModalBox("Drop Caches", '"Drop Caches" requires running Pi Dashboard Go as root user') + // $("#loading").show(); + // unScroll(); + $("#dropcaches").css("pointer-events", "auto"); } else { - showModalBox("Drop Caches", "Fail"); + showModalBox("Drop Caches", "Unknown Error"); // $("#loading").show(); // unScroll(); $("#dropcaches").css("pointer-events", "auto"); } }).fail(function() { - showModalBox("Drop Caches", "Fail"); + showModalBox("Drop Caches", "Request Error"); // $("#loading").show(); // unScroll(); $("#dropcaches").css("pointer-events", "auto"); diff --git a/server/assets/js/login.js b/server/assets/js/login.js index 42a7a3a..39c7674 100644 --- a/server/assets/js/login.js +++ b/server/assets/js/login.js @@ -3,7 +3,7 @@ // @Author: github.com/plutobell // @Creation: 2020-08-01 // @Last modification: 2021-08-24 -// @Version: 1.4.0 +// @Version: 1.4.1 $("form").keyup(function(event){ if(event.keyCode == 13){ diff --git a/server/assets/views/index.tmpl b/server/assets/views/index.tmpl index 9b3f6f6..cccf3f4 100644 --- a/server/assets/views/index.tmpl +++ b/server/assets/views/index.tmpl @@ -336,6 +336,6 @@ @Author: github.com/plutobell @Creation: 2020-08-01 @Last modification: 2021-08-24 -@Version: 1.4.0 --> +@Version: 1.4.1 --> \ No newline at end of file diff --git a/server/assets/views/login.tmpl b/server/assets/views/login.tmpl index f927224..81a3826 100644 --- a/server/assets/views/login.tmpl +++ b/server/assets/views/login.tmpl @@ -80,6 +80,6 @@ @Author: github.com/plutobell @Creation: 2020-08-01 @Last modification: 2021-08-24 -@Version: 1.4.0 --> +@Version: 1.4.1 --> \ No newline at end of file diff --git a/server/server.go b/server/server.go index fd7c497..7c41528 100644 --- a/server/server.go +++ b/server/server.go @@ -3,7 +3,7 @@ // @Author: github.com/plutobell // @Creation: 2020-08-01 // @Last modification: 2021-08-24 -// @Version: 1.4.0 +// @Version: 1.4.1 package server @@ -100,6 +100,9 @@ func Run() { // 启动服务 e.HideBanner = true fmt.Println("⇨ Pi Dashboard Go v" + config.VERSION) + if isRootUser() != true { + fmt.Println("⇨ Some functions are unavailable to non-root users") + } e.Logger.Fatal(e.Start(port)) } @@ -231,7 +234,7 @@ func API(c echo.Context) error { if userName != username || isLogin != true { status := map[string]string{ - "result": "Unauthorized", + "status": "Unauthorized", } return c.JSON(http.StatusUnauthorized, status) } @@ -254,26 +257,36 @@ func API(c echo.Context) error { if userName != username || isLogin != true { status := map[string]string{ - "result": "Unauthorized", + "status": "Unauthorized", } return c.JSON(http.StatusUnauthorized, status) } + operation := c.QueryParam("action") + + if operation != "checknewversion" && isRootUser() != true { + status := map[string]string{ + "status": "NotRootUser", + } + + return c.JSON(http.StatusOK, status) + } + status := map[string]bool{ "status": true, } - switch operation := c.QueryParam("action"); { - case operation == "reboot": + switch operation { + case "reboot": go device.Popen("reboot") return c.JSON(http.StatusOK, status) - case operation == "shutdown": + case "shutdown": go device.Popen("shutdown -h now") return c.JSON(http.StatusOK, status) - case operation == "dropcaches": + case "dropcaches": go device.Popen("echo 3 > /proc/sys/vm/drop_caches") return c.JSON(http.StatusOK, status) - case operation == "checknewversion": + case "checknewversion": nowVersion, releaseNotes, _ := getLatestVersionFromGitHub() result := make(map[string]string) if nowVersion > config.VERSION { @@ -383,3 +396,13 @@ func getLatestVersionFromGitHub() ( return nowVersion, releaseNotes, downloadURL } + +func isRootUser() bool { + if config.LinuxUserInfo.Gid == "0" && + config.LinuxUserInfo.Uid == "0" && + config.LinuxUserInfo.Username == "root" { + return true + } + + return false +} diff --git a/server/server_test.go b/server/server_test.go index 94be0a5..6fa3202 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -38,3 +38,11 @@ func Test_getLatestVersionFromGitHub(t *testing.T) { t.Error("Fail") } } + +func Test_isRootUser(t *testing.T) { + if res := isRootUser(); res == true || res == false { + t.Log("Pass") + } else { + t.Error("Fail") + } +}