support -f option to read config from the filename

addon-dailer
lqqyt2423 2 years ago
parent 3ca08c33b1
commit 85d141538f

@ -1,14 +1,30 @@
package main package main
import ( import (
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"os"
log "github.com/sirupsen/logrus"
) )
func loadConfig() *Config { func loadConfigFromFile(filename string) (*Config, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
var config Config
if err := json.Unmarshal(data, &config); err != nil {
return nil, err
}
return &config, nil
}
func loadConfigFromCli() *Config {
config := new(Config) config := new(Config)
flag.BoolVar(&config.Version, "version", false, "show version") flag.BoolVar(&config.version, "version", false, "show go-mitmproxy version")
flag.StringVar(&config.Addr, "addr", ":9080", "proxy listen addr") flag.StringVar(&config.Addr, "addr", ":9080", "proxy listen addr")
flag.StringVar(&config.WebAddr, "web_addr", ":9081", "web interface listen addr") flag.StringVar(&config.WebAddr, "web_addr", ":9081", "web interface listen addr")
flag.BoolVar(&config.SslInsecure, "ssl_insecure", false, "not verify upstream server SSL/TLS certificates.") flag.BoolVar(&config.SslInsecure, "ssl_insecure", false, "not verify upstream server SSL/TLS certificates.")
@ -19,11 +35,65 @@ func loadConfig() *Config {
flag.StringVar(&config.Dump, "dump", "", "dump filename") flag.StringVar(&config.Dump, "dump", "", "dump filename")
flag.IntVar(&config.DumpLevel, "dump_level", 0, "dump level: 0 - header, 1 - header + body") flag.IntVar(&config.DumpLevel, "dump_level", 0, "dump level: 0 - header, 1 - header + body")
flag.StringVar(&config.MapperDir, "mapper_dir", "", "mapper files dirpath") flag.StringVar(&config.MapperDir, "mapper_dir", "", "mapper files dirpath")
flag.StringVar(&config.filename, "f", "", "read config from the filename")
flag.Parse() flag.Parse()
return config return config
} }
func mergeConfigs(fileConfig, cliConfig *Config) *Config {
config := new(Config)
*config = *fileConfig
if cliConfig.Addr != "" {
config.Addr = cliConfig.Addr
}
if cliConfig.WebAddr != "" {
config.WebAddr = cliConfig.WebAddr
}
if cliConfig.SslInsecure {
config.SslInsecure = cliConfig.SslInsecure
}
if len(cliConfig.IgnoreHosts) > 0 {
config.IgnoreHosts = cliConfig.IgnoreHosts
}
if len(cliConfig.AllowHosts) > 0 {
config.AllowHosts = cliConfig.AllowHosts
}
if cliConfig.CertPath != "" {
config.CertPath = cliConfig.CertPath
}
if cliConfig.Debug != 0 {
config.Debug = cliConfig.Debug
}
if cliConfig.Dump != "" {
config.Dump = cliConfig.Dump
}
if cliConfig.DumpLevel != 0 {
config.DumpLevel = cliConfig.DumpLevel
}
if cliConfig.MapperDir != "" {
config.MapperDir = cliConfig.MapperDir
}
return config
}
func loadConfig() *Config {
cliConfig := loadConfigFromCli()
if cliConfig.version {
return cliConfig
}
if cliConfig.filename == "" {
return cliConfig
}
fileConfig, err := loadConfigFromFile(cliConfig.filename)
if err != nil {
log.Warnf("read config from %v error %v", cliConfig.filename, err)
return cliConfig
}
return mergeConfigs(fileConfig, cliConfig)
}
// arrayValue 实现了 flag.Value 接口 // arrayValue 实现了 flag.Value 接口
type arrayValue []string type arrayValue []string

@ -13,7 +13,8 @@ import (
) )
type Config struct { type Config struct {
Version bool // show version version bool // show go-mitmproxy version
Addr string // proxy listen addr Addr string // proxy listen addr
WebAddr string // web interface listen addr WebAddr string // web interface listen addr
SslInsecure bool // not verify upstream server SSL/TLS certificates. SslInsecure bool // not verify upstream server SSL/TLS certificates.
@ -24,6 +25,8 @@ type Config struct {
Dump string // dump filename Dump string // dump filename
DumpLevel int // dump level: 0 - header, 1 - header + body DumpLevel int // dump level: 0 - header, 1 - header + body
MapperDir string // mapper files dirpath MapperDir string // mapper files dirpath
filename string // read config from the filename
} }
func main() { func main() {
@ -56,7 +59,7 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
if config.Version { if config.version {
fmt.Println("go-mitmproxy: " + p.Version) fmt.Println("go-mitmproxy: " + p.Version)
os.Exit(0) os.Exit(0)
} }

Loading…
Cancel
Save