-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
5,532 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2017 Tom Harwood | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// OpenRDAP | ||
// Copyright 2017 Tom Harwood | ||
// MIT License, see the LICENSE file. | ||
|
||
package bootstrap | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/url" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type ASNRegistry struct { | ||
ASNs []ASNRange | ||
} | ||
|
||
// ASNRange represents a range of AS numbers (including a range of one, i.e. MinASN=MaxASN). | ||
type ASNRange struct { | ||
MinASN uint32 | ||
MaxASN uint32 | ||
URLs []*url.URL // List of RDAP service URLs for the range. | ||
} | ||
|
||
func (a ASNRange) String() string { | ||
if a.MinASN == a.MaxASN { | ||
return fmt.Sprintf("AS%d", a.MinASN) | ||
} | ||
|
||
return fmt.Sprintf("AS%d-AS%d", a.MinASN, a.MaxASN) | ||
} | ||
|
||
type asnRangeSorter []ASNRange | ||
|
||
func (a asnRangeSorter) Len() int { | ||
return len(a) | ||
} | ||
|
||
func (a asnRangeSorter) Swap(i int, j int) { | ||
a[i], a[j] = a[j], a[i] | ||
} | ||
|
||
func (a asnRangeSorter) Less(i int, j int) bool { | ||
return a[i].MinASN < a[j].MinASN | ||
} | ||
|
||
func NewASNRegistry(json []byte) (*ASNRegistry, error) { | ||
var registry *registryFile | ||
registry, err := parse(json) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("Error parsing ASN registry: %s\n", err) | ||
} | ||
|
||
a := make([]ASNRange, 0, len(registry.Entries)) | ||
|
||
var asn string | ||
var urls []*url.URL | ||
for asn, urls = range registry.Entries { | ||
minASN, maxASN, err := parseASNRange(asn) | ||
|
||
if err != nil { | ||
continue | ||
} | ||
|
||
a = append(a, ASNRange{MinASN: minASN, MaxASN: maxASN, URLs: urls}) | ||
} | ||
|
||
sort.Sort(asnRangeSorter(a)) | ||
|
||
return &ASNRegistry{ | ||
ASNs: a, | ||
}, nil | ||
} | ||
|
||
func (a *ASNRegistry) Lookup(input string) (*Result, error) { | ||
var asn uint32 | ||
asn, err := ParseASN(input) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
index := sort.Search(len(a.ASNs), func(i int) bool { | ||
return asn <= a.ASNs[i].MaxASN | ||
}) | ||
|
||
var entry string | ||
var urls []*url.URL | ||
|
||
if index != len(a.ASNs) && (asn >= a.ASNs[index].MinASN && asn <= a.ASNs[index].MaxASN) { | ||
entry = a.ASNs[index].String() | ||
urls = a.ASNs[index].URLs | ||
} | ||
|
||
return &Result{ | ||
Query: string(asn), | ||
Entry: entry, | ||
URLs: urls, | ||
}, nil | ||
} | ||
|
||
func ParseASN(asn string) (uint32, error) { | ||
asn = strings.ToLower(asn) | ||
asn = strings.TrimLeft(asn, "as") | ||
result, err := strconv.ParseUint(asn, 10, 32) | ||
|
||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return uint32(result), nil | ||
} | ||
|
||
func parseASNRange(asnRange string) (uint32, uint32, error) { | ||
var minASN uint64 | ||
var maxASN uint64 | ||
var err error | ||
|
||
asns := strings.Split(asnRange, "-") | ||
|
||
if len(asns) != 1 && len(asns) != 2 { | ||
return 0, 0, errors.New("Malformed ASN range") | ||
} | ||
|
||
minASN, err = strconv.ParseUint(asns[0], 10, 32) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
|
||
if len(asns) == 2 { | ||
maxASN, err = strconv.ParseUint(asns[1], 10, 32) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
} else { | ||
maxASN = minASN | ||
} | ||
|
||
if minASN > maxASN { | ||
minASN, maxASN = maxASN, minASN | ||
} | ||
|
||
return uint32(minASN), uint32(maxASN), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// OpenRDAP | ||
// Copyright 2017 Tom Harwood | ||
// MIT License, see the LICENSE file. | ||
|
||
package bootstrap | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/skip2/rdap/test" | ||
) | ||
|
||
func TestNetRegistryLookupsASN(t *testing.T) { | ||
test.Start(test.Bootstrap) | ||
defer test.Finish() | ||
|
||
var bytes []byte = test.Get("https://data.iana.org/rdap/asn.json") | ||
|
||
var n *ASNRegistry | ||
n, err := NewASNRegistry(bytes) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
tests := []registryTest{ | ||
{ | ||
"as287", | ||
false, | ||
"AS287", | ||
[]string{"https://rdap.arin.net/registry", "http://rdap.arin.net/registry"}, | ||
}, | ||
{ | ||
"As1768", | ||
false, | ||
"AS1768-AS1769", | ||
[]string{"https://rdap.apnic.net/"}, | ||
}, | ||
{ | ||
"266652", | ||
false, | ||
"AS265629-AS266652", | ||
[]string{"https://rdap.lacnic.net/rdap/"}, | ||
}, | ||
{ | ||
"not-a-number", | ||
true, | ||
"", | ||
[]string{}, | ||
}, | ||
{ | ||
"999999", | ||
false, | ||
"", | ||
[]string{}, | ||
}, | ||
} | ||
|
||
runRegistryTests(t, tests, n) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// OpenRDAP | ||
// Copyright 2017 Tom Harwood | ||
// MIT License, see the LICENSE file. | ||
|
||
package cache | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
homedir "github.com/mitchellh/go-homedir" | ||
) | ||
|
||
const ( | ||
DefaultCacheDir = ".openrdap" | ||
) | ||
|
||
type DiskCache struct { | ||
Timeout time.Duration | ||
Dir string | ||
cache map[string][]byte | ||
mtime map[string]time.Time | ||
} | ||
|
||
func NewDiskCache() *DiskCache { | ||
d := &DiskCache{ | ||
cache: make(map[string][]byte), | ||
mtime: make(map[string]time.Time), | ||
Timeout: time.Hour * 24, | ||
} | ||
|
||
dir, err := homedir.Dir() | ||
|
||
if err != nil { | ||
panic("Can't determine your home directory") | ||
} | ||
|
||
d.Dir = filepath.Join(dir, DefaultCacheDir) | ||
|
||
return d | ||
} | ||
|
||
func (d *DiskCache) InitDir() error { | ||
fileinfo, err := os.Stat(d.Dir) | ||
if err == nil { | ||
if fileinfo.IsDir() { | ||
return nil | ||
} else { | ||
return errors.New("Cache dir is not a dir") | ||
} | ||
} | ||
|
||
if os.IsNotExist(err) { | ||
return os.Mkdir(d.Dir, 0775) | ||
} else { | ||
return err | ||
} | ||
} | ||
|
||
func (d *DiskCache) SetTimeout(timeout time.Duration) { | ||
d.Timeout = timeout | ||
} | ||
|
||
func (d *DiskCache) Save(filename string, data []byte) error { | ||
err := d.InitDir() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Save and copy into place | ||
|
||
d.cache[filename] = make([]byte, len(data)) | ||
copy(d.cache[filename], data) | ||
|
||
d.mtime[filename] = time.Now() | ||
|
||
return nil | ||
} | ||
|
||
func (d *DiskCache) Load(filename string) ([]byte, bool, error) { | ||
// Stat file | ||
// if in cache and mtime the same, return that | ||
|
||
// Otherwise try and reload the file, put into cache, return that | ||
|
||
data, ok := d.cache[filename] | ||
|
||
if !ok { | ||
return nil, false, nil | ||
} | ||
|
||
result := make([]byte, len(data)) | ||
copy(result, data) | ||
|
||
return result, false, nil | ||
} | ||
|
||
func (d *DiskCache) IsStale(filename string) bool { | ||
|
||
mtime, ok := d.mtime[filename] | ||
|
||
if !ok { | ||
return true | ||
} | ||
|
||
expiry := mtime.Add(d.Timeout) | ||
|
||
if expiry.Before(time.Now()) { | ||
return true | ||
} | ||
|
||
return false | ||
} |
Oops, something went wrong.