Extend .debug.* flags and hide them by default

Allow to override DNS resolver address via the -debug.dnsoverride flag
and SMTP port via -debug.smtpport.

All flags are not available unless maddy is built using the 'debugflags'
tag.
This commit is contained in:
fox.cpp 2019-12-08 15:19:09 +03:00
parent a574b9fbb2
commit 48e21f566e
No known key found for this signature in database
GPG key ID: E76D97CCEDE90B6C
15 changed files with 110 additions and 21 deletions

View file

@ -41,10 +41,6 @@ emptor.*
*-debug*
Enable debug log. You want to use it when reporting bugs.
*-debug.pprof, -debug.blockprofrate, -debug.mutexproffract*
These options are meant for use by developers and are not interesting for
end users.
# Modules
maddy is built of many small components called "modules". Each module does one

View file

@ -52,7 +52,7 @@ func NewDNSBL(_, instName string, _, inlineArgs []string) (module.Module, error)
instName: instName,
inlineBls: inlineArgs,
resolver: net.DefaultResolver,
resolver: dns.DefaultResolver(),
log: log.Logger{Name: "dnsbl"},
}, nil
}

View file

@ -215,7 +215,7 @@ func (s *state) relyOnDMARC(hdr textproto.Header) bool {
return false
}
policyDomain, record, err := maddydmarc.FetchRecord(net.DefaultResolver, context.Background(), fromDomain)
policyDomain, record, err := maddydmarc.FetchRecord(dns.DefaultResolver(), context.Background(), fromDomain)
if err != nil {
s.log.Error("DMARC fetch", err, "from_domain", fromDomain)
return false

View file

@ -2,7 +2,6 @@ package check
import (
"fmt"
"net"
"github.com/emersion/go-message/textproto"
"github.com/foxcpp/maddy/internal/buffer"
@ -152,7 +151,7 @@ func RegisterStatelessCheck(name string, defaultFailAction FailAction, connCheck
return &statelessCheck{
modName: modName,
instName: instName,
resolver: net.DefaultResolver,
resolver: dns.DefaultResolver(),
logger: log.Logger{Name: modName},
defaultFailAction: defaultFailAction,

View file

@ -0,0 +1,11 @@
//+build debugflags
package dns
import (
"flag"
)
func init() {
flag.StringVar(&overrideServ, "debug.dnsoverride", "system-default", "replace the DNS resolver address")
}

View file

@ -174,6 +174,16 @@ func NewExtResolver() (*ExtResolver, error) {
if err != nil {
return nil, err
}
if overrideServ != "" && overrideServ != "system-default" {
host, port, err := net.SplitHostPort(overrideServ)
if err != nil {
panic(err)
}
cfg.Servers = []string{host}
cfg.Port = port
}
cl := new(dns.Client)
cl.Dialer = &net.Dialer{
Timeout: time.Duration(cfg.Timeout) * time.Second,

36
internal/dns/override.go Normal file
View file

@ -0,0 +1,36 @@
package dns
import (
"context"
"net"
"time"
)
var (
overrideServ string
)
// override globally overrides the used DNS server address with one provided.
// This function is meant only for testing. It should be called before any modules are
// initialized to have full effect.
//
// The server argument is in form of "IP:PORT". It is expected that the server
// will be available both using TCP and UDP on the same port.
func override(server string) {
net.DefaultResolver.Dial = func(ctx context.Context, network, address string) (net.Conn, error) {
dialer := net.Dialer{
// This is localhost, it is either running or not. Fail quickly if
// we can't connect.
Timeout: 1 * time.Second,
}
switch network {
case "udp", "udp4", "udp6":
return dialer.DialContext(ctx, "udp4", server)
case "tcp", "tcp4", "tcp6":
return dialer.DialContext(ctx, "tcp4", server)
default:
panic("OverrideDNS.Dial: unknown network")
}
}
}

View file

@ -2,7 +2,7 @@
// lookups.
//
// Currently, there is only Resolver interface which is implemented
// by net.DefaultResolver. In the future, DNSSEC-enabled stub resolver
// by dns.DefaultResolver(). In the future, DNSSEC-enabled stub resolver
// implementation will be added here.
package dns
@ -14,7 +14,7 @@ import (
// Resolver is an interface that describes DNS-related methods used by maddy.
//
// It is implemented by net.DefaultResolver. Methods behave the same way.
// It is implemented by dns.DefaultResolver(). Methods behave the same way.
type Resolver interface {
LookupAddr(ctx context.Context, addr string) (names []string, err error)
LookupHost(ctx context.Context, host string) (addrs []string, err error)
@ -33,3 +33,11 @@ func LookupAddr(ctx context.Context, r Resolver, ip net.IP) (string, error) {
}
return strings.TrimRight(names[0], "."), nil
}
func DefaultResolver() Resolver {
if overrideServ != "" && overrideServ != "system-default" {
override(overrideServ)
}
return net.DefaultResolver
}

View file

@ -451,7 +451,7 @@ func New(modName string, addrs []string) (module.Module, error) {
addrs: addrs,
submission: modName == "submission",
lmtp: modName == "lmtp",
resolver: net.DefaultResolver,
resolver: dns.DefaultResolver(),
Log: log.Logger{Name: modName},
}
return endp, nil

View file

@ -1,8 +1,6 @@
package msgpipeline
import (
"net"
"github.com/emersion/go-message/textproto"
"github.com/emersion/go-smtp"
"github.com/foxcpp/maddy/internal/address"
@ -50,7 +48,7 @@ func New(globals map[string]interface{}, cfg []config.Node) (*MsgPipeline, error
parsedCfg, err := parseMsgPipelineRootCfg(globals, cfg)
return &MsgPipeline{
msgpipelineCfg: parsedCfg,
Resolver: net.DefaultResolver,
Resolver: dns.DefaultResolver(),
}, err
}

View file

@ -12,7 +12,6 @@ import (
"encoding/hex"
"errors"
"fmt"
"net"
"os"
"path/filepath"
"strconv"
@ -145,7 +144,7 @@ func New(_, instName string, _, inlineArgs []string) (module.Module, error) {
store := &Storage{
instName: instName,
Log: log.Logger{Name: "sql"},
resolver: net.DefaultResolver,
resolver: dns.DefaultResolver(),
}
if len(inlineArgs) != 0 {
if len(inlineArgs) == 1 {

View file

@ -0,0 +1,9 @@
//+build debugflags
package remote
import "flag"
func init() {
flag.StringVar(&smtpPort, "debug.smtpport", "25", "SMTP port to use for connections in tests")
}

View file

@ -79,9 +79,9 @@ func New(_, instName string, _, inlineArgs []string) (module.Module, error) {
}
return &Target{
name: instName,
resolver: net.DefaultResolver,
resolver: dns.DefaultResolver(),
dialer: net.Dial,
mtastsCache: mtasts.Cache{Resolver: net.DefaultResolver},
mtastsCache: mtasts.Cache{Resolver: dns.DefaultResolver()},
Log: log.Logger{Name: "remote"},
stsCacheUpdateDone: make(chan struct{}),

View file

@ -80,9 +80,10 @@ var (
// only for purposes of modification using -X linker flag.
DefaultLibexecDirectory = "/usr/lib/maddy"
profileEndpoint = flag.String("debug.pprof", "", "enable live profiler HTTP endpoint and listen on the specified endpoint")
blockProfileRate = flag.Int("debug.blockprofrate", 0, "set blocking profile rate")
mutexProfileFract = flag.Int("debug.mutexproffract", 0, "set mutex profile fraction)")
enableDebugFlags = false
profileEndpoint *string
blockProfileRate *int
mutexProfileFract *int
)
func BuildInfo() string {
@ -107,6 +108,13 @@ func Run() int {
logTargets = flag.String("log", "stderr", "default logging target(s)")
printVersion = flag.Bool("v", false, "print version and exit")
)
if enableDebugFlags {
profileEndpoint = flag.String("debug.pprof", "", "enable live profiler HTTP endpoint and listen on the specified address")
blockProfileRate = flag.Int("debug.blockprofrate", 0, "set blocking profile rate")
mutexProfileFract = flag.Int("debug.mutexproffract", 0, "set mutex profile fraction")
}
flag.Parse()
if len(flag.Args()) != 0 {
@ -156,6 +164,10 @@ func Run() int {
}
func initDebug() {
if !enableDebugFlags {
return
}
if *profileEndpoint != "" {
go func() {
log.Println("listening on", "http://"+*profileEndpoint, "for profiler requests")

11
maddy_debug.go Normal file
View file

@ -0,0 +1,11 @@
//+build debugflags
package maddy
import (
_ "net/http/pprof"
)
func init() {
enableDebugFlags = true
}