mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 08:31:55 +08:00
增强请求头设置,添加浏览器User-Agent和Referer,以提高目标URL的可访问性和兼容性。
This commit is contained in:
parent
1aed50444e
commit
11378a7e0c
@ -222,6 +222,12 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
// 复制并处理请求头
|
// 复制并处理请求头
|
||||||
copyHeader(proxyReq.Header, r.Header)
|
copyHeader(proxyReq.Header, r.Header)
|
||||||
|
|
||||||
|
// 添加常见浏览器User-Agent
|
||||||
|
proxyReq.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
|
||||||
|
|
||||||
|
// 设置Referer为源站的host
|
||||||
|
proxyReq.Header.Set("Referer", fmt.Sprintf("%s://%s", parsedURL.Scheme, parsedURL.Host))
|
||||||
|
|
||||||
// 特别处理图片请求
|
// 特别处理图片请求
|
||||||
if utils.IsImageRequest(r.URL.Path) {
|
if utils.IsImageRequest(r.URL.Path) {
|
||||||
// 获取 Accept 头
|
// 获取 Accept 头
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
neturl "net/url"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"proxy-go/internal/config"
|
"proxy-go/internal/config"
|
||||||
"slices"
|
"slices"
|
||||||
@ -337,36 +338,44 @@ func GetTargetURL(client *http.Client, r *http.Request, pathConfig config.PathCo
|
|||||||
}
|
}
|
||||||
|
|
||||||
// isTargetAccessible 检查目标URL是否可访问
|
// isTargetAccessible 检查目标URL是否可访问
|
||||||
func isTargetAccessible(client *http.Client, url string) bool {
|
func isTargetAccessible(client *http.Client, targetURL string) bool {
|
||||||
// 先查缓存
|
// 先查缓存
|
||||||
if cache, ok := accessCache.Load(url); ok {
|
if cache, ok := accessCache.Load(targetURL); ok {
|
||||||
cacheItem := cache.(accessibilityCache)
|
cacheItem := cache.(accessibilityCache)
|
||||||
if time.Since(cacheItem.timestamp) < accessTTL {
|
if time.Since(cacheItem.timestamp) < accessTTL {
|
||||||
return cacheItem.accessible
|
return cacheItem.accessible
|
||||||
}
|
}
|
||||||
accessCache.Delete(url)
|
accessCache.Delete(targetURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest("HEAD", url, nil)
|
req, err := http.NewRequest("HEAD", targetURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[Check] Failed to create request for %s: %v", url, err)
|
log.Printf("[Check] Failed to create request for %s: %v", targetURL, err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 添加浏览器User-Agent
|
||||||
|
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
|
||||||
|
|
||||||
|
// 设置Referer为目标域名
|
||||||
|
if parsedURL, parseErr := neturl.Parse(targetURL); parseErr == nil {
|
||||||
|
req.Header.Set("Referer", fmt.Sprintf("%s://%s", parsedURL.Scheme, parsedURL.Host))
|
||||||
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
req = req.WithContext(ctx)
|
req = req.WithContext(ctx)
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[Check] Failed to access %s: %v", url, err)
|
log.Printf("[Check] Failed to access %s: %v", targetURL, err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
accessible := resp.StatusCode >= 200 && resp.StatusCode < 400
|
accessible := resp.StatusCode >= 200 && resp.StatusCode < 400
|
||||||
// 缓存结果
|
// 缓存结果
|
||||||
accessCache.Store(url, accessibilityCache{
|
accessCache.Store(targetURL, accessibilityCache{
|
||||||
accessible: accessible,
|
accessible: accessible,
|
||||||
timestamp: time.Now(),
|
timestamp: time.Now(),
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user