11package config
22
33import (
4+ "errors"
45 "flag"
56 "os"
7+ "strings"
68
9+ "github.com/Escape-Technologies/goctopus/internal/utils"
710 log "github.com/sirupsen/logrus"
811)
912
10- var (
11- Conf * Config
12- )
13-
1413type Config struct {
1514 InputFile string
15+ Addresses []string
1616 OutputFile string
1717 MaxWorkers int
1818 Verbose bool
@@ -24,16 +24,34 @@ type Config struct {
2424 SubdomainEnumeration bool
2525}
2626
27- func ParseFlags () {
27+ var (
28+ c * Config
29+ )
30+
31+ func Get () * Config {
32+ if c == nil {
33+ log .Panic ("Config not initialized" )
34+ }
35+ return c
36+ }
37+
38+ func parseArgs () []string {
39+ if flag .Arg (0 ) == "" {
40+ log .Error ("Invalid addresses argument" )
41+ utils .PrintUsage ()
42+ os .Exit (1 )
43+ }
44+ input := flag .Arg (0 )
45+ return strings .Split (input , "," )
46+ }
47+
48+ func LoadFromArgs () {
49+ flag .Usage = utils .PrintUsage
2850 config := Config {}
2951 // -- INPUT --
30- flag .StringVar (& config .InputFile , "i" , "" , "Input file" )
31- // @TODO
32- // flag.StringVar(&config.InputFile, "d", "", "Input domains (comma separated)")
33- // flag.StringVar(&config.InputFile, "u", "", "Input urls (comma separated)")
52+ flag .StringVar (& config .InputFile , "f" , "" , "Input file" )
3453
3554 // -- CONFIG --
36- // @todo make output file optional ?
3755 flag .StringVar (& config .OutputFile , "o" , "output.jsonl" , "Output file (json-lines format)" )
3856 flag .StringVar (& config .WebhookUrl , "webhook" , "" , "Webhook URL" )
3957 flag .IntVar (& config .MaxWorkers , "w" , 100 , "Max workers" )
@@ -46,6 +64,10 @@ func ParseFlags() {
4664
4765 flag .Parse ()
4866
67+ if config .InputFile == "" {
68+ config .Addresses = parseArgs ()
69+ }
70+
4971 if config .Verbose {
5072 log .SetLevel (log .DebugLevel )
5173 }
@@ -54,34 +76,40 @@ func ParseFlags() {
5476 log .SetLevel (log .ErrorLevel )
5577 }
5678
57- ValidateConfig (& config )
58- Conf = & config
79+ if err := ValidateConfig (& config ); err != nil {
80+ log .Error (err )
81+ flag .PrintDefaults ()
82+ os .Exit (1 )
83+ }
84+
85+ c = & config
5986}
6087
61- func ValidateConfig (conf * Config ) {
88+ func ValidateConfig (conf * Config ) error {
6289 if conf .MaxWorkers < 1 {
63- log .Error ("[Invalid args] Max workers must be greater than 0" )
64- configError ()
90+ return errors .New ("[Invalid config] Max workers must be greater than 0" )
6591 }
6692
6793 if conf .Timeout < 1 {
68- log .Error ("[Invalid args] Timeout must be greater than 0" )
69- configError ()
94+ return errors .New ("[Invalid config] Timeout must be greater than 0" )
7095 }
7196
7297 if ! conf .Introspection && conf .FieldSuggestion {
73- log .Error ("[Invalid args] Introspection has to be enabled to use field suggestion fingerprinting" )
74- configError ()
98+ return errors .New ("[Invalid config] Introspection has to be enabled to use field suggestion fingerprinting" )
7599 }
76100
77- if conf .InputFile == "" {
78- log .Error ("[Invalid args] Please specify an input file" )
79- configError ()
101+ if conf .InputFile == "" && len (conf .Addresses ) == 0 {
102+ return errors .New ("[Invalid config] Please specify an input file or a list of addresses" )
80103 }
81104
105+ return nil
82106}
83107
84- func configError () {
85- flag .PrintDefaults ()
86- os .Exit (1 )
108+ func Load (config * Config ) {
109+ if err := ValidateConfig (config ); err != nil {
110+ log .Error (err )
111+ flag .PrintDefaults ()
112+ os .Exit (1 )
113+ }
114+ c = config
87115}
0 commit comments