2017-11-08 00:10:54 +01:00
|
|
|
package main
|
|
|
|
|
2018-09-16 16:18:39 +02:00
|
|
|
import (
|
|
|
|
"flag"
|
2023-05-29 17:07:39 +02:00
|
|
|
"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 {
|
2023-05-29 17:07:39 +02:00
|
|
|
defaultTargetURL := "https://jsonplaceholder.typicode.com"
|
|
|
|
defaultProxyPort := "9000"
|
|
|
|
defaultDashboardPort := "9001"
|
|
|
|
defaultMaxCaptures := 16
|
2023-06-06 14:03:12 +02:00
|
|
|
defaultConfigFile := ".capture.ini"
|
2023-08-03 15:42:22 +02:00
|
|
|
defaultTLSSkipVerify := false
|
2023-05-29 17:07:39 +02:00
|
|
|
|
2023-06-06 14:03:12 +02:00
|
|
|
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")
|
2023-05-29 17:07:39 +02:00
|
|
|
|
2023-06-06 14:03:12 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if _, err := os.Stat(*configFile); err == nil {
|
|
|
|
cfg, err := ini.Load(*configFile)
|
2023-05-29 17:07:39 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2023-06-06 14:03:12 +02:00
|
|
|
fmt.Printf("Fail to read file %s: %v", *configFile, err)
|
2023-05-29 17:07:39 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
section := cfg.Section("")
|
|
|
|
|
2023-06-06 14:03:12 +02:00
|
|
|
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),
|
2023-06-06 14:03:12 +02:00
|
|
|
}
|
2023-05-29 17:07:39 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|