Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Clean code Improve network image download to the local

Daniel082e4a9ca9d0
diff --git a/kernel/model/assets.go b/kernel/model/assets.go
index 90d41d641d..2cba74ac01 100644
--- a/kernel/model/assets.go
+++ b/kernel/model/assets.go
@@ -100,8 +100,7 @@ func NetAssets2LocalAssets(rootID string, onlyImg bool, originalURL string) (err
}
}
- browserClient := util.CreateCustomReqClient().
- EnableInsecureSkipVerify() // 添加了自定义TLS指纹, 可以完成对于CDN的资源下载
+ browserClient := util.NewCustomReqClient() // 自定义了 TLS 指纹,增加下载成功率
forbiddenCount := 0
destNodes := getRemoteAssetsLinkDestsInTree(tree, onlyImg)
diff --git a/kernel/util/tls.go b/kernel/util/tls.go
index 8bd338f76e..24fd9997b9 100644
--- a/kernel/util/tls.go
+++ b/kernel/util/tls.go
@@ -18,18 +18,26 @@ package util
import (
"crypto/tls"
- "net"
- "net/http"
"time"
"github.com/imroc/req/v3"
"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 配置
func createCustomTLSConfig() *tls.Config {
return &tls.Config{
- InsecureSkipVerify: false,
+ InsecureSkipVerify: true,
MinVersion: tls.VersionTLS12,
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
-}