diff --git a/proxy/rewriter.go b/proxy/rewriter.go index 76755a1..69e7228 100644 --- a/proxy/rewriter.go +++ b/proxy/rewriter.go @@ -10,9 +10,10 @@ import ( type ContentRewriter struct { baseURL *url.URL + token string } -func NewContentRewriter(baseURL string) (*ContentRewriter, error) { +func NewContentRewriter(baseURL, token string) (*ContentRewriter, error) { u, err := url.Parse(baseURL) if err != nil { return nil, err @@ -20,14 +21,13 @@ func NewContentRewriter(baseURL string) (*ContentRewriter, error) { return &ContentRewriter{ baseURL: u, + token: token, }, nil } -// RewriteHTML 重写 HTML 内容中的所有 URL func (r *ContentRewriter) RewriteHTML(body []byte) ([]byte, error) { doc, err := html.Parse(bytes.NewReader(body)) if err != nil { - // 如果解析失败,使用简单的字符串替换 return r.simpleRewriteHTML(body), nil } @@ -41,16 +41,9 @@ func (r *ContentRewriter) RewriteHTML(body []byte) ([]byte, error) { return buf.Bytes(), nil } -// rewriteNode 递归重写 HTML 节点 func (r *ContentRewriter) rewriteNode(n *html.Node) { if n.Type == html.ElementNode { - // 重写需要处理的属性 - attrs := map[string]bool{ - "href": true, - "src": true, - "action": true, - "data": true, - } + attrs := map[string]bool{"href": true, "src": true, "action": true, "data": true} for i, attr := range n.Attr { if attrs[attr.Key] { @@ -59,31 +52,26 @@ func (r *ContentRewriter) rewriteNode(n *html.Node) { } } - // 处理 srcset 属性 if attr.Key == "srcset" { n.Attr[i].Val = r.rewriteSrcset(attr.Val) } - // 处理 style 属性中的 URL if attr.Key == "style" { n.Attr[i].Val = r.rewriteInlineCSS(attr.Val) } } - // 处理