aaaaand i broke it

This commit is contained in:
kayos@tcp.direct 2022-08-21 21:38:13 -07:00
parent 5e89a270a7
commit a1ad036f5e
Signed by: kayos
GPG Key ID: 4B841471B4BEE979
6 changed files with 139 additions and 50 deletions

1
.gitignore vendored

@ -1,3 +1,4 @@
*.json
*.db
*.gz
.idea/

23
cloc/aws.go Normal file

@ -0,0 +1,23 @@
package cloc
type Prefixes struct {
Prefixes []Prefix `json:"prefixes"`
}
type Prefixesv6 struct {
Prefixesv6 []Prefixv6 `json:"ipv6_prefixes"`
}
type Prefix struct {
Ip_prefix string `json:"ip_prefix"`
Region string `json:"region"`
Service string `json:"service"`
NBG string `json:"network_border_group"`
}
type Prefixv6 struct {
Ipv6_prefix string `json:"ipv6_prefix"`
Region string `json:"region"`
Service string `json:"service"`
NBG string `json:"network_border_group"`
}

21
cloc/azure.go Normal file

@ -0,0 +1,21 @@
package cloc
type Values struct {
Values []Value `json:"values"`
}
type Value struct {
Name string `json:"name"`
Id string `json:"id"`
Properties Properties `json:"properties"`
}
type Properties struct {
Changenumber string `json:"changeNumber"`
Region string `json:"region"`
Regionid string `json:"regionId"`
Platform string `json:"platform"`
Systemservice string `json:"systemService"`
Addressprefixes []string `json:"addressPrefixes"`
Networkfeatures []string `json:"networkFeatures"`
}

10
cloc/google.go Normal file

@ -0,0 +1,10 @@
package cloc
type GPrefixes struct {
GPrefixes []GPrefix `json:"prefixes"`
}
type GPrefix struct {
Ipv4prefix string `json:"ipv4Prefix"`
Ipv6prefix string `json:"ipv6Prefix"`
}

3
go.mod

@ -3,6 +3,7 @@ module cinnanet
go 1.19
require (
git.tcp.direct/kayos/common v0.7.0
github.com/manifoldco/promptui v0.9.0
inet.af/netaddr v0.0.0-20220811202034-502d2d690317
)
@ -11,5 +12,5 @@ require (
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
go4.org/intern v0.0.0-20211027215823-ae77deb06f29 // indirect
go4.org/unsafe/assume-no-moving-gc v0.0.0-20220617031537-928513b29760 // indirect
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
)

131
main.go

