mirror of
https://github.com/fankes/komari-agent.git
synced 2025-10-19 02:59:23 +08:00
73 lines
2.7 KiB
Go
73 lines
2.7 KiB
Go
package cmd
|
||
|
||
import (
|
||
"crypto/tls"
|
||
"log"
|
||
"net/http"
|
||
"os"
|
||
|
||
"github.com/komari-monitor/komari-agent/cmd/flags"
|
||
"github.com/komari-monitor/komari-agent/server"
|
||
"github.com/komari-monitor/komari-agent/update"
|
||
"github.com/spf13/cobra"
|
||
)
|
||
|
||
var RootCmd = &cobra.Command{
|
||
Use: "komari-agent",
|
||
Short: "komari agent",
|
||
Long: `komari agent`,
|
||
Run: func(cmd *cobra.Command, args []string) {
|
||
log.Println("Komari Agent", update.CurrentVersion)
|
||
log.Println("Github Repo:", update.Repo)
|
||
|
||
// 忽略不安全的证书
|
||
if flags.IgnoreUnsafeCert {
|
||
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||
}
|
||
// 自动更新
|
||
if !flags.DisableAutoUpdate {
|
||
err := update.CheckAndUpdate()
|
||
if err != nil {
|
||
log.Println("[ERROR]", err)
|
||
}
|
||
go update.DoUpdateWorks()
|
||
}
|
||
go server.DoUploadBasicInfoWorks()
|
||
for {
|
||
server.UpdateBasicInfo()
|
||
server.EstablishWebSocketConnection()
|
||
}
|
||
},
|
||
}
|
||
|
||
func Execute() {
|
||
for i, arg := range os.Args {
|
||
if arg == "-autoUpdate" || arg == "--autoUpdate" {
|
||
log.Println("WARNING: The -autoUpdate flag is deprecated in version 0.0.9 and later. Use --disable-auto-update to configure auto-update behavior.")
|
||
// 从参数列表中移除该参数,防止cobra解析错误
|
||
os.Args = append(os.Args[:i], os.Args[i+1:]...)
|
||
break
|
||
}
|
||
}
|
||
|
||
if err := RootCmd.Execute(); err != nil {
|
||
log.Println(err)
|
||
}
|
||
}
|
||
|
||
func init() {
|
||
RootCmd.PersistentFlags().StringVarP(&flags.Token, "token", "t", "", "API token")
|
||
RootCmd.MarkPersistentFlagRequired("token")
|
||
RootCmd.PersistentFlags().StringVarP(&flags.Endpoint, "endpoint", "e", "", "API endpoint")
|
||
RootCmd.MarkPersistentFlagRequired("endpoint")
|
||
RootCmd.PersistentFlags().BoolVar(&flags.DisableAutoUpdate, "disable-auto-update", false, "Disable automatic updates")
|
||
RootCmd.PersistentFlags().BoolVar(&flags.DisableWebSsh, "disable-web-ssh", false, "Disable remote control(web ssh and rce)")
|
||
RootCmd.PersistentFlags().BoolVar(&flags.MemoryModeAvailable, "memory-mode-available", false, "Report memory as available instead of used.")
|
||
RootCmd.PersistentFlags().Float64VarP(&flags.Interval, "interval", "i", 1.0, "Interval in seconds")
|
||
RootCmd.PersistentFlags().BoolVarP(&flags.IgnoreUnsafeCert, "ignore-unsafe-cert", "u", false, "Ignore unsafe certificate errors")
|
||
RootCmd.PersistentFlags().IntVarP(&flags.MaxRetries, "max-retries", "r", 3, "Maximum number of retries")
|
||
RootCmd.PersistentFlags().IntVarP(&flags.ReconnectInterval, "reconnect-interval", "c", 5, "Reconnect interval in seconds")
|
||
RootCmd.PersistentFlags().IntVar(&flags.InfoReportInterval, "info-report-interval", 5, "Interval in minutes for reporting basic info")
|
||
RootCmd.PersistentFlags().ParseErrorsWhitelist.UnknownFlags = true
|
||
}
|