Skip to content

Commit

Permalink
add RegisterServiceEntry function
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverpool authored and falanger committed Sep 7, 2023
1 parent c258cbf commit 6c97e70
Showing 1 changed file with 26 additions and 31 deletions.
57 changes: 26 additions & 31 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,11 @@ func TTL(ttl uint32) ServerOption {
}
}

// Register a service by given arguments. This call will take the system's hostname
// and lookup IP by that hostname.
func Register(instance, service, domain string, port int, text []string, ifaces []net.Interface, opts ...ServerOption) (*Server, error) {
entry := newServiceEntry(instance, service, domain)
entry.Port = port
entry.Text = text

// RegisterServiceEntry registers a service entry.
// If no HostName is set, the system's hostname will be set and used.
// If ifaces is empty, it will use all the available interfaces.
// If neither AddrIPv4 nor AddrIPv6 is set, it will set and use all available IPs.
func RegisterServiceEntry(entry *ServiceEntry, ifaces []net.Interface) (*Server, error) {
if entry.Instance == "" {
return nil, fmt.Errorf("missing service instance name")
}
Expand All @@ -78,18 +76,20 @@ func Register(instance, service, domain string, port int, text []string, ifaces
}
}

if !strings.HasSuffix(trimDot(entry.HostName), entry.Domain) {
entry.HostName = fmt.Sprintf("%s.%s.", trimDot(entry.HostName), trimDot(entry.Domain))
if !strings.HasSuffix(trimDot(entry.HostName), trimDot(entry.Domain)) {
entry.HostName = trimDot(entry.HostName) + "." + trimDot(entry.Domain) + "."
}

if len(ifaces) == 0 {
ifaces = listMulticastInterfaces()
}

for _, iface := range ifaces {
v4, v6 := addrsForInterface(&iface)
entry.AddrIPv4 = append(entry.AddrIPv4, v4...)
entry.AddrIPv6 = append(entry.AddrIPv6, v6...)
if entry.AddrIPv4 == nil && entry.AddrIPv6 == nil {
for _, iface := range ifaces {
v4, v6 := addrsForInterface(&iface)
entry.AddrIPv4 = append(entry.AddrIPv4, v4...)
entry.AddrIPv6 = append(entry.AddrIPv6, v6...)
}
}

if entry.AddrIPv4 == nil && entry.AddrIPv6 == nil {
Expand All @@ -107,6 +107,16 @@ func Register(instance, service, domain string, port int, text []string, ifaces
return s, nil
}

// Register a service by given arguments. This call will take the system's hostname
// and lookup IP by that hostname.
func Register(instance, service, domain string, port int, text []string, ifaces []net.Interface) (*Server, error) {
entry := NewServiceEntry(instance, service, domain)
entry.Port = port
entry.Text = text

return RegisterServiceEntry(entry, ifaces)
}

// RegisterProxy registers a service proxy. This call will skip the hostname/IP lookup and
// will use the provided values.
func RegisterProxy(instance, service, domain string, port int, host string, ips []string, text []string, ifaces []net.Interface, opts ...ServerOption) (*Server, error) {
Expand All @@ -115,25 +125,9 @@ func RegisterProxy(instance, service, domain string, port int, host string, ips
entry.Text = text
entry.HostName = host

if entry.Instance == "" {
return nil, fmt.Errorf("missing service instance name")
}
if entry.Service == "" {
return nil, fmt.Errorf("missing service name")
}
if entry.HostName == "" {
return nil, fmt.Errorf("missing host name")
}
if entry.Domain == "" {
entry.Domain = "local"
}
if entry.Port == 0 {
return nil, fmt.Errorf("missing port")
}

if !strings.HasSuffix(trimDot(entry.HostName), entry.Domain) {
entry.HostName = fmt.Sprintf("%s.%s.", trimDot(entry.HostName), trimDot(entry.Domain))
}

for _, ip := range ips {
ipAddr := net.ParseIP(ip)
Expand All @@ -152,13 +146,14 @@ func RegisterProxy(instance, service, domain string, port int, host string, ips
ifaces = listMulticastInterfaces()
}

s, err := newServer(ifaces, applyServerOpts(opts...))
s, err := newServer(ifaces)
if err != nil {
return nil, err
}

s.service = entry
s.start()
go s.mainloop()
go s.probe()

return s, nil
}
Expand Down

0 comments on commit 6c97e70

Please sign in to comment.