Skip to content

Commit 2900589

Browse files
committed
优化获取网站图标
1 parent 6e9c5f0 commit 2900589

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

main.go

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -659,29 +659,63 @@ func getIconHandler(w http.ResponseWriter, r *http.Request) {
659659
}
660660

661661
iconURL := fmt.Sprintf("%s://%s/favicon.ico", parsedURL.Scheme, parsedURL.Host)
662-
resp, err := http.Get(iconURL)
663-
log.Printf("Fetching icon from URL:%s", iconURL)
664-
if err != nil || resp.StatusCode != http.StatusOK ||
665-
resp.Header.Get("Content-Type") != "image/x-icon" ||
666-
resp.Header.Get("Content-Type") != "image/vnd.microsoft.icon" {
662+
// Create HTTP client and request
663+
tr := &http.Transport{
664+
TLSHandshakeTimeout: 10 * time.Second,
665+
DisableKeepAlives: true,
666+
ForceAttemptHTTP2: true,
667+
}
668+
client := &http.Client{Transport: tr}
669+
req, err := http.NewRequest("GET", iconURL, nil)
670+
if err != nil {
671+
fmt.Println("Error creating request:", err)
672+
return
673+
}
674+
675+
// Set headers to mimic Chrome
676+
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36")
677+
req.Header.Set("Accept", "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8")
678+
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
679+
680+
// Perform the HTTP GET request
681+
resp, err := client.Do(req)
682+
if err != nil {
683+
fmt.Printf("Error performing GET request:%+v\n", err)
684+
return
685+
}
686+
defer resp.Body.Close()
667687

668-
log.Printf("Try fetching icon from HTML URL:%s code:%d Content-Type:%s", iconURL, resp.StatusCode, resp.Header.Get("Content-Type"))
688+
finalURL := resp.Request.URL.String() // 获取跳转后的真实 URL
689+
log.Printf("Fetched icon from: %s", finalURL)
690+
691+
contentType := resp.Header.Get("Content-Type")
692+
if resp.StatusCode != http.StatusOK || !strings.HasPrefix(contentType, "image/") {
693+
// fallback:从 HTML 中获取图标
694+
log.Printf("Fallback to HTML icon parsing, code:%d Content-Type:%s", resp.StatusCode, contentType)
669695
// 尝试解析 HTML 来获取图标
670696
iconURL, err = fetchIconFromHTML(urlParam)
671697
if err != nil {
672698
log.Printf("Failed to fetch icon: %v", err)
673699
http.Error(w, "Failed to fetch icon", http.StatusInternalServerError)
674700
return
675701
}
676-
log.Printf("Fetching icon from html URL:%s", iconURL)
677-
resp, err = http.Get(iconURL)
702+
703+
log.Printf("Fetching icon from HTML URL: %s", iconURL)
704+
req, _ = http.NewRequest("GET", iconURL, nil)
705+
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0...)")
706+
req.Header.Set("Accept", "image/*,*/*;q=0.8")
707+
resp, err = client.Do(req)
678708
if err != nil || resp.StatusCode != http.StatusOK {
679709
log.Printf("Failed to fetch icon from HTML: %v", err)
680710
http.Error(w, "Failed to fetch icon", http.StatusInternalServerError)
681711
return
682712
}
713+
defer resp.Body.Close()
714+
contentType = resp.Header.Get("Content-Type")
715+
if contentType == "" {
716+
contentType = "image/x-icon"
717+
}
683718
}
684-
defer resp.Body.Close()
685719

686720
// 读取图标数据
687721
iconData, err := io.ReadAll(resp.Body)
@@ -694,12 +728,6 @@ func getIconHandler(w http.ResponseWriter, r *http.Request) {
694728
// 将图标数据转换为base64编码
695729
base64Data := base64.StdEncoding.EncodeToString(iconData)
696730

697-
// 获取Content-Type
698-
contentType := resp.Header.Get("Content-Type")
699-
if contentType == "" {
700-
contentType = "image/x-icon" // 默认Content-Type
701-
}
702-
703731
// 返回base64编码的图标数据
704732
iconResponse := map[string]string{
705733
"iconData": fmt.Sprintf("data:%s;base64,%s", contentType, base64Data),

0 commit comments

Comments
 (0)