1
0
mirror of https://github.com/DNSCrypt/dnscrypt-proxy.git synced 2025-01-14 02:25:52 +01:00

fix: proxy: Trigger query logging plugins using defer

This is more robust and uses lot less lines.
This commit is contained in:
Markus Linnala 2019-10-20 18:35:27 +03:00 committed by Frank Denis
parent 1b4f873026
commit fc9509a8c8

View File

@ -373,6 +373,7 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
return return
} }
pluginsState := NewPluginsState(proxy, clientProto, clientAddr, start) pluginsState := NewPluginsState(proxy, clientProto, clientAddr, start)
defer pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
serverName := "-" serverName := "-"
if serverInfo != nil { if serverInfo != nil {
serverName = serverInfo.Name serverName = serverInfo.Name
@ -388,13 +389,11 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
response, err = pluginsState.synthResponse.PackBuffer(response) response, err = pluginsState.synthResponse.PackBuffer(response)
if err != nil { if err != nil {
pluginsState.returnCode = PluginsReturnCodeParseError pluginsState.returnCode = PluginsReturnCodeParseError
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
return return
} }
} }
if pluginsState.action == PluginsActionDrop { if pluginsState.action == PluginsActionDrop {
pluginsState.returnCode = PluginsReturnCodeDrop pluginsState.returnCode = PluginsReturnCodeDrop
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
return return
} }
} else { } else {
@ -406,7 +405,6 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
sharedKey, encryptedQuery, clientNonce, err := proxy.Encrypt(serverInfo, query, serverProto) sharedKey, encryptedQuery, clientNonce, err := proxy.Encrypt(serverInfo, query, serverProto)
if err != nil { if err != nil {
pluginsState.returnCode = PluginsReturnCodeParseError pluginsState.returnCode = PluginsReturnCodeParseError
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
return return
} }
serverInfo.noticeBegin(proxy) serverInfo.noticeBegin(proxy)
@ -417,7 +415,6 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
sharedKey, encryptedQuery, clientNonce, err := proxy.Encrypt(serverInfo, query, serverProto) sharedKey, encryptedQuery, clientNonce, err := proxy.Encrypt(serverInfo, query, serverProto)
if err != nil { if err != nil {
pluginsState.returnCode = PluginsReturnCodeParseError pluginsState.returnCode = PluginsReturnCodeParseError
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
} }
response, err = proxy.exchangeWithTCPServer(serverInfo, sharedKey, encryptedQuery, clientNonce) response, err = proxy.exchangeWithTCPServer(serverInfo, sharedKey, encryptedQuery, clientNonce)
} }
@ -430,7 +427,6 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
} else { } else {
pluginsState.returnCode = PluginsReturnCodeServerError pluginsState.returnCode = PluginsReturnCodeServerError
} }
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
serverInfo.noticeFailure(proxy) serverInfo.noticeFailure(proxy)
return return
} }
@ -442,14 +438,12 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
SetTransactionID(query, tid) SetTransactionID(query, tid)
if err != nil { if err != nil {
pluginsState.returnCode = PluginsReturnCodeServerError pluginsState.returnCode = PluginsReturnCodeServerError
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
serverInfo.noticeFailure(proxy) serverInfo.noticeFailure(proxy)
return return
} }
response, err = ioutil.ReadAll(io.LimitReader(resp.Body, int64(MaxDNSPacketSize))) response, err = ioutil.ReadAll(io.LimitReader(resp.Body, int64(MaxDNSPacketSize)))
if err != nil { if err != nil {
pluginsState.returnCode = PluginsReturnCodeServerError pluginsState.returnCode = PluginsReturnCodeServerError
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
serverInfo.noticeFailure(proxy) serverInfo.noticeFailure(proxy)
return return
} }
@ -461,14 +455,12 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
} }
if len(response) < MinDNSPacketSize || len(response) > MaxDNSPacketSize { if len(response) < MinDNSPacketSize || len(response) > MaxDNSPacketSize {
pluginsState.returnCode = PluginsReturnCodeParseError pluginsState.returnCode = PluginsReturnCodeParseError
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
serverInfo.noticeFailure(proxy) serverInfo.noticeFailure(proxy)
return return
} }
response, err = pluginsState.ApplyResponsePlugins(&proxy.pluginsGlobals, response, ttl) response, err = pluginsState.ApplyResponsePlugins(&proxy.pluginsGlobals, response, ttl)
if err != nil { if err != nil {
pluginsState.returnCode = PluginsReturnCodeParseError pluginsState.returnCode = PluginsReturnCodeParseError
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
serverInfo.noticeFailure(proxy) serverInfo.noticeFailure(proxy)
return return
} }
@ -481,7 +473,6 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
} }
if len(response) < MinDNSPacketSize || len(response) > MaxDNSPacketSize { if len(response) < MinDNSPacketSize || len(response) > MaxDNSPacketSize {
pluginsState.returnCode = PluginsReturnCodeParseError pluginsState.returnCode = PluginsReturnCodeParseError
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
if serverInfo != nil { if serverInfo != nil {
serverInfo.noticeFailure(proxy) serverInfo.noticeFailure(proxy)
} }
@ -492,7 +483,6 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
response, err = TruncatedResponse(response) response, err = TruncatedResponse(response)
if err != nil { if err != nil {
pluginsState.returnCode = PluginsReturnCodeParseError pluginsState.returnCode = PluginsReturnCodeParseError
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
return return
} }
} }
@ -506,7 +496,6 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
response, err = PrefixWithSize(response) response, err = PrefixWithSize(response)
if err != nil { if err != nil {
pluginsState.returnCode = PluginsReturnCodeParseError pluginsState.returnCode = PluginsReturnCodeParseError
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
if serverInfo != nil { if serverInfo != nil {
serverInfo.noticeFailure(proxy) serverInfo.noticeFailure(proxy)
} }
@ -514,7 +503,6 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
} }
clientPc.Write(response) clientPc.Write(response)
} }
pluginsState.ApplyLoggingPlugins(&proxy.pluginsGlobals)
} }
func NewProxy() *Proxy { func NewProxy() *Proxy {