-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (53 loc) · 1.33 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
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
)
func getToken(url string) string {
var body struct {
Token string `json:"token"`
}
resp, _ := http.Get(url)
defer resp.Body.Close()
err := json.NewDecoder(resp.Body).Decode(&body)
if err != nil {
panic(err)
}
return body.Token
}
func getCurrentLimit(token string) (int, error) {
url := "https://registry-1.docker.io/v2/ratelimitpreview/test/manifests/latest"
req, err := http.NewRequest("HEAD", url, nil)
if err != nil {
return -1, err
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return -1, err
}
if resp.StatusCode != http.StatusOK {
return -1, fmt.Errorf("wron response: %v", resp.StatusCode)
}
rateLimit := resp.Header.Get("RateLimit-Remaining")
if rateLimit == "" {
return -1, fmt.Errorf("failed to get RateLimit-Remaining")
}
rateLimitRemain, err := strconv.Atoi(strings.Split(rateLimit, ";")[0])
if err != nil {
return -1, fmt.Errorf("failed to parse RateLimit-Remaining")
}
return rateLimitRemain, nil
}
func main() {
url := "https://auth.docker.io/token?service=registry.docker.io&scope=repository:ratelimitpreview/test:pull"
remain, err := getCurrentLimit(getToken(url))
if err != nil {
panic(err)
}
fmt.Println(remain)
}