forked from dizel-by/ripego
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.go
75 lines (55 loc) · 1.14 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
package ripego
import (
"bufio"
"io/ioutil"
"net"
"regexp"
"strings"
"time"
)
var (
rpslLinePattern = regexp.MustCompile(`(.+):\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) {
if len(sectionValue) > 0 {
sectionValue += "\n"
}
sectionValue += parseRPSLine(line)
}
}
}
return sectionValue
}
func parseRPSLine(whoisLine string) string {
s := rpslLinePattern.FindAllStringSubmatch(whoisLine, -1)
if len(s) >= 1 {
return s[0][2]
}
return ""
}