Skip to content

Commit

Permalink
add RegisterServiceEntry function
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverpool committed Apr 24, 2022
1 parent a393c0e commit a939f9c
Showing 1 changed file with 24 additions and 43 deletions.
67 changes: 24 additions & 43 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ const (
multicastRepetitions = 2
)

// 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

// 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 set and 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 @@ -49,18 +47,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 @@ -79,6 +79,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) (*Server, error) {
Expand All @@ -87,25 +97,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 @@ -120,20 +114,7 @@ func RegisterProxy(instance, service, domain string, port int, host string, ips
}
}

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

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

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

return s, nil
return RegisterServiceEntry(entry, ifaces)
}

const (
Expand Down

0 comments on commit a939f9c

Please sign in to comment.