| 1 | package main |
| 2 | |
| 3 | import "codeberg.org/miekg/dns" |
| 4 | |
| 5 | type PluginGetSetPayloadSize struct{} |
| 6 | |
| 7 | func (plugin *PluginGetSetPayloadSize) Name() string { |
| 8 | return "get_set_payload_size" |
| 9 | } |
| 10 | |
| 11 | func (plugin *PluginGetSetPayloadSize) Description() string { |
| 12 | return "Adjusts the maximum payload size advertised in queries sent to upstream servers." |
| 13 | } |
| 14 | |
| 15 | func (plugin *PluginGetSetPayloadSize) Init(proxy *Proxy) error { |
| 16 | return nil |
| 17 | } |
| 18 | |
| 19 | func (plugin *PluginGetSetPayloadSize) Drop() error { |
| 20 | return nil |
| 21 | } |
| 22 | |
| 23 | func (plugin *PluginGetSetPayloadSize) Reload() error { |
| 24 | return nil |
| 25 | } |
| 26 | |
| 27 | func (plugin *PluginGetSetPayloadSize) Eval(pluginsState *PluginsState, msg *dns.Msg) error { |
| 28 | pluginsState.originalMaxPayloadSize = 512 - ResponseOverhead |
| 29 | |
| 30 | // In v2, EDNS0 info is directly on msg |
| 31 | dnssec := msg.Security |
| 32 | if msg.UDPSize > 0 { |
| 33 | pluginsState.maxUnencryptedUDPSafePayloadSize = int(msg.UDPSize) |
| 34 | pluginsState.originalMaxPayloadSize = Max( |
| 35 | pluginsState.maxUnencryptedUDPSafePayloadSize-ResponseOverhead, |
| 36 | pluginsState.originalMaxPayloadSize, |
| 37 | ) |
| 38 | } |
| 39 | |
| 40 | pluginsState.dnssec = dnssec |
| 41 | pluginsState.maxPayloadSize = Min( |
| 42 | MaxDNSUDPPacketSize-ResponseOverhead, |
| 43 | Max(pluginsState.originalMaxPayloadSize, pluginsState.maxPayloadSize), |
| 44 | ) |
| 45 | |
| 46 | if pluginsState.maxPayloadSize > 512 { |
| 47 | // Set the EDNS0 parameters on msg directly |
| 48 | msg.UDPSize = uint16(pluginsState.maxPayloadSize) |
| 49 | msg.Security = dnssec |
| 50 | } |
| 51 | |
| 52 | return nil |
| 53 | } |