From 935cd73f0de43f1fc45b5411cc7687a2f03aea8a Mon Sep 17 00:00:00 2001 From: dexter <178529795@qq.com> Date: Sun, 17 Jul 2022 09:36:27 +0800 Subject: [PATCH] =?UTF-8?q?sysInfo=E5=A2=9E=E5=8A=A0LocalIP=E5=AD=97?= =?UTF-8?q?=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http.go | 18 +----------------- main.go | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/http.go b/http.go index fa0e147..0b67ce8 100644 --- a/http.go +++ b/http.go @@ -2,7 +2,6 @@ package engine import ( "encoding/json" - "net" "net/http" "time" @@ -51,22 +50,7 @@ func (conf *GlobalConfig) API_stream(rw http.ResponseWriter, r *http.Request) { } func (conf *GlobalConfig) API_sysInfo(rw http.ResponseWriter, r *http.Request) { - var IP []string - if addrs, err := net.InterfaceAddrs(); err == nil { - for _, address := range addrs { - if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { - if ipnet.IP.To4() != nil { - IP = append(IP, ipnet.IP.String()) - } - } - } - } - - if err := json.NewEncoder(rw).Encode(&struct { - Version string - StartTime string - IP []string - }{Engine.Version, StartTime.Format("2006-01-02 15:04:05"), IP}); err != nil { + if err := json.NewEncoder(rw).Encode(&SysInfo); err != nil { http.Error(rw, err.Error(), http.StatusInternalServerError) } } diff --git a/main.go b/main.go index 6c1fab7..eb056c1 100755 --- a/main.go +++ b/main.go @@ -5,10 +5,12 @@ import ( "context" "fmt" "io/ioutil" + "net" "net/http" "os" "path/filepath" "runtime" + "strings" "time" "github.com/google/uuid" @@ -21,11 +23,15 @@ import ( ) var ( + SysInfo struct { + StartTime time.Time //启动时间 + LocalIP string + Version string + } ExecPath = os.Args[0] ExecDir = filepath.Dir(ExecPath) // ConfigRaw 配置信息的原始数据 ConfigRaw []byte - StartTime time.Time //启动时间 Plugins = make(map[string]*Plugin) // Plugins 所有的插件配置 EngineConfig = &GlobalConfig{ Engine: config.Global, @@ -37,9 +43,16 @@ var ( apiList []string //注册到引擎的API接口列表 ) +func init() { + if conn, err := net.Dial("udp", "114.114.114.114:80"); err == nil { + SysInfo.LocalIP, _, _ = strings.Cut(conn.LocalAddr().String(), ":") + } +} + // Run 启动Monibuca引擎,传入总的Context,可用于关闭所有 func Run(ctx context.Context, configFile string) (err error) { - StartTime = time.Now() + SysInfo.StartTime = time.Now() + SysInfo.Version = Engine.Version Engine.Context = ctx if _, err := os.Stat(configFile); err != nil { configFile = filepath.Join(ExecDir, configFile)