-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
121 lines (103 loc) · 2.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"flag"
"io"
"log"
"runtime"
"strings"
"time"
"github.com/valyala/fasthttp"
)
func main() {
/* go func() {
defer os.Exit(0)
cpuProfile, err := os.Create("fasthttp-cpu.prof")
if err != nil {
log.Fatal(err)
}
defer pprof.StopCPUProfile()
pprof.StartCPUProfile(cpuProfile)
time.Sleep(time.Second * 10)
fmt.Println("Writing cpu & mem profile...")
// Memory Profile
memProfile, err := os.Create("fasthttp-mem.prof")
if err != nil {
log.Fatal(err)
}
defer memProfile.Close()
runtime.GC()
if err := pprof.WriteHeapProfile(memProfile); err != nil {
log.Fatal(err)
}
}()*/
var res string
var listenAddr string
var aaaa int
var keepAlive bool
var aes128 bool
var sha bool
var sleep int
var aesKey = []byte("0123456789ABCDEF")
flag.StringVar(&listenAddr, "listen", "127.0.0.1:8000", "server listen addr")
flag.IntVar(&aaaa, "aaaa", 0, "aaaaa.... (default output is 'Hello World')")
flag.BoolVar(&keepAlive, "keepalive", true, "use HTTP Keep-Alive")
flag.BoolVar(&aes128, "aes128", false, "encrypt response with aes-128-cbc")
flag.BoolVar(&sha, "sha", false, "output sha256 instead of plain response")
flag.IntVar(&sleep, "sleep", 0, "sleep number of milliseconds per request")
flag.Parse()
if aaaa > 0 {
res = strings.Repeat("a", aaaa)
} else {
res = "Hello World!\r\n"
}
resbytes := []byte(res)
log.Printf("http server using valyala/fasthttp starting on %s with GOMAXPROCS=%d", listenAddr, runtime.GOMAXPROCS(0))
s := &fasthttp.Server{
Handler: func(c *fasthttp.RequestCtx) {
if aes128 {
cryptedResbytes, _ := encryptCBC(resbytes, aesKey)
c.Write(cryptedResbytes)
} else if sha {
sha256sum := sha256.Sum256(resbytes)
c.WriteString(hex.EncodeToString(sha256sum[:]))
} else {
c.Write(resbytes)
}
if sleep > 0 {
time.Sleep(time.Millisecond * time.Duration(sleep))
}
},
DisableKeepalive: !keepAlive,
}
s.ListenAndServe(listenAddr)
}
// Encrypts given cipher text (prepended with the IV) with AES-128 or AES-256
// (depending on the length of the key)
func encryptCBC(plainText, key []byte) (cipherText []byte, err error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
plainText = pad(aes.BlockSize, plainText)
cipherText = make([]byte, aes.BlockSize+len(plainText))
iv := cipherText[:aes.BlockSize]
_, err = io.ReadFull(rand.Reader, iv)
if err != nil {
return nil, err
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(cipherText[aes.BlockSize:], plainText)
return cipherText, nil
}
// Adds PKCS#7 padding (variable block length <= 255 bytes)
func pad(blockSize int, buf []byte) []byte {
padLen := blockSize - (len(buf) % blockSize)
padding := bytes.Repeat([]byte{byte(padLen)}, padLen)
return append(buf, padding...)
}