init
This commit is contained in:
commit
5e89a270a7
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
*.json
|
||||
*.db
|
||||
*.gz
|
15
go.mod
Normal file
15
go.mod
Normal file
@ -0,0 +1,15 @@
|
||||
module cinnanet
|
||||
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/manifoldco/promptui v0.9.0
|
||||
inet.af/netaddr v0.0.0-20220811202034-502d2d690317
|
||||
)
|
||||
|
||||
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
|
||||
)
|
149
main.go
Normal file
149
main.go
Normal file
@ -0,0 +1,149 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/manifoldco/promptui"
|
||||
"inet.af/netaddr"
|
||||
)
|
||||
|
||||
var (
|
||||
cufrd *bufio.Reader
|
||||
xerox *bufio.Scanner
|
||||
)
|
||||
|
||||
func init() {
|
||||
dbfile, err := os.OpenFile("ripe_cleaned.db", os.O_RDWR|os.O_CREATE, 0600)
|
||||
if err != nil {
|
||||
println(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
cufrd = bufio.NewReader(dbfile)
|
||||
xerox = bufio.NewScanner(cufrd)
|
||||
}
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
var asnMap = make(map[string]*Entry)
|
||||
|
||||
var (
|
||||
tmpMap = make(map[int]*Entry)
|
||||
|
||||
count = 0
|
||||
innerCount = 0
|
||||
)
|
||||
|
||||
func processLine(line string) {
|
||||
if len(strings.TrimSpace(line)) == 0 || count == 0 {
|
||||
if tmpMap[count] != nil && tmpMap[count].OriginASN != "" {
|
||||
asnMap[tmpMap[count].OriginASN] = tmpMap[count]
|
||||
}
|
||||
count++
|
||||
tmpMap[count] = &Entry{}
|
||||
innerCount = 0
|
||||
if count > 1 {
|
||||
return
|
||||
}
|
||||
}
|
||||
innerCount++
|
||||
lineFields := strings.Fields(line)
|
||||
switch {
|
||||
case len(lineFields) < 2:
|
||||
return
|
||||
case innerCount == 1 && !strings.HasPrefix(line, "route:"):
|
||||
return
|
||||
case innerCount == 1 && strings.HasPrefix(line, "route:"):
|
||||
route, err := netaddr.ParseIPPrefix(lineFields[1])
|
||||
if err != nil {
|
||||
println(err.Error())
|
||||
return
|
||||
}
|
||||
tmpMap[count].Route = route
|
||||
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, "mnt-by:"),
|
||||
strings.HasPrefix(line, "mnt-lower"),
|
||||
strings.HasPrefix(line, "mnt-routes"):
|
||||
tmpMap[count].MaintainedBy = append(tmpMap[count].MaintainedBy, lineFields[1])
|
||||
case strings.HasPrefix(line, "source:"):
|
||||
tmpMap[count].Source = lineFields[1]
|
||||
case strings.HasPrefix(line, "notify:"):
|
||||
tmpMap[count].Notify = append(tmpMap[count].Notify, lineFields[1])
|
||||
case strings.HasPrefix(line, "member-of:"):
|
||||
tmpMap[count].MemberOf = append(tmpMap[count].MemberOf, lineFields[1])
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
selector := promptui.Select{
|
||||
Label: "Known ASNs",
|
||||
Items: asnNames,
|
||||
Size: 25,
|
||||
CursorPos: 0,
|
||||
Templates: &promptui.SelectTemplates{
|
||||
Label: "{{ . | bold }}",
|
||||
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)
|
||||
},
|
||||
StartInSearchMode: true,
|
||||
}
|
||||
|
||||
_, _, err := selector.Run()
|
||||
if err != nil {
|
||||
println(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user