-
Notifications
You must be signed in to change notification settings - Fork 1
/
services.go
46 lines (37 loc) · 766 Bytes
/
services.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
package badcapt
import (
"encoding/csv"
"io"
"os"
)
// DefaultNmapServicesPath is used to translate port number to service name.
var DefaultNmapServicesPath = "/usr/share/nmap/nmap-services"
// NmapServices is map of "port/protocol" and service name.
type NmapServices map[string]string
// ParseNmapServices file to a map.
func ParseNmapServices(path string) (NmapServices, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
r := csv.NewReader(f)
r.Comma = '\t'
r.Comment = '#'
r.FieldsPerRecord = -1
r.LazyQuotes = true
m := make(NmapServices)
for {
rec, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
if len(rec) < 2 {
continue
}
m[rec[1]] = rec[0]
}
return m, nil
}