capture/config.go

64 lines
1.8 KiB
Go
Raw Normal View History

2017-11-08 00:10:54 +01:00
package main
2018-09-16 16:18:39 +02:00
import (
"flag"
"fmt"
"gopkg.in/ini.v1"
"os"
2018-09-16 16:18:39 +02:00
)
2017-11-08 00:10:54 +01:00
2021-04-06 12:19:16 +02:00
// Config has all the configuration parsed from the command line.
2018-09-16 16:21:36 +02:00
type Config struct {
2019-11-09 00:27:02 +01:00
TargetURL string
ProxyPort string
DashboardPort string
2023-08-03 15:42:22 +02:00
TLSSkipVerify bool
2019-11-09 00:27:02 +01:00
MaxCaptures int
2018-07-21 19:50:53 +02:00
}
2021-04-06 12:19:16 +02:00
// ReadConfig reads the arguments from the command line.
2018-09-16 16:21:36 +02:00
func ReadConfig() Config {
defaultTargetURL := "https://jsonplaceholder.typicode.com"
defaultProxyPort := "9000"
defaultDashboardPort := "9001"
defaultMaxCaptures := 16
defaultConfigFile := ".capture.ini"
2023-08-03 15:42:22 +02:00
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")
2023-08-03 15:42:22 +02:00
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),
2023-08-03 15:42:22 +02:00
TLSSkipVerify: section.Key("tls_skip_verify").MustBool(*TLSSkipVerify),
}
}
2018-09-16 16:21:36 +02:00
return Config{
2019-11-09 00:27:02 +01:00
TargetURL: *targetURL,
ProxyPort: *proxyPort,
MaxCaptures: *maxCaptures,
DashboardPort: *dashboardPort,
2023-08-03 15:42:22 +02:00
TLSSkipVerify: *TLSSkipVerify,
2018-09-16 16:18:39 +02:00
}
2017-11-08 00:10:54 +01:00
}