Subscribed unsubscribe Subscribe Subscribe

go-dnssd で irkit を探す

irkit

package main
 
import (
        "fmt"
        "github.com/soh335/go-dnssd"
        "net"
        "strconv"
)
 
func main() {
        bc := make(chan *dnssd.BrowseReply)
        ctx, err := dnssd.Browse(dnssd.DNSServiceInterfaceIndexAny, "_irkit._tcp", bc)
 
        if err != nil {
                fmt.Println(err)
                return
        }
 
        go dnssd.Process(ctx)
 
        for {
                browseReply, ok := <-bc
                if !ok {
                        break
                }
 
                rc := make(chan *dnssd.ResolveReply)
                rctx, err := dnssd.Resolve(
                        dnssd.DNSServiceFlagsForceMulticast,
                        browseReply.InterfaceIndex,
                        browseReply.ServiceName,
                        browseReply.RegType,
                        browseReply.ReplyDomain,
                        rc,
                )
 
                if err != nil {
                        fmt.Println(err)
                        return
                }
 
                go dnssd.Process(rctx)
 
                resolveReply, _ := <-rc
 
                qc := make(chan *dnssd.QueryRecordReply)
                qctx, err := dnssd.QueryRecord(
                        dnssd.DNSServiceFlagsForceMulticast,
                        resolveReply.InterfaceIndex,
                        resolveReply.FullName,
                        dnssd.DNSServiceType_SRV,
                        dnssd.DNSServiceClass_IN,
                        qc,
                )
 
                if err != nil {
                        fmt.Println(err)
                        return
                }
 
                go dnssd.Process(qctx)
 
                queryRecordReply, _ := <-qc
 
                gc := make(chan *dnssd.GetAddrInfoReply)
                gctx, err := dnssd.GetAddrInfo(
                        dnssd.DNSServiceFlagsForceMulticast,
                        0,
                        dnssd.DNSServiceProtocol_IPv4,
                        resolveReply.HostTarget,
                        gc,
                )
 
                if err != nil {
                        fmt.Println(err)
                        return
                }
 
                go dnssd.Process(gctx)
 
                getAddrInfoReply, _ := <-gc
 
                fmt.Println(getAddrInfoReply.HostName, "=>", net.JoinHostPort(getAddrInfoReply.Ip, strconv.Itoa((int)(queryRecordReply.SRV().Port))))
        }
}
irkitd2a6.local. => 10.0.1.15:80