maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

 1/*
 2Maddy Mail Server - Composable all-in-one email server.
 3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
 4
 5This program is free software: you can redistribute it and/or modify
 6it under the terms of the GNU General Public License as published by
 7the Free Software Foundation, either version 3 of the License, or
 8(at your option) any later version.
 9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program.  If not, see <https://www.gnu.org/licenses/>.
17*/
18
19// Package dns defines interfaces used by maddy modules to perform DNS
20// lookups.
21//
22// Currently, there is only Resolver interface which is implemented
23// by dns.DefaultResolver(). In the future, DNSSEC-enabled stub resolver
24// implementation will be added here.
25package dns
26
27import (
28	"context"
29	"net"
30	"strings"
31)
32
33// Resolver is an interface that describes DNS-related methods used by maddy.
34//
35// It is implemented by dns.DefaultResolver(). Methods behave the same way.
36type Resolver interface {
37	LookupAddr(ctx context.Context, addr string) (names []string, err error)
38	LookupHost(ctx context.Context, host string) (addrs []string, err error)
39	LookupMX(ctx context.Context, name string) ([]*net.MX, error)
40	LookupTXT(ctx context.Context, name string) ([]string, error)
41	LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr, error)
42}
43
44// LookupAddr is a convenience wrapper for Resolver.LookupAddr.
45//
46// It returns the first name with trailing dot stripped.
47func LookupAddr(ctx context.Context, r Resolver, ip net.IP) (string, error) {
48	names, err := r.LookupAddr(ctx, ip.String())
49	if err != nil || len(names) == 0 {
50		return "", err
51	}
52	return strings.TrimRight(names[0], "."), nil
53}
54
55func DefaultResolver() Resolver {
56	if overrideServ != "" && overrideServ != "system-default" {
57		override(overrideServ)
58	}
59
60	return net.DefaultResolver
61}