mirror of
https://github.com/WJQSERVER-STUDIO/ghproxy.git
synced 2026-02-03 16:21:11 +08:00
update
This commit is contained in:
parent
6c3280f850
commit
55158c0cb1
4 changed files with 141 additions and 241 deletions
|
|
@ -9,9 +9,8 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/WJQSERVER-STUDIO/go-utils/copyb"
|
|
||||||
"github.com/cloudwego/hertz/pkg/app"
|
"github.com/cloudwego/hertz/pkg/app"
|
||||||
hresp "github.com/cloudwego/hertz/pkg/protocol/http1/resp"
|
//hresp "github.com/cloudwego/hertz/pkg/protocol/http1/resp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ChunkedProxyRequest(ctx context.Context, c *app.RequestContext, u string, cfg *config.Config, matcher string) {
|
func ChunkedProxyRequest(ctx context.Context, c *app.RequestContext, u string, cfg *config.Config, matcher string) {
|
||||||
|
|
@ -71,7 +70,7 @@ func ChunkedProxyRequest(ctx context.Context, c *app.RequestContext, u string, c
|
||||||
HandleError(c, fmt.Sprintf("Failed to send request: %v", err))
|
HandleError(c, fmt.Sprintf("Failed to send request: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
//defer resp.Body.Close()
|
||||||
|
|
||||||
// 错误处理(404)
|
// 错误处理(404)
|
||||||
if resp.StatusCode == 404 {
|
if resp.StatusCode == 404 {
|
||||||
|
|
@ -117,8 +116,8 @@ func ChunkedProxyRequest(ctx context.Context, c *app.RequestContext, u string, c
|
||||||
c.Header("Access-Control-Allow-Origin", cfg.Server.Cors)
|
c.Header("Access-Control-Allow-Origin", cfg.Server.Cors)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Status(resp.StatusCode)
|
//c.Status(resp.StatusCode)
|
||||||
c.Response.HijackWriter(hresp.NewChunkedBodyWriter(&c.Response, c.GetWriter()))
|
//c.Response.HijackWriter(hresp.NewChunkedBodyWriter(&c.Response, c.GetWriter()))
|
||||||
|
|
||||||
if MatcherShell(u) && matchString(matcher, matchedMatchers) && cfg.Shell.Editor {
|
if MatcherShell(u) && matchString(matcher, matchedMatchers) && cfg.Shell.Editor {
|
||||||
// 判断body是不是gzip
|
// 判断body是不是gzip
|
||||||
|
|
@ -131,17 +130,18 @@ func ChunkedProxyRequest(ctx context.Context, c *app.RequestContext, u string, c
|
||||||
c.Header("Content-Length", "")
|
c.Header("Content-Length", "")
|
||||||
|
|
||||||
//err := ProcessLinksAndWriteChunked(resp.Body, compress, string(c.Request.Host()), cfg, c)
|
//err := ProcessLinksAndWriteChunked(resp.Body, compress, string(c.Request.Host()), cfg, c)
|
||||||
_, err := processLinks(resp.Body, c.Response.BodyWriter(), compress, string(c.Request.Host()), cfg)
|
reader, _, err := processLinks(resp.Body, compress, string(c.Request.Host()), cfg)
|
||||||
|
c.SetBodyStream(reader, -1)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logError("%s %s %s %s %s Failed to copy response body: %v", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Header.GetProtocol(), err)
|
logError("%s %s %s %s %s Failed to copy response body: %v", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Header.GetProtocol(), err)
|
||||||
return
|
return
|
||||||
} else {
|
|
||||||
c.Flush() // 确保刷入
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//err = hwriter.Writer(resp.Body, c)
|
//err = hwriter.Writer(resp.Body, c)
|
||||||
//writer := c.Response.BodyWriter()
|
//writer := c.Response.BodyWriter()
|
||||||
|
|
||||||
|
/*
|
||||||
_, err := copyb.Copy(c.Response.BodyWriter(), resp.Body)
|
_, err := copyb.Copy(c.Response.BodyWriter(), resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logError("%s %s %s %s %s Failed to copy response body: %v", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Header.GetProtocol(), err)
|
logError("%s %s %s %s %s Failed to copy response body: %v", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Header.GetProtocol(), err)
|
||||||
|
|
@ -149,5 +149,8 @@ func ChunkedProxyRequest(ctx context.Context, c *app.RequestContext, u string, c
|
||||||
} else {
|
} else {
|
||||||
c.Flush() // 确保刷入
|
c.Flush() // 确保刷入
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
c.SetBodyStream(resp.Body, -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,11 +5,9 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"ghproxy/config"
|
"ghproxy/config"
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/WJQSERVER-STUDIO/go-utils/copyb"
|
|
||||||
"github.com/cloudwego/hertz/pkg/app"
|
"github.com/cloudwego/hertz/pkg/app"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -30,7 +28,7 @@ func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Co
|
||||||
|
|
||||||
var (
|
var (
|
||||||
resp *http.Response
|
resp *http.Response
|
||||||
err error
|
//err error
|
||||||
)
|
)
|
||||||
|
|
||||||
body := c.Request.Body()
|
body := c.Request.Body()
|
||||||
|
|
@ -46,7 +44,7 @@ func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Co
|
||||||
}
|
}
|
||||||
setRequestHeaders(c, req)
|
setRequestHeaders(c, req)
|
||||||
removeWSHeader(req)
|
removeWSHeader(req)
|
||||||
reWriteEncodeHeader(req)
|
//reWriteEncodeHeader(req)
|
||||||
AuthPassThrough(c, cfg, req)
|
AuthPassThrough(c, cfg, req)
|
||||||
|
|
||||||
resp, err = gitclient.Do(req)
|
resp, err = gitclient.Do(req)
|
||||||
|
|
@ -62,7 +60,7 @@ func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Co
|
||||||
}
|
}
|
||||||
setRequestHeaders(c, req)
|
setRequestHeaders(c, req)
|
||||||
removeWSHeader(req)
|
removeWSHeader(req)
|
||||||
reWriteEncodeHeader(req)
|
//reWriteEncodeHeader(req)
|
||||||
AuthPassThrough(c, cfg, req)
|
AuthPassThrough(c, cfg, req)
|
||||||
|
|
||||||
resp, err = client.Do(req)
|
resp, err = client.Do(req)
|
||||||
|
|
@ -71,12 +69,14 @@ func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Co
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
//defer resp.Body.Close()
|
//defer resp.Body.Close()
|
||||||
defer func(Body io.ReadCloser) {
|
defer func(Body io.ReadCloser) {
|
||||||
if err := Body.Close(); err != nil {
|
if err := Body.Close(); err != nil {
|
||||||
logError("Failed to close response body: %v", err)
|
logError("Failed to close response body: %v", err)
|
||||||
}
|
}
|
||||||
}(resp.Body)
|
}(resp.Body)
|
||||||
|
*/
|
||||||
|
|
||||||
contentLength := resp.Header.Get("Content-Length")
|
contentLength := resp.Header.Get("Content-Length")
|
||||||
if contentLength != "" {
|
if contentLength != "" {
|
||||||
|
|
@ -118,7 +118,9 @@ func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Co
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Status(resp.StatusCode)
|
c.Status(resp.StatusCode)
|
||||||
|
c.SetBodyStream(resp.Body, -1)
|
||||||
//err = hwriter.Writer(resp.Body, c)
|
//err = hwriter.Writer(resp.Body, c)
|
||||||
|
/*
|
||||||
_, err = copyb.Copy(c.Response.BodyWriter(), resp.Body)
|
_, err = copyb.Copy(c.Response.BodyWriter(), resp.Body)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -128,5 +130,6 @@ func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Co
|
||||||
|
|
||||||
c.Flush() // 确保刷入
|
c.Flush() // 确保刷入
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
221
proxy/match.go
221
proxy/match.go
|
|
@ -6,15 +6,9 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"ghproxy/config"
|
"ghproxy/config"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/cloudwego/hertz/pkg/app"
|
|
||||||
"github.com/cloudwego/hertz/pkg/protocol/http1/resp"
|
|
||||||
"github.com/valyala/bytebufferpool"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// 定义错误类型, error承载描述, 便于处理
|
// 定义错误类型, error承载描述, 便于处理
|
||||||
|
|
@ -185,9 +179,9 @@ func modifyURL(url string, host string, cfg *config.Config) string {
|
||||||
return url
|
return url
|
||||||
}
|
}
|
||||||
if matched {
|
if matched {
|
||||||
|
var u = url
|
||||||
u := strings.TrimPrefix(url, "https://")
|
u = strings.TrimPrefix(u, "https://")
|
||||||
u = strings.TrimPrefix(url, "http://")
|
u = strings.TrimPrefix(u, "http://")
|
||||||
logDump("Modified URL: %s", "https://"+host+"/"+u)
|
logDump("Modified URL: %s", "https://"+host+"/"+u)
|
||||||
return "https://" + host + "/" + u
|
return "https://" + host + "/" + u
|
||||||
}
|
}
|
||||||
|
|
@ -212,136 +206,6 @@ func matchString(target string, stringsToMatch []string) bool {
|
||||||
return exists
|
return exists
|
||||||
}
|
}
|
||||||
|
|
||||||
func ProcessLinksAndWriteChunked(input io.Reader, compress string, host string, cfg *config.Config, c *app.RequestContext) error {
|
|
||||||
pr, pw := io.Pipe() // 创建一个管道,用于进程间通信
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
wg.Add(2)
|
|
||||||
|
|
||||||
var processErr error // 用于存储处理过程中发生的错误
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer wg.Done() // 协程结束时通知 WaitGroup
|
|
||||||
defer pw.Close() // 协程结束时关闭管道的写端
|
|
||||||
|
|
||||||
var reader *bufio.Reader
|
|
||||||
if compress == "gzip" { // 如果需要解压
|
|
||||||
gzipReader, err := gzip.NewReader(input) // 创建 gzip 解压器
|
|
||||||
if err != nil {
|
|
||||||
c.String(http.StatusInternalServerError, fmt.Sprintf("gzip 解压错误: %v", err)) // 设置 HTTP 状态码和错误信息
|
|
||||||
processErr = fmt.Errorf("gzip decompression error: %w", err) // gzip decompression error
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer gzipReader.Close() // 延迟关闭 gzip 解压器
|
|
||||||
reader = bufio.NewReader(gzipReader) // 使用 bufio 读取解压后的数据
|
|
||||||
} else {
|
|
||||||
reader = bufio.NewReader(input) // 直接使用 bufio 读取原始数据
|
|
||||||
}
|
|
||||||
|
|
||||||
var writer io.Writer = pw // 默认写入管道
|
|
||||||
var gzipWriter *gzip.Writer
|
|
||||||
|
|
||||||
if compress == "gzip" { // 如果需要压缩
|
|
||||||
gzipWriter = gzip.NewWriter(writer) // 创建 gzip 压缩器
|
|
||||||
writer = gzipWriter // 将 writer 设置为 gzip 压缩器
|
|
||||||
defer func() { // 延迟关闭 gzip 压缩器
|
|
||||||
if err := gzipWriter.Close(); err != nil {
|
|
||||||
logError("gzipWriter close failed: %v", err)
|
|
||||||
processErr = fmt.Errorf("gzipwriter close failed: %w", err) // gzipwriter close failed
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
urlPattern := regexp.MustCompile(`https?://[^\s'"]+`) // 编译正则表达式,用于匹配 URL
|
|
||||||
scanner := bufio.NewScanner(reader) // 创建 scanner 用于逐行扫描
|
|
||||||
for scanner.Scan() { // 循环读取每一行
|
|
||||||
line := scanner.Text() // 获取当前行
|
|
||||||
modifiedLine := urlPattern.ReplaceAllStringFunc(line, func(originalURL string) string { // 替换 URL
|
|
||||||
return modifyURL(originalURL, host, cfg) // 调用 modifyURL 函数修改 URL
|
|
||||||
})
|
|
||||||
modifiedLineWithNewline := modifiedLine + "\n" // 添加换行符
|
|
||||||
|
|
||||||
_, err := writer.Write([]byte(modifiedLineWithNewline)) // 将修改后的行写入管道/gzip
|
|
||||||
if err != nil {
|
|
||||||
logError("写入 pipe 错误: %v", err) // 记录错误
|
|
||||||
processErr = fmt.Errorf("write to pipe error: %w", err) // write to pipe error
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := scanner.Err(); err != nil {
|
|
||||||
logError("读取输入错误: %v", err) // 记录错误
|
|
||||||
c.String(http.StatusInternalServerError, fmt.Sprintf("读取输入错误: %v", err)) // 设置 HTTP 状态码和错误信息
|
|
||||||
processErr = fmt.Errorf("read input error: %w", err) // read input error
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer wg.Done() // 协程结束时通知 WaitGroup
|
|
||||||
|
|
||||||
c.Response.HijackWriter(resp.NewChunkedBodyWriter(&c.Response, c.GetWriter())) // 劫持 writer,启用分块编码
|
|
||||||
|
|
||||||
bufWrapper := bytebufferpool.Get() // 从对象池获取 bytebuffer
|
|
||||||
buf := bufWrapper.B
|
|
||||||
size := 32768 // 32KB, 设置缓冲区大小
|
|
||||||
buf = buf[:cap(buf)]
|
|
||||||
if len(buf) < size {
|
|
||||||
buf = append(buf, make([]byte, size-len(buf))...)
|
|
||||||
}
|
|
||||||
buf = buf[:size] // 将缓冲区限制为 'size'
|
|
||||||
defer bytebufferpool.Put(bufWrapper) // 延迟将 bytebuffer 放回对象池
|
|
||||||
|
|
||||||
for { // 循环读取和写入数据
|
|
||||||
n, err := pr.Read(buf) // 从管道读取数据
|
|
||||||
if err != nil {
|
|
||||||
if err == io.EOF { // 如果读取到文件末尾
|
|
||||||
if n > 0 { // 确保写入所有剩余数据
|
|
||||||
_, err := c.Write(buf[:n]) // 写入最后的数据块
|
|
||||||
if err != nil {
|
|
||||||
processErr = fmt.Errorf("failed to write final chunk: %w", err) // failed to write final chunk
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c.Flush() // 刷新缓冲区
|
|
||||||
break // 读取到文件末尾, 退出循环
|
|
||||||
}
|
|
||||||
logError("hwriter.Writer read error: %v", err) // 记录错误
|
|
||||||
if processErr == nil {
|
|
||||||
processErr = fmt.Errorf("failed to read from pipe: %w", err) // failed to read from pipe
|
|
||||||
// 不要在这里设置 http status code. 如果 read 失败, process 协程可能还没有完成, 它可能正在尝试设置 status code. 两个地方都设置会导致 race condition.
|
|
||||||
}
|
|
||||||
break // 读取错误,退出循环
|
|
||||||
}
|
|
||||||
|
|
||||||
if n > 0 { // 只有在实际读取到数据时才写入
|
|
||||||
_, err = c.Write(buf[:n]) // 将数据写入响应
|
|
||||||
if err != nil {
|
|
||||||
// 处理写入错误 (考虑记录日志并可能中止)
|
|
||||||
logError("hwriter.Writer write error: %v", err)
|
|
||||||
if processErr == nil { // 仅当 processErr 尚未设置时才设置.
|
|
||||||
processErr = fmt.Errorf("failed to write chunk: %w", err) // failed to write chunk
|
|
||||||
}
|
|
||||||
break // 写入错误, 退出循环
|
|
||||||
}
|
|
||||||
|
|
||||||
// 在大多数情况下,考虑移除 Flush. 仅在 *真正* 需要时保留它。
|
|
||||||
if err := c.Flush(); err != nil {
|
|
||||||
// 更强大的 Flush() 错误处理
|
|
||||||
c.AbortWithStatus(http.StatusInternalServerError) // 中止响应
|
|
||||||
logError("hwriter.Writer flush error: %v", err)
|
|
||||||
if processErr == nil {
|
|
||||||
processErr = fmt.Errorf("failed to flush chunk: %w", err) // failed to flush chunk
|
|
||||||
}
|
|
||||||
break // 刷新错误, 退出循环
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
wg.Wait() // 等待两个协程结束
|
|
||||||
return processErr // 返回错误
|
|
||||||
}
|
|
||||||
|
|
||||||
// extractParts 从给定的 URL 中提取所需的部分
|
// extractParts 从给定的 URL 中提取所需的部分
|
||||||
func extractParts(rawURL string) (string, string, string, url.Values, error) {
|
func extractParts(rawURL string) (string, string, string, url.Values, error) {
|
||||||
// 解析 URL
|
// 解析 URL
|
||||||
|
|
@ -374,31 +238,53 @@ func extractParts(rawURL string) (string, string, string, url.Values, error) {
|
||||||
return repoOwner, repoName, remainingPath, queryParams, nil
|
return repoOwner, repoName, remainingPath, queryParams, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// processLinks 处理链接并将结果写入输出流
|
// processLinks 处理链接,返回包含处理后数据的 io.Reader
|
||||||
func processLinks(input io.Reader, output io.Writer, compress string, host string, cfg *config.Config) (written int64, err error) {
|
func processLinks(input io.Reader, compress string, host string, cfg *config.Config) (readerOut io.Reader, written int64, err error) {
|
||||||
var reader *bufio.Reader
|
pipeReader, pipeWriter := io.Pipe() // 创建 io.Pipe
|
||||||
|
readerOut = pipeReader
|
||||||
|
|
||||||
|
go func() { // 在 Goroutine 中执行写入操作
|
||||||
|
defer func() {
|
||||||
|
if pipeWriter != nil { // 确保 pipeWriter 关闭,即使发生错误
|
||||||
|
if err != nil {
|
||||||
|
if closeErr := pipeWriter.CloseWithError(err); closeErr != nil { // 如果有错误,传递错误给 reader
|
||||||
|
logError("pipeWriter close with error failed: %v, original error: %v", closeErr, err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if closeErr := pipeWriter.Close(); closeErr != nil { // 没有错误,正常关闭
|
||||||
|
logError("pipeWriter close failed: %v", closeErr)
|
||||||
|
if err == nil { // 如果之前没有错误,记录关闭错误
|
||||||
|
err = closeErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
var bufReader *bufio.Reader
|
||||||
|
|
||||||
if compress == "gzip" {
|
if compress == "gzip" {
|
||||||
// 解压gzip
|
// 解压gzip
|
||||||
gzipReader, err := gzip.NewReader(input)
|
gzipReader, gzipErr := gzip.NewReader(input)
|
||||||
if err != nil {
|
if gzipErr != nil {
|
||||||
return 0, fmt.Errorf("gzip解压错误: %v", err)
|
err = fmt.Errorf("gzip解压错误: %v", gzipErr)
|
||||||
|
return // Goroutine 中使用 return 返回错误
|
||||||
}
|
}
|
||||||
defer gzipReader.Close()
|
defer gzipReader.Close()
|
||||||
reader = bufio.NewReader(gzipReader)
|
bufReader = bufio.NewReader(gzipReader)
|
||||||
} else {
|
} else {
|
||||||
reader = bufio.NewReader(input)
|
bufReader = bufio.NewReader(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
var writer *bufio.Writer
|
var bufWriter *bufio.Writer
|
||||||
var gzipWriter *gzip.Writer
|
var gzipWriter *gzip.Writer
|
||||||
|
|
||||||
// 根据是否gzip确定 writer 的创建
|
// 根据是否gzip确定 writer 的创建
|
||||||
if compress == "gzip" {
|
if compress == "gzip" {
|
||||||
gzipWriter = gzip.NewWriter(output)
|
gzipWriter = gzip.NewWriter(pipeWriter) // 使用 pipeWriter
|
||||||
writer = bufio.NewWriterSize(gzipWriter, 4096) //设置缓冲区大小
|
bufWriter = bufio.NewWriterSize(gzipWriter, 4096) //设置缓冲区大小
|
||||||
} else {
|
} else {
|
||||||
writer = bufio.NewWriterSize(output, 4096)
|
bufWriter = bufio.NewWriterSize(pipeWriter, 4096) // 使用 pipeWriter
|
||||||
}
|
}
|
||||||
|
|
||||||
//确保writer关闭
|
//确保writer关闭
|
||||||
|
|
@ -414,7 +300,7 @@ func processLinks(input io.Reader, output io.Writer, compress string, host strin
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if flushErr := writer.Flush(); flushErr != nil {
|
if flushErr := bufWriter.Flush(); flushErr != nil {
|
||||||
logError("writer flush failed %v", flushErr)
|
logError("writer flush failed %v", flushErr)
|
||||||
// 如果已经存在错误,则保留。否则,记录此错误。
|
// 如果已经存在错误,则保留。否则,记录此错误。
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
@ -426,30 +312,37 @@ func processLinks(input io.Reader, output io.Writer, compress string, host strin
|
||||||
// 使用正则表达式匹配 http 和 https 链接
|
// 使用正则表达式匹配 http 和 https 链接
|
||||||
urlPattern := regexp.MustCompile(`https?://[^\s'"]+`)
|
urlPattern := regexp.MustCompile(`https?://[^\s'"]+`)
|
||||||
for {
|
for {
|
||||||
line, err := reader.ReadString('\n')
|
line, readErr := bufReader.ReadString('\n')
|
||||||
if err != nil {
|
if readErr != nil {
|
||||||
if err == io.EOF {
|
if readErr == io.EOF {
|
||||||
break // 文件结束
|
break // 文件结束
|
||||||
}
|
}
|
||||||
return written, fmt.Errorf("读取行错误: %v", err) // 传递错误
|
err = fmt.Errorf("读取行错误: %v", readErr) // 传递错误
|
||||||
|
return // Goroutine 中使用 return 返回错误
|
||||||
}
|
}
|
||||||
|
|
||||||
// 替换所有匹配的 URL
|
// 替换所有匹配的 URL
|
||||||
modifiedLine := urlPattern.ReplaceAllStringFunc(line, func(originalURL string) string {
|
modifiedLine := urlPattern.ReplaceAllStringFunc(line, func(originalURL string) string {
|
||||||
return modifyURL(originalURL, host, cfg)
|
logDump("originalURL: %s", originalURL)
|
||||||
|
return modifyURL(originalURL, host, cfg) // 假设 modifyURL 函数已定义
|
||||||
})
|
})
|
||||||
|
|
||||||
n, werr := writer.WriteString(modifiedLine)
|
n, writeErr := bufWriter.WriteString(modifiedLine)
|
||||||
written += int64(n) // 更新写入的字节数
|
written += int64(n) // 更新写入的字节数
|
||||||
if werr != nil {
|
if writeErr != nil {
|
||||||
return written, fmt.Errorf("写入文件错误: %v", werr) // 传递错误
|
err = fmt.Errorf("写入文件错误: %v", writeErr) // 传递错误
|
||||||
|
return // Goroutine 中使用 return 返回错误
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 在返回之前,再刷新一次
|
// 在返回之前,再刷新一次 (虽然 defer 中已经有 flush,但这里再加一次确保及时刷新)
|
||||||
if fErr := writer.Flush(); fErr != nil {
|
if flushErr := bufWriter.Flush(); flushErr != nil {
|
||||||
return written, fErr
|
if err == nil { // 避免覆盖之前的错误
|
||||||
|
err = flushErr
|
||||||
}
|
}
|
||||||
|
return // Goroutine 中使用 return 返回错误
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
return written, nil
|
return readerOut, written, nil // 返回 reader 和 written,error 由 Goroutine 通过 pipeWriter.CloseWithError 传递
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ func removeWSHeader(req *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func reWriteEncodeHeader(req *http.Request) {
|
func reWriteEncodeHeader(req *http.Request) {
|
||||||
|
|
||||||
if isGzipAccepted(req.Header) {
|
if isGzipAccepted(req.Header) {
|
||||||
req.Header.Set("Content-Encoding", "gzip")
|
req.Header.Set("Content-Encoding", "gzip")
|
||||||
req.Header.Set("Accept-Encoding", "gzip")
|
req.Header.Set("Accept-Encoding", "gzip")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue