Make logging plugins independent from query/response plugins

This commit is contained in:
Frank Denis 2018-06-04 20:52:16 +02:00
parent cf41921183
commit b6e6a19b50
2 changed files with 37 additions and 4 deletions

View file

@ -23,6 +23,7 @@ type PluginsGlobals struct {
sync.RWMutex sync.RWMutex
queryPlugins *[]Plugin queryPlugins *[]Plugin
responsePlugins *[]Plugin responsePlugins *[]Plugin
loggingPlugins *[]Plugin
} }
type PluginsState struct { type PluginsState struct {
@ -39,13 +40,11 @@ type PluginsState struct {
cacheNegMaxTTL uint32 cacheNegMaxTTL uint32
cacheMinTTL uint32 cacheMinTTL uint32
cacheMaxTTL uint32 cacheMaxTTL uint32
questionMsg *dns.Msg
} }
func InitPluginsGlobals(pluginsGlobals *PluginsGlobals, proxy *Proxy) error { func InitPluginsGlobals(pluginsGlobals *PluginsGlobals, proxy *Proxy) error {
queryPlugins := &[]Plugin{} queryPlugins := &[]Plugin{}
if len(proxy.queryLogFile) != 0 {
*queryPlugins = append(*queryPlugins, Plugin(new(PluginQueryLog)))
}
if len(proxy.whitelistNameFile) != 0 { if len(proxy.whitelistNameFile) != 0 {
*queryPlugins = append(*queryPlugins, Plugin(new(PluginWhitelistName))) *queryPlugins = append(*queryPlugins, Plugin(new(PluginWhitelistName)))
} }
@ -76,6 +75,12 @@ func InitPluginsGlobals(pluginsGlobals *PluginsGlobals, proxy *Proxy) error {
if proxy.cache { if proxy.cache {
*responsePlugins = append(*responsePlugins, Plugin(new(PluginCacheResponse))) *responsePlugins = append(*responsePlugins, Plugin(new(PluginCacheResponse)))
} }
loggingPlugins := &[]Plugin{}
if len(proxy.queryLogFile) != 0 {
*loggingPlugins = append(*loggingPlugins, Plugin(new(PluginQueryLog)))
}
for _, plugin := range *queryPlugins { for _, plugin := range *queryPlugins {
if err := plugin.Init(proxy); err != nil { if err := plugin.Init(proxy); err != nil {
return err return err
@ -86,9 +91,15 @@ func InitPluginsGlobals(pluginsGlobals *PluginsGlobals, proxy *Proxy) error {
return err return err
} }
} }
for _, plugin := range *loggingPlugins {
if err := plugin.Init(proxy); err != nil {
return err
}
}
(*pluginsGlobals).queryPlugins = queryPlugins (*pluginsGlobals).queryPlugins = queryPlugins
(*pluginsGlobals).responsePlugins = responsePlugins (*pluginsGlobals).responsePlugins = responsePlugins
(*pluginsGlobals).loggingPlugins = loggingPlugins
return nil return nil
} }
@ -112,6 +123,7 @@ func NewPluginsState(proxy *Proxy, clientProto string, clientAddr *net.Addr) Plu
cacheNegMaxTTL: proxy.cacheNegMaxTTL, cacheNegMaxTTL: proxy.cacheNegMaxTTL,
cacheMinTTL: proxy.cacheMinTTL, cacheMinTTL: proxy.cacheMinTTL,
cacheMaxTTL: proxy.cacheMaxTTL, cacheMaxTTL: proxy.cacheMaxTTL,
questionMsg: nil,
} }
} }
@ -127,6 +139,7 @@ func (pluginsState *PluginsState) ApplyQueryPlugins(pluginsGlobals *PluginsGloba
if len(msg.Question) > 1 { if len(msg.Question) > 1 {
return packet, errors.New("Unexpected number of questions") return packet, errors.New("Unexpected number of questions")
} }
pluginsState.questionMsg = &msg
pluginsGlobals.RLock() pluginsGlobals.RLock()
for _, plugin := range *pluginsGlobals.queryPlugins { for _, plugin := range *pluginsGlobals.queryPlugins {
if ret := plugin.Eval(pluginsState, &msg); ret != nil { if ret := plugin.Eval(pluginsState, &msg); ret != nil {
@ -194,3 +207,22 @@ func (pluginsState *PluginsState) ApplyResponsePlugins(pluginsGlobals *PluginsGl
} }
return packet2, nil return packet2, nil
} }
func (pluginsState *PluginsState) ApplyLoggingPlugins(pluginsGlobals *PluginsGlobals) error {
if len(*pluginsGlobals.loggingPlugins) == 0 {
return nil
}
questionMsg := pluginsState.questionMsg
if questionMsg == nil || len(questionMsg.Question) > 1 {
return errors.New("Unexpected number of questions")
}
pluginsGlobals.RLock()
for _, plugin := range *pluginsGlobals.loggingPlugins {
if ret := plugin.Eval(pluginsState, questionMsg); ret != nil {
pluginsGlobals.RUnlock()
return ret
}
}
pluginsGlobals.RUnlock()
return nil
}

View file

@ -9,8 +9,8 @@ import (
"time" "time"
"github.com/jedisct1/dlog" "github.com/jedisct1/dlog"
stamps "github.com/jedisct1/go-dnsstamps"
clocksmith "github.com/jedisct1/go-clocksmith" clocksmith "github.com/jedisct1/go-clocksmith"
stamps "github.com/jedisct1/go-dnsstamps"
"golang.org/x/crypto/curve25519" "golang.org/x/crypto/curve25519"
) )
@ -260,6 +260,7 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
} }
pluginsState := NewPluginsState(proxy, clientProto, clientAddr) pluginsState := NewPluginsState(proxy, clientProto, clientAddr)
query, _ = pluginsState.ApplyQueryPlugins(&proxy.pluginsGlobals, query) query, _ = pluginsState.ApplyQueryPlugins(&proxy.pluginsGlobals, query)
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
var response []byte var response []byte
var err error var err error
if pluginsState.action != PluginsActionForward { if pluginsState.action != PluginsActionForward {