-
Notifications
You must be signed in to change notification settings - Fork 51
/
options.go
141 lines (124 loc) · 4.23 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package runner
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/gologger/levels"
"github.com/projectdiscovery/simplehttpserver/pkg/httpserver"
)
// Options of the tool
type Options struct {
ListenAddress string
Folder string
BasicAuth string
username string
password string
Realm string
TLSCertificate string
TLSKey string
TLSDomain string
HTTPS bool
Verbose bool
EnableUpload bool
EnableTCP bool
RulesFile string
TCPWithTLS bool
Version bool
Silent bool
Sandbox bool
MaxFileSize int
HTTP1Only bool
MaxDumpBodySize int
Python bool
CORS bool
HTTPHeaders HTTPHeaders
}
// ParseOptions parses the command line options for application
func ParseOptions() *Options {
options := &Options{}
flag.StringVar(&options.ListenAddress, "listen", "0.0.0.0:8000", "Address:Port")
flag.BoolVar(&options.EnableTCP, "tcp", false, "TCP Server")
flag.BoolVar(&options.TCPWithTLS, "tls", false, "Enable TCP TLS")
flag.StringVar(&options.RulesFile, "rules", "", "Rules yaml file")
currentPath := "."
if p, err := os.Getwd(); err == nil {
currentPath = p
}
flag.StringVar(&options.Folder, "path", currentPath, "Folder")
flag.BoolVar(&options.EnableUpload, "upload", false, "Enable upload via PUT")
flag.BoolVar(&options.HTTPS, "https", false, "HTTPS")
flag.StringVar(&options.TLSCertificate, "cert", "", "HTTPS Certificate")
flag.StringVar(&options.TLSKey, "key", "", "HTTPS Certificate Key")
flag.StringVar(&options.TLSDomain, "domain", "local.host", "Domain")
flag.BoolVar(&options.Verbose, "verbose", false, "Verbose")
flag.StringVar(&options.BasicAuth, "basic-auth", "", "Basic auth (username:password)")
flag.StringVar(&options.Realm, "realm", "Please enter username and password", "Realm")
flag.BoolVar(&options.Version, "version", false, "Show version of the software")
flag.BoolVar(&options.Silent, "silent", false, "Show only results in the output")
flag.BoolVar(&options.Sandbox, "sandbox", false, "Enable sandbox mode")
flag.BoolVar(&options.HTTP1Only, "http1", false, "Enable only HTTP1")
flag.IntVar(&options.MaxFileSize, "max-file-size", 50, "Max Upload File Size")
flag.IntVar(&options.MaxDumpBodySize, "max-dump-body-size", -1, "Max Dump Body Size")
flag.BoolVar(&options.Python, "py", false, "Emulate Python Style")
flag.BoolVar(&options.CORS, "cors", false, "Enable Cross-Origin Resource Sharing (CORS)")
flag.Var(&options.HTTPHeaders, "header", "Add HTTP Response Header (name: value), can be used multiple times")
flag.Parse()
// Read the inputs and configure the logging
options.configureOutput()
showBanner()
if options.Version {
gologger.Info().Msgf("Current Version: %s\n", Version)
os.Exit(0)
}
options.validateOptions()
return options
}
func (options *Options) validateOptions() {
if flag.NArg() > 0 && options.Folder == "." {
options.Folder = flag.Args()[0]
}
if options.BasicAuth != "" {
baTokens := strings.SplitN(options.BasicAuth, ":", 2)
if len(baTokens) > 0 {
options.username = baTokens[0]
}
if len(baTokens) > 1 {
options.password = baTokens[1]
}
}
}
// configureOutput configures the output on the screen
func (options *Options) configureOutput() {
// If the user desires verbose output, show verbose output
if options.Verbose {
gologger.DefaultLogger.SetMaxLevel(levels.LevelVerbose)
}
if options.Silent {
gologger.DefaultLogger.SetMaxLevel(levels.LevelSilent)
}
}
// FolderAbsPath of the fileserver folder
func (options *Options) FolderAbsPath() string {
abspath, err := filepath.Abs(options.Folder)
if err != nil {
return options.Folder
}
return abspath
}
// HTTPHeaders is a slice of HTTPHeader structs
type HTTPHeaders []httpserver.HTTPHeader
func (h *HTTPHeaders) String() string {
return fmt.Sprint(*h)
}
// Set sets a new header, which must be a string of the form 'name: value'
func (h *HTTPHeaders) Set(value string) error {
tokens := strings.SplitN(value, ":", 2)
if len(tokens) != 2 {
return fmt.Errorf("header '%s' not in format 'name: value'", value)
}
*h = append(*h, httpserver.HTTPHeader{Name: tokens[0], Value: tokens[1]})
return nil
}