From b1bcc3d7d7c2e0b37440d6ebd48798cade362dad Mon Sep 17 00:00:00 2001 From: orange_wolf Date: Thu, 22 Dec 2022 19:10:48 +0800 Subject: [PATCH 1/2] feat: fix panic --- inbound/handler.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/inbound/handler.go b/inbound/handler.go index 0b40302..c6d922b 100644 --- a/inbound/handler.go +++ b/inbound/handler.go @@ -3,6 +3,12 @@ package inbound import ( "errors" "fmt" + "strconv" + "strings" + "sync/atomic" + "time" + "unsafe" + "github.com/miekg/dns" "github.com/sirupsen/logrus" "github.com/wolf-joe/ts-dns/cache" @@ -10,11 +16,6 @@ import ( "github.com/wolf-joe/ts-dns/hosts" "github.com/wolf-joe/ts-dns/outbound" "github.com/wolf-joe/ts-dns/redirector" - "strconv" - "strings" - "sync/atomic" - "time" - "unsafe" ) // region interface @@ -240,7 +241,7 @@ func (h *handlerImpl) handle(writer dns.ResponseWriter, req *dns.Msg) (resp *dns _info.matched = matched // redirect - if h.redirector != nil { + if resp != nil && h.redirector != nil { if group := h.redirector(matched, req, resp); group != nil { matched = group resp = group.Handle(req) From e3c47116211adfc6831ee61db3f2a496ce13ff0f Mon Sep 17 00:00:00 2001 From: orange_wolf Date: Thu, 22 Dec 2022 21:50:38 +0800 Subject: [PATCH 2/2] feat: fix panic in another way --- inbound/handler.go | 2 +- redirector/redirector.go | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/inbound/handler.go b/inbound/handler.go index c6d922b..eef244c 100644 --- a/inbound/handler.go +++ b/inbound/handler.go @@ -241,7 +241,7 @@ func (h *handlerImpl) handle(writer dns.ResponseWriter, req *dns.Msg) (resp *dns _info.matched = matched // redirect - if resp != nil && h.redirector != nil { + if h.redirector != nil { if group := h.redirector(matched, req, resp); group != nil { matched = group resp = group.Handle(req) diff --git a/redirector/redirector.go b/redirector/redirector.go index 123e402..bbed5eb 100644 --- a/redirector/redirector.go +++ b/redirector/redirector.go @@ -3,14 +3,15 @@ package redirector import ( "bufio" "fmt" + "net" + "os" + "strings" + "github.com/miekg/dns" "github.com/sirupsen/logrus" "github.com/wolf-joe/ts-dns/config" "github.com/wolf-joe/ts-dns/outbound" "github.com/yl2chen/cidranger" - "net" - "os" - "strings" ) const ( @@ -49,7 +50,7 @@ func NewRedirector(globalConf config.Conf, groups map[string]outbound.IGroup) (R // return runtime redirector redirector := func(src outbound.IGroup, req, resp *dns.Msg) outbound.IGroup { instance, exists := group2redir[src.Name()] - if !exists { + if resp == nil || !exists { return nil } newGroup := instance.Redirect(req, resp)