-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cowsay.go
113 lines (105 loc) · 2.52 KB
/
cowsay.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
package main
import (
"fmt"
"math/rand"
"net"
"net/http"
"strings"
"time"
)
const (
basicCow = ` o ^__^
o (oo)\_______
(__)\ )\/\
||----w |
|| ||
`
basicTux = ` o
o
.--.
|o_o |
|:_/ |
// \ \
(| | )
/'\_ _/` + "`" + `\
\___)=(___/
`
ipCow = ` \ ^__^
\ (oo)\_______ ip.wtf
(__)\ )\/\
||----w |
|| ||
`
ipBCow = ` ^__^ /
ip.wtf _______/(oo) /
\/\( /(__)
| w----||
|| ||
`
)
func cowsay(w http.ResponseWriter, req *http.Request, rConn *RecordingConn) {
if !strings.Contains(req.Header.Get("User-Agent"), "curl/") {
w.Write([]byte("<b><a href=/>ip.wtf!</a></b><br><br>To see the super cow powers, please run: <code>curl ip.wtf/moo</code>"))
} else {
remoteAddr := rConn.RemoteAddr().(*net.TCPAddr)
w.Write([]byte("\x1bc"))
basicTmpl := basicCow
if rand.Intn(9) <= 1 {
basicTmpl = basicTux
}
w.Write([]byte(cowsayText(0, "What the fuck is my IP address?", basicTmpl, "")))
if w, ok := w.(http.Flusher); ok {
w.Flush()
}
time.Sleep(950 * time.Millisecond)
w.Write([]byte("\x1bc"))
v4 := remoteAddr.IP.To4()
ip := remoteAddr.IP.String()
proto := "v6"
if v4 != nil {
ip = v4.String()
proto = "v4"
}
if rand.Intn(3) > 1 {
w.Write([]byte(cowsayText(0, fmt.Sprintf("It's fucking %v", ip), ipCow, proto)))
} else {
w.Write([]byte(cowsayText(50, fmt.Sprintf("It's fucking %v", ip), ipBCow, proto)))
}
// iTerm2 special, shame we can't work out what terminal is on the other end...
w.Write([]byte("\x1b]1337;RequestAttention=fireworks\a\r"))
}
}
func cowsayText(align int, text, template, proto string) string {
var o strings.Builder
// OSC8 URL escape.
template = strings.Replace(template, "ip.wtf", "\x1b]8;;http://ip.wtf\aip.wtf\x1b]8;;\a", 1)
template = strings.Replace(template, "xx", proto, 1)
n := align - 4 - len(text)
if n > 0 {
for i := 0; i < n; i++ {
o.WriteRune(' ')
}
}
o.WriteRune(' ')
o.WriteString(strings.Repeat("_", len(text)+2))
o.WriteRune('\n')
if n > 0 {
for i := 0; i < n; i++ {
o.WriteRune(' ')
}
}
o.WriteString("< ")
o.WriteString(text)
o.WriteString(" >")
o.WriteRune('\n')
if n > 0 {
for i := 0; i < n; i++ {
o.WriteRune(' ')
}
}
o.WriteRune(' ')
o.WriteString(strings.Repeat("-", len(text)+2))
o.WriteRune('\n')
o.WriteString(template)
return o.String()
}