elazarl / goproxy

An HTTP proxy library for Go
BSD 3-Clause "New" or "Revised" License
6.06k stars 1.1k forks source link

An existing connection was forcibly closed by the remote host. #499

Closed baier233 closed 1 year ago

baier233 commented 1 year ago

i am doing a small example to rewirte response,but it seems like some error occured

2023/04/28 18:57:13 [001] WARN: Error copying to client: readfrom tcp 10.15.175.38:65494->182.92.217.114:443: read tcp 127.0.0.1:15937->127.0.0.1:65493: wsarecv: An existing connection was forcibly closed by the remote host.
2023/04/28 18:57:13 [001] WARN: Error copying to client: readfrom tcp 127.0.0.1:15937->127.0.0.1:65493: write tcp 127.0.0.1:15937->127.0.0.1:65493: wsasend: An existing connection was forcibly closed by the remote host
package main

import (
    "io"
    "log"
    "net/http"
    "strings"

    "github.com/elazarl/goproxy"
)

func main() {
    proxy := goproxy.NewProxyHttpServer()

    proxy.OnResponse().DoFunc(
        func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
            log.Println(resp.Body, resp.Request.Body, resp.Request.URL)
            resp.Body = io.NopCloser(strings.NewReader("0"))
            resp.ContentLength = int64(len("0"))

            return resp
        })
    log.Fatal(http.ListenAndServe(":15937", proxy))
}