-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
120 lines (104 loc) · 2.5 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"regexp"
"update-roa/env"
"update-roa/s3"
)
func init() {
env.Init()
s3.Init()
}
func main() {
ReadPerline()
for _, object := range ObjectList {
prefixList := BGPQ3OutPut(object)
//name: object-v4.juniper.update
//name: object-v6.juniper.update
//upload to minio
//string to file
v4File := StrToTempFile(prefixList.IPv4List, prefixList.IPv4PrefixName+".juniper.update")
v6File := StrToTempFile(prefixList.IPv6List, prefixList.IPv6PrefixName+".juniper.update")
//upload to minio
file := &s3.File{
Name: prefixList.IPv4PrefixName + ".juniper.update",
Path: v4File.Name(),
}
file.Upload()
file = &s3.File{
Name: prefixList.IPv6PrefixName + ".juniper.update",
Path: v6File.Name(),
}
file.Upload()
}
}
var ObjectList []string
type PrefixList struct {
IPv4PrefixName string
IPv6PrefixName string
IPv4List string
IPv6List string
}
func ReadPerline() {
//read ./update.list
file, err := os.Open("./update.lists")
if err != nil {
panic(err)
}
defer file.Close()
//read file line by line
scanner := bufio.NewScanner(file)
for scanner.Scan() {
ObjectList = append(ObjectList, scanner.Text())
}
}
func BGPQ3OutPut(object string) PrefixList {
//exec bgpq3
var prefixList PrefixList
if IsASN(object) {
prefixList.IPv4PrefixName = asntoasset(object) + "-v4"
prefixList.IPv6PrefixName = asntoasset(object) + "-v6"
} else {
prefixList.IPv4PrefixName = object + "-v4"
prefixList.IPv6PrefixName = object + "-v6"
}
v4Cmd, err := exec.Command("bgpq3", "-4", "-J", "-l", prefixList.IPv4PrefixName, object).Output()
if err != nil {
fmt.Println(err)
}
v6Cmd, err := exec.Command("bgpq3", "-6", "-J", "-l", prefixList.IPv6PrefixName, object).Output()
if err != nil {
fmt.Println(err)
}
prefixList.IPv4List = string(v4Cmd)
prefixList.IPv6List = string(v6Cmd)
return prefixList
}
func StrToTempFile(str string, name string) *os.File {
//create temp file
tmpFile, err := os.CreateTemp("", name)
if err != nil {
panic(err)
}
//write string to temp file
_, err = tmpFile.WriteString(str)
if err != nil {
panic(err)
}
return tmpFile
}
func asntoasset(asn string) string {
//if string was like as1234 convert to as-1234
asnRegex := "as([0-9]{1,6})"
re := regexp.MustCompile(asnRegex)
return re.ReplaceAllString(asn, "as-$1")
}
func IsASN(asn string) bool {
//if string was like as1234 convert to as-1234
asnRegex := "as([0-9]{1,6})"
re := regexp.MustCompile(asnRegex)
return re.MatchString(asn)
}