2017-11-28 23:54:05 +01:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-12-03 22:42:25 +01:00
|
|
|
"github.com/pboehm/ddns/shared"
|
2017-11-28 23:54:05 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Request struct {
|
|
|
|
QType string
|
|
|
|
QName string
|
|
|
|
Remote string
|
|
|
|
Local string
|
|
|
|
RealRemote string
|
|
|
|
ZoneId string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Response struct {
|
2017-11-29 00:43:00 +01:00
|
|
|
QType string `json:"qtype"`
|
|
|
|
QName string `json:"qname"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
TTL int `json:"ttl"`
|
2017-11-28 23:54:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type HostLookup struct {
|
2017-12-03 22:42:25 +01:00
|
|
|
config *shared.Config
|
|
|
|
hosts shared.HostBackend
|
2017-11-28 23:54:05 +01:00
|
|
|
}
|
|
|
|
|
2017-12-03 22:42:25 +01:00
|
|
|
func NewHostLookup(config *shared.Config, hostsBackend shared.HostBackend) *HostLookup {
|
2017-11-29 00:43:00 +01:00
|
|
|
return &HostLookup{config, hostsBackend}
|
|
|
|
}
|
|
|
|
|
2017-11-28 23:54:05 +01:00
|
|
|
func (l *HostLookup) Lookup(request *Request) (*Response, error) {
|
|
|
|
responseRecord := request.QType
|
|
|
|
responseContent := ""
|
|
|
|
|
|
|
|
switch request.QType {
|
|
|
|
case "SOA":
|
|
|
|
responseContent = fmt.Sprintf("%s. hostmaster%s. %d 1800 3600 7200 5",
|
|
|
|
l.config.SOAFqdn, l.config.Domain, l.currentSOASerial())
|
|
|
|
|
|
|
|
case "NS":
|
|
|
|
responseContent = l.config.SOAFqdn
|
|
|
|
|
|
|
|
case "A", "AAAA", "ANY":
|
|
|
|
hostname, err := l.extractHostname(request.QName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-12-03 22:42:25 +01:00
|
|
|
var host *shared.Host
|
2017-11-28 23:54:05 +01:00
|
|
|
if host, err = l.hosts.GetHost(hostname); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
responseContent = host.Ip
|
|
|
|
|
|
|
|
responseRecord = "A"
|
|
|
|
if !host.IsIPv4() {
|
|
|
|
responseRecord = "AAAA"
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request.QType == "A" || request.QType == "AAAA") && request.QType != responseRecord {
|
|
|
|
return nil, errors.New("IP address is not valid for requested record")
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.New("Invalid request")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Response{QType: responseRecord, QName: request.QName, Content: responseContent, TTL: 5}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractHostname extract the host part of the fqdn: pi.d.example.org -> pi
|
|
|
|
func (l *HostLookup) extractHostname(rawQueryName string) (string, error) {
|
|
|
|
queryName := strings.ToLower(rawQueryName)
|
|
|
|
|
|
|
|
hostname := ""
|
|
|
|
if strings.HasSuffix(queryName, l.config.Domain) {
|
|
|
|
hostname = queryName[:len(queryName)-len(l.config.Domain)]
|
|
|
|
}
|
|
|
|
|
|
|
|
if hostname == "" {
|
|
|
|
return "", errors.New("Query name does not correspond to our domain")
|
|
|
|
}
|
|
|
|
|
|
|
|
return hostname, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// currentSOASerial get the current SOA serial by returning the current time in seconds
|
|
|
|
func (l *HostLookup) currentSOASerial() int64 {
|
|
|
|
return time.Now().Unix()
|
|
|
|
}
|