Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support custom config dir #341

Merged
merged 6 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ EXPORT:
-kafka-topic string kafka topic to publish messages on (default "proxify")

CONFIGURATION:
-config string Directory for storing program information (default "$HOME/.config/proxify")
-config string override the default config path ($home/.config/proxify)
-cert-cache-size int Number of certificates to cache (default 256)
-a, -allow string[] Allowed list of IP/CIDR's to be proxied
-d, -deny string[] Denied list of IP/CIDR's to be proxied
Expand Down
50 changes: 41 additions & 9 deletions internal/runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/projectdiscovery/proxify/pkg/logger/elastic"
"github.com/projectdiscovery/proxify/pkg/logger/kafka"
"github.com/projectdiscovery/proxify/pkg/types"
fileutil "github.com/projectdiscovery/utils/file"
updateutils "github.com/projectdiscovery/utils/update"
)

Expand Down Expand Up @@ -49,12 +50,6 @@ type Options struct {
}

func ParseOptions() *Options {
homeDir, err := os.UserHomeDir()
if err != nil {
// Almost never here but panic
panic(err)
}

options := &Options{}

flagSet := goflags.NewFlagSet()
Expand Down Expand Up @@ -108,8 +103,7 @@ func ParseOptions() *Options {
)

flagSet.CreateGroup("configuration", "Configuration",
// Todo: default config file support (homeDir/.config/proxify/config.yaml)
flagSet.StringVar(&options.Directory, "config", filepath.Join(homeDir, ".config", "proxify"), "Directory for storing program information"),
flagSet.StringVar(&options.Directory, "config", "", "override the default config path ($home/.config/proxify)"),
flagSet.IntVar(&options.CertCacheSize, "cert-cache-size", 256, "Number of certificates to cache"),
flagSet.StringSliceVarP(&options.Allow, "allow", "a", nil, "Allowed list of IP/CIDR's to be proxied", goflags.FileNormalizedStringSliceOptions),
flagSet.StringSliceVarP(&options.Deny, "deny", "d", nil, "Denied list of IP/CIDR's to be proxied", goflags.FileNormalizedStringSliceOptions),
Expand All @@ -126,7 +120,16 @@ func ParseOptions() *Options {
)

_ = flagSet.Parse()
os.MkdirAll(options.Directory, os.ModePerm) //nolint
if options.Directory != "" {
_ = os.MkdirAll(options.Directory, os.ModePerm)
readFlagsConfig(flagSet, options.Directory)
} else {
homeDir, err := os.UserHomeDir()
if err != nil {
dogancanbakir marked this conversation as resolved.
Show resolved Hide resolved
panic(err)
}
options.Directory = filepath.Join(homeDir, ".config", "proxify")
}

// Read the inputs and configure the logging
options.configureVerbosity(silent, verbose, veryVerbose)
Expand Down Expand Up @@ -154,6 +157,35 @@ func ParseOptions() *Options {
return options
}

// readFlagsConfig reads the config file from the default config dir and copies it to the current config dir.
func readFlagsConfig(flagset *goflags.FlagSet, configDir string) {
// check if config.yaml file exists
defaultCfgFile, err := flagset.GetConfigFilePath()
if err != nil {
// something went wrong either dir is not readable or something else went wrong upstream in `goflags`
// warn and exit in this case
gologger.Warning().Msgf("Could not read config file: %s\n", err)
return
}
cfgFile := filepath.Join(configDir, "config.yaml")
if !fileutil.FileExists(cfgFile) {
if !fileutil.FileExists(defaultCfgFile) {
// if default config does not exist, warn and exit
gologger.Warning().Msgf("missing default config file : %s", defaultCfgFile)
return
}
// if does not exist copy it from the default config
if err = fileutil.CopyFile(defaultCfgFile, cfgFile); err != nil {
gologger.Warning().Msgf("Could not copy config file: %s\n", err)
}
return
}
// if config file exists, merge it with the default config
if err = flagset.MergeConfigFile(cfgFile); err != nil {
gologger.Warning().Msgf("failed to merge configfile with flags got: %s\n", err)
}
}

func (options *Options) configureVerbosity(silent, verbose, veryVerbose bool) {
if silent && (verbose || veryVerbose) {
gologger.Error().Msgf("The -silent flag and -v/-vv flags cannot be set together\n")
Expand Down
Loading