forked from projectdiscovery/simplehttpserver
/
options.go
112 lines (98 loc) · 3.11 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
package runner
import (
"flag"
"os"
"path/filepath"
"strings"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/gologger/levels"
)
// 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
}
// 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.IntVar(&options.MaxFileSize, "max-file-size", 50, "Max Upload File Size")
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
}