Skip to content

Commit

Permalink
Update IPASN
Browse files Browse the repository at this point in the history
  • Loading branch information
kinosang committed Oct 17, 2021
1 parent 6d901de commit 80ced36
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:

- name: Download datasource
run: |
curl https://iptoasn.com/data/ip2asn-combined.tsv.gz | gunzip -c | tee data/ip2asn.tsv
curl https://iptoasn.com/data/ip2asn-combined.tsv.gz | gunzip -c | dd status=none of=data/ip2asn.tsv
- name: Build dataset
run: |
Expand All @@ -27,6 +27,6 @@ jobs:
- uses: eine/tip@master
with:
tag: "nightly"
token: ${{ github.token }}
token: ${{ secrets.GITHUB_TOKEN }}
files: |
./data/ipasn.mmdb
38 changes: 29 additions & 9 deletions cmd/ipasn/ipasn.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strconv"

"github.com/maxmind/mmdbwriter"
"github.com/maxmind/mmdbwriter/inserter"
"github.com/maxmind/mmdbwriter/mmdbtype"
)

Expand Down Expand Up @@ -43,11 +44,8 @@ func main() {
continue
}

start := net.ParseIP(record[0])
end := net.ParseIP(record[1])
asn, err := strconv.ParseUint(record[2], 10, 32)

if start == nil || end == nil || err != nil {
start, end, asn, name := parseRow(record)
if start == nil || end == nil {
log.Printf("Invalid IP range: %s - %s %s", record[0], record[1], record[2])
continue
}
Expand All @@ -56,12 +54,10 @@ func main() {
continue
}

name := record[4]

if err := writer.InsertRange(start, end, mmdbtype.Map{
if err := writer.InsertRangeFunc(start, end, inserter.TopLevelMergeWith(mmdbtype.Map{
"asn": mmdbtype.Uint32(asn),
"name": mmdbtype.String(name),
}); err != nil {
})); err != nil {
log.Printf("%s - %s %s", record[0], record[1], record[2])
log.Println(err)
continue
Expand All @@ -77,3 +73,27 @@ func main() {
log.Fatal(err)
}
}

func parseRow(record []string) (net.IP, net.IP, uint32, string) {
start := net.ParseIP(record[0])
end := net.ParseIP(record[1])
asn, err := strconv.ParseUint(record[2], 10, 32)

if start == nil || end == nil || err != nil {
return nil, nil, 0, ""
}

if asn == 0 {
return start, end, 0, ""
}

// map 6to4 address back to IPv4
// if start[0] == 0x20 && start[1] == 0x02 {
// start = net.IPv4(start[2], start[3], start[4], start[5]).To4()
// end = net.IPv4(end[2], end[3], end[4], end[5]).To4()
// }

name := record[4]

return start, end, uint32(asn), name
}

0 comments on commit 80ced36

Please sign in to comment.