forked from projectdiscovery/simplehttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
157 lines (135 loc) · 4.79 KB
/
options.go
File metadata and controls
157 lines (135 loc) · 4.79 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package runner
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/projectdiscovery/goflags"
"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{}
flagSet := goflags.NewFlagSet()
flagSet.SetDescription(`SimpleHTTPserver is a go enhanced version of the well known python simplehttpserver with in addition a fully customizable TCP server, both supporting TLS`)
currentPath := "."
if p, err := os.Getwd(); err == nil {
currentPath = p
}
flagSet.CreateGroup("input", "Input",
flagSet.StringVar(&options.ListenAddress, "listen", "0.0.0.0:8000", "Address:Port"),
)
flagSet.CreateGroup("config", "Config",
flagSet.BoolVar(&options.EnableTCP, "tcp", false, "TCP Server"),
flagSet.BoolVar(&options.TCPWithTLS, "tls", false, "Enable TCP TLS"),
flagSet.StringVar(&options.RulesFile, "rules", "", "Rules yaml file"),
flagSet.StringVar(&options.Folder, "path", currentPath, "Folder"),
flagSet.BoolVar(&options.EnableUpload, "upload", false, "Enable upload via PUT"),
flagSet.BoolVar(&options.HTTPS, "https", false, "HTTPS"),
flagSet.StringVar(&options.TLSCertificate, "cert", "", "HTTPS Certificate"),
flagSet.StringVar(&options.TLSKey, "key", "", "HTTPS Certificate Key"),
flagSet.StringVar(&options.TLSDomain, "domain", "local.host", "Domain"),
flagSet.StringVar(&options.BasicAuth, "basic-auth", "", "Basic auth (username:password),"),
flagSet.StringVar(&options.Realm, "realm", "Please enter username and password", "Realm"),
flagSet.BoolVar(&options.Silent, "silent", false, "Show only results in the output"),
flagSet.BoolVar(&options.Sandbox, "sandbox", false, "Enable sandbox mode"),
flagSet.BoolVar(&options.HTTP1Only, "http1", false, "Enable only HTTP1"),
flagSet.IntVar(&options.MaxFileSize, "max-file-size", 50, "Max Upload File Size"),
flagSet.IntVar(&options.MaxDumpBodySize, "max-dump-body-size", -1, "Max Dump Body Size"),
flagSet.BoolVar(&options.Python, "py", false, "Emulate Python Style"),
flagSet.BoolVar(&options.CORS, "cors", false, "Enable Cross-Origin Resource Sharing (CORS)"),
flagSet.Var(&options.HTTPHeaders, "header", "Add HTTP Response Header (name: value), can be used multiple times"),
)
flagSet.CreateGroup("debug", "Debug",
flagSet.BoolVar(&options.Version, "version", false, "Show version of the software"),
flagSet.BoolVar(&options.Verbose, "verbose", false, "Verbose"),
)
if err := flagSet.Parse(); err != nil {
gologger.Fatal().Msgf("%v: parse error", err.Error())
}
// 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
}