From b94aa1376821b1da576f0f9e4b0eb13265860f48 Mon Sep 17 00:00:00 2001 From: lqqyt2423 <974923609@qq.com> Date: Tue, 28 Sep 2021 15:02:37 +0800 Subject: [PATCH] example: add change-html --- examples/change-html/main.go | 47 ++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 examples/change-html/main.go diff --git a/examples/change-html/main.go b/examples/change-html/main.go new file mode 100644 index 0000000..6da61e1 --- /dev/null +++ b/examples/change-html/main.go @@ -0,0 +1,47 @@ +package main + +import ( + "regexp" + "strconv" + "strings" + + log "github.com/sirupsen/logrus" + + "github.com/lqqyt2423/go-mitmproxy/addon" + "github.com/lqqyt2423/go-mitmproxy/flow" + "github.com/lqqyt2423/go-mitmproxy/proxy" +) + +var titleRegexp = regexp.MustCompile("()(.*?)()") + +type ChangeHtml struct { + addon.Base +} + +func (c *ChangeHtml) Response(f *flow.Flow) { + contentType := f.Response.Header.Get("Content-Type") + if !strings.Contains(contentType, "text/html") { + return + } + + // change html end with: " - go-mitmproxy" + f.Response.ReplaceToDecodedBody() + f.Response.Body = titleRegexp.ReplaceAll(f.Response.Body, []byte("${1}${2} - go-mitmproxy${3}")) + f.Response.Header.Set("Content-Length", strconv.Itoa(len(f.Response.Body))) +} + +func main() { + opts := &proxy.Options{ + Addr: ":9080", + StreamLargeBodies: 1024 * 1024 * 5, + } + + p, err := proxy.NewProxy(opts) + if err != nil { + log.Fatal(err) + } + + p.AddAddon(&ChangeHtml{}) + + log.Fatal(p.Start()) +}