mirror of
https://github.com/DNSCrypt/dnscrypt-proxy.git
synced 2025-04-05 22:27:37 +03:00
Store the normalized qName in the plugin state
We now enforce the fact that a query always include a question. It holds true for all practical use cases of dnscrypt-proxy. This avoids quite a lot of redundant code in plugins, and is faster.
This commit is contained in:
parent
ee24bf0421
commit
4fd54a4919
15 changed files with 107 additions and 144 deletions
|
@ -80,6 +80,7 @@ type PluginsState struct {
|
|||
cacheMaxTTL uint32
|
||||
rejectTTL uint32
|
||||
questionMsg *dns.Msg
|
||||
qName string
|
||||
requestStart time.Time
|
||||
requestEnd time.Time
|
||||
cacheHit bool
|
||||
|
@ -235,25 +236,31 @@ func NewPluginsState(proxy *Proxy, clientProto string, clientAddr *net.Addr, sta
|
|||
cacheMaxTTL: proxy.cacheMaxTTL,
|
||||
rejectTTL: proxy.rejectTTL,
|
||||
questionMsg: nil,
|
||||
qName: "",
|
||||
requestStart: start,
|
||||
maxUnencryptedUDPSafePayloadSize: MaxDNSUDPSafePacketSize,
|
||||
}
|
||||
}
|
||||
|
||||
func (pluginsState *PluginsState) ApplyQueryPlugins(pluginsGlobals *PluginsGlobals, packet []byte, serverName string) ([]byte, error) {
|
||||
if len(*pluginsGlobals.queryPlugins) == 0 && len(*pluginsGlobals.loggingPlugins) == 0 {
|
||||
return packet, nil
|
||||
}
|
||||
pluginsState.serverName = serverName
|
||||
pluginsState.action = PluginsActionForward
|
||||
msg := dns.Msg{}
|
||||
if err := msg.Unpack(packet); err != nil {
|
||||
return packet, err
|
||||
}
|
||||
if len(msg.Question) > 1 {
|
||||
if len(msg.Question) != 1 {
|
||||
return packet, errors.New("Unexpected number of questions")
|
||||
}
|
||||
qName, err := NormalizeQName(msg.Question[0].Name)
|
||||
if err != nil {
|
||||
return packet, err
|
||||
}
|
||||
pluginsState.qName = qName
|
||||
pluginsState.questionMsg = &msg
|
||||
if len(*pluginsGlobals.queryPlugins) == 0 && len(*pluginsGlobals.loggingPlugins) == 0 {
|
||||
return packet, nil
|
||||
}
|
||||
pluginsGlobals.RLock()
|
||||
defer pluginsGlobals.RUnlock()
|
||||
for _, plugin := range *pluginsGlobals.queryPlugins {
|
||||
|
@ -307,7 +314,6 @@ func (pluginsState *PluginsState) ApplyResponsePlugins(pluginsGlobals *PluginsGl
|
|||
}
|
||||
if pluginsState.action == PluginsActionReject {
|
||||
synth := RefusedResponseFromMessage(&msg, pluginsGlobals.refusedCodeInResponses, pluginsGlobals.respondWithIPv4, pluginsGlobals.respondWithIPv6, pluginsState.rejectTTL)
|
||||
dlog.Infof("Blocking [%s]", synth.Question[0].Name)
|
||||
pluginsState.synthResponse = synth
|
||||
}
|
||||
if pluginsState.action != PluginsActionForward {
|
||||
|
@ -330,8 +336,8 @@ func (pluginsState *PluginsState) ApplyLoggingPlugins(pluginsGlobals *PluginsGlo
|
|||
}
|
||||
pluginsState.requestEnd = time.Now()
|
||||
questionMsg := pluginsState.questionMsg
|
||||
if questionMsg == nil || len(questionMsg.Question) > 1 {
|
||||
return errors.New("Unexpected number of questions")
|
||||
if questionMsg == nil {
|
||||
return errors.New("Question not found")
|
||||
}
|
||||
pluginsGlobals.RLock()
|
||||
defer pluginsGlobals.RUnlock()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue