Skip to content

Commit

Permalink
结构调整
Browse files Browse the repository at this point in the history
  • Loading branch information
Leviathangk committed Jan 8, 2024
1 parent d8d4f46 commit 52f6259
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 48 deletions.
56 changes: 27 additions & 29 deletions demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -14,124 +13,123 @@ 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: "百度一下,你就知道",
ToContent: "百度一下,你也不知道",
})

// 内容查找替换(仅当请求无 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))
}
91 changes: 91 additions & 0 deletions mitmtools/config.go
Original file line number Diff line number Diff line change
@@ -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
}
}
18 changes: 1 addition & 17 deletions mitmtools/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions mitmtools/mustDo.go → mitmtools/method.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package mitmtools

import (
"strconv"

"github.com/lqqyt2423/go-mitmproxy/proxy"
"strconv"
)

type decodeRule struct {
Expand Down

0 comments on commit 52f6259

Please sign in to comment.