mdns

package
v0.0.0-...-344c929 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2022 License: MIT Imports: 9 Imported by: 0

README

GoDoc

mdns

Package mdns provides Instancer and Registrar implementations for mDNS.

mDNS or Multicast DNS can be used to discover services on the local network without the use of an authoritative DNS server. This enables peer-to-peer discovery. It is important to note that many networks restrict the use of multicasting, which prevents mDNS from functioning. Notably, multicast cannot be used in any sort of cloud, or shared infrastructure environment. However it works well in most office, home, or private infrastructure environments.

example

	var (
		serverName = "/services/kit-mdns"
		instance   = "127.0.0.1:8080"
		port       = 8080

		logger = log.NewLogfmtLogger(&bytes.Buffer{})
	)

	// Build the registrar
	service := Service{
		Instance: instance,
		Service:  serverName,
		Port:     port,
	}
	registrar, err := NewRegistrar(service, logger)
	if err != nil {
		logger.Log(err)
		return
	}
	// Register my instance
	registrar.Register()
	defer registrar.Deregister()

	// Build the instancer
	instancer, err := NewInstancer(serverName, InstancerOptions{}, logger)
	if err != nil {
		logger.Log(err)
		return
	}

	// Build the endpoint
	endpointer := sd.NewEndpointer(instancer, fakeFactory, logger)
	_ = endpointer

Documentation

Overview

Package mdns provides Instancer and Registrar implementations for mDNS. mDNS or Multicast DNS can be used to discover services on the local network without the use of an authoritative DNS server. This enables peer-to-peer discovery. It is important to note that many networks restrict the use of multicasting, which prevents mDNS from functioning. Notably, multicast cannot be used in any sort of cloud, or shared infrastructure environment. However it works well in most office, home, or private infrastructure environments.

Example
package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"net"
	"os"

	"github.com/go-kit/kit/endpoint"
	"github.com/go-kit/kit/log"
	"github.com/go-kit/kit/sd"
)

func main() {
	var (
		serverName = "/services/kit-mdns"
		instance   = "127.0.0.1:8080"
		port       = 8080

		logger = log.NewLogfmtLogger(os.Stdout)
	)

	// Build the registrar
	service := Service{
		Instance: instance,
		Service:  serverName,
		Port:     port,
		Ips:      []net.IP{net.IPv4(127, 0, 0, 1)}, // Just for test
	}
	registrar, err := NewRegistrar(service, logger)
	if err != nil {
		fmt.Println(err)
		return
	}
	// Register my instance
	registrar.Register()
	defer registrar.Deregister()

	// Build the instancer
	instancer, err := NewInstancer(serverName, InstancerOptions{}, logger)
	if err != nil {
		fmt.Println(err)
		return
	}

	// Build the endpoint
	endpointer := sd.NewEndpointer(instancer, fakeFactory, logger)
	_, err = endpointer.Endpoints()
	if err != nil {
		fmt.Println(err)
	}

}

func fakeFactory(instance string) (endpoint.Endpoint, io.Closer, error) {
	// Print instance
	fmt.Println(instance)

	return endpoint.Nop, ioutil.NopCloser(nil), nil
}
Output:

127.0.0.1:8080

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Instancer

type Instancer struct {
	// contains filtered or unexported fields
}

Instancer an mDns instancer. It will flushes the cache at intervals.

func NewInstancer

func NewInstancer(service string, opts InstancerOptions, logger log.Logger) (*Instancer, error)

NewInstancer returns an mDNS instancer.

func (*Instancer) Deregister

func (inst *Instancer) Deregister(ch chan<- sd.Event)

Deregister implements Instancer.

func (*Instancer) Register

func (inst *Instancer) Register(ch chan<- sd.Event)

Register implements Instancer.

func (*Instancer) State

func (inst *Instancer) State() sd.Event

State returns the current state of discovery (instances or error) as sd.Event

func (*Instancer) Stop

func (inst *Instancer) Stop()

Stop terminates the Instancer.

type InstancerOptions

type InstancerOptions struct {
	RefreshInterval     time.Duration  // Refresh intervals, default 3 second
	Domain              string         // Lookup domain, default "local"
	LookupTimeout       time.Duration  // Lookup timeout, default 1 second
	Interface           *net.Interface // Multicast interface to use
	WantUnicastResponse bool           // Unicast response desired, as per 5.4 in RFC
}

InstancerOptions is used to customize how a Lookup is performed.

type Registrar

type Registrar struct {
	// contains filtered or unexported fields
}

Registrar is used to listen for mDNS queries and respond if we have a matching local record.

func NewRegistrar

func NewRegistrar(service Service, logger log.Logger) (*Registrar, error)

NewRegistrar is used to create a new registrar from a service config.

func (*Registrar) Deregister

func (registrar *Registrar) Deregister()

Deregister is used to shutdown the listener.

func (*Registrar) Register

func (registrar *Registrar) Register()

Register is used to listen for mDNS queries.

type Service

type Service struct {
	Instance string // Required and unique
	Service  string // Required
	Domain   string
	HostName string
	Port     int // Required
	Ips      []net.IP
	Txt      []string
}

Service holds the instance config.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL