Skip to content

Commit

Permalink
feat: add DisableQTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf-joe committed Oct 12, 2020
1 parent e32993e commit b7fd46f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
38 changes: 23 additions & 15 deletions cmd/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package conf

import (
"fmt"
"io/ioutil"
"os"
"strings"
"sync"
"time"

"github.com/BurntSushi/toml"
log "github.com/Sirupsen/logrus"
"github.com/janeczku/go-ipset/ipset"
Expand All @@ -13,11 +19,6 @@ import (
"github.com/wolf-joe/ts-dns/matcher"
"github.com/wolf-joe/ts-dns/outbound"
"golang.org/x/net/proxy"
"io/ioutil"
"os"
"strings"
"sync"
"time"
)

// 自定义查询日志的输出格式
Expand Down Expand Up @@ -153,16 +154,17 @@ func (conf *QueryLog) GenLogger() (logger *log.Logger, err error) {

// Conf 配置文件总体结构
type Conf struct {
Listen string
GFWList string
GFWb64 bool `toml:"gfwlist_b64"`
CNIP string
Logger *QueryLog `toml:"query_log"`
HostsFiles []string `toml:"hosts_files"`
Hosts map[string]string
Cache *Cache
Groups map[string]*Group
DisableIPv6 bool `toml:"disable_ipv6"`
Listen string
GFWList string
GFWb64 bool `toml:"gfwlist_b64"`
CNIP string
Logger *QueryLog `toml:"query_log"`
HostsFiles []string `toml:"hosts_files"`
Hosts map[string]string
Cache *Cache
Groups map[string]*Group
DisableIPv6 bool `toml:"disable_ipv6"`
DisableQTypes []string `toml:"disable_qtypes"`
}

// SetDefault 为部分字段默认配置
Expand Down Expand Up @@ -269,6 +271,12 @@ func NewHandler(filename string) (handler *inbound.Handler, err error) {
if handler.DisableIPv6 {
log.Warn("disable ipv6 resolve")
}
handler.DisableQTypes = map[string]bool{}
for _, qType := range config.DisableQTypes {
if qType = strings.TrimSpace(qType); qType != "" {
handler.DisableQTypes[strings.ToUpper(qType)] = true
}
}
// 读取gfwlist
if handler.GFWMatcher, err = matcher.NewABPByFile(config.GFWList, config.GFWb64); err != nil {
log.WithField("file", config.GFWList).Errorf("read gfwlist error: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/coreos/go-semver v0.3.0 // indirect
github.com/fsnotify/fsnotify v1.4.9
github.com/janeczku/go-ipset v0.0.0-20170206212442-499ed3217c4b
github.com/miekg/dns v1.1.28
github.com/miekg/dns v1.1.32
github.com/sparrc/go-ping v0.0.0-20190613174326-4e5b6552494c
github.com/stretchr/testify v1.2.2
github.com/valyala/fastrand v1.0.0
Expand Down
25 changes: 15 additions & 10 deletions inbound/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,17 @@ func (group *Group) AddIPSet(ctx *context.Context, r *dns.Msg) {

// Handler 存储主要配置的dns请求处理器,程序核心
type Handler struct {
Mux *sync.RWMutex
Listen string
Network string
DisableIPv6 bool
Cache *cache.DNSCache
GFWMatcher *matcher.ABPlus
CNIP *cache.RamSet
HostsReaders []hosts.Reader
Groups map[string]*Group
QueryLogger *log.Logger
Mux *sync.RWMutex
Listen string
Network string
DisableIPv6 bool
Cache *cache.DNSCache
GFWMatcher *matcher.ABPlus
CNIP *cache.RamSet
HostsReaders []hosts.Reader
Groups map[string]*Group
QueryLogger *log.Logger
DisableQTypes map[string]bool
}

// HitHosts 如dns请求匹配hosts,则生成对应dns记录并返回。否则返回nil
Expand Down Expand Up @@ -157,6 +158,10 @@ func (handler *Handler) ServeDNS(resp dns.ResponseWriter, request *dns.Msg) {
r = &dns.Msg{}
return // 禁用IPv6时直接返回
}
if qType := dns.TypeToString[question.Qtype]; handler.DisableQTypes[qType] {
r = &dns.Msg{}
return // 禁用指定查询类型
}
// 检测是否命中hosts
if r = handler.HitHosts(ctx, request); r != nil {
handler.LogQuery(ctx.Fields(), "hit hosts", "")
Expand Down

0 comments on commit b7fd46f

Please sign in to comment.