diff --git a/api/zones.go b/api/zones.go index d14c36a..7b3c57c 100644 --- a/api/zones.go +++ b/api/zones.go @@ -130,7 +130,7 @@ func addZoneService(opts *config.Options, domain *happydns.Domain, zone *happydn subdomain := strings.TrimSuffix(strings.TrimSuffix(strings.TrimSuffix(ps.ByName("subdomain"), "."+domain.DomainName), "@"), domain.DomainName) - records := usc.Service.GenRRs(subdomain, usc.Ttl) + records := usc.Service.GenRRs(subdomain, usc.Ttl, domain.DomainName) if len(records) == 0 { return APIErrorResponse{ err: fmt.Errorf("No record can be generated from your service."), diff --git a/model/service.go b/model/service.go index e9cb291..671e2e0 100644 --- a/model/service.go +++ b/model/service.go @@ -40,7 +40,7 @@ type Service interface { GetNbResources() int GenComment(origin string) string // genRRs generates corresponding RRs. - GenRRs(domain string, ttl uint32) []dns.RR + GenRRs(domain string, ttl uint32, origin string) []dns.RR } type ServiceType struct { diff --git a/model/zone.go b/model/zone.go index 69fe7e5..6fa3261 100644 --- a/model/zone.go +++ b/model/zone.go @@ -130,7 +130,7 @@ func (z *Zone) GenerateRRs(origin string) (rrs []dns.RR) { } else { ttl = svc.Ttl } - rrs = append(rrs, svc.GenRRs(subdomain, ttl)...) + rrs = append(rrs, svc.GenRRs(subdomain, ttl, origin)...) } } diff --git a/services/alias.go b/services/alias.go index 1ac100a..dc27d38 100644 --- a/services/alias.go +++ b/services/alias.go @@ -52,7 +52,12 @@ func (s *CNAME) GenComment(origin string) string { return strings.TrimSuffix(s.Target, "."+origin) } -func (s *CNAME) GenRRs(domain string, ttl uint32) (rrs []dns.RR) { +func (s *CNAME) GenRRs(domain string, ttl uint32, origin string) (rrs []dns.RR) { + target := s.Target + if target[len(target)-1] != '.' { + target += origin + } + rrs = append(rrs, &dns.CNAME{ Hdr: dns.RR_Header{ Name: domain, @@ -60,7 +65,7 @@ func (s *CNAME) GenRRs(domain string, ttl uint32) (rrs []dns.RR) { Class: dns.ClassINET, Ttl: ttl, }, - Target: s.Target, + Target: target, }) return } @@ -78,7 +83,12 @@ func (s *SpecialCNAME) GenComment(origin string) string { return "(" + s.SubDomain + ") -> " + strings.TrimSuffix(s.Target, "."+origin) } -func (s *SpecialCNAME) GenRRs(domain string, ttl uint32) (rrs []dns.RR) { +func (s *SpecialCNAME) GenRRs(domain string, ttl uint32, origin string) (rrs []dns.RR) { + target := s.Target + if target[len(target)-1] != '.' { + target += origin + } + rrs = append(rrs, &dns.CNAME{ Hdr: dns.RR_Header{ Name: s.SubDomain + "." + domain, @@ -86,7 +96,7 @@ func (s *SpecialCNAME) GenRRs(domain string, ttl uint32) (rrs []dns.RR) { Class: dns.ClassINET, Ttl: ttl, }, - Target: s.Target, + Target: target, }) return } diff --git a/services/email.go b/services/email.go index d444334..7092800 100644 --- a/services/email.go +++ b/services/email.go @@ -173,9 +173,14 @@ func (s *EMail) GenComment(origin string) string { return buffer.String() } -func (s *EMail) GenRRs(domain string, ttl uint32) (rrs []dns.RR) { +func (s *EMail) GenRRs(domain string, ttl uint32, origin string) (rrs []dns.RR) { if len(s.MX) > 0 { for _, mx := range s.MX { + target := mx.Target + if target[len(target)-1] != '.' { + target += origin + } + rrs = append(rrs, &dns.MX{ Hdr: dns.RR_Header{ Name: domain, @@ -183,7 +188,7 @@ func (s *EMail) GenRRs(domain string, ttl uint32) (rrs []dns.RR) { Class: dns.ClassINET, Ttl: ttl, }, - Mx: mx.Target, + Mx: target, Preference: mx.Preference, }) } diff --git a/services/matrix.go b/services/matrix.go index 449857a..eb5a52c 100644 --- a/services/matrix.go +++ b/services/matrix.go @@ -88,9 +88,9 @@ destloop: return buffer.String() } -func (s *MatrixIM) GenRRs(domain string, ttl uint32) (rrs []dns.RR) { +func (s *MatrixIM) GenRRs(domain string, ttl uint32, origin string) (rrs []dns.RR) { for _, matrix := range s.Matrix { - rrs = append(rrs, matrix.GenRRs("_matrix._tcp."+domain, ttl)...) + rrs = append(rrs, matrix.GenRRs("_matrix._tcp."+domain, ttl, origin)...) } return } diff --git a/services/openpgp.go b/services/openpgp.go index 0fa904e..9cc9442 100644 --- a/services/openpgp.go +++ b/services/openpgp.go @@ -57,7 +57,7 @@ func (s *OpenPGP) GenComment(origin string) string { return fmt.Sprintf("%s", s.Username) } -func (s *OpenPGP) GenRRs(domain string, ttl uint32) (rrs []dns.RR) { +func (s *OpenPGP) GenRRs(domain string, ttl uint32, origin string) (rrs []dns.RR) { if len(s.PublicKey) > 0 { if s.Username != "" { s.Identifier = fmt.Sprintf("%x", sha256.Sum224([]byte(s.Username))) @@ -65,7 +65,7 @@ func (s *OpenPGP) GenRRs(domain string, ttl uint32) (rrs []dns.RR) { rrs = append(rrs, &dns.OPENPGPKEY{ Hdr: dns.RR_Header{ - Name: fmt.Sprintf("_%s._openpgpkey.%d", s.Identifier, domain), + Name: fmt.Sprintf("_%s._openpgpkey.%s", s.Identifier, domain), Rrtype: dns.TypeOPENPGPKEY, Class: dns.ClassINET, Ttl: ttl, @@ -93,7 +93,7 @@ func (s *SMimeCert) GenComment(origin string) string { return fmt.Sprintf("%s", s.Username) } -func (s *SMimeCert) GenRRs(domain string, ttl uint32) (rrs []dns.RR) { +func (s *SMimeCert) GenRRs(domain string, ttl uint32, origin string) (rrs []dns.RR) { if len(s.Certificate) > 0 { if s.Username != "" { s.Identifier = fmt.Sprintf("%x", sha256.Sum224([]byte(s.Username))) @@ -101,7 +101,7 @@ func (s *SMimeCert) GenRRs(domain string, ttl uint32) (rrs []dns.RR) { rrs = append(rrs, &dns.SMIMEA{ Hdr: dns.RR_Header{ - Name: fmt.Sprintf("_%s._smimecert.%d", s.Identifier, domain), + Name: fmt.Sprintf("_%s._smimecert.%s", s.Identifier, domain), Rrtype: dns.TypeSMIMEA, Class: dns.ClassINET, Ttl: ttl, diff --git a/services/origin.go b/services/origin.go index 6f7719b..e2df064 100644 --- a/services/origin.go +++ b/services/origin.go @@ -59,7 +59,15 @@ func (s *Origin) GenComment(origin string) string { return fmt.Sprintf("%s %s %d", strings.TrimSuffix(s.Ns, "."+origin), strings.TrimSuffix(s.Mbox, "."+origin), s.Serial) } -func (s *Origin) GenRRs(domain string, ttl uint32) (rrs []dns.RR) { +func (s *Origin) GenRRs(domain string, ttl uint32, origin string) (rrs []dns.RR) { + ns := s.Ns + if ns[len(ns)-1] != '.' { + ns += origin + } + mbox := s.Mbox + if mbox[len(mbox)-1] != '.' { + mbox += origin + } rrs = append(rrs, &dns.SOA{ Hdr: dns.RR_Header{ Name: domain, @@ -67,8 +75,8 @@ func (s *Origin) GenRRs(domain string, ttl uint32) (rrs []dns.RR) { Class: dns.ClassINET, Ttl: ttl, }, - Ns: s.Ns, - Mbox: s.Mbox, + Ns: ns, + Mbox: mbox, Serial: s.Serial, Refresh: uint32(s.Refresh.Seconds()), Retry: uint32(s.Retry.Seconds()), diff --git a/services/orphan.go b/services/orphan.go index f33f57e..15dd66d 100644 --- a/services/orphan.go +++ b/services/orphan.go @@ -51,7 +51,7 @@ func (s *Orphan) GenComment(origin string) string { return s.RR } -func (s *Orphan) GenRRs(domain string, ttl uint32) (rrs []dns.RR) { +func (s *Orphan) GenRRs(domain string, ttl uint32, origin string) (rrs []dns.RR) { rr, _ := dns.NewRR(fmt.Sprintf("%s %d IN %s", domain, ttl, s.RR)) if rr != nil { rrs = append(rrs, rr) diff --git a/services/server.go b/services/server.go index def954c..2b60cbe 100644 --- a/services/server.go +++ b/services/server.go @@ -78,7 +78,7 @@ func (s *Server) GenComment(origin string) string { return buffer.String() } -func (s *Server) GenRRs(domain string, ttl uint32) (rrs []dns.RR) { +func (s *Server) GenRRs(domain string, ttl uint32, origin string) (rrs []dns.RR) { if s.A != nil { rrs = append(rrs, &dns.A{ Hdr: dns.RR_Header{ diff --git a/services/srv.go b/services/srv.go index 252c457..a6a4c06 100644 --- a/services/srv.go +++ b/services/srv.go @@ -56,7 +56,12 @@ func (s *SRV) GenComment(origin string) string { return fmt.Sprintf("%s:%d", strings.TrimSuffix(s.Target, "."+origin), s.Port) } -func (s *SRV) GenRRs(domain string, ttl uint32) (rrs []dns.RR) { +func (s *SRV) GenRRs(domain string, ttl uint32, origin string) (rrs []dns.RR) { + target := s.Target + if target[len(target)-1] != '.' { + target += origin + } + rrs = append(rrs, &dns.SRV{ Hdr: dns.RR_Header{ Name: domain, @@ -67,7 +72,7 @@ func (s *SRV) GenRRs(domain string, ttl uint32) (rrs []dns.RR) { Priority: s.Priority, Weight: s.Weight, Port: s.Port, - Target: s.Target, + Target: target, }) return } @@ -103,9 +108,9 @@ func (s *UnknownSRV) GenComment(origin string) string { return fmt.Sprintf("%s (%s)", s.Name, s.Proto) } -func (s *UnknownSRV) GenRRs(domain string, ttl uint32) (rrs []dns.RR) { +func (s *UnknownSRV) GenRRs(domain string, ttl uint32, origin string) (rrs []dns.RR) { for _, service := range s.SRV { - rrs = append(rrs, service.GenRRs(fmt.Sprintf("_%s._%s.%s", s.Name, s.Proto, domain), ttl)...) + rrs = append(rrs, service.GenRRs(fmt.Sprintf("_%s._%s.%s", s.Name, s.Proto, domain), ttl, origin)...) } return } diff --git a/services/tlsa.go b/services/tlsa.go index 74a6d0b..ad6a828 100644 --- a/services/tlsa.go +++ b/services/tlsa.go @@ -97,12 +97,12 @@ protoloop: return buffer.String() } -func (ss *TLSAs) GenRRs(domain string, ttl uint32) (rrs []dns.RR) { +func (ss *TLSAs) GenRRs(domain string, ttl uint32, origin string) (rrs []dns.RR) { for _, s := range ss.TLSA { if len(s.Certificate) > 0 { rrs = append(rrs, &dns.TLSA{ Hdr: dns.RR_Header{ - Name: fmt.Sprintf("_%d._%s.%d", s.Port, s.Proto, domain), + Name: fmt.Sprintf("_%d._%s.%s", s.Port, s.Proto, domain), Rrtype: dns.TypeTLSA, Class: dns.ClassINET, Ttl: ttl, diff --git a/services/xmpp.go b/services/xmpp.go index ea09561..8896693 100644 --- a/services/xmpp.go +++ b/services/xmpp.go @@ -95,17 +95,17 @@ destloop: return buffer.String() } -func (s *XMPP) GenRRs(domain string, ttl uint32) (rrs []dns.RR) { +func (s *XMPP) GenRRs(domain string, ttl uint32, origin string) (rrs []dns.RR) { for _, jabber := range s.Client { - rrs = append(rrs, jabber.GenRRs("_jabber._tcp."+domain, ttl)...) + rrs = append(rrs, jabber.GenRRs("_jabber._tcp."+domain, ttl, origin)...) } for _, client := range s.Client { - rrs = append(rrs, client.GenRRs("_xmpp-client._tcp."+domain, ttl)...) + rrs = append(rrs, client.GenRRs("_xmpp-client._tcp."+domain, ttl, origin)...) } for _, server := range s.Server { - rrs = append(rrs, server.GenRRs("_xmpp-server._tcp."+domain, ttl)...) + rrs = append(rrs, server.GenRRs("_xmpp-server._tcp."+domain, ttl, origin)...) } return