package favicon import ( "bytes" "context" "errors" "io" "net/http" "net/url" "strings" "time" "github.com/PuerkitoBio/goquery" "github.com/vincent-petithory/dataurl" "github.com/yusing/go-proxy/internal/gperr" "github.com/yusing/go-proxy/internal/homepage" "github.com/yusing/go-proxy/internal/logging" gphttp "github.com/yusing/go-proxy/internal/net/gphttp" "github.com/yusing/go-proxy/internal/route/routes" route "github.com/yusing/go-proxy/internal/route/types" "github.com/yusing/go-proxy/internal/utils/strutils" ) type fetchResult struct { icon []byte contentType string statusCode int errMsg string } func (res *fetchResult) OK() bool { return res.icon != nil } func (res *fetchResult) ContentType() string { if res.contentType == "" { if bytes.HasPrefix(res.icon, []byte(" MaxRedirectDepth { return &fetchResult{statusCode: http.StatusBadGateway, errMsg: "too many redirects"} } loc = strutils.SanitizeURI(loc) if loc == "/" || loc == newReq.URL.Path { return &fetchResult{statusCode: http.StatusBadGateway, errMsg: "circular redirect"} } return findIconSlow(r, req, loc, depth+1) } } return &fetchResult{statusCode: c.status, errMsg: "upstream error: " + string(c.data)} } // return icon data if !gphttp.GetContentType(c.header).IsHTML() { return &fetchResult{icon: c.data, contentType: c.header.Get("Content-Type")} } // try extract from "link[rel=icon]" from path "/" doc, err := goquery.NewDocumentFromReader(bytes.NewBuffer(c.data)) if err != nil { logging.Error().Err(err). Str("route", r.TargetName()). Msg("failed to parse html") return &fetchResult{statusCode: http.StatusInternalServerError, errMsg: "internal error"} } ele := doc.Find("head > link[rel=icon]").First() if ele.Length() == 0 { return &fetchResult{statusCode: http.StatusNotFound, errMsg: "icon element not found"} } href := ele.AttrOr("href", "") if href == "" { return &fetchResult{statusCode: http.StatusNotFound, errMsg: "icon href not found"} } // https://en.wikipedia.org/wiki/Data_URI_scheme if strings.HasPrefix(href, "data:image/") { dataURI, err := dataurl.DecodeString(href) if err != nil { logging.Error().Err(err). Str("route", r.TargetName()). Msg("failed to decode favicon") return &fetchResult{statusCode: http.StatusInternalServerError, errMsg: "internal error"} } return &fetchResult{icon: dataURI.Data, contentType: dataURI.ContentType()} } switch { case strings.HasPrefix(href, "http://"), strings.HasPrefix(href, "https://"): return fetchIconAbsolute(href) default: return findIconSlow(r, req, href, 0) } }