@ -5,8 +5,8 @@ import (
"fmt"
"os"
"strings"
"text/template"
"git.tcp.direct/kayos/common/network"
"github.com/manifoldco/promptui"
"inet.af/netaddr"
)
@ -27,20 +27,24 @@ func init() {
}
type Entry struct {
Route netaddr.IPPrefix `json:"route"`
OriginASN string `json:"origin_asn,omitempty"`
Description []string `json:"description,omitempty"`
MaintainedBy []string `json:"maintained_by,omitempty"`
MemberOf []string `json:"member_of,omitempty"`
Notify []string `json:"notify,omitempty"`
Source string `json:"source,omitempty"`
RoutePrefix []netaddr.IPPrefix `json:"route"`
IPRange []netaddr.IPRange `json:"iprange"`
OriginASN string `json:"origin_asn,omitempty"`
NetName string `json:"netname,omitempty"`
Description []string `json:"description,omitempty"`
MaintainedBy []string `json:"maintained_by,omitempty"`
MemberOf []string `json:"member_of,omitempty"`
Notify []string `json:"notify,omitempty"`
Source string `json:"source,omitempty"`
}
var asnMap = make(map[string]*Entry)
var (
asnMap = make(map[string]*Entry)
prefixMap = make(map[string][]*Entry)
)
var (
tmpMap = make(map[int]*Entry)
tmpMap = make(map[int]*Entry)
count = 0
innerCount = 0
)
@ -50,6 +54,14 @@ func processLine(line string) {
if tmpMap[count] != nil && tmpMap[count].OriginASN != "" {
asnMap[tmpMap[count].OriginASN] = tmpMap[count]
}
if tmpMap[count] != nil && tmpMap[count].RoutePrefix != nil {
for _, prefix := range tmpMap[count].RoutePrefix {
prefixMap[prefix.String()] = append(
prefixMap[prefix.String()],
tmpMap[count],
)
}
}
count++
tmpMap[count] = &Entry{}
innerCount = 0
@ -62,7 +74,9 @@ func processLine(line string) {
switch {
case len(lineFields) < 2:
return
case innerCount == 1 && !strings.HasPrefix(line, "route:"):
case innerCount == 1 &&
!strings.HasPrefix(line, "route:") &&
!strings.HasPrefix(line, "inetnum:"):
return
case innerCount == 1 && strings.HasPrefix(line, "route:"):
route, err := netaddr.ParseIPPrefix(lineFields[1])
@ -70,14 +84,22 @@ func processLine(line string) {
println(err.Error())
return
}
tmpMap[count].Route = route
tmpMap[count].RoutePrefix = append(tmpMap[count].RoutePrefix, route)
case innerCount == 1 && strings.HasPrefix(line, "inetnum:"):
ipr, err := netaddr.ParseIPRange(strings.Join(lineFields[1:], ""))
if err == nil {
tmpMap[count].IPRange = append(tmpMap[count].IPRange, ipr)
}
case strings.HasPrefix(line, "origin:"):
tmpMap[count].OriginASN = lineFields[1]
case strings.HasPrefix(line, "descr:"):
tmpMap[count].Description = append(tmpMap[count].Description, strings.Join(lineFields[1:], " "))
case strings.HasPrefix(line, "netname:"):
tmpMap[count].NetName = lineFields[1]
case strings.HasPrefix(line, "mnt-by:"),
strings.HasPrefix(line, "mnt-lower"),
strings.HasPrefix(line, "mnt-routes"):
strings.HasPrefix(line, "mnt-routes"),
strings.HasPrefix(line, "mnt-domains"):
tmpMap[count].MaintainedBy = append(tmpMap[count].MaintainedBy, lineFields[1])
case strings.HasPrefix(line, "source:"):
tmpMap[count].Source = lineFields[1]
@ -88,56 +110,67 @@ func processLine(line string) {
}
}
var newFuncMap = template.FuncMap{}
func init() {
for k, v := range promptui.FuncMap {
newFuncMap[k] = v
}
newFuncMap["AS"] = func(s string) *Entry {
return asnMap[s]
}
}
func main() {
for xerox.Scan() {
processLine(xerox.Text())
}
fmt.Println("Total entries:", len(asnMap))
var asnNames = make([]string, len(asnMap))
for name := range asnMap {
asnNames = append(asnNames, name)
var asnNames []*Entry
for _, entry := range asnMap {
asnNames = append(asnNames, entry)
}
search := func(input string, index int) bool {
aut := asnNames[index]
if len(aut.OriginASN) == 0 {
return false
}
name := strings.ToLower(aut.OriginASN)
var ipaddrs []string
for _, prfx := range aut.RoutePrefix {
for ipa := range network.IterateNetRange(prfx) {
ipaddrs = append(ipaddrs, ipa.String())
}
}
for _, ipr := range aut.IPRange {
for ipa := range network.IterateNetRange(ipr) {
ipaddrs = append(ipaddrs, ipa.String())
}
}
input = strings.ToLower(input)
if strings.Contains(name, input) {
return true
}
for _, ipa := range ipaddrs {
if strings.Contains(ipa, input) {
return true
}
}
return true
}
selector := promptui.Select{
Label: "Known ASNs",
Items: asnNames,
Size: 25,
Size: 15,
CursorPos: 0,
Templates: &promptui.SelectTemplates{
Label: "{{ . | bold }}",
Label: "{{ . | bold }}",
Active: "> {{ .OriginASN | cyan | bold }} {{ .Route | green }}",
Inactive: "{{ .OriginASN | faint }} {{ .Route | faint }}",
Selected: "> {{ .OriginASN | cyan | bold }} {{ .Route | green }}",
Details: `
--------- Autonomous System ----------
{{ " Name:" | faint }} {{ AS.OriginASN }}
{{ " Route:" | faint }} {{ AS.Route }}
{{ " Description:" | faint }} {{ AS.Description }}
{{ " Maintained By:" | faint }} {{ AS.MaintainedBy }}
{{ " Member Of:" | faint }} {{ AS.MemberOf }}
{{ " Notify:" | faint }} {{ AS.Notify }}
{{ " Source:" | faint }} {{ AS.Source }}`,
FuncMap: newFuncMap,
},
Searcher: func(input string, index int) bool {
aut, ok := asnMap[asnNames[index]]
if !ok {
return false
}
name := strings.Replace(strings.ToLower(aut.OriginASN), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
------- Autonomous System --------
{{ " ASN: " | faint }} {{ .OriginASN }}
{{ " Route: " | faint }} {{ printf "%s" .Route | green }}
{{ " Description: " | faint }} {{ printf "%+s" .Description | cyan }}
{{ " Maintained By: " | faint }} {{ .MaintainedBy }}
{{ " Member Of: " | faint }} {{ .MemberOf }}
{{ " Notify: " | faint }} {{ .Notify }}
{{ " Source: " | faint }} {{ .Source }}`,
},
Searcher: search,
StartInSearchMode: true,
}