This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
discovery.go
114 lines (97 loc) · 3.24 KB
/
discovery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2014-2016 Fraunhofer Institute for Applied Information Technology FIT
package main
import (
"fmt"
"log"
"net"
"strings"
"github.com/grandcat/zeroconf"
"github.com/linksmart/go-sec/auth/obtainer"
sc "github.com/linksmart/service-catalog/v3/catalog"
"github.com/linksmart/service-catalog/v3/client"
"github.com/linksmart/thing-directory/wot"
)
// escape special characters as recommended by https://tools.ietf.org/html/rfc6763#section-4.3
func escapeDNSSDServiceInstance(instance string) (escaped string) {
// replace \ by \\
escaped = strings.ReplaceAll(instance, "\\", "\\\\")
// replace . by \.
escaped = strings.ReplaceAll(escaped, ".", "\\.")
return escaped
}
// register as a DNS-SD Service
func registerDNSSDService(conf *Config) (func(), error) {
instance := escapeDNSSDServiceInstance(conf.DNSSD.Publish.Instance)
log.Printf("DNS-SD: registering as \"%s.%s.%s\", subtype: %s",
instance, wot.DNSSDServiceType, conf.DNSSD.Publish.Domain, wot.DNSSDServiceSubtypeDirectory)
var ifs []net.Interface
for _, name := range conf.DNSSD.Publish.Interfaces {
iface, err := net.InterfaceByName(name)
if err != nil {
return nil, fmt.Errorf("error finding interface %s: %s", name, err)
}
if (iface.Flags & net.FlagMulticast) > 0 {
ifs = append(ifs, *iface)
} else {
return nil, fmt.Errorf("interface %s does not support multicast", name)
}
log.Printf("DNS-SD: will register to interface: %s", name)
}
if len(ifs) == 0 {
log.Println("DNS-SD: publish interfaces not set. Will register to all interfaces with multicast support.")
}
sd, err := zeroconf.Register(
instance,
wot.DNSSDServiceType+","+wot.DNSSDServiceSubtypeDirectory,
conf.DNSSD.Publish.Domain,
conf.HTTP.BindPort,
[]string{"td=/td", "version=" + Version},
ifs,
)
if err != nil {
return sd.Shutdown, err
}
return sd.Shutdown, nil
}
// register in LinkSmart Service Catalog
func registerInServiceCatalog(conf *Config) (func() error, error) {
cat := conf.ServiceCatalog
service := sc.Service{
ID: conf.ServiceID,
Type: "_linksmart-td._tcp",
Title: "LinkSmart Thing Directory",
Description: conf.Description,
APIs: []sc.API{{
ID: "things",
Title: "Thing Directory API",
//Description: "API description",
Protocol: "HTTP",
URL: conf.HTTP.PublicEndpoint,
Spec: sc.Spec{
MediaType: "application/vnd.oai.swagger;version=3.0.0",
URL: "https://raw.githubusercontent.com/linksmart/thing-directory/master/apidoc/openapi-spec.yml",
//Schema: map[string]interface{}{},
},
Meta: map[string]interface{}{
"apiVersion": Version,
},
}},
Doc: "https://github.com/linksmart/thing-directory",
//Meta: map[string]interface{}{},
TTL: uint32(conf.ServiceCatalog.TTL),
}
var ticket *obtainer.Client
var err error
if cat.Auth.Enabled {
// Setup ticket client
ticket, err = obtainer.NewClient(cat.Auth.Provider, cat.Auth.ProviderURL, cat.Auth.Username, cat.Auth.Password, cat.Auth.ClientID)
if err != nil {
return nil, fmt.Errorf("error creating auth client: %s", err)
}
}
stopRegistrator, _, err := client.RegisterServiceAndKeepalive(cat.Endpoint, service, ticket)
if err != nil {
return nil, fmt.Errorf("error registering service: %s", err)
}
return stopRegistrator, nil
}