-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathservice-parser.go
58 lines (50 loc) · 1.2 KB
/
service-parser.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
// Parse the embedded service list to provide a map from port to the port's service description.
package main
import (
"bytes"
"encoding/csv"
"fmt"
"strconv"
)
// serviceFuture is a future (a channel with a single write) containing the map from
// port number to abbreviated description of the port.
type serviceFuture chan map[int]string
func parseServiceList() chan map[int]string {
future := make(serviceFuture)
go internalParse(future)
return future
}
func internalParse(future serviceFuture) {
services := make(map[int]string)
// Read data.
bs, err := Asset("data/service-names-port-numbers.csv")
if err != nil {
fmt.Println(err)
panic("Unable to access service file list")
}
// Parse CSV.
r := csv.NewReader(bytes.NewReader(bs))
records, err := r.ReadAll()
if err != nil {
fmt.Println(err)
return
}
// Create map.
for _, rec := range records {
// Remove UDP information for now.
if rec[2] != "tcp" {
continue
}
// Remove empty port numbers
if rec[1] == "" {
continue
}
port, err := strconv.Atoi(rec[1])
if err != nil {
// We ignore port ranges such as xwindow's 6000-6003 for now.
}
techDesc := rec[0]
services[port] = techDesc
}
future <- services
}