Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add disable_query_types in group #45

Merged
merged 1 commit into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ type CacheConf struct {

// Group 配置文件中每个groups section对应的结构
type Group struct {
ECS string `toml:"ecs"`
NoCookie bool `toml:"no_cookie"`
DisableIPv6 bool `toml:"disable_ipv6"`
DisableQTypes []string `toml:"disable_qtypes"`
ECS string `toml:"ecs"`
NoCookie bool `toml:"no_cookie"`

Rules []string `toml:"rules"`
RulesFile string `toml:"rules_file"`
Expand Down
70 changes: 45 additions & 25 deletions outbound/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ import (
"encoding/base64"
"errors"
"fmt"
"github.com/miekg/dns"
"github.com/sirupsen/logrus"
"github.com/wolf-joe/go-ipset/ipset"
"github.com/wolf-joe/ts-dns/config"
"github.com/wolf-joe/ts-dns/matcher"
"github.com/wolf-joe/ts-dns/utils"
"golang.org/x/net/proxy"
"io/ioutil"
"net"
"net/http"
"strings"
"sync/atomic"
"time"
"unsafe"

"github.com/miekg/dns"
"github.com/sirupsen/logrus"
"github.com/wolf-joe/go-ipset/ipset"
"github.com/wolf-joe/ts-dns/config"
"github.com/wolf-joe/ts-dns/matcher"
"github.com/wolf-joe/ts-dns/utils"
"golang.org/x/net/proxy"
)

type IGroup interface {
Expand Down Expand Up @@ -55,22 +56,35 @@ func BuildGroups(globalConf config.Conf) (map[string]IGroup, error) {
seenGFWList = true
}
g := &groupImpl{
name: name,
fallback: conf.Fallback,
matcher: nil,
gfwList: nil,
gfwListURL: conf.GFWListURL,
noCookie: conf.NoCookie,
withECS: nil,
callers: nil,
concurrent: conf.Concurrent,
proxy: nil,
fastestIP: conf.FastestV4,
tcpPingPort: conf.TCPPingPort,
ipSet: nil,
stopCh: make(chan struct{}),
stopped: make(chan struct{}),
name: name,
fallback: conf.Fallback,
matcher: nil,
gfwList: nil,
gfwListURL: conf.GFWListURL,
noCookie: conf.NoCookie,
withECS: nil,
callers: nil,
concurrent: conf.Concurrent,
proxy: nil,
fastestIP: conf.FastestV4,
tcpPingPort: conf.TCPPingPort,
ipSet: nil,
stopCh: make(chan struct{}),
stopped: make(chan struct{}),
disableQTypes: map[uint16]bool{},
}
// disable query types
if conf.DisableIPv6 {
g.disableQTypes[dns.TypeAAAA] = true
}
for _, qTypeStr := range conf.DisableQTypes {
qTypeStr = strings.ToUpper(qTypeStr)
if _, exists := dns.StringToType[qTypeStr]; !exists {
return nil, fmt.Errorf("unknown query type: %q", qTypeStr)
}
g.disableQTypes[dns.StringToType[qTypeStr]] = true
}

// read rules
text := strings.Join(conf.Rules, "\n")
g.matcher = matcher.NewABPByText(text)
Expand Down Expand Up @@ -165,9 +179,10 @@ type groupImpl struct {
name string
fallback bool

matcher *matcher.ABPlus
gfwList unsafe.Pointer // type: *matcher.ABPlus
gfwListURL string
disableQTypes map[uint16]bool
matcher *matcher.ABPlus
gfwList unsafe.Pointer // type: *matcher.ABPlus
gfwListURL string

noCookie bool // 是否删除请求中的cookie
withECS *dns.EDNS0_SUBNET // 是否在请求中附加ECS信息
Expand Down Expand Up @@ -210,6 +225,11 @@ func (g *groupImpl) Match(req *dns.Msg) bool {
}

func (g *groupImpl) Handle(req *dns.Msg) *dns.Msg {
for _, question := range req.Question {
if g.disableQTypes[question.Qtype] {
return nil // disabled
}
}
// 预处理请求
if g.noCookie || g.withECS != nil {
req = req.Copy()
Expand Down
21 changes: 20 additions & 1 deletion outbound/groups_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package outbound

import (
"testing"

"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
"github.com/wolf-joe/ts-dns/config"
"testing"
)

func TestBuildGroups(t *testing.T) {
Expand Down Expand Up @@ -46,3 +48,20 @@ func TestBuildGroups(t *testing.T) {
t.Log(err)
})
}

func TestDisableIPv6(t *testing.T) {
groups, err := BuildGroups(config.Conf{Groups: map[string]config.Group{
"g1": {DisableIPv6: true, DisableQTypes: []string{"AAAA"}},
}})
assert.Nil(t, err)
g := groups["g1"]
assert.NotNil(t, g)
resp := g.Handle(&dns.Msg{
Question: []dns.Question{{
Name: "z.cn.",
Qtype: dns.TypeAAAA,
Qclass: 0,
}},
})
assert.Nil(t, resp)
}
2 changes: 2 additions & 0 deletions ts-dns-full.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ max_ttl = 86400 # 最大ttl,单位为秒
redirector = "oversea_ip2dirty" # 解析后判断是否需要重定向

[groups.dirty]
disable_qtypes = ["AAAA", "HTTPS"] # 对指定组单独屏蔽IPv6/HTTPS查询

gfwlist_file = "gfwlist.txt" # 匹配到gfwlist规则时使用该组

socks5 = "127.0.0.1:1080" # 当使用国外53端口dns解析时推荐用socks5代理解析
Expand Down