Improve the plugins interface

This commit is contained in:
Frank Denis 2018-01-10 10:11:59 +01:00
parent efd0477c2b
commit 20e3182692
1 changed files with 31 additions and 6 deletions

View File

@ -9,6 +9,8 @@ type PluginsAction int
const ( const (
PluginsActionNone = 0 PluginsActionNone = 0
PluginsActionForward = 1 PluginsActionForward = 1
PluginsActionDrop = 2
PluginsActionReject = 3
) )
type PluginsState struct { type PluginsState struct {
@ -17,22 +19,35 @@ type PluginsState struct {
originalMaxPayloadSize int originalMaxPayloadSize int
maxPayloadSize int maxPayloadSize int
proto string proto string
queryPlugins *[]Plugin
responsePlugins *[]Plugin
}
type Plugin interface {
Name() string
Description() string
Eval(pluginsState *PluginsState, msg *dns.Msg) error
} }
func NewPluginsState() PluginsState { func NewPluginsState() PluginsState {
return PluginsState{action: PluginsActionForward, maxPayloadSize: MaxDNSUDPPacketSize - ResponseOverhead} queryPlugins := &[]Plugin{Plugin(new(PluginGetSetPayloadSize))}
responsePlugins := &[]Plugin{}
return PluginsState{action: PluginsActionForward, maxPayloadSize: MaxDNSUDPPacketSize - ResponseOverhead,
queryPlugins: queryPlugins, responsePlugins: responsePlugins}
} }
func (pluginsState *PluginsState) ApplyQueryPlugins(packet []byte) ([]byte, error) { func (pluginsState *PluginsState) ApplyQueryPlugins(packet []byte) ([]byte, error) {
pluginsState.action = PluginsActionForward
msg := dns.Msg{} msg := dns.Msg{}
if err := msg.Unpack(packet); err != nil { if err := msg.Unpack(packet); err != nil {
return packet, err return packet, err
} }
for _, plugin := range *pluginsState.queryPlugins {
if ret := pluginsState.BuiltinPluginsGetSetPayloadSize(&msg); ret != nil { if ret := plugin.Eval(pluginsState, &msg); ret != nil {
return packet, ret pluginsState.action = PluginsActionDrop
return packet, ret
}
} }
packet2, err := msg.PackBuffer(packet) packet2, err := msg.PackBuffer(packet)
if err != nil { if err != nil {
return packet, err return packet, err
@ -40,7 +55,17 @@ func (pluginsState *PluginsState) ApplyQueryPlugins(packet []byte) ([]byte, erro
return packet2, nil return packet2, nil
} }
func (pluginsState *PluginsState) BuiltinPluginsGetSetPayloadSize(msg *dns.Msg) error { type PluginGetSetPayloadSize struct{}
func (plugin *PluginGetSetPayloadSize) Name() string {
return "get_set_payload_size"
}
func (plugin *PluginGetSetPayloadSize) Description() string {
return "Adjusts the maximum payload size advertised in queries sent to upstream servers."
}
func (plugin *PluginGetSetPayloadSize) Eval(pluginsState *PluginsState, msg *dns.Msg) error {
pluginsState.originalMaxPayloadSize = 512 - ResponseOverhead pluginsState.originalMaxPayloadSize = 512 - ResponseOverhead
opt := msg.IsEdns0() opt := msg.IsEdns0()
dnssec := false dnssec := false