From 37bb9c4e19cb822e9690c3d14dd291ac15743bc3 Mon Sep 17 00:00:00 2001 From: Kelvin Chua Date: Wed, 24 Feb 2021 10:55:27 +0800 Subject: [PATCH] simplify the buildup of suffixes --- matchrelay.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/matchrelay.go b/matchrelay.go index 0d55cff..f1dd4f6 100644 --- a/matchrelay.go +++ b/matchrelay.go @@ -51,19 +51,20 @@ func (mr *MatchRelay) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns state := request.Request{W: w, Req: r} if len(mr.domains) > 0 { - // state.Name() will always have a trailing . sArr := strings.Split(state.Name(), ".") - l := len(sArr) - if l > 2 { - base := sArr[l - 3] + "." + sArr[l - 2] + "." - for i := l - 3; i > 0; i = i - 1 { - str := sArr[i - 1] + "." + base - if _, ok := mr.domains[str]; ok { - mr.fwd.ServeDNS(ctx, w, r) - return 0, nil - } - base = str + if len(sArr) > 0 { + // state.Name() will always have a trailing . + // remove last element + sArr = sArr[:len(sArr)-1] + } + base := sArr[len(sArr) - 1] + for i := len(sArr) - 2; i >= 0; i = i - 1 { + str := sArr[i] + "." + base + if _, ok := mr.domains[str]; ok { + mr.fwd.ServeDNS(ctx, w, r) + return 0, nil } + base = str } }