-
Notifications
You must be signed in to change notification settings - Fork 8
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
8 changed files
with
111 additions
and
474,320 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 |
---|---|---|
|
@@ -7,4 +7,4 @@ bin/**/socket-connect-bpf | |
bpf_*_bpfeb.go | ||
bpf_*_bpfel.go | ||
|
||
ip2asn-v6.tsv | ||
*.tsv |
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
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,74 @@ | ||
package as | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"net" | ||
"os" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
var asList []ASInfoIPv6 | ||
|
||
// ParseASNumbersIPv6 parses the autonomous system (AS) Numbers and IPv6 ranges from a .tsv file | ||
func ParseASNumbersIPv6(asTsvFile string) { | ||
csvFile, err := os.Open(asTsvFile) | ||
|
||
if err != nil { | ||
fmt.Println("Could not read AS Number file") | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
defer csvFile.Close() | ||
|
||
scanner := bufio.NewScanner(csvFile) | ||
for scanner.Scan() { | ||
fields := strings.Fields(scanner.Text()) | ||
if len(fields) != 5 { | ||
continue // Skip invalid lines | ||
} | ||
|
||
start := net.ParseIP(fields[0]) | ||
end := net.ParseIP(fields[1]) | ||
if start == nil || end == nil { | ||
continue // Skip invalid IP ranges | ||
} | ||
|
||
asNumber, _ := strconv.ParseUint(fields[2], 10, 32) | ||
|
||
asList = append(asList, ASInfoIPv6{ | ||
StartIP: start, | ||
EndIP: end, | ||
AsNumber: uint32(asNumber), | ||
Name: fields[4], | ||
}) | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
fmt.Println("Could not read AS Number file") | ||
fmt.Println(err) | ||
return | ||
} | ||
} | ||
|
||
// GetASInfoIPv6 returns information about an autonomous system (AS) of which the given IP is part of. | ||
func GetASInfoIPv6(ip net.IP) ASInfoIPv6 { | ||
for _, r := range asList { | ||
if bytes.Compare(ip, r.StartIP) >= 0 && bytes.Compare(ip, r.EndIP) <= 0 { | ||
return r | ||
} | ||
} | ||
var empty ASInfoIPv6 | ||
return empty | ||
} | ||
|
||
// ASInfoIPv6 contains information about an autonomous system (AS) | ||
type ASInfoIPv6 struct { | ||
StartIP net.IP | ||
EndIP net.IP | ||
AsNumber uint32 | ||
Name string | ||
} |
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,20 @@ | ||
package as | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
) | ||
|
||
func TestIP6ToAsRange(t *testing.T) { | ||
ParseASNumbersIPv6("./ip2asn-v6.tsv") | ||
ip := "2620:2d:4000:1::1" | ||
got := GetASInfoIPv6(net.ParseIP(ip)) | ||
wantName := "CANONICAL-AS" | ||
if got.Name != wantName { | ||
t.Errorf("GetASInfo(%s) = %s; want %s", ip, got.Name, wantName) | ||
} | ||
wantAsNumber := uint32(41231) | ||
if got.AsNumber != wantAsNumber { | ||
t.Errorf("GetASInfo(%s) = %d; want %d", ip, got.AsNumber, wantAsNumber) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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