forked from RedMapleTech/vpn-split-tunneling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
194 lines (155 loc) · 4.32 KB
/
github.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
)
const (
githubURL = "https://api.github.com/meta"
)
type GitHubData struct {
VerifiablePasswordAuthentication bool `json:"verifiable_password_authentication"`
SSHKeyFingerprints struct {
SHA256ECDSA string `json:"SHA256_ECDSA"`
SHA256ED25519 string `json:"SHA256_ED25519"`
SHA256RSA string `json:"SHA256_RSA"`
} `json:"ssh_key_fingerprints"`
SSHKeys []string `json:"ssh_keys"`
Hooks []string `json:"hooks"`
Web []string `json:"web"`
API []string `json:"api"`
Git []string `json:"git"`
GithubEnterpriseImporter []string `json:"github_enterprise_importer"`
Packages []string `json:"packages"`
Pages []string `json:"pages"`
Importer []string `json:"importer"`
Actions []string `json:"actions"`
ActionsMacos []string `json:"actions_macos"`
Codespaces []string `json:"codespaces"`
Dependabot []string `json:"dependabot"`
Copilot []string `json:"copilot"`
Domains struct {
Website []string `json:"website"`
Codespaces []string `json:"codespaces"`
Copilot []string `json:"copilot"`
Packages []string `json:"packages"`
Actions []string `json:"actions"`
ArtifactAttestations struct {
TrustDomain string `json:"trust_domain"`
Services []string `json:"services"`
} `json:"artifact_attestations"`
} `json:"domains"`
}
func getGitHubIPs() (map[string]bool, error) {
// get the data from the endpoint
data, err := getGitHubData()
if err != nil {
log.Fatalf("Failed to get data: %s\n", err.Error())
}
// parse the received data
log.Println("Parsing data")
ips, err := parseGitHubData(data)
return ips, err
}
func getGitHubData() ([]byte, error) {
// prep the URL
log.Printf("Getting data from %s\n", githubURL)
// get the data
resp, err := http.Get(githubURL)
if err != nil {
return nil, err
}
// read the data from the response body
var content []byte
content, err = io.ReadAll(resp.Body)
resp.Body.Close()
if len(content) == 0 {
return nil, fmt.Errorf("failed to get any data")
}
return content, err
}
func parseGitHubData(data []byte) (map[string]bool, error) {
// unmarshal the JSON data
var parsed GitHubData
err := json.Unmarshal(data, &parsed)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal JSON: %q", err.Error())
}
// map to store all unique IPs
ips := make(map[string]bool)
// for all the groups in the struct
for _, ip := range parsed.Hooks {
address, _, err := net.ParseCIDR(ip)
if err == nil {
// add it to the map if we don't have it already
_, ok := ips[address.String()]
if !ok {
ips[address.String()] = true
}
}
}
for _, ip := range parsed.API {
address, _, err := net.ParseCIDR(ip)
if err == nil {
// add it to the map if we don't have it already
_, ok := ips[address.String()]
if !ok {
ips[address.String()] = true
}
}
}
for _, ip := range parsed.Git {
address, _, err := net.ParseCIDR(ip)
if err == nil {
// add it to the map if we don't have it already
_, ok := ips[address.String()]
if !ok {
ips[address.String()] = true
}
}
}
for _, ip := range parsed.Pages {
address, _, err := net.ParseCIDR(ip)
if err == nil {
// add it to the map if we don't have it already
_, ok := ips[address.String()]
if !ok {
ips[address.String()] = true
}
}
}
for _, ip := range parsed.Codespaces {
address, _, err := net.ParseCIDR(ip)
if err == nil {
// add it to the map if we don't have it already
_, ok := ips[address.String()]
if !ok {
ips[address.String()] = true
}
}
}
for _, ip := range parsed.Copilot {
address, _, err := net.ParseCIDR(ip)
if err == nil {
// add it to the map if we don't have it already
_, ok := ips[address.String()]
if !ok {
ips[address.String()] = true
}
}
}
for _, ip := range parsed.Dependabot {
address, _, err := net.ParseCIDR(ip)
if err == nil {
// add it to the map if we don't have it already
_, ok := ips[address.String()]
if !ok {
ips[address.String()] = true
}
}
}
return ips, nil
}