package main import ( "flag" "fmt" "gopkg.in/ini.v1" "os" ) // Config has all the configuration parsed from the command line. type Config struct { TargetURL string ProxyPort string DashboardPort string TLSSkipVerify bool MaxCaptures int } // ReadConfig reads the arguments from the command line. func ReadConfig() Config { defaultTargetURL := "https://jsonplaceholder.typicode.com" defaultProxyPort := "9000" defaultDashboardPort := "9001" defaultMaxCaptures := 16 defaultConfigFile := ".capture.ini" defaultTLSSkipVerify := false targetURL := flag.String("url", defaultTargetURL, "Required. Set the url you want to proxy") configFile := flag.String("config", defaultConfigFile, "Set the configuration file") proxyPort := flag.String("port", defaultProxyPort, "Set the proxy port") dashboardPort := flag.String("dashboard", defaultDashboardPort, "Set the dashboard port") maxCaptures := flag.Int("captures", defaultMaxCaptures, "Set how many captures to show in the dashboard") TLSSkipVerify := flag.Bool("tls-skip-verify", defaultTLSSkipVerify, "Skip TLS vertification") flag.Parse() if _, err := os.Stat(*configFile); err == nil { cfg, err := ini.Load(*configFile) if err != nil { fmt.Printf("Fail to read file %s: %v", *configFile, err) os.Exit(1) } section := cfg.Section("") return Config{ TargetURL: section.Key("url").MustString(*targetURL), ProxyPort: section.Key("port").MustString(*proxyPort), MaxCaptures: section.Key("captures").MustInt(*maxCaptures), DashboardPort: section.Key("dashboard").MustString(*dashboardPort), TLSSkipVerify: section.Key("tls_skip_verify").MustBool(*TLSSkipVerify), } } return Config{ TargetURL: *targetURL, ProxyPort: *proxyPort, MaxCaptures: *maxCaptures, DashboardPort: *dashboardPort, TLSSkipVerify: *TLSSkipVerify, } }