From 52f62590d378162824bed385d74879d60eed9180 Mon Sep 17 00:00:00 2001 From: gk <1015295213@qq.com> Date: Mon, 8 Jan 2024 23:54:19 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=93=E6=9E=84=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo.go | 56 +++++++++--------- mitmtools/config.go | 91 ++++++++++++++++++++++++++++++ mitmtools/main.go | 18 +----- mitmtools/{mustDo.go => method.go} | 3 +- 4 files changed, 120 insertions(+), 48 deletions(-) create mode 100644 mitmtools/config.go rename mitmtools/{mustDo.go => method.go} (99%) diff --git a/demo.go b/demo.go index ea1dfa3..ea1189f 100644 --- a/demo.go +++ b/demo.go @@ -5,7 +5,6 @@ import ( "github.com/Leviathangk/go-mitmtools/mitmtools" "github.com/Leviathangk/go-mitmtools/mitmtools/handler/req" "github.com/Leviathangk/go-mitmtools/mitmtools/handler/resp" - "strconv" ) const ( @@ -14,34 +13,33 @@ const ( ) func main() { - opts := &mitmtools.MitmConfig{ - Addr: ":" + strconv.Itoa(port), - StreamLargeBodies: 1024 * 1024 * 5, - SslInsecure: true, - Upstream: proxyUrl, - ShowLog: true, - } + config := mitmtools.NewConfig( + mitmtools.SetAddr("", port), + mitmtools.SetSslInsecure(true), + mitmtools.SetProxy(proxyUrl), + mitmtools.SetShowLog(true), + ) // 打印请求 - opts.AddHandler(&req.ShowReq{ + config.AddHandler(&req.ShowReq{ Pattern: "", }) // 文件、内容整体替换 - opts.AddHandler(&resp.ReplaceFile{ - Pattern: "https://www.baidu.com/", + config.AddHandler(&resp.ReplaceFile{ + Pattern: "^https://www.baidu.com/$", Times: 1, // 0 为无限次 Content: []byte("我不是百度"), }) // 文件、内容整体替换(仅当请求无 cookie 时) - opts.AddHandler(&resp.ReplaceFileIfNoCookie{ - Pattern: "https://www.baidu.com/", + config.AddHandler(&resp.ReplaceFileIfNoCookie{ + Pattern: "^https://www.baidu.com/$", Content: []byte("我不是百度"), }) // 内容查找替换 - opts.AddHandler(&resp.ReplaceContent{ + config.AddHandler(&resp.ReplaceContent{ Pattern: "^https://www.baidu.com/$", Times: 1, // 0 为无限次 FindContent: "百度一下,你就知道", @@ -49,89 +47,89 @@ func main() { }) // 内容查找替换(仅当请求无 cookie 时) - opts.AddHandler(&resp.ReplaceContentIfNoCookie{ + config.AddHandler(&resp.ReplaceContentIfNoCookie{ Pattern: "^https://www.baidu.com/$", FindContent: "百度一下,你就知道", ToContent: "百度一下,你也不知道", }) // 在 html 标签开始后插入 script 标签,并添加 js 代码:body 无就 head - opts.AddHandler(&resp.AddScriptToHead{ + config.AddHandler(&resp.AddScriptToHead{ Pattern: "^https://www.baidu.com/$", Content: []byte("console.log('我不是百度');"), }) // 在 html 标签结束前插入 script 标签,并添加 js 代码:body 无就 head - opts.AddHandler(&resp.AddScriptToTail{ + config.AddHandler(&resp.AddScriptToTail{ Pattern: "^https://www.baidu.com/$", Content: []byte("console.log('我不是百度');"), }) // 在头部增加内容 - opts.AddHandler(&resp.AddContentToHead{ + config.AddHandler(&resp.AddContentToHead{ Pattern: "^https://tysf.cponline.cnipa.gov.cn/am/js/chunk-d7b9a01a.c7f12daa.js$", Content: []byte("console.log(2);"), }) // 在尾部增加内容 - opts.AddHandler(&resp.AddContentToTail{ + config.AddHandler(&resp.AddContentToTail{ Pattern: "^https://tysf.cponline.cnipa.gov.cn/am/js/chunk-d7b9a01a.c7f12daa.js$", Content: []byte("console.log(1);"), }) // 添加请求头 - opts.AddHandler(&resp.AddHeader{ + config.AddHandler(&resp.AddHeader{ Pattern: "^https://tysf.cponline.cnipa.gov.cn/am/js/chunk-d7b9a01a.c7f12daa.js$", Header: map[string]string{"k": "v"}, }) // 删除请求头 - opts.AddHandler(&resp.RemoveHeader{ + config.AddHandler(&resp.RemoveHeader{ Pattern: "^https://tysf.cponline.cnipa.gov.cn/am/js/chunk-d7b9a01a.c7f12daa.js$", Header: []string{"Last-Modified", "Content-Type"}, }) // 输出含有指定字符的 url - opts.AddHandler(&resp.FindContent{ + config.AddHandler(&resp.FindContent{ Pattern: "", // 为空则为任何响应 ContentPattern: "百度一下", }) // 输出含有指定 响应 Cookie 的 url:匹配的就是 document.cookie 后的那部分 - opts.AddHandler(&resp.FindCookie{ + config.AddHandler(&resp.FindCookie{ Pattern: "^https://www.baidu.com/$", KeyPattern: []string{"BAIDUID"}, }) // 输出含有指定 响应头 的 url:匹配的是响应头的 key - opts.AddHandler(&resp.FindHeader{ + config.AddHandler(&resp.FindHeader{ Pattern: "^https://www.baidu.com/$", KeyPattern: []string{"Bdqid", "Set-Cookie"}, }) // 修改响应头 - opts.AddHandler(&resp.ChangeHeader{ + config.AddHandler(&resp.ChangeHeader{ Pattern: "^https://www.baidu.com/$", Header: map[string][]string{"Bdqid": {"baidu"}}, }) // 修改响应 cookie - opts.AddHandler(&resp.ChangeCookie{ + config.AddHandler(&resp.ChangeCookie{ Pattern: "^https://www.baidu.com/$", Cookie: map[string]string{"H_PS_PSSID": "baidu"}, }) // 修改请求头 - opts.AddHandler(&req.ChangeHeader{ + config.AddHandler(&req.ChangeHeader{ Pattern: "^http://127.0.0.1:8877/headerTest$", Header: map[string][]string{"X": {"qiandu"}}, }) // 修改请求 cookie - opts.AddHandler(&req.ChangeCookie{ + config.AddHandler(&req.ChangeCookie{ Pattern: "^http://127.0.0.1:8877/cookieTest$", Cookie: map[string]string{"x": "qiandu"}, }) - glog.DLogger.Fatal(mitmtools.Start(opts)) + glog.DLogger.Fatal(mitmtools.Start(config)) } diff --git a/mitmtools/config.go b/mitmtools/config.go new file mode 100644 index 0000000..b39dcf6 --- /dev/null +++ b/mitmtools/config.go @@ -0,0 +1,91 @@ +package mitmtools + +import ( + "github.com/Leviathangk/go-mitmtools/mitmtools/handler" + "strconv" +) + +const ( + defaultPort = 8866 + defaultStreamLargeBodies = 1024 * 1024 * 5 + defaultSslInsecure = true + defaultShowLog = true +) + +type Config struct { + Debug int + Addr string + StreamLargeBodies int64 // 当请求或响应体大于此字节时,转为 stream 模式 + SslInsecure bool + CaRootPath string + Upstream string + ShowLog bool // 是否打印日志 + handlers []handler.Addon +} +type SetFunc func(c *Config) + +// NewConfig 新建配置 +func NewConfig(opt ...SetFunc) *Config { + config := new(Config) + + for _, o := range opt { + o(config) + } + + // 参数检查 + if config.Addr == "" { + config.Addr = ":8866" + } + if config.StreamLargeBodies == 0 { + config.StreamLargeBodies = 1024 * 1024 * 5 + } + + return config +} + +// AddHandler 添加规则 +func (c *Config) AddHandler(h handler.Addon) { + c.handlers = append(c.handlers, h) +} + +// AddHandler 添加规则 +func AddHandler(h handler.Addon) SetFunc { + return func(c *Config) { + c.handlers = append(c.handlers, h) + } +} + +// SetProxy 设置代理 +func SetProxy(p string) SetFunc { + return func(c *Config) { + c.Upstream = p + } +} + +// SetAddr 设置端口 +func SetAddr(ip string, p int) SetFunc { + return func(c *Config) { + c.Addr = ip + ":" + strconv.Itoa(p) + } +} + +// SetStreamLargeBodies 当请求或响应体大于此字节时,转为 stream 模式 +func SetStreamLargeBodies(p int) SetFunc { + return func(c *Config) { + c.Addr = ":" + strconv.Itoa(p) + } +} + +// SetSslInsecure ssl 校验 +func SetSslInsecure(b bool) SetFunc { + return func(c *Config) { + c.SslInsecure = b + } +} + +// SetShowLog 日志输出 +func SetShowLog(b bool) SetFunc { + return func(c *Config) { + c.ShowLog = b + } +} diff --git a/mitmtools/main.go b/mitmtools/main.go index ce8801d..d6b44cc 100644 --- a/mitmtools/main.go +++ b/mitmtools/main.go @@ -6,24 +6,8 @@ import ( "github.com/lqqyt2423/go-mitmproxy/proxy" ) -type MitmConfig struct { - Debug int - Addr string - StreamLargeBodies int64 // 当请求或响应体大于此字节时,转为 stream 模式 - SslInsecure bool - CaRootPath string - Upstream string - ShowLog bool // 是否打印日志 - handlers []handler.Addon -} - -// AddHandler 添加规则 -func (m *MitmConfig) AddHandler(h handler.Addon) { - m.handlers = append(m.handlers, h) -} - // Start 启动入口 -func Start(opts *MitmConfig, handlers ...handler.Addon) error { +func Start(opts *Config, handlers ...handler.Addon) error { p, err := proxy.NewProxy(&proxy.Options{ Debug: opts.Debug, Addr: opts.Addr, diff --git a/mitmtools/mustDo.go b/mitmtools/method.go similarity index 99% rename from mitmtools/mustDo.go rename to mitmtools/method.go index daf9567..ad44a93 100644 --- a/mitmtools/mustDo.go +++ b/mitmtools/method.go @@ -1,9 +1,8 @@ package mitmtools import ( - "strconv" - "github.com/lqqyt2423/go-mitmproxy/proxy" + "strconv" ) type decodeRule struct {