summaryrefslogtreecommitdiff
path: root/asn/mapping.go
blob: 79372a2ce7b28762e1c6d0659169674fb07aaae7 (plain)
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
package asn

import (
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"path/filepath"

	"github.com/adrg/xdg"
	"github.com/gocarina/gocsv"
)

const SOURCE string = "https://cdn.jsdelivr.net/gh/ipverse/as-metadata@master/as.csv"

type AsnEntry struct {
	ASN         int    `csv:"asn"`
	Handle      string `csv:"handle"`
	Description string `csv:"description"`
	CountryCode string `csv:"country-code"`
}

type AsnMap map[int]AsnEntry

// localPath returns the destination of local copy of asn metadata
func localPath() string {
	return filepath.Join(xdg.DataHome, "/audidi/as.csv")
}

// downloadAsnSource downloads ASN metadata source to disk
func downloadAsnSource() error {
	resp, err := http.Get(SOURCE)
	if err != nil {
		return fmt.Errorf("error fetching ASN metadata: %s", err)
	}
	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("error fetching ASN metadata: http %d", resp.StatusCode)
	}
	defer resp.Body.Close()

	file, err := os.Create(localPath())
	if err != nil {
		return fmt.Errorf("can't open file to write ASN metadata: %s", err)
	}

	if _, err := io.Copy(file, resp.Body); err != nil {
		return fmt.Errorf("can't copy ASN metadata to file: %s", err)
	}

	return nil
}

// useAsnSource returns either a local cached version of
// ASN source bytes or download from source
func useAsnSource() ([]byte, error) {
	path := localPath()
	os.MkdirAll(filepath.Dir(path), os.ModePerm)

	if info, err := os.Stat(path); os.IsNotExist(err) || info.Size() == 0 {
		log.Println("downloading ASN metadata...")
		if err := downloadAsnSource(); err != nil {
			return nil, err
		}
		log.Println("ASN metadata downloaded")
	}

	bytes, err := os.ReadFile(path)
	if err != nil {
		return nil, fmt.Errorf("can't open file to read ASN metadata: %s", err)
	}

	return bytes, nil
}

// parseMetadata parse raw bytes of ASN metadata into readable entries
func parseMetadata(bytes []byte) ([]AsnEntry, error) {
	var entries []AsnEntry

	if err := gocsv.UnmarshalBytes(bytes, &entries); err != nil {
		return nil, fmt.Errorf("can't parse ASN metadata: %s", err)
	}

	return entries, nil
}

// GetMapping returns a pointer to ASN-metadata mappings
func GetMapping() *AsnMap {
	bytes, err := useAsnSource()
	if err != nil {
		log.Fatalln("Failed to load ASN metadata: ", err)
	}

	entries, err := parseMetadata(bytes)
	if err != nil {
		log.Fatalln("Failed to parse ASN metadata (Delete data and retry): ", err)
	}

	mapping := make(AsnMap)
	for _, entry := range entries {
		mapping[entry.ASN] = entry
	}

	return &mapping
}