Skip to content

Commit

Permalink
增加请求 url 的修改
Browse files Browse the repository at this point in the history
  • Loading branch information
guok committed Feb 2, 2024
1 parent a513c87 commit 29ef3d9
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 11 deletions.
62 changes: 51 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go get github.com/Leviathangk/go-mitmtools@latest
请求 handler

- ShowReq:打印请求
- ChangeUrl:修改请求url(注意编码问题)
- ChangeHeader:修改请求头(注意大小写)
- ChangeCookie:修改请求 Cookie(注意大小写)

Expand Down Expand Up @@ -71,6 +72,12 @@ func main() {
config.AddHandler(&req.ShowReq{
Pattern: "",
})
// 修改请求 url(注意实际发出的编码的问题)
config.AddHandler(&req.ChangeUrl{
Pattern: "wd=%E7%8B%97",
ReplaceVal: "wd=%E7%8C%AB",
})
// 文件、内容整体替换
config.AddHandler(&resp.ReplaceFile{
Expand Down Expand Up @@ -197,19 +204,52 @@ go install github.com/cosmtrek/air@latest
这里可以直接使用 air init 方式创建(推荐),或者使用以下文件

```
# air.toml
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"
[build]
# 指定入口文件
entry = "main.go"
# 监听目录
watch = ["."]
# 指定命令
cmd = "go run main.go"
# 监听的文件类型
include_ext = ["go", "tpl", "tmpl", "html", "js"]
# 延迟重新构建
delay = 3000
args_bin = []
bin = "tmp\\main.exe"
cmd = "go build -o ./tmp/main.exe ."
delay = 2000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html", "js"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false
[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"
[log]
main_only = false
time = false
[misc]
clean_on_exit = false
[screen]
clear_on_rebuild = false
keep_scroll = true
```

# 执行
Expand Down
6 changes: 6 additions & 0 deletions demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ func main() {
Pattern: "",
})

// 修改请求 url(注意实际发出的编码的问题)
config.AddHandler(&req.ChangeUrl{
Pattern: "wd=%E7%8B%97",
ReplaceVal: "wd=%E7%8C%AB",
})

// 文件、内容整体替换
config.AddHandler(&resp.ReplaceFile{
Pattern: "^https://www.baidu.com/$",
Expand Down
36 changes: 36 additions & 0 deletions mitmtools/handler/req/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"github.com/Leviathangk/go-glog/glog"
"github.com/Leviathangk/go-mitmtools/mitmtools/handler"
"github.com/lqqyt2423/go-mitmproxy/proxy"
"net/url"
"regexp"
"strings"
)

Expand Down Expand Up @@ -75,3 +77,37 @@ func (fin *ChangeCookie) Check() error {

return nil
}

type ChangeUrl struct {
handler.BaseHandler
Pattern string // url 匹配规则
ReplaceVal string // 替换的值
}

func (r *ChangeUrl) Request(f *proxy.Flow) {
reqUrl := f.Request.URL.String()

if handler.IsMatch(r.Pattern, reqUrl) {
fmt.Println(reqUrl)
// 匹配替换
re := regexp.MustCompile(r.Pattern)
newUrlStr := re.ReplaceAllString(reqUrl, r.ReplaceVal)
newUrl, err := url.Parse(newUrlStr)
if err == nil {
f.Request.URL = newUrl
glog.DLogger.Debugf("URL 替换成功为:%s\n", newUrl.String())
} else {
glog.DLogger.Warnf("URL 替换失败:%s\n", err.Error())
}
}
}

// Check 检查是否符合启动要求
func (r *ChangeUrl) Check() error {

if r.ReplaceVal == "" {
return fmt.Errorf("ChangeUrl 接收到需要替换的 ReplaceVal!")
}

return nil
}

0 comments on commit 29ef3d9

Please sign in to comment.