支持 stream 的修改

addon-dailer
yu.deng 3 years ago
parent 4246818db8
commit a94d64bed1

@ -1,6 +1,7 @@
package proxy
import (
"io"
"time"
log "github.com/sirupsen/logrus"
@ -33,6 +34,12 @@ type Addon interface {
// The full HTTP response has been read.
Response(*Flow)
// Stream request body modifier
StreamRequestModifier(io.Reader) io.Reader
// Stream response body modifier
StreamResponseModifier(io.Reader) io.Reader
}
// BaseAddon do nothing
@ -49,6 +56,12 @@ func (addon *BaseAddon) Requestheaders(*Flow) {}
func (addon *BaseAddon) Request(*Flow) {}
func (addon *BaseAddon) Responseheaders(*Flow) {}
func (addon *BaseAddon) Response(*Flow) {}
func (addon *BaseAddon) StreamRequestModifier(in io.Reader) io.Reader {
return in
}
func (addon *BaseAddon) StreamResponseModifier(in io.Reader) io.Reader {
return in
}
// LogAddon log connection and flow
type LogAddon struct {

@ -189,6 +189,9 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
}
}
for _, addon := range proxy.Addons {
reqBody = addon.StreamRequestModifier(reqBody)
}
proxyReq, err := http.NewRequest(f.Request.Method, f.Request.URL.String(), reqBody)
if err != nil {
log.Error(err)
@ -247,6 +250,9 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
}
}
}
for _, addon := range proxy.Addons {
resBody = addon.StreamResponseModifier(resBody)
}
reply(f.Response, resBody)
}

Loading…
Cancel
Save