-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceinfo.go
153 lines (143 loc) · 3.76 KB
/
serviceinfo.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2020 Ewout Prangsma
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author Ewout Prangsma
//
package v1
import (
context "context"
"crypto/sha1"
fmt "fmt"
"io/ioutil"
"net"
"os"
"runtime"
"sort"
"strconv"
"strings"
"github.com/grandcat/zeroconf"
)
const (
textVersion = "version"
textAPIVersion = "api_version"
textSecure = "secure"
)
// ParseServiceInfo parses a ServiceEntry into a ServiceInfo message.
func ParseServiceInfo(se *zeroconf.ServiceEntry) (*ServiceInfo, error) {
result := &ServiceInfo{
ApiPort: int32(se.Port),
ApiAddress: se.HostName,
Secure: true,
}
if len(se.AddrIPv4) > 0 {
result.ApiAddress = se.AddrIPv4[0].String()
} else if len(se.AddrIPv6) > 0 {
result.ApiAddress = se.AddrIPv6[0].String()
}
for _, t := range se.Text {
parts := strings.SplitN(t, "=", 2)
if len(parts) != 2 {
continue
}
switch parts[0] {
case textVersion:
result.Version = parts[1]
case textAPIVersion:
result.ApiVersion = parts[1]
case textSecure:
if b, err := strconv.ParseBool(parts[1]); err == nil {
result.Secure = b
}
}
}
return result, nil
}
// RegisterServiceEntry runs a registration service for the given service entry
// until the given context is canceled.
func RegisterServiceEntry(ctx context.Context, serviceType string, info ServiceInfo) error {
text := []string{
textVersion + "=" + info.GetVersion(),
textAPIVersion + "=" + info.GetApiVersion(),
textSecure + "=" + strconv.FormatBool(info.GetSecure()),
}
instance, err := os.Hostname()
if err != nil {
instance, err = CreateHostID()
if err != nil {
return err
}
}
var ifaces []net.Interface
if host, found := GetServiceInfoHost(ctx); found && host != "" {
// Find interfaces with given IP
var interfaces []net.Interface
ifaces, err := net.Interfaces()
if err != nil {
return err
}
for _, ifi := range ifaces {
if (ifi.Flags & net.FlagUp) == 0 {
continue
}
if (ifi.Flags & net.FlagMulticast) == 0 {
continue
}
addrs, _ := ifi.Addrs()
for _, addr := range addrs {
if addr.String() == host {
interfaces = append(interfaces, ifi)
break
}
}
}
if len(ifaces) == 0 {
return fmt.Errorf("Did not find interface with address '%s'", host)
}
}
srv, err := zeroconf.Register(instance, serviceType, "local.", int(info.GetApiPort()), text, ifaces)
if err != nil {
return err
}
<-ctx.Done()
srv.Shutdown()
return nil
}
// CreateHostID creates a host ID based on network hardware addresses.
func CreateHostID() (string, error) {
if content, err := ioutil.ReadFile("/etc/machine-id"); err == nil {
content = []byte(strings.TrimSpace(string(content)))
id := fmt.Sprintf("%x", sha1.Sum(content))
return id[:10], nil
}
ifs, err := net.Interfaces()
if err != nil {
return "", err
}
list := make([]string, 0, len(ifs))
for _, v := range ifs {
f := v.Flags
if f&net.FlagUp != 0 && f&net.FlagLoopback == 0 {
fmt.Printf("Using intf %s with addr %s\n", v.Name, v.HardwareAddr.String())
h := v.HardwareAddr.String()
if len(h) > 0 {
list = append(list, h)
}
}
}
sort.Strings(list) // sort host IDs
list = append(list, runtime.GOOS, runtime.GOARCH)
data := []byte(strings.Join(list, ","))
id := fmt.Sprintf("%x", sha1.Sum(data))
return id[:10], nil
}