2018-01-10 09:04:03 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-01-17 02:40:47 +01:00
|
|
|
"errors"
|
2018-01-14 23:47:49 +01:00
|
|
|
"net"
|
2018-01-10 18:32:05 +01:00
|
|
|
"sync"
|
|
|
|
|
2018-01-17 02:40:47 +01:00
|
|
|
"github.com/jedisct1/dlog"
|
2018-01-10 09:04:03 +01:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PluginsAction int
|
|
|
|
|
|
|
|
const (
|
|
|
|
PluginsActionNone = 0
|
|
|
|
PluginsActionForward = 1
|
2018-01-10 10:11:59 +01:00
|
|
|
PluginsActionDrop = 2
|
|
|
|
PluginsActionReject = 3
|
2018-01-10 17:23:20 +01:00
|
|
|
PluginsActionSynth = 4
|
2018-01-10 09:04:03 +01:00
|
|
|
)
|
|
|
|
|
2018-01-15 23:07:41 +01:00
|
|
|
type PluginsGlobals struct {
|
|
|
|
sync.RWMutex
|
|
|
|
queryPlugins *[]Plugin
|
|
|
|
responsePlugins *[]Plugin
|
|
|
|
}
|
|
|
|
|
2018-01-10 09:04:03 +01:00
|
|
|
type PluginsState struct {
|
|
|
|
sessionData map[string]interface{}
|
|
|
|
action PluginsAction
|
|
|
|
originalMaxPayloadSize int
|
|
|
|
maxPayloadSize int
|
2018-01-14 23:47:49 +01:00
|
|
|
clientProto string
|
|
|
|
clientAddr *net.Addr
|
2018-01-10 17:23:20 +01:00
|
|
|
synthResponse *dns.Msg
|
2018-01-10 18:32:05 +01:00
|
|
|
dnssec bool
|
2018-01-10 19:32:56 +01:00
|
|
|
cacheSize int
|
|
|
|
cacheNegTTL uint32
|
|
|
|
cacheMinTTL uint32
|
|
|
|
cacheMaxTTL uint32
|
2018-01-10 10:11:59 +01:00
|
|
|
}
|
|
|
|
|
2018-01-15 23:07:41 +01:00
|
|
|
func InitPluginsGlobals(pluginsGlobals *PluginsGlobals, proxy *Proxy) error {
|
2018-01-10 17:23:20 +01:00
|
|
|
queryPlugins := &[]Plugin{}
|
2018-01-16 00:23:16 +01:00
|
|
|
if len(proxy.queryLogFile) != 0 {
|
|
|
|
*queryPlugins = append(*queryPlugins, Plugin(new(PluginQueryLog)))
|
|
|
|
}
|
2018-01-17 02:40:47 +01:00
|
|
|
if len(proxy.blockNameFile) != 0 {
|
|
|
|
*queryPlugins = append(*queryPlugins, Plugin(new(PluginBlockName)))
|
|
|
|
}
|
2018-01-10 17:23:20 +01:00
|
|
|
if proxy.pluginBlockIPv6 {
|
|
|
|
*queryPlugins = append(*queryPlugins, Plugin(new(PluginBlockIPv6)))
|
|
|
|
}
|
|
|
|
*queryPlugins = append(*queryPlugins, Plugin(new(PluginGetSetPayloadSize)))
|
2018-01-10 18:53:09 +01:00
|
|
|
if proxy.cache {
|
|
|
|
*queryPlugins = append(*queryPlugins, Plugin(new(PluginCache)))
|
|
|
|
}
|
2018-01-17 09:44:03 +01:00
|
|
|
if len(proxy.forwardFile) != 0 {
|
|
|
|
*queryPlugins = append(*queryPlugins, Plugin(new(PluginForward)))
|
|
|
|
}
|
2018-01-10 10:11:59 +01:00
|
|
|
responsePlugins := &[]Plugin{}
|
2018-01-10 18:32:05 +01:00
|
|
|
if proxy.cache {
|
2018-01-10 18:53:09 +01:00
|
|
|
*responsePlugins = append(*responsePlugins, Plugin(new(PluginCacheResponse)))
|
2018-01-10 18:32:05 +01:00
|
|
|
}
|
2018-01-10 17:23:20 +01:00
|
|
|
|
2018-01-15 23:07:41 +01:00
|
|
|
for _, plugin := range *queryPlugins {
|
|
|
|
if err := plugin.Init(proxy); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, plugin := range *responsePlugins {
|
|
|
|
if err := plugin.Init(proxy); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(*pluginsGlobals).queryPlugins = queryPlugins
|
|
|
|
(*pluginsGlobals).responsePlugins = responsePlugins
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Plugin interface {
|
|
|
|
Name() string
|
|
|
|
Description() string
|
|
|
|
Init(proxy *Proxy) error
|
|
|
|
Drop() error
|
|
|
|
Reload() error
|
|
|
|
Eval(pluginsState *PluginsState, msg *dns.Msg) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPluginsState(proxy *Proxy, clientProto string, clientAddr *net.Addr) PluginsState {
|
2018-01-10 18:32:05 +01:00
|
|
|
return PluginsState{
|
2018-01-15 23:07:41 +01:00
|
|
|
action: PluginsActionForward,
|
|
|
|
maxPayloadSize: MaxDNSUDPPacketSize - ResponseOverhead,
|
|
|
|
clientProto: clientProto,
|
|
|
|
clientAddr: clientAddr,
|
|
|
|
cacheSize: proxy.cacheSize,
|
|
|
|
cacheNegTTL: proxy.cacheNegTTL,
|
|
|
|
cacheMinTTL: proxy.cacheMinTTL,
|
|
|
|
cacheMaxTTL: proxy.cacheMaxTTL,
|
2018-01-10 18:32:05 +01:00
|
|
|
}
|
2018-01-10 09:04:03 +01:00
|
|
|
}
|
|
|
|
|
2018-01-15 23:07:41 +01:00
|
|
|
func (pluginsState *PluginsState) ApplyQueryPlugins(pluginsGlobals *PluginsGlobals, packet []byte) ([]byte, error) {
|
|
|
|
if len(*pluginsGlobals.queryPlugins) == 0 {
|
2018-01-10 18:32:05 +01:00
|
|
|
return packet, nil
|
|
|
|
}
|
2018-01-10 10:11:59 +01:00
|
|
|
pluginsState.action = PluginsActionForward
|
2018-01-10 09:04:03 +01:00
|
|
|
msg := dns.Msg{}
|
|
|
|
if err := msg.Unpack(packet); err != nil {
|
|
|
|
return packet, err
|
|
|
|
}
|
2018-01-17 02:40:47 +01:00
|
|
|
if len(msg.Question) > 1 {
|
|
|
|
return packet, errors.New("Unexpected number of questions")
|
|
|
|
}
|
2018-01-15 23:07:41 +01:00
|
|
|
pluginsGlobals.RLock()
|
|
|
|
for _, plugin := range *pluginsGlobals.queryPlugins {
|
2018-01-10 10:11:59 +01:00
|
|
|
if ret := plugin.Eval(pluginsState, &msg); ret != nil {
|
2018-01-15 23:07:41 +01:00
|
|
|
pluginsGlobals.RUnlock()
|
2018-01-10 10:11:59 +01:00
|
|
|
pluginsState.action = PluginsActionDrop
|
|
|
|
return packet, ret
|
|
|
|
}
|
2018-01-17 02:40:47 +01:00
|
|
|
if pluginsState.action == PluginsActionReject {
|
|
|
|
synth, err := RefusedResponseFromMessage(&msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pluginsState.synthResponse = synth
|
|
|
|
}
|
2018-01-10 17:23:20 +01:00
|
|
|
if pluginsState.action != PluginsActionForward {
|
|
|
|
break
|
|
|
|
}
|
2018-01-10 09:04:03 +01:00
|
|
|
}
|
2018-01-15 23:07:41 +01:00
|
|
|
pluginsGlobals.RUnlock()
|
2018-01-10 09:04:03 +01:00
|
|
|
packet2, err := msg.PackBuffer(packet)
|
|
|
|
if err != nil {
|
|
|
|
return packet, err
|
|
|
|
}
|
|
|
|
return packet2, nil
|
|
|
|
}
|
|
|
|
|
2018-01-15 23:07:41 +01:00
|
|
|
func (pluginsState *PluginsState) ApplyResponsePlugins(pluginsGlobals *PluginsGlobals, packet []byte) ([]byte, error) {
|
|
|
|
if len(*pluginsGlobals.responsePlugins) == 0 {
|
2018-01-10 18:32:05 +01:00
|
|
|
return packet, nil
|
|
|
|
}
|
|
|
|
pluginsState.action = PluginsActionForward
|
|
|
|
msg := dns.Msg{}
|
|
|
|
if err := msg.Unpack(packet); err != nil {
|
|
|
|
return packet, err
|
|
|
|
}
|
2018-01-15 23:07:41 +01:00
|
|
|
pluginsGlobals.RLock()
|
|
|
|
for _, plugin := range *pluginsGlobals.responsePlugins {
|
2018-01-10 18:32:05 +01:00
|
|
|
if ret := plugin.Eval(pluginsState, &msg); ret != nil {
|
2018-01-15 23:07:41 +01:00
|
|
|
pluginsGlobals.RUnlock()
|
2018-01-10 18:32:05 +01:00
|
|
|
pluginsState.action = PluginsActionDrop
|
|
|
|
return packet, ret
|
|
|
|
}
|
2018-01-17 02:40:47 +01:00
|
|
|
if pluginsState.action == PluginsActionReject {
|
|
|
|
synth, err := RefusedResponseFromMessage(&msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dlog.Infof("Blocking [%s]", synth.Question[0].Name)
|
|
|
|
pluginsState.synthResponse = synth
|
|
|
|
}
|
2018-01-10 18:32:05 +01:00
|
|
|
if pluginsState.action != PluginsActionForward {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-01-15 23:07:41 +01:00
|
|
|
pluginsGlobals.RUnlock()
|
2018-01-10 18:32:05 +01:00
|
|
|
packet2, err := msg.PackBuffer(packet)
|
|
|
|
if err != nil {
|
|
|
|
return packet, err
|
|
|
|
}
|
|
|
|
return packet2, nil
|
|
|
|
}
|