generated from djthorpe/goapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servicedb.go
179 lines (149 loc) · 3.67 KB
/
servicedb.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package mdns
import (
"encoding/xml"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
// Modules
. "github.com/djthorpe/go-server"
)
///////////////////////////////////////////////////////////////////////////////
// TYPES
type servicedb struct {
Title string `xml:"title"`
Category string `xml:"category"`
Updated string `xml:"updated"`
Records []*namerecord `xml:"record"`
// Lookup table
lut map[string]*namerecord
}
type ServiceDescription interface {
Name() string
Service() string
Protocol() string
Description() string
Note() string
}
type namerecord struct {
Name_ string `xml:"name"`
Description_ string `xml:"description"`
Protocol_ string `xml:"protocol"`
Note_ string `xml:"note"`
Port_ string `xml:"number"`
}
///////////////////////////////////////////////////////////////////////////////
// GLOBALS
const (
DefaultServiceDatabase = "https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml"
)
///////////////////////////////////////////////////////////////////////////////
// LIFECYCLE
func NewServiceDatabase() *servicedb {
this := new(servicedb)
this.lut = make(map[string]*namerecord)
return this
}
func ReadServiceDatabase(v string) (*servicedb, error) {
this := NewServiceDatabase()
// Read databases
if err := this.Read(v); err != nil {
return nil, err
}
// Return success
return this, nil
}
///////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// Read records into database
func (this *servicedb) Read(v string) error {
if url, err := url.Parse(v); err != nil {
return err
} else if url.Scheme == "file" || url.Scheme == "" {
r, err := os.Open(url.Path)
if err != nil {
return err
}
defer r.Close()
if err := this.read(r); err != nil {
return err
}
} else if url.Scheme == "http" || url.Scheme == "https" {
r, err := http.Get(url.String())
if err != nil {
return err
}
defer r.Body.Close()
if r.StatusCode != http.StatusOK {
return ErrUnexpectedResponse.With(r.Status)
}
if err := this.read(r.Body); err != nil {
return err
}
} else {
return ErrBadParameter.With("Unsupported scheme: ", url.Scheme)
}
// Return success
return nil
}
// Lookup a record
func (this *servicedb) Lookup(service string) ServiceDescription {
service = fqn(strings.ToLower(service))
if desc, exists := this.lut[service]; exists {
return desc
} else {
return nil
}
}
// Name returns the service name
func (r *namerecord) Name() string {
return r.Name_
}
func (r *namerecord) Description() string {
return r.Description_
}
func (r *namerecord) Protocol() string {
if r.Protocol_ == "" {
return "tcp"
} else {
return r.Protocol_
}
}
func (r *namerecord) Note() string {
return r.Note_
}
// Service returns the service name or empty string if not found
func (r *namerecord) Service() string {
if name := r.Name(); name == "" {
return ""
} else {
return strings.ToLower("_" + name + "._" + r.Protocol() + ".")
}
}
///////////////////////////////////////////////////////////////////////////////
// STRINGIFY
func (this *servicedb) String() string {
str := "<service-database"
for k, v := range this.lut {
str += fmt.Sprintf(" %v => %q", k, v.Description())
}
return str + ">"
}
///////////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
func (this *servicedb) read(r io.Reader) error {
dec := xml.NewDecoder(r)
if err := dec.Decode(this); err != nil {
return err
}
// Add lookup table
for _, record := range this.Records {
if service := record.Service(); service != "" {
this.lut[service] = record
}
}
// Return success
return nil
}