-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathfetch.go
More file actions
97 lines (78 loc) · 1.88 KB
/
fetch.go
File metadata and controls
97 lines (78 loc) · 1.88 KB
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
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
)
type Release struct {
Assets []Asset `json:"assets"`
}
type Asset struct {
Name string `json:"name"`
BrowserDownloadURL string `json:"browser_download_url"`
}
func githubTlsTransporter(sni string) *http.Client {
tr := http.Transport{
TLSClientConfig: &tls.Config{ServerName: sni, NextProtos: []string{"h2", "http/1.1"}},
Protocols: &http.Protocols{},
}
tr.Protocols.SetHTTP1(true)
tr.Protocols.SetHTTP2(true)
return &http.Client{
Transport: &tr,
}
}
func GithubAPI(api string, fileName string, saveAs string) error {
apiUrl, parseUrlErr := url.Parse(api)
if parseUrlErr != nil {
return parseUrlErr
}
client := githubTlsTransporter(apiUrl.Host)
resp, respErr := client.Get(api)
if respErr != nil {
return respErr
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("response status code: %d", resp.StatusCode)
}
respBuffer, readErr := io.ReadAll(resp.Body)
if readErr != nil {
return readErr
}
var release Release
releaseErr := json.Unmarshal(respBuffer, &release)
if releaseErr != nil {
return releaseErr
}
for _, asset := range release.Assets {
if asset.Name == fileName {
browserUrl, parseUrlErr := url.Parse(asset.BrowserDownloadURL)
if parseUrlErr != nil {
return parseUrlErr
}
client := githubTlsTransporter(browserUrl.Host)
resp, respErr := client.Get(asset.BrowserDownloadURL)
if respErr != nil {
return respErr
}
defer resp.Body.Close()
ipv4Bytes, readErr := io.ReadAll(resp.Body)
if readErr != nil {
return readErr
}
ipv4File, ipv4FileErr := os.OpenFile(saveAs, os.O_WRONLY|os.O_CREATE, 0600)
if ipv4FileErr != nil {
return ipv4FileErr
}
ipv4File.Write(ipv4Bytes)
ipv4File.Close()
return nil
}
}
return fmt.Errorf("failed to get ipv4.txt")
}