🔖 dashboard v0.10.0

This commit is contained in:
naiba
2021-09-27 21:18:09 +08:00
parent ac3cfa162c
commit 4249fa82d7
11 changed files with 81 additions and 69 deletions

View File

@@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
@@ -29,15 +28,22 @@ import (
"github.com/naiba/nezha/service/rpc"
)
type AgentConfig struct {
SkipConnectionCount bool
DisableAutoUpdate bool
Debug bool
Server string
ClientSecret string
}
func init() {
http.DefaultClient.Timeout = time.Second * 5
flag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true
}
var (
server, clientSecret, version string
debug bool
stateConf monitor.GetStateConfig
version string
agentConf AgentConfig
)
var (
@@ -61,13 +67,14 @@ func main() {
// 来自于 GoReleaser 的版本号
monitor.Version = version
flag.BoolVarP(&debug, "debug", "d", false, "开启调试信息")
flag.StringVarP(&server, "*server", "s", "localhost:5555", "管理面板RPC端口")
flag.StringVarP(&clientSecret, "password", "p", "", "Agent连接Secret")
flag.BoolVar(&stateConf.SkipConnectionCount, "skip-conn", false, "不监控连接数")
flag.BoolVarP(&agentConf.Debug, "debug", "d", false, "开启调试信息")
flag.StringVarP(&agentConf.Server, "*server", "s", "localhost:5555", "管理面板RPC端口")
flag.StringVarP(&agentConf.ClientSecret, "password", "p", "", "Agent连接Secret")
flag.BoolVar(&agentConf.SkipConnectionCount, "skip-conn", false, "不监控连接数")
flag.BoolVar(&agentConf.DisableAutoUpdate, "disable-auto-update", false, "禁用自动升级")
flag.Parse()
if clientSecret == "" {
if agentConf.ClientSecret == "" {
flag.Usage()
return
}
@@ -77,7 +84,7 @@ func main() {
func run() {
auth := rpc.AuthHandler{
ClientSecret: clientSecret,
ClientSecret: agentConf.ClientSecret,
}
go pty.DownloadDependency()
@@ -87,7 +94,7 @@ func run() {
// 更新IP信息
go monitor.UpdateIP()
if _, err := semver.Parse(version); err == nil {
if _, err := semver.Parse(version); err == nil && !agentConf.DisableAutoUpdate {
go func() {
for range updateCh {
go doSelfUpdate()
@@ -111,7 +118,7 @@ func run() {
for {
timeOutCtx, cancel := context.WithTimeout(context.Background(), networkTimeOut)
conn, err = grpc.DialContext(timeOutCtx, server, grpc.WithInsecure(), grpc.WithPerRPCCredentials(&auth))
conn, err = grpc.DialContext(timeOutCtx, agentConf.Server, grpc.WithInsecure(), grpc.WithPerRPCCredentials(&auth))
if err != nil {
println("与面板建立连接失败:", err)
cancel()
@@ -194,7 +201,7 @@ func reportState() {
if client != nil && inited {
monitor.TrackNetworkSpeed()
timeOutCtx, cancel := context.WithTimeout(context.Background(), networkTimeOut)
_, err = client.ReportSystemState(timeOutCtx, monitor.GetState(stateConf).PB())
_, err = client.ReportSystemState(timeOutCtx, monitor.GetState(agentConf.SkipConnectionCount).PB())
cancel()
if err != nil {
println("reportState error", err)
@@ -334,7 +341,7 @@ func handleTerminalTask(task *pb.Task) {
protocol += "s"
}
header := http.Header{}
header.Add("Secret", clientSecret)
header.Add("Secret", agentConf.ClientSecret)
conn, _, err := websocket.DefaultDialer.Dial(fmt.Sprintf("%s://%s/terminal/%s", protocol, terminal.Host, terminal.Session), header)
if err != nil {
println("Terminal 连接失败:", err)
@@ -404,7 +411,8 @@ func handleTerminalTask(task *pb.Task) {
}
func println(v ...interface{}) {
if debug {
log.Println(v...)
if agentConf.Debug {
fmt.Printf("NEZHA@%s>> ", time.Now().Format("2006-01-02 15:04:05"))
fmt.Println(v...)
}
}

View File

@@ -74,11 +74,7 @@ func GetHost() *model.Host {
}
}
type GetStateConfig struct {
SkipConnectionCount bool
}
func GetState(conf GetStateConfig) *model.HostState {
func GetState(skipConnectionCount bool) *model.HostState {
hi, _ := host.Info()
mv, _ := mem.VirtualMemory()
@@ -101,7 +97,7 @@ func GetState(conf GetStateConfig) *model.HostState {
var tcpConnCount, udpConnCount uint64
if !conf.SkipConnectionCount {
if !skipConnectionCount {
conns, _ := net.Connections("all")
for i := 0; i < len(conns); i++ {
switch conns[i].Type {

View File

@@ -271,8 +271,8 @@ func (cp *commonPage) terminal(c *gin.Context) {
}
defer conn.Close()
log.Println("terminal connected", isAgent, c.Request.URL)
defer log.Println("terminal disconnected", isAgent, c.Request.URL)
log.Println("NEZHA>> terminal connected", isAgent, c.Request.URL)
defer log.Println("NEZHA>> terminal disconnected", isAgent, c.Request.URL)
if isAgent {
terminal.agentConn = conn

View File

@@ -198,7 +198,7 @@ func main() {
}
}
if err := dao.DB.Create(txs).Error; err != nil {
log.Println("流量统计入库", err)
log.Println("NEZHA>> 流量统计入库", err)
}
srv.Shutdown(c)
return nil