Skip to content

Commit

Permalink
修复提取域名&端口
Browse files Browse the repository at this point in the history
  • Loading branch information
ac0d3r committed May 6, 2023
1 parent 4d81bd1 commit ca6b50a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 11 deletions.
5 changes: 3 additions & 2 deletions internal/handler/restful.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package handler
import (
"embed"
"io/fs"
"net"
"net/http"

"github.com/ac0d3r/hyuga/internal/config"
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions internal/handler/restful_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion internal/oob/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions internal/oob/http.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package oob

import (
"net"
"net/http"
"net/http/httputil"
"time"
Expand All @@ -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()
Expand Down Expand Up @@ -53,6 +52,6 @@ func getRealIP(r *http.Request) string {
if ip == "" {
ip = r.RemoteAddr
}
host, _, _ := net.SplitHostPort(ip)
host, _ := SplitHostPort(ip)
return host
}
10 changes: 10 additions & 0 deletions internal/oob/oob.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package oob
import (
"context"
"fmt"
"net"
"strings"

"github.com/ac0d3r/hyuga/internal/config"
Expand Down Expand Up @@ -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
}
4 changes: 1 addition & 3 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit ca6b50a

Please sign in to comment.