🎨 Clean code Improve network image download to the local

This commit is contained in:
Daniel 2025-05-29 18:12:29 +08:00
parent f535a13372
commit 082e4a9ca9
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
2 changed files with 12 additions and 43 deletions

View file

@ -100,8 +100,7 @@ func NetAssets2LocalAssets(rootID string, onlyImg bool, originalURL string) (err
} }
} }
browserClient := util.CreateCustomReqClient(). browserClient := util.NewCustomReqClient() // 自定义了 TLS 指纹,增加下载成功率
EnableInsecureSkipVerify() // 添加了自定义TLS指纹, 可以完成对于CDN的资源下载
forbiddenCount := 0 forbiddenCount := 0
destNodes := getRemoteAssetsLinkDestsInTree(tree, onlyImg) destNodes := getRemoteAssetsLinkDestsInTree(tree, onlyImg)

View file

@ -18,18 +18,26 @@ package util
import ( import (
"crypto/tls" "crypto/tls"
"net"
"net/http"
"time" "time"
"github.com/imroc/req/v3" "github.com/imroc/req/v3"
"github.com/siyuan-note/httpclient" "github.com/siyuan-note/httpclient"
) )
// NewCustomReqClient 创建自定义 req 客户端
func NewCustomReqClient() *req.Client {
client := req.C().
SetTLSClientConfig(createCustomTLSConfig()).
SetUserAgent(UserAgent).
SetTimeout(30 * time.Second).
SetProxy(httpclient.ProxyFromEnvironment)
return client
}
// createCustomTLSConfig 创建自定义 TLS 配置 // createCustomTLSConfig 创建自定义 TLS 配置
func createCustomTLSConfig() *tls.Config { func createCustomTLSConfig() *tls.Config {
return &tls.Config{ return &tls.Config{
InsecureSkipVerify: false, InsecureSkipVerify: true,
MinVersion: tls.VersionTLS12, MinVersion: tls.VersionTLS12,
MaxVersion: tls.VersionTLS13, MaxVersion: tls.VersionTLS13,
@ -53,41 +61,3 @@ func createCustomTLSConfig() *tls.Config {
}, },
} }
} }
// CreateCustomHTTPClient 创建自定义 HTTP 客户端
func CreateCustomHTTPClient() *http.Client {
dialer := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}
transport := &http.Transport{
TLSClientConfig: createCustomTLSConfig(),
DialContext: dialer.DialContext,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 10,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
ForceAttemptHTTP2: false,
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
}
return &http.Client{
Transport: transport,
Timeout: 60 * time.Second,
}
}
// CreateCustomReqClient 创建自定义 req 客户端
func CreateCustomReqClient() *req.Client {
client := req.C()
client.SetTLSClientConfig(createCustomTLSConfig())
client.SetUserAgent(UserAgent).
SetTimeout(30 * time.Second).
SetProxy(httpclient.ProxyFromEnvironment)
return client
}