-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
157 lines (140 loc) · 3.79 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// @Program : Pi Dashboard Go (https://github.com/plutobell/pi-dashboard-go)
// @Description: Golang implementation of pi-dashboard
// @Author: github.com/plutobell
// @Creation: 2020-08-01
// @Last modification: 2023-04-05
// @Version: 1.7.0
package main
import (
"flag"
"fmt"
"log"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
"unicode"
"github.com/plutobell/pi-dashboard-go/config"
"github.com/plutobell/pi-dashboard-go/device"
"github.com/plutobell/pi-dashboard-go/server"
)
func init() {
flag.BoolVar(&config.Help, "help", false, "this help")
flag.BoolVar(&config.Version, "version", false, "show version and exit")
flag.StringVar(&config.Port, "port", "8080", "specify the running port")
flag.StringVar(&config.Title, "title", "Pi Dashboard Go", "specify the website title")
flag.StringVar(&config.Net, "net", "lo", "specify the network device")
flag.StringVar(&config.Disk, "disk", "/", "specify the filesystem path")
flag.StringVar(&config.Auth, "auth", config.USERNAME+":"+config.PASSWORD, "specify username and password")
flag.StringVar(&config.Interval, "interval", "1", "specify the update interval in seconds")
flag.StringVar(&config.SessionMaxAge, "session", "7", "specify the login status validity in days")
flag.StringVar(&config.Theme, "theme", "light", "specify the theme between 'light' and 'dark'")
flag.BoolVar(&config.EnableLogger, "log", false, "enable log display")
config.SessionName = "logged_in"
config.FileName = filepath.Base(os.Args[0])
config.LinuxUserInfo, _ = user.Current()
flag.Usage = usage
}
func main() {
flag.Parse()
if config.Help {
flag.Usage()
return
}
if config.Version {
fmt.Println("Pi Dashboard Go v" + config.VERSION)
fmt.Println("Project address: " + config.PROJECT)
return
}
netDevs, err := device.Popen("cat /proc/net/dev")
if err != nil {
log.Fatal(err)
return
}
if !strings.Contains(netDevs, config.Net+":") {
fmt.Println("Network card does not exist")
return
}
pathExists := false
_, err = os.Stat(config.Disk)
if err == nil {
pathExists = true
}
if os.IsNotExist(err) {
pathExists = false
}
if config.Disk != "/" {
if !pathExists {
fmt.Println("Disk does not exist")
return
}
}
authSlice := strings.Split(config.Auth, ":")
if len(authSlice) != 2 {
fmt.Println("Auth format error")
return
}
if len([]rune(authSlice[0])) > 15 || len([]rune(authSlice[0])) == 0 {
fmt.Println("Username is too long")
return
}
if len([]rune(authSlice[1])) > 15 || len([]rune(authSlice[1])) == 0 {
fmt.Println("Password is too long")
return
}
if len([]rune(config.Title)) > 25 {
fmt.Println("Title is too long")
return
}
isDigit := true
for _, r := range config.Interval {
if !unicode.IsDigit(rune(r)) {
isDigit = false
break
}
}
if !isDigit {
fmt.Println("Interval parameter value is invalid")
return
}
IntervalInt, err := strconv.Atoi(config.Interval)
if err != nil {
log.Fatal(err)
return
}
if IntervalInt > 900 {
fmt.Println("Interval is too long")
return
} else if IntervalInt < 0 {
fmt.Println("Interval should be no less than 0")
return
}
SessionMaxAgeInt, err := strconv.Atoi(config.SessionMaxAge)
if err != nil {
log.Fatal(err)
return
}
if SessionMaxAgeInt > 365 {
fmt.Println("Session days is too long")
return
} else if SessionMaxAgeInt < 0 {
fmt.Println("Session days should be no less than 0")
return
}
if config.Theme != "light" && config.Theme != "dark" {
fmt.Println("Theme name not supported")
return
}
server.Run()
}
func usage() {
fmt.Fprintf(os.Stderr, `Pi Dashboard Go version: v%s
Project address: %s
Usage: %s [-auth USR:PSW] [-disk Paths] [-help]
[-interval Seconds] [-log] [-net NIC] [-port Port]
[-session Days] [-theme Theme] [-title Title] [-version]
Options:
`, config.VERSION, config.PROJECT, config.FileName)
flag.PrintDefaults()
}