-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (52 loc) · 1.48 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
package main
/*
FlameIT - Immersion Cooling - Entropy Server
Author: Paweł 'felixd' Wojciechowski
*/
import (
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
)
const (
maxBytes = 100 * 1024 * 1024 * 1024 // 100 GB
defaultBytes = 512 // 4096 bits
port = 8080
)
func main() {
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
queryBytes := r.URL.Query().Get("bytes")
numBytes := int64(defaultBytes)
if queryBytes != "" {
n, err := strconv.ParseInt(queryBytes, 10, 64)
if err == nil && n > 0 && n <= maxBytes {
numBytes = int64(n)
}
}
randomFile, err := os.Open("/dev/random")
if err != nil {
http.Error(w, "Failed to read from /dev/random", http.StatusInternalServerError)
return
}
defer randomFile.Close()
log.Printf("Request to provide %d bytes", numBytes)
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", "attachment; filename=random.bits")
w.Header().Set("Content-Length", strconv.FormatInt(numBytes, 10))
bytesWritten, err := io.CopyN(w, randomFile, int64(numBytes))
if err != nil {
http.Error(w, "Failed to read from /dev/random", http.StatusInternalServerError)
return
}
log.Printf("Bytes provided: %d\n", bytesWritten)
})
log.Printf("Listening on :%d...\n", port)
err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
if err != nil {
log.Printf("Error: %s\n", err)
}
}