Skip to content

Commit d35177c

Browse files
committed
merged dev
2 parents d55e2f8 + 8d698e9 commit d35177c

File tree

8 files changed

+54
-22
lines changed

8 files changed

+54
-22
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.16.6-alpine as build-env
1+
FROM golang:1.17.4-alpine as build-env
22
RUN GO111MODULE=on go get -v github.com/projectdiscovery/simplehttpserver/cmd/simplehttpserver
33

44
FROM alpine:latest

internal/runner/options.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ type Options struct {
3131
Silent bool
3232
Sandbox bool
3333
MaxFileSize int
34+
HTTP1Only bool
35+
MaxDumpBodySize int
3436
}
3537

3638
// ParseOptions parses the command line options for application
@@ -56,8 +58,9 @@ func ParseOptions() *Options {
5658
flag.BoolVar(&options.Version, "version", false, "Show version of the software")
5759
flag.BoolVar(&options.Silent, "silent", false, "Show only results in the output")
5860
flag.BoolVar(&options.Sandbox, "sandbox", false, "Enable sandbox mode")
61+
flag.BoolVar(&options.HTTP1Only, "http1", false, "Enable only HTTP1")
5962
flag.IntVar(&options.MaxFileSize, "max-file-size", 50, "Max Upload File Size")
60-
63+
flag.IntVar(&options.MaxDumpBodySize, "max-dump-body-size", -1, "Max Dump Body Size")
6164
flag.Parse()
6265

6366
// Read the inputs and configure the logging

internal/runner/runner.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/projectdiscovery/simplehttpserver/pkg/binder"
66
"github.com/projectdiscovery/simplehttpserver/pkg/httpserver"
77
"github.com/projectdiscovery/simplehttpserver/pkg/tcpserver"
8+
"github.com/projectdiscovery/simplehttpserver/pkg/unit"
89
)
910

1011
// Runner is a client for running the enumeration process.
@@ -59,6 +60,8 @@ func New(options *Options) (*Runner, error) {
5960
Verbose: r.options.Verbose,
6061
Sandbox: r.options.Sandbox,
6162
MaxFileSize: r.options.MaxFileSize,
63+
HTTP1Only: r.options.HTTP1Only,
64+
MaxDumpBodySize: unit.ToMb(r.options.MaxDumpBodySize),
6265
})
6366
if err != nil {
6467
return nil, err

pkg/httpserver/httpserver.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package httpserver
22

33
import (
4+
"crypto/tls"
45
"errors"
56
"net/http"
67
"os"
@@ -23,7 +24,9 @@ type Options struct {
2324
BasicAuthReal string
2425
Verbose bool
2526
Sandbox bool
27+
HTTP1Only bool
2628
MaxFileSize int // 50Mb
29+
MaxDumpBodySize int64
2730
}
2831

2932
// HTTPServer instance
@@ -77,9 +80,20 @@ func New(options *Options) (*HTTPServer, error) {
7780
return &h, nil
7881
}
7982

83+
func (t *HTTPServer) makeHTTPServer(tlsConfig *tls.Config) *http.Server {
84+
httpServer := &http.Server{Addr: t.options.ListenAddress}
85+
if t.options.HTTP1Only {
86+
httpServer.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
87+
}
88+
httpServer.TLSConfig = tlsConfig
89+
httpServer.Handler = t.layers
90+
return httpServer
91+
}
92+
8093
// ListenAndServe requests over http
8194
func (t *HTTPServer) ListenAndServe() error {
82-
return http.ListenAndServe(t.options.ListenAddress, t.layers)
95+
httpServer := t.makeHTTPServer(nil)
96+
return httpServer.ListenAndServe()
8397
}
8498

8599
// ListenAndServeTLS requests over https
@@ -91,11 +105,7 @@ func (t *HTTPServer) ListenAndServeTLS() error {
91105
if err != nil {
92106
return err
93107
}
94-
httpServer := &http.Server{
95-
Addr: t.options.ListenAddress,
96-
TLSConfig: tlsConfig,
97-
}
98-
httpServer.Handler = t.layers
108+
httpServer := t.makeHTTPServer(tlsConfig)
99109
return httpServer.ListenAndServeTLS("", "")
100110
}
101111
return http.ListenAndServeTLS(t.options.ListenAddress, t.options.Certificate, t.options.CertificateKey, t.layers)

pkg/httpserver/loglayer.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,49 @@ var (
1414
EnableVerbose bool
1515
)
1616

17+
func (t *HTTPServer) shouldDumpBody(bodysize int64) bool {
18+
return t.options.MaxDumpBodySize > 0 && bodysize > t.options.MaxDumpBodySize
19+
}
20+
1721
func (t *HTTPServer) loglayer(handler http.Handler) http.Handler {
1822
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
19-
fullRequest, _ := httputil.DumpRequest(r, true)
20-
lrw := newLoggingResponseWriter(w)
23+
var fullRequest []byte
24+
if t.shouldDumpBody(r.ContentLength) {
25+
fullRequest, _ = httputil.DumpRequest(r, false)
26+
} else {
27+
fullRequest, _ = httputil.DumpRequest(r, true)
28+
}
29+
lrw := newLoggingResponseWriter(w, t.options.MaxDumpBodySize)
2130
handler.ServeHTTP(lrw, r)
2231

2332
if EnableVerbose {
2433
headers := new(bytes.Buffer)
2534
lrw.Header().Write(headers) //nolint
2635
gologger.Print().Msgf("\nRemote Address: %s\n%s\n%s %d %s\n%s\n%s\n", r.RemoteAddr, string(fullRequest), r.Proto, lrw.statusCode, http.StatusText(lrw.statusCode), headers.String(), string(lrw.Data))
2736
} else {
28-
gologger.Print().Msgf("%s \"%s %s %s\" %d %d", r.RemoteAddr, r.Method, r.URL, r.Proto, lrw.statusCode, len(lrw.Data))
37+
gologger.Print().Msgf("%s \"%s %s %s\" %d %d", r.RemoteAddr, r.Method, r.URL, r.Proto, lrw.statusCode, lrw.Size)
2938
}
3039
})
3140
}
3241

3342
type loggingResponseWriter struct {
3443
http.ResponseWriter
35-
statusCode int
36-
Data []byte
44+
statusCode int
45+
Data []byte
46+
Size int
47+
MaxDumpSize int64
3748
}
3849

39-
func newLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter {
40-
return &loggingResponseWriter{w, http.StatusOK, []byte{}}
50+
func newLoggingResponseWriter(w http.ResponseWriter, maxSize int64) *loggingResponseWriter {
51+
return &loggingResponseWriter{w, http.StatusOK, []byte{}, 0, maxSize}
4152
}
4253

4354
// Write the data
4455
func (lrw *loggingResponseWriter) Write(data []byte) (int, error) {
45-
lrw.Data = append(lrw.Data, data...)
56+
if len(lrw.Data) < int(lrw.MaxDumpSize) {
57+
lrw.Data = append(lrw.Data, data...)
58+
}
59+
lrw.Size += len(data)
4660
return lrw.ResponseWriter.Write(data)
4761
}
4862

pkg/httpserver/uploadlayer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111

1212
"github.com/projectdiscovery/gologger"
13+
"github.com/projectdiscovery/simplehttpserver/pkg/unit"
1314
)
1415

1516
// uploadlayer handles PUT requests and save the file to disk
@@ -44,7 +45,7 @@ func (t *HTTPServer) uploadlayer(handler http.Handler) http.Handler {
4445
err error
4546
)
4647
if t.options.Sandbox {
47-
maxFileSize := toMb(t.options.MaxFileSize)
48+
maxFileSize := unit.ToMb(t.options.MaxFileSize)
4849
// check header content length
4950
if r.ContentLength > maxFileSize {
5051
gologger.Print().Msg("request too large")

pkg/httpserver/util.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

pkg/unit/unit.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package unit
2+
3+
// ToMb converts bytes to megabytes
4+
func ToMb(n int) int64 {
5+
return int64(n) * 1024 * 1024
6+
}

0 commit comments

Comments
 (0)