-
Notifications
You must be signed in to change notification settings - Fork 20
/
helper.go
105 lines (76 loc) · 1.56 KB
/
helper.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
package ripego
import (
"bufio"
"io/ioutil"
"net"
"regexp"
"strings"
"time"
)
const (
rpsl_line_pattern = `(.+):\W+(.+)`
)
func getTcpContent(search string, host string) (s string, err error) {
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, "43"), time.Second*28)
defer conn.Close()
if err != nil {
return s, err
}
conn.Write([]byte(search + "\r\n"))
buffer, err := ioutil.ReadAll(conn)
if err != nil {
return s, err
}
s = string(buffer[:])
return s, err
}
func parseRPSLValue(whoisText string, class string, section string) string {
var sectionValue = ""
var hasIn = false
sc := bufio.NewScanner(strings.NewReader(whoisText))
for sc.Scan() {
var line = sc.Text()
if strings.HasPrefix(line, class) {
if hasIn == false {
hasIn = true
}
}
if hasIn {
if strings.HasPrefix(line, section) {
sectionValue = parseRPSLine(line)
break
}
}
}
return sectionValue
}
func parseRPSLine(whoisLine string) string {
rx, _ := regexp.Compile(rpsl_line_pattern)
s := rx.FindAllStringSubmatch(whoisLine, -1)
if len(s) >= 1 {
return s[0][2]
}
return ""
}
func isProviderIP(ipaddr string, ips []string) bool {
hasip := false
octet := firstOctec(ipaddr)
for i := range ips {
if octet == ips[i] {
hasip = true
break
}
}
return hasip
}
func firstOctec(ipaddr string) string {
return strings.Split(ipaddr, ".")[0]
}
func isValidIp(ipaddr string) bool {
ip := net.ParseIP(ipaddr)
return ip.To4() != nil
}
func isValidIPv6(ipaddr string) bool {
ip := net.ParseIP(ipaddr)
return ip.To16() != nil
}