Skip to content

Commit

Permalink
v1.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
plutobell committed Aug 24, 2021
1 parent 63be9df commit 713f311
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 31 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 6 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 默认密码
Expand Down Expand Up @@ -43,4 +45,6 @@ var (
SessionName string
// FileName 当前文件名
FileName string
// LinuxUserInfo 当前Linux用户信息
LinuxUserInfo *user.User
)
5 changes: 3 additions & 2 deletions device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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=",
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -12,6 +12,7 @@ import (
"fmt"
"log"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
Expand All @@ -36,6 +37,7 @@ func init() {

config.SessionName = "logged_in"
config.FileName = filepath.Base(os.Args[0])
config.LinuxUserInfo, _ = user.Current()

flag.Usage = usage
}
Expand Down
2 changes: 1 addition & 1 deletion server/assets/css/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/assets/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion server/assets/css/login.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/assets/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
31 changes: 22 additions & 9 deletions server/assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion server/assets/js/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
2 changes: 1 addition & 1 deletion server/assets/views/index.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->
</body>
</html>
2 changes: 1 addition & 1 deletion server/assets/views/login.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->
</body>
</html>
39 changes: 31 additions & 8 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit 713f311

Please sign in to comment.