forked from librespeed/speedtest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (71 loc) · 1.84 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
package main
//go:generate statik -src=./public
import (
"io"
"log"
"net/http"
"strconv"
"github.com/rakyll/statik/fs"
_ "github.com/npflan/speedtest/statik"
)
func empty(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
w.Header().Add("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
w.Header().Add("Cache-Control", "post-check=0, pre-check=0")
w.Header().Set("Pragma", "no-cache")
}
b := make([]byte, 1<<20)
for {
_, e := r.Body.Read(b)
if e == io.EOF || e == io.ErrUnexpectedEOF {
break
} else if e != nil {
panic(e)
}
}
defer r.Body.Close()
w.WriteHeader(200)
}
var garbageBuf []byte = make([]byte, 1<<20)
func garbage(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
w.Header().Add("Cache-Control", "post-check=0, pre-check=0")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Content-Description", "File Transfer")
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", "attachment; filename=random.dat")
w.Header().Set("Content-Transfer-Encoding", "binary")
chunks := 4
if ckSize := r.FormValue("ckSize"); ckSize != "" {
if iSize, err := strconv.ParseInt(ckSize, 10, 32); err == nil {
if iSize > 1024 {
chunks = 1024
} else {
chunks = int(iSize)
}
}
}
w.WriteHeader(200)
for i := 0; i < chunks; i++ {
w.Write(garbageBuf)
}
}
func ip(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(r.RemoteAddr))
}
func do() error {
statikFS, err := fs.New()
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/empty", empty)
http.HandleFunc("/garbage", garbage)
http.HandleFunc("/getIP", ip)
http.Handle("/", http.FileServer(statikFS))
return http.ListenAndServe(":8080", nil)
}
func main() {
if err := do(); err != nil {
panic(err)
}
}