From 7b90295a1c03ab0318a56b3a5bfb4cc2e4d4960c Mon Sep 17 00:00:00 2001 From: plutobell <53148519+plutobell@users.noreply.github.com> Date: Fri, 13 Aug 2021 23:16:33 +0800 Subject: [PATCH] v1.3.2 --- CHANGELOG.md | 2 ++ Makefile | 2 +- README.md | 2 +- assets/dashboard.js | 28 +++++++++++++++++--- assets/login.tmpl | 2 +- assets/view.tmpl | 3 ++- device.go | 2 +- main.go | 8 +++--- server.go | 64 +++++++++++++++++++++++++++++++++++++++++---- server_test.go | 8 ++++++ 10 files changed, 104 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41af055..6277dc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ **2021-08-13** +* v1.3.2 : + * Added automatic version detection and prompting * v1.3.1 : * Added csrf protection * Adjusted some details diff --git a/Makefile b/Makefile index 2af964c..3a7d0a7 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ # @Author: github.com/plutobell # @Creation: 2020-08-10 # @Last modification: 2021-08-13 -# @Version: 1.3.1 +# @Version: 1.3.2 PROGRAM = pi-dashboard-go OUTPUT = build diff --git a/README.md b/README.md index 21aef28..49cc15a 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: 1.3.1 +Pi Dashboard Go version: 1.3.2 Project address: https://github.com/plutobell/pi-dashboard-go Usage: Pi Dashboard Go [-auth USR:PSW] [-disk Paths] [-help] diff --git a/assets/dashboard.js b/assets/dashboard.js index 6915ca8..9ecb09d 100644 --- a/assets/dashboard.js +++ b/assets/dashboard.js @@ -3,7 +3,7 @@ // @Author: github.com/plutobell // @Creation: 2020-08-01 // @Last modification: 2021-08-13 -// @Version: 1.3.1 +// @Version: 1.3.2 window.oncontextmenu=function(){return false;} window.onkeydown = window.onkeyup = window.onkeypress = function (event) { @@ -493,7 +493,7 @@ $("#reboot").click(function(){ window.alert("Fail"); $("#loading").show(); unScroll(); - }); + }); }); $("#shutdown").click(function(){ @@ -508,7 +508,7 @@ $("#shutdown").click(function(){ window.alert("Fail"); $("#loading").show(); unScroll(); - }); + }); }); $("#dropcaches").click(function(){ @@ -523,7 +523,27 @@ $("#dropcaches").click(function(){ window.alert("Fail"); // $("#loading").show(); // unScroll(); - }); + }); +}); + +// Check New Version +$(document).ready(function(){ + $.ajaxSetup(csrfAddToAjaxHeader()); + $.post('/api/operation?action=checknewversion', function(data){ + if (data.new_version != "" && data.new_version_url != "") { + $("#new-url").attr("href", data.new_version_url); + $("#new-version").attr("title", "v" + data.new_version); + $("#new-box").show(1000); + } else { + $("#new-box").hide(1000); + $("#new-url").attr("href", "javascript:void(0);"); + $("#new-version").attr("title", "New Version"); + } + }).fail(function() { + $("#new-box").hide(1000); + $("#new-url").attr("href", "javascript:void(0);"); + $("#new-version").attr("title", "New Version"); + }); }); diff --git a/assets/login.tmpl b/assets/login.tmpl index eb25368..fbe66b3 100644 --- a/assets/login.tmpl +++ b/assets/login.tmpl @@ -104,6 +104,6 @@ @Author: github.com/plutobell @Creation: 2020-08-01 @Last modification: 2021-08-13 -@Version: 1.3.1 --> +@Version: 1.3.2 --> \ No newline at end of file diff --git a/assets/view.tmpl b/assets/view.tmpl index e9b1ee1..3144540 100644 --- a/assets/view.tmpl +++ b/assets/view.tmpl @@ -191,6 +191,7 @@
  • Reboot Device
  • Shutdown Device
  • Drop Caches
  • + @@ -392,6 +393,6 @@ @Author: github.com/plutobell @Creation: 2020-08-01 @Last modification: 2021-08-13 -@Version: 1.3.1 --> +@Version: 1.3.2 --> \ No newline at end of file diff --git a/device.go b/device.go index c71456e..3ea981f 100644 --- a/device.go +++ b/device.go @@ -3,7 +3,7 @@ // @Author: github.com/plutobell // @Creation: 2020-08-01 // @Last modification: 2021-08-13 -// @Version: 1.3.1 +// @Version: 1.3.2 package main diff --git a/main.go b/main.go index f988f12..efff5d2 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,7 @@ // @Author: github.com/plutobell // @Creation: 2020-08-01 // @Last modification: 2021-08-13 -// @Version: 1.3.1 +// @Version: 1.3.2 package main @@ -18,10 +18,12 @@ import ( ) const ( + //PROJECT 项目地址 + PROJECT string = "https://github.com/plutobell/pi-dashboard-go" //AUTHOR 作者信息 AUTHOR string = "github:plutobell" //VERSION 版本信息 - VERSION string = "1.3.1" + VERSION string = "1.3.2" //USERNAME 默认用户 USERNAME string = "pi" //PASSWORD 默认密码 @@ -77,7 +79,7 @@ func main() { } if version { fmt.Println("Pi Dashboard Go " + VERSION) - fmt.Println("Project address: https://github.com/plutobell/pi-dashboard-go") + fmt.Println("Project address: " + PROJECT) return } netDevs, err := Popen("cat /proc/net/dev") diff --git a/server.go b/server.go index 98b4589..f1501f6 100644 --- a/server.go +++ b/server.go @@ -3,7 +3,7 @@ // @Author: github.com/plutobell // @Creation: 2020-08-01 // @Last modification: 2021-08-13 -// @Version: 1.3.1 +// @Version: 1.3.2 package main @@ -13,6 +13,7 @@ import ( "fmt" "io" "io/fs" + "io/ioutil" "math/rand" "net/http" "os" @@ -248,17 +249,31 @@ func API(c echo.Context) error { "status": true, } - switch operate := c.QueryParam("action"); { - case operate == "reboot": + switch operation := c.QueryParam("action"); { + case operation == "reboot": go Popen("reboot") return c.JSON(http.StatusOK, status) - case operate == "shutdown": + case operation == "shutdown": go Popen("shutdown -h now") return c.JSON(http.StatusOK, status) - case operate == "dropcaches": + case operation == "dropcaches": go Popen("echo 3 > /proc/sys/vm/drop_caches") return c.JSON(http.StatusOK, status) + case operation == "checknewversion": + nowVersion, _ := getLatestVersionFromGitHub() + result := make(map[string]string) + if nowVersion > VERSION { + result["new_version"] = nowVersion + result["new_version_url"] = PROJECT + "/releases/tag/v" + nowVersion + } else { + result["new_version"] = "" + result["new_version_url"] = "" + } + + return c.JSON(http.StatusOK, result) + } + } status := map[string]string{ @@ -306,3 +321,42 @@ func getRandomString(len int) string { return string(result) } + +func getLatestVersionFromGitHub() (nowVersion string, downloadURL []string) { + url := "https://api.github.com/repos/plutobell/pi-dashboard-go/releases/latest" + + resp, err := http.Get(url) + if err != nil { + panic(err) + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + panic(err) + } + + result := make(map[string]interface{}) + err = json.Unmarshal(body, &result) + if err != nil { + panic(err) + } + + for key, value := range result { + if key == "tag_name" { + nowVersion = value.(string)[1:] + } + if key == "assets" { + assets := value.([]interface{}) + for _, architecture := range assets { + for key, value := range architecture.(map[string]interface{}) { + if key == "browser_download_url" { + downloadURL = append(downloadURL, value.(string)) + } + } + } + } + } + + return nowVersion, downloadURL +} diff --git a/server_test.go b/server_test.go index 1a23adf..821d64d 100644 --- a/server_test.go +++ b/server_test.go @@ -28,3 +28,11 @@ func Test_getRandomString(t *testing.T) { t.Error("Fail") } } + +func Test_getLatestVersionFromGitHub(t *testing.T) { + if nowVersion, downloadURL := getLatestVersionFromGitHub(); nowVersion != "" && len(downloadURL) > 0 { + t.Log("Pass") + } else { + t.Error("Fail") + } +}