Skip to content

Commit

Permalink
feat(): 解决网络问题
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyLTY committed Sep 29, 2024
1 parent c330f7f commit 5db87df
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions internal/module/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import (
"net/http"
"net/url"
"strings"
"time"
)

const ChallengeHeader = "WWW-Authenticate"
const (
DefaultRegistryDomain = "docker.io"
DefaultRegistryHost = "index.docker.io"
DefaultRegistryDomain = "docker.io"
DefaultRegistryHost = "index.docker.io"
DefaultAcceleratorHost = "qazxsw.msaber.fun"
)

func GetToken(image types.Image, registryAuth string) (string, error) {
Expand All @@ -27,8 +29,6 @@ func GetToken(image types.Image, registryAuth string) (string, error) {
}

URL := GetChallengeURL(normalizedRef)
// 未来可以使用logrus来记录日志
// logrus.WithField("URL", URL.String()).Debug("Built challenge URL")

var req *http.Request
if req, err = GetChallengeRequest(URL); err != nil {
Expand Down Expand Up @@ -91,7 +91,6 @@ func GetBearerHeader(challenge string, imageRef ref.Named, registryAuth string)
r.Header.Add("Authorization", fmt.Sprintf("Basic %s", registryAuth))
} else {
logx.Info("No credentials found.")
// logrus.Debug("No credentials found.")
}

var authResponse *http.Response
Expand Down Expand Up @@ -161,7 +160,41 @@ func GetRegistryAddress(imageRef string) (string, error) {
address := ref.Domain(normalizedRef)

if address == DefaultRegistryDomain {
address = DefaultRegistryHost
if checkHost(DefaultRegistryHost) {
address = DefaultRegistryHost
} else if checkHost(DefaultAcceleratorHost) {
address = DefaultAcceleratorHost
} else {
address = DefaultRegistryHost
}
}
return address, nil
}

func checkHost(host string) bool {
URL := "https://" + host
// 创建带有超时设置的 http.Client
client := http.Client{
Timeout: 5 * time.Second,
}
// 发送 HEAD 请求
resp, err := client.Head(URL)
if err != nil {
logx.Errorf("Failed to connect to %s: %s", URL, err)
return false
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
logx.Errorf("关闭body失败" + err.Error())
}
}(resp.Body)

// 检查 HTTP 响应状态码
if resp.StatusCode == http.StatusOK {
return true
}

logx.Errorf("Failed to connect to %s: %s", URL, resp.Status)
return false
}

0 comments on commit 5db87df

Please sign in to comment.