From 57770b464f42592adcd91cec8974666a4df2f27a Mon Sep 17 00:00:00 2001 From: lqqyt2423 <974923609@qq.com> Date: Wed, 8 Jun 2022 16:13:28 +0800 Subject: [PATCH] add debug option --- cmd/go-mitmproxy/main.go | 22 +++++++++++++--------- proxy/proxy.go | 1 + 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/cmd/go-mitmproxy/main.go b/cmd/go-mitmproxy/main.go index e92bd62..cfe6eee 100644 --- a/cmd/go-mitmproxy/main.go +++ b/cmd/go-mitmproxy/main.go @@ -3,9 +3,7 @@ package main import ( "flag" "fmt" - - // slog "log" - + rawLog "log" "os" "github.com/lqqyt2423/go-mitmproxy/addon" @@ -16,6 +14,7 @@ import ( ) type Config struct { + debug int version bool certPath string @@ -32,6 +31,7 @@ type Config struct { func loadConfig() *Config { config := new(Config) + flag.IntVar(&config.debug, "debug", 0, "debug mode: 1 - print debug log, 2 - show debug from") flag.BoolVar(&config.version, "version", false, "show version") flag.StringVar(&config.addr, "addr", ":9080", "proxy listen addr") flag.StringVar(&config.webAddr, "web_addr", ":9081", "web interface listen addr") @@ -48,18 +48,22 @@ func loadConfig() *Config { func main() { config := loadConfig() - // for debug - // slog.SetFlags(slog.LstdFlags | slog.Lshortfile) - // log.SetReportCaller(true) - - log.SetLevel(log.InfoLevel) - log.SetReportCaller(false) + if config.debug > 0 { + rawLog.SetFlags(rawLog.LstdFlags | rawLog.Lshortfile) + log.SetLevel(log.DebugLevel) + } else { + log.SetLevel(log.InfoLevel) + } + if config.debug == 2 { + log.SetReportCaller(true) + } log.SetOutput(os.Stdout) log.SetFormatter(&log.TextFormatter{ FullTimestamp: true, }) opts := &proxy.Options{ + Debug: config.debug, Addr: config.addr, StreamLargeBodies: 1024 * 1024 * 5, SslInsecure: config.ssl_insecure, diff --git a/proxy/proxy.go b/proxy/proxy.go index d586833..92c3cf6 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -17,6 +17,7 @@ import ( var log = _log.WithField("at", "proxy") type Options struct { + Debug int Addr string StreamLargeBodies int64 // 当请求或响应体大于此字节时,转为 stream 模式 SslInsecure bool