mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-19 02:37:24 +08:00
dns: 支持反解
This commit is contained in:
+5
-6
@@ -32,6 +32,9 @@ import (
|
||||
|
||||
const (
|
||||
PluginName string = "yunion"
|
||||
|
||||
// defaultTTL to apply to all answers
|
||||
defaultTTL = 10
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -52,6 +55,7 @@ type SRegionDNS struct {
|
||||
Next plugin.Handler
|
||||
Fall fall.F
|
||||
Zones []string
|
||||
PrimaryZone string
|
||||
Upstream upstream.Upstream
|
||||
SqlConnection string
|
||||
K8sConfigFile string
|
||||
@@ -234,11 +238,6 @@ func (r *SRegionDNS) Services(state request.Request, exact bool, opt plugin.Opti
|
||||
return internal, e
|
||||
}
|
||||
|
||||
// Reverse implements the ServiceBackend interface
|
||||
func (r *SRegionDNS) Reverse(state request.Request, exact bool, opt plugin.Options) (services []msg.Service, err error) {
|
||||
return r.Services(state, exact, opt)
|
||||
}
|
||||
|
||||
// Lookup implements the ServiceBackend interface
|
||||
func (r *SRegionDNS) Lookup(state request.Request, name string, typ uint16) (*dns.Msg, error) {
|
||||
return r.Upstream.Lookup(state, name, typ)
|
||||
@@ -398,7 +397,7 @@ func (r *SRegionDNS) findInternalRecordIps(req *recordRequest) ([]string, error)
|
||||
func ips2DnsRecords(ips []string) []msg.Service {
|
||||
recs := make([]msg.Service, 0)
|
||||
for _, ip := range ips {
|
||||
s := msg.Service{Host: ip, TTL: 5 * 60}
|
||||
s := msg.Service{Host: ip, TTL: defaultTTL}
|
||||
recs = append(recs, s)
|
||||
}
|
||||
return recs
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package dns
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/coredns/coredns/plugin"
|
||||
"github.com/coredns/coredns/plugin/etcd/msg"
|
||||
"github.com/coredns/coredns/plugin/pkg/dnsutil"
|
||||
"github.com/coredns/coredns/request"
|
||||
|
||||
"github.com/yunionio/log"
|
||||
|
||||
"github.com/yunionio/onecloud/pkg/compute/models"
|
||||
)
|
||||
|
||||
// Reverse implements the ServiceBackend interface
|
||||
func (r *SRegionDNS) Reverse(state request.Request, exact bool, opt plugin.Options) (services []msg.Service, err error) {
|
||||
ip := dnsutil.ExtractAddressFromReverse(state.Name())
|
||||
if ip == "" {
|
||||
_, e := r.Records(state, exact)
|
||||
return nil, e
|
||||
}
|
||||
records, err := r.getNameForIp(ip, state)
|
||||
if err != nil {
|
||||
log.Errorf("Reverse get name for ip: %v", err)
|
||||
}
|
||||
if len(records) == 0 {
|
||||
return records, errNoItems
|
||||
}
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func (r *SRegionDNS) getNameForIp(ip string, state request.Request) ([]msg.Service, error) {
|
||||
req, e := parseRequest(state)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
|
||||
// 1. try local dns records table
|
||||
records := models.DnsRecordManager.QueryDnsIps(req.ProjectId(), req.Name(), req.Type())
|
||||
for _, rec := range records {
|
||||
return []msg.Service{{Host: rec.Addr, TTL: uint32(rec.Ttl)}}, nil
|
||||
}
|
||||
|
||||
// 2. try hosts table
|
||||
host := models.HostnetworkManager.GetHostByAddress(ip)
|
||||
if host != nil {
|
||||
return []msg.Service{{Host: r.joinDomain(host.Name), TTL: defaultTTL}}, nil
|
||||
}
|
||||
|
||||
// 3. try guests table
|
||||
guest := models.GuestnetworkManager.GetGuestByAddress(ip)
|
||||
if guest != nil {
|
||||
return []msg.Service{{Host: r.joinDomain(guest.Name), TTL: defaultTTL}}, nil
|
||||
}
|
||||
return nil, errNoItems
|
||||
}
|
||||
|
||||
func (r *SRegionDNS) joinDomain(name string) string {
|
||||
return strings.Join([]string{name, r.PrimaryZone}, ".")
|
||||
}
|
||||
@@ -1,12 +1,15 @@
|
||||
package dns
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/coredns/coredns/core/dnsserver"
|
||||
"github.com/coredns/coredns/plugin"
|
||||
"github.com/coredns/coredns/plugin/pkg/upstream"
|
||||
"github.com/mholt/caddy"
|
||||
|
||||
"github.com/yunionio/pkg/util/regutils"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -24,6 +27,13 @@ func setup(c *caddy.Controller) error {
|
||||
return plugin.Error(PluginName, err)
|
||||
}
|
||||
|
||||
if rDNS.PrimaryZone == "" {
|
||||
return fmt.Errorf("dns_domain must provided")
|
||||
}
|
||||
if !regutils.MatchDomainName(rDNS.PrimaryZone) {
|
||||
return fmt.Errorf("dns_domain %q not match domain format", rDNS.PrimaryZone)
|
||||
}
|
||||
|
||||
err = rDNS.initDB(c)
|
||||
if err != nil {
|
||||
return plugin.Error(PluginName, err)
|
||||
@@ -58,6 +68,11 @@ func parseConfig(c *caddy.Controller) (*SRegionDNS, error) {
|
||||
if c.NextBlock() {
|
||||
for {
|
||||
switch c.Val() {
|
||||
case "dns_domain":
|
||||
if !c.NextArg() {
|
||||
return nil, c.ArgErr()
|
||||
}
|
||||
rDNS.PrimaryZone = c.Val()
|
||||
case "fallthrough":
|
||||
rDNS.Fall.SetZonesFromArgs(c.RemainingArgs())
|
||||
case "sql_connection":
|
||||
|
||||
Reference in New Issue
Block a user