-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (85 loc) · 1.65 KB
/
main.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
package main
import (
"encoding/base64"
"net/http"
"net/http/httputil"
"os"
"sync"
"github.com/mjwhitta/cli"
hl "github.com/mjwhitta/hilighter"
"github.com/mjwhitta/log"
)
var (
b64 bool
bytesPerG float64 = 1024 * 1024 * 1024
count float64
m *sync.Mutex
port uint
showCount bool
)
func handler(w http.ResponseWriter, req *http.Request) {
var b []byte
var e error
var tmp int
if showCount {
if req.ContentLength > 0 {
tmp = int(req.ContentLength)
if b64 {
tmp = base64.StdEncoding.DecodedLen(tmp)
}
m.Lock()
count += float64(tmp) / bytesPerG // In GBs
m.Unlock()
}
hl.Printf("\x1b[1A%f GB\n", count)
} else {
if b, e = httputil.DumpRequest(req, true); e != nil {
log.Err(e.Error())
return
}
log.Good(string(b))
}
w.Write([]byte("Success"))
}
func init() {
cli.Align = true
cli.Banner = hl.Sprintf("%s [OPTIONS]", os.Args[0])
cli.Info("Super simple HTTP listener.")
cli.Flag(
&b64,
"b",
"b64",
true,
"Incoming data is base64 encoded (default: true).",
)
cli.Flag(&showCount, "c", "count", false, "Show running count.")
cli.Flag(
&port,
"p",
"port",
8080,
"Listen on specified port (default: 8080).",
)
cli.Parse()
m = &sync.Mutex{}
}
func main() {
var addr string
var e error
var mux *http.ServeMux
var server *http.Server
addr = hl.Sprintf("0.0.0.0:%d", port)
mux = http.NewServeMux()
mux.HandleFunc("/", handler)
server = &http.Server{Addr: addr, Handler: mux}
log.Infof("Listening on %s", addr)
if showCount {
hl.Printf("%f GB\n", count)
}
e = server.ListenAndServe()
switch e {
case nil, http.ErrServerClosed:
default:
panic(e)
}
}