-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
174 lines (139 loc) · 3.92 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"encoding/json"
"flag"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
"golang.org/x/net/html"
)
type AzureIpRange struct {
ChangeNumber int `json:"changeNumber"`
Cloud string `json:"cloud"`
Values []Value `json:"values"`
}
type Value struct {
Name string `json:"name"`
ID string `json:"id"`
Properties Properties `json:"properties"`
}
type Properties struct {
ChangeNumber int `json:"changeNumber"`
Region string `json:"region"`
RegionID int `json:"regionId"`
Platform string `json:"platform"`
SystemService string `json:"systemService"`
AddressPrefixes []string `json:"addressPrefixes"`
NetworkFeatures []string `json:"networkFeatures"`
}
func outputFilename(cWeek int, cYear int, region string, service string) *string {
filename := ""
filename += "ip-ranges-w"
filename += strconv.Itoa(cWeek)
filename += "y"
filename += strconv.Itoa(cYear)
filename += "-"
if region == "" {
region = "no-region"
}
if service == "" {
service = "no-service"
}
filename += region
filename += "-"
filename += service
filename += ".txt"
return &filename
}
func writeToFile(content string, f os.File) {
f.WriteString(content)
}
// This function removes the subnet mask from the IPv4 Address
func formatIpv4(ip string) string {
return (strings.Split(ip, "/")[0] + "\n")
}
func matchSystemFilter(filter string) {
}
func main() {
// Parse command line arguments
region := flag.String("region", "", "filter on Azure region")
service := flag.String("service", "", "filter on Azure service")
flag.Parse()
tn := time.Now().UTC()
currentYear, currentWeek := tn.ISOWeek()
url := "https://www.microsoft.com/en-us/download/confirmation.aspx?id=56519"
filename := outputFilename(currentWeek, currentYear, *region, *service)
file, err := os.Create(*filename)
if err != nil {
panic(err)
}
defer file.Close()
var client http.Client
resp, err := client.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
// Parse HTTP response to HTML object
doc, err := html.Parse(strings.NewReader(string(bodyBytes)))
var f func(*html.Node)
f = func(n *html.Node) {
// Search in HTML Object for all <a> tags
if n.Type == html.ElementNode && n.Data == "a" {
for _, a := range n.Attr {
// Fetch the href attribute from all <a> tags
if a.Key == "href" && strings.Contains(a.Val, "ServiceTags_Public") {
// Make HTTP request to the href value of the <a>
res, err := http.Get(a.Val)
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err.Error())
}
//Write output to a JSON File
err = ioutil.WriteFile("AzurePublicIp.json", body, 0644)
fileContent, err := os.Open("./AzurePublicIp.json")
if err != nil {
log.Fatal(err)
return
}
defer fileContent.Close()
byteResult, _ := ioutil.ReadAll(fileContent)
var ipRanges AzureIpRange
json.Unmarshal([]byte(byteResult), &ipRanges)
for i := 0; i < len(ipRanges.Values); i++ {
// Only add values that match our region filter
if ipRanges.Values[i].Properties.Region == *region {
for j := 0; j < len(ipRanges.Values[i].Properties.AddressPrefixes); j++ {
if *service != "" {
if ipRanges.Values[i].Properties.SystemService == *service {
// Create file content string
writeToFile(formatIpv4(ipRanges.Values[i].Properties.AddressPrefixes[j]), *file)
}
} else {
// Create file content string
writeToFile(formatIpv4(ipRanges.Values[i].Properties.AddressPrefixes[j]), *file)
}
}
}
}
break
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(doc)
}
}