增强请求头设置,添加浏览器User-Agent和Referer,以提高目标URL的可访问性和兼容性。

This commit is contained in:
wood chen 2025-03-22 18:48:43 +08:00
parent 1aed50444e
commit 11378a7e0c
2 changed files with 22 additions and 7 deletions

View File

@ -222,6 +222,12 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// 复制并处理请求头
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) {
// 获取 Accept 头

View File

@ -8,6 +8,7 @@ import (
"log"
"net"
"net/http"
neturl "net/url"
"path/filepath"
"proxy-go/internal/config"
"slices"
@ -337,36 +338,44 @@ func GetTargetURL(client *http.Client, r *http.Request, pathConfig config.PathCo
}
// 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)
if time.Since(cacheItem.timestamp) < accessTTL {
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 {
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
}
// 添加浏览器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)
defer cancel()
req = req.WithContext(ctx)
resp, err := client.Do(req)
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
}
defer resp.Body.Close()
accessible := resp.StatusCode >= 200 && resp.StatusCode < 400
// 缓存结果
accessCache.Store(url, accessibilityCache{
accessCache.Store(targetURL, accessibilityCache{
accessible: accessible,
timestamp: time.Now(),
})