From ca6b50acabb62a55db7bb77f15ac64cf9566d0b7 Mon Sep 17 00:00:00 2001 From: ac0d3r Date: Sat, 6 May 2023 11:24:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8F=90=E5=8F=96=E5=9F=9F?= =?UTF-8?q?=E5=90=8D&=E7=AB=AF=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/handler/restful.go | 5 +++-- internal/handler/restful_user.go | 4 ++-- internal/oob/dns.go | 2 +- internal/oob/http.go | 5 ++--- internal/oob/oob.go | 10 ++++++++++ internal/server/server.go | 4 +--- 6 files changed, 19 insertions(+), 11 deletions(-) diff --git a/internal/handler/restful.go b/internal/handler/restful.go index 6072301..9c7c027 100644 --- a/internal/handler/restful.go +++ b/internal/handler/restful.go @@ -3,7 +3,6 @@ package handler import ( "embed" "io/fs" - "net" "net/http" "github.com/ac0d3r/hyuga/internal/config" @@ -13,6 +12,7 @@ import ( "github.com/ac0d3r/hyuga/pkg/event" "github.com/gin-contrib/static" "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" ) type restfulHandler struct { @@ -91,7 +91,8 @@ func (r *restfulHandler) oobHttp() gin.HandlerFunc { httplog := oob.NewHTTP(&r.oob.DNS, r.recorder) return func(c *gin.Context) { - host, _, _ := net.SplitHostPort(c.Request.Host) + host, _ := oob.SplitHostPort(c.Request.Host) + logrus.Debugf("oobHttp host: %s", host) if host != r.oob.DNS.Main { httplog.Record(c) c.Abort() diff --git a/internal/handler/restful_user.go b/internal/handler/restful_user.go index f8a1860..8ed15b7 100644 --- a/internal/handler/restful_user.go +++ b/internal/handler/restful_user.go @@ -7,10 +7,10 @@ import ( "errors" "fmt" "io" - "net" "net/http" "github.com/ac0d3r/hyuga/internal/db" + "github.com/ac0d3r/hyuga/internal/oob" "github.com/gin-gonic/gin" "github.com/gorilla/websocket" "github.com/sirupsen/logrus" @@ -165,7 +165,7 @@ func (w *restfulHandler) info(c *gin.Context) { return } - _, port, _ := net.SplitHostPort(w.oob.JNDI.Address) + _, port := oob.SplitHostPort(w.oob.JNDI.Address) ReturnJSON(c, map[string]any{ "name": user.Name, "avatar": user.Avatar, diff --git a/internal/oob/dns.go b/internal/oob/dns.go index 593a483..3a20c11 100644 --- a/internal/oob/dns.go +++ b/internal/oob/dns.go @@ -53,7 +53,7 @@ func (d *Dns) ServeDNS(w dns.ResponseWriter, r *dns.Msg) { u, err := d.db.GetUserBySid(sid) if err == nil && u != nil { user = u - ip, _, _ := net.SplitHostPort(w.RemoteAddr().String()) + ip, _ := SplitHostPort(w.RemoteAddr().String()) if err := d.recorder.Record(sid, Record{ Sid: sid, Type: TypeDNS, diff --git a/internal/oob/http.go b/internal/oob/http.go index 49202df..623c344 100644 --- a/internal/oob/http.go +++ b/internal/oob/http.go @@ -1,7 +1,6 @@ package oob import ( - "net" "net/http" "net/http/httputil" "time" @@ -22,7 +21,7 @@ func NewHTTP(cnf *config.DNS, recorder *record.Recorder) *HTTP { } func (h *HTTP) Record(c *gin.Context) { - host, _, _ := net.SplitHostPort(c.Request.Host) + host, _ := SplitHostPort(c.Request.Host) sid := parseSid(host, h.cnf.Main) remote := getRealIP(c.Request) url := c.Request.URL.String() @@ -53,6 +52,6 @@ func getRealIP(r *http.Request) string { if ip == "" { ip = r.RemoteAddr } - host, _, _ := net.SplitHostPort(ip) + host, _ := SplitHostPort(ip) return host } diff --git a/internal/oob/oob.go b/internal/oob/oob.go index 933cd6a..5b04507 100644 --- a/internal/oob/oob.go +++ b/internal/oob/oob.go @@ -3,6 +3,7 @@ package oob import ( "context" "fmt" + "net" "strings" "github.com/ac0d3r/hyuga/internal/config" @@ -99,3 +100,12 @@ func parseSid(domain, mainDomain string) string { pre := strings.Split(strings.Trim(domain[:i], "."), ".") return pre[len(pre)-1] } + +func SplitHostPort(host string) (h, p string) { + i := strings.Index(host, ":") + if i < 0 { + return host, "" + } + h, p, _ = net.SplitHostPort(host) + return +} diff --git a/internal/server/server.go b/internal/server/server.go index 331a7ee..4a75f75 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -50,12 +50,10 @@ func Run(ctx context.Context, if !ok { continue } - logrus.Infof("[server][notify] eventbus '*' receive msg: %v", r) + logrus.Infof("[server][notify] eventbus '*' receive msg'%s', '%s'", r.Type.String(), r.Name) u, err := db.GetUserBySid(r.Sid) if err == nil && u != nil && u.Notify.Enable { - logrus.Infof("[server][notify] msg: '%s', '%s'", r.Type.String(), r.Name) - if u.Notify.Bark.Key != "" { notifier.WithBark(u.Notify.Bark.Key, u.Notify.Bark.Server, r.Type.String(), r.Name) }