Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ This will display help for the tool. Here are all the switches it supports.
| `-realm` | Basic auth message | `simplehttpserver -realm "insert the credentials"` |
| `-version` | Show version | `simplehttpserver -version` |
| `-silent` | Show only results | `simplehttpserver -silent` |
| `-header` | HTTP response header (can be used multiple times) | `simplehttpserver -header 'X-Powered-By: Go'` |

### Running simplehttpserver in the current folder

Expand Down
62 changes: 42 additions & 20 deletions internal/runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,40 @@ 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
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
HTTPHeaders HTTPHeaders
}

// ParseOptions parses the command line options for application
Expand Down Expand Up @@ -61,6 +64,7 @@ func ParseOptions() *Options {
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.Var(&options.HTTPHeaders, "header", "Add HTTP Response Header (name: value), can be used multiple times")
flag.Parse()

// Read the inputs and configure the logging
Expand Down Expand Up @@ -113,3 +117,21 @@ func (options *Options) FolderAbsPath() string {
}
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
}
1 change: 1 addition & 0 deletions internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func New(options *Options) (*Runner, error) {
MaxFileSize: r.options.MaxFileSize,
HTTP1Only: r.options.HTTP1Only,
MaxDumpBodySize: unit.ToMb(r.options.MaxDumpBodySize),
HTTPHeaders: r.options.HTTPHeaders,
})
if err != nil {
return nil, err
Expand Down
20 changes: 20 additions & 0 deletions pkg/httpserver/headerlayer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package httpserver

import (
"net/http"
)

// HTTPHeader represents an HTTP header
type HTTPHeader struct {
Name string
Value string
}

func (t *HTTPServer) headerlayer(handler http.Handler, headers []HTTPHeader) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, header := range headers {
w.Header().Set(header.Name, header.Value)
}
handler.ServeHTTP(w, r)
})
}
2 changes: 2 additions & 0 deletions pkg/httpserver/httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Options struct {
HTTP1Only bool
MaxFileSize int // 50Mb
MaxDumpBodySize int64
HTTPHeaders []HTTPHeader
}

// HTTPServer instance
Expand Down Expand Up @@ -72,6 +73,7 @@ func New(options *Options) (*HTTPServer, error) {
}

httpHandler = h.loglayer(httpHandler)
httpHandler = h.headerlayer(httpHandler, options.HTTPHeaders)

// add handler
h.layers = httpHandler
Expand Down