dnscrypt-proxy/dnscrypt-proxy/plugins.go

189 lines
4.8 KiB
Go
Raw Normal View History

package main
import (
"errors"
"net"
2018-01-10 18:32:05 +01:00
"sync"
"github.com/jedisct1/dlog"
"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
)
type PluginsGlobals struct {
sync.RWMutex
queryPlugins *[]Plugin
responsePlugins *[]Plugin
}
type PluginsState struct {
sessionData map[string]interface{}
action PluginsAction
originalMaxPayloadSize int
maxPayloadSize int
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
cacheSize int
cacheNegTTL uint32
cacheMinTTL uint32
cacheMaxTTL uint32
2018-01-10 10:11:59 +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)))
}
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)))
}
if len(proxy.cloakFile) != 0 {
*queryPlugins = append(*queryPlugins, Plugin(new(PluginCloak)))
}
2018-01-10 17:23:20 +01:00
*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{}
if len(proxy.nxLogFile) != 0 {
*responsePlugins = append(*responsePlugins, Plugin(new(PluginNxLog)))
}
2018-01-21 16:07:44 +01:00
if len(proxy.blockIPFile) != 0 {
*responsePlugins = append(*responsePlugins, Plugin(new(PluginBlockIP)))
}
if proxy.cache {
*responsePlugins = append(*responsePlugins, Plugin(new(PluginCacheResponse)))
}
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{
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
}
}
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
msg := dns.Msg{}
if err := msg.Unpack(packet); err != nil {
return packet, err
}
if len(msg.Question) > 1 {
return packet, errors.New("Unexpected number of questions")
}
pluginsGlobals.RLock()
for _, plugin := range *pluginsGlobals.queryPlugins {
2018-01-10 10:11:59 +01:00
if ret := plugin.Eval(pluginsState, &msg); ret != nil {
pluginsGlobals.RUnlock()
2018-01-10 10:11:59 +01:00
pluginsState.action = PluginsActionDrop
return packet, ret
}
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
}
}
pluginsGlobals.RUnlock()
packet2, err := msg.PackBuffer(packet)
if err != nil {
return packet, err
}
return packet2, nil
}
func (pluginsState *PluginsState) ApplyResponsePlugins(pluginsGlobals *PluginsGlobals, packet []byte, ttl *uint32) ([]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
}
pluginsGlobals.RLock()
for _, plugin := range *pluginsGlobals.responsePlugins {
2018-01-10 18:32:05 +01:00
if ret := plugin.Eval(pluginsState, &msg); ret != nil {
pluginsGlobals.RUnlock()
2018-01-10 18:32:05 +01:00
pluginsState.action = PluginsActionDrop
return packet, ret
}
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
}
}
pluginsGlobals.RUnlock()
if ttl != nil {
setMaxTTL(&msg, *ttl)
}
2018-01-10 18:32:05 +01:00
packet2, err := msg.PackBuffer(packet)
if err != nil {
return packet, err
}
return packet2, nil
}