-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
97 lines (76 loc) · 1.59 KB
/
main.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
package main
import (
"bufio"
"fmt"
"io"
"net"
"os"
"strings"
"sync"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
var wg sync.WaitGroup
concurrency := 10
semaphore := make(chan struct{}, concurrency)
for scanner.Scan() {
ip := strings.TrimSpace(scanner.Text())
if net.ParseIP(ip) == nil {
fmt.Fprintf(os.Stderr, "%s is not a valid IP address\n", ip)
continue
}
wg.Add(1)
semaphore <- struct{}{}
go func(ip string) {
defer func() {
<-semaphore
wg.Done()
}()
asn, err := lookupASN(ip)
if err != nil {
fmt.Fprintf(os.Stderr, "Error looking up ASN for %s: %s\n", ip, err)
return
}
if asn == "NA" {
return
}
fmt.Println(asn)
}(ip)
}
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Error reading input: %s\n", err)
os.Exit(1)
}
wg.Wait()
}
func lookupASN(ip string) (string, error) {
conn, err := net.Dial("tcp", "whois.cymru.com:43")
if err != nil {
return "", err
}
defer conn.Close()
fmt.Fprintf(conn, "-r %s\n", ip)
var buf strings.Builder
if _, err := io.Copy(&buf, conn); err != nil {
return "", err
}
scanner := bufio.NewScanner(strings.NewReader(buf.String()))
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "AS") {
continue
}
return extractNumber(line)
}
if err := scanner.Err(); err != nil {
return "", err
}
return "", fmt.Errorf("ASN not found for %s", ip)
}
func extractNumber(s string) (string, error) {
fields := strings.Fields(s)
if len(fields) == 0 {
return "", fmt.Errorf("invalid string: %s", s)
}
return fields[0], nil
}