-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASNg.go
147 lines (130 loc) · 3.71 KB
/
ASNg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"os"
"strings"
"regexp"
"github.com/miekg/dns"
"github.com/spf13/cobra"
"github.com/valyala/fasthttp"
)
type BGPViewData struct {
Data struct {
Prefixes []struct {
Prefix string `json:"prefix"`
ASN struct {
ASN int `json:"asn"`
} `json:"asn"`
} `json:"prefixes"`
} `json:"data"`
}
func isDomain(input string) bool {
domainPattern := `^([a-zA-Z0-9-]+\.){1,}[a-zA-Z]{2,}$`
match, _ := regexp.MatchString(domainPattern, input)
return match
}
func isIPAddress(input string) bool {
return net.ParseIP(input) != nil
}
func resolveDomain(domain string) ([]string, error) {
var IPs []string
m := new(dns.Msg)
m.SetQuestion(domain+".", dns.TypeA)
r, err := dns.Exchange(m, "8.8.8.8:53")
if err != nil {
return nil, err
}
for _, answer := range r.Answer {
if a, ok := answer.(*dns.A); ok {
IPs = append(IPs, a.A.String())
}
}
return IPs, nil
}
func fetchBGPViewData(ip string) (*BGPViewData, error) {
url := fmt.Sprintf("https://api.bgpview.io/ip/%s", ip)
statusCode, resp, err := fasthttp.Get(nil, url)
if err != nil {
return nil, err
}
if statusCode != fasthttp.StatusOK {
return nil, fmt.Errorf("HTTP request failed with status code: %d", statusCode)
}
var data BGPViewData
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return &data, nil
}
func processBGPViewIP(ips []string, asn bool, prefix bool) {
for _, ip := range ips {
fmt.Printf("[+] %s:\n", ip)
data, err := fetchBGPViewData(ip)
if err != nil {
fmt.Println(err)
continue
}
for _, prefixData := range data.Data.Prefixes {
if asn {
fmt.Println(prefixData.ASN.ASN)
} else if prefix {
fmt.Println(prefixData.Prefix)
} else {
fmt.Printf("prefix: %s, ASN: %d\n", prefixData.Prefix, prefixData.ASN.ASN)
}
}
}
}
func main() {
var url, list string
var asn, prefix bool
cmd := &cobra.Command{
Use: "bgpview",
Short: "Process IPs or subdomains using BGPView API",
Run: func(cmd *cobra.Command, args []string) {
var urls []string
if url != "" {
urls = append(urls, url)
} else if list != "" {
content, err := ioutil.ReadFile(list)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
urls = strings.Split(strings.TrimSpace(string(content)), "\n")
}
for _, u := range urls {
u := strings.TrimSpace(u) // Trim any leading/trailing whitespace
if u == "" {
continue // Skip empty lines
}
if isDomain(u) {
IPs, err := resolveDomain(u)
if err != nil {
fmt.Println(err)
continue
}
processBGPViewIP(IPs, asn, prefix)
} else if isIPAddress(u) {
IPs := []string{u}
processBGPViewIP(IPs, asn, prefix)
} else {
fmt.Printf("[!] The '%s' is not my type :)\n", u)
continue
}
}
},
}
cmd.Flags().StringVarP(&url, "url", "u", "", "Single URL to check")
cmd.Flags().StringVarP(&list, "list", "l", "", "List of URLs to check")
cmd.Flags().BoolVarP(&asn, "asn", "a", false, "Only print ASNs of target IP")
cmd.Flags().BoolVarP(&prefix, "prefix", "p", false, "Only print prefixes of target IP")
if err := cmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}