-
Notifications
You must be signed in to change notification settings - Fork 0
/
bofh.go
61 lines (52 loc) · 1.07 KB
/
bofh.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
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net"
"time"
)
func main() {
var (
lst net.Listener
con net.Conn
err error
addr = flag.String("addr", "localhost", "listen address")
port = flag.String("port", "666", "listen port")
)
flag.Usage = usage
flag.Parse()
// Initialize the server.
lst, err = net.Listen("tcp", fmt.Sprintf("%s:%s", *addr, *port))
if err != nil {
log.Fatalf("listen: %s", err)
}
defer lst.Close()
log.Printf(
"[%s:%s] Started `The Bastard Operator From Hell' excuse server.",
*addr, *port,
)
// Accept requests.
for {
con, err = lst.Accept()
if err != nil {
log.Fatalf("accept: %s", err)
}
go getExcuse(con)
}
}
// getExcuse gets a random excuse from a list of excuses and writes it out
// to the connection.
func getExcuse(c net.Conn) {
c.Write([]byte(Excuses[randIdx(len(Excuses)-1)] + "\n"))
c.Close()
io.Copy(ioutil.Discard, c)
}
// randIdx returns a random number within the specified range.
func randIdx(n int) uint {
rand.Seed(time.Now().UnixNano())
return uint(rand.Intn(n))
}