@@ -277,6 +277,7 @@ func (p *ProxyDownloadStruct) ProxyWorker(req *http.Request) {
277277 newHeader [name ] = value
278278 }
279279 }
280+ newHeader ["Accept-Encoding" ] = []string {"identity" }
280281
281282 maxRetries := 5
282283 if startOffset < int64 (1048576 ) || (p .EndOffset - startOffset )/ p .EndOffset * 1000 < 2 {
@@ -287,7 +288,7 @@ func (p *ProxyDownloadStruct) ProxyWorker(req *http.Request) {
287288 var err error
288289 for retry := 0 ; retry < maxRetries ; retry ++ {
289290 resp , err = base .RestyClient .
290- SetTimeout (10 * time .Second ).
291+ SetTimeout (30 * time .Second ).
291292 SetRetryCount (1 ).
292293 SetCookieJar (p .CookieJar ).
293294 R ().
@@ -345,7 +346,12 @@ func (p *ProxyDownloadStruct) ProxyWorker(req *http.Request) {
345346
346347 // 接收数据
347348 if resp != nil {
348- chunk .put (resp .Body ())
349+ body := resp .Body ()
350+ expectedLen := int (chunk .endOffset - chunk .startOffset + 1 )
351+ if len (body ) != expectedLen {
352+ logrus .Warnf ("【警告】收到数据长度不匹配! 请求 range=%d-%d (预期 %d), 实际收到 %d bytes, Content-Range: %s" , chunk .startOffset , chunk .endOffset , expectedLen , len (body ), resp .Header ().Get ("Content-Range" ))
353+ }
354+ chunk .put (body )
349355 } else {
350356 logrus .Debugf ("Chunk range=%d-%d 无法获取数据,写入 nil 并停止调度新任务" , chunk .startOffset , chunk .endOffset )
351357 chunk .put (nil ) // 放入 nil 标记此 chunk 失败或结束
@@ -450,6 +456,8 @@ func handleGetMethod(w http.ResponseWriter, req *http.Request) {
450456 newHeader [name ] = value
451457 }
452458 }
459+ // 强制要求服务器不进行 gzip 压缩,否则可能导致分片数据大小不匹配
460+ newHeader ["Accept-Encoding" ] = []string {"identity" }
453461
454462 // 移除错误的URL参数追加逻辑,因为这会破坏原始URL
455463 // 代理服务应该直接使用解码后的完整URL,而不是修改它
@@ -551,27 +559,30 @@ func handleGetMethod(w http.ResponseWriter, req *http.Request) {
551559 }
552560
553561 contentType := responseHeaders .Get ("Content-Type" )
554- if contentType == "" {
555- if strings .HasSuffix (fileName , ".webm" ) {
562+ if contentType == "" || contentType == "application/octet-stream" {
563+ urlLower := strings .ToLower (url )
564+ if strings .HasSuffix (fileName , ".webm" ) || strings .Contains (urlLower , "fext=webm" ) || strings .Contains (urlLower , ".webm" ) {
556565 contentType = "video/webm"
557- } else if strings .HasSuffix (fileName , ".avi" ) {
566+ } else if strings .HasSuffix (fileName , ".avi" ) || strings . Contains ( urlLower , "fext=avi" ) || strings . Contains ( urlLower , ".avi" ) {
558567 contentType = "video/x-msvideo"
559- } else if strings .HasSuffix (fileName , ".wmv" ) {
568+ } else if strings .HasSuffix (fileName , ".wmv" ) || strings . Contains ( urlLower , "fext=wmv" ) || strings . Contains ( urlLower , ".wmv" ) {
560569 contentType = "video/x-ms-wmv"
561- } else if strings .HasSuffix (fileName , ".flv" ) {
570+ } else if strings .HasSuffix (fileName , ".flv" ) || strings . Contains ( urlLower , "fext=flv" ) || strings . Contains ( urlLower , ".flv" ) {
562571 contentType = "video/x-flv"
563- } else if strings .HasSuffix (fileName , ".mov" ) {
572+ } else if strings .HasSuffix (fileName , ".mov" ) || strings . Contains ( urlLower , "fext=mov" ) || strings . Contains ( urlLower , ".mov" ) {
564573 contentType = "video/quicktime"
565- } else if strings .HasSuffix (fileName , ".mkv" ) {
574+ } else if strings .HasSuffix (fileName , ".mkv" ) || strings . Contains ( urlLower , "fext=mkv" ) || strings . Contains ( urlLower , ".mkv" ) {
566575 contentType = "video/x-matroska"
567- } else if strings .HasSuffix (fileName , ".ts" ) {
576+ } else if strings .HasSuffix (fileName , ".ts" ) || strings . Contains ( urlLower , "fext=ts" ) || strings . Contains ( urlLower , ".ts" ) {
568577 contentType = "video/mp2t"
569578 } else if strings .HasSuffix (fileName , ".mpeg" ) || strings .HasSuffix (fileName , ".mpg" ) {
570579 contentType = "video/mpeg"
571580 } else if strings .HasSuffix (fileName , ".3gpp" ) || strings .HasSuffix (fileName , ".3gp" ) {
572581 contentType = "video/3gpp"
573- } else if strings .HasSuffix (fileName , ".mp4" ) || strings .HasSuffix (fileName , ".m4s" ) {
582+ } else if strings .HasSuffix (fileName , ".mp4" ) || strings .HasSuffix (fileName , ".m4s" ) || strings . Contains ( urlLower , "fext=mp4" ) || strings . Contains ( urlLower , ".mp4" ) {
574583 contentType = "video/mp4"
584+ } else {
585+ contentType = "video/mp4" // 默认降级为 mp4,解决播放器无法识别 octet-stream 的问题
575586 }
576587 responseHeaders .Set ("Content-Type" , contentType )
577588 }
@@ -594,6 +605,7 @@ func handleGetMethod(w http.ResponseWriter, req *http.Request) {
594605 // const maxBufferSize = 1*1024 // 1KB
595606
596607 // 必须先写入 Header
608+ responseHeaders .Del ("Transfer-Encoding" )
597609 for key , values := range responseHeaders {
598610 if strings .EqualFold (strings .ToLower (key ), "connection" ) || strings .EqualFold (strings .ToLower (key ), "proxy-connection" ) {
599611 continue
@@ -746,6 +758,7 @@ func handleGetMethod(w http.ResponseWriter, req *http.Request) {
746758 responseHeaders .Set ("Accept-Ranges" , "bytes" )
747759
748760 // 先设置响应头,再开始数据传输
761+ responseHeaders .Del ("Transfer-Encoding" ) // 避免播放器因为存在 Chunked 而拒绝解析 Content-Length
749762 for key , values := range responseHeaders {
750763 if strings .EqualFold (strings .ToLower (key ), "connection" ) || strings .EqualFold (strings .ToLower (key ), "proxy-connection" ) {
751764 continue
@@ -844,6 +857,8 @@ func handleOtherMethod(w http.ResponseWriter, req *http.Request) {
844857 newHeader [name ] = value
845858 }
846859 }
860+ // 强制要求服务器不进行 gzip 压缩
861+ newHeader ["Accept-Encoding" ] = []string {"identity" }
847862
848863 // 移除错误的URL参数追加逻辑,直接使用解码后的完整URL
849864
0 commit comments