forked from rawdigits/go-flashpaper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflashpaper.go
265 lines (231 loc) · 6.36 KB
/
flashpaper.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//Ryan Huber [email protected] - 2016
package main
import (
"bytes"
"context"
"crypto/rand"
"errors"
"fmt"
"io"
"log"
"net/http"
"strings"
"sync"
"time"
)
const MAXUPLOADSIZE = 104857600 //10mb
const MAXHOURSTOKEEP = 24.0 //colloquially: "a day"
const SECRETSTOREKEY = 234 //if you don't know what this is, don't worry about it
//Because typing this over and over is silly
type smap map[string]*secret
//Secrets are this
type secret struct {
Id string `json:"id"`
Type string `json:"type"`
Data []byte `json:"data"`
time time.Time
Name string `json:"name"`
}
//Clear the actual bytes in memory .. hopefully.
func (s *secret) Wipe() {
for i, _ := range s.Data {
s.Data[i] = 0
}
}
func NewSecret() *secret {
id, err := randPathString()
if err != nil {
log.Fatal(err)
}
return &secret{Id: id, time: time.Now()}
}
func secretHandler(w http.ResponseWriter, r *http.Request) {
secrets := r.Context().Value(SECRETSTOREKEY).(smap)
path := r.URL.Path[1:]
r.ParseForm()
//prevent slackbot from exploding links when posted to a channel
if strings.Contains(r.UserAgent(), "Slack") {
http.NotFound(w, r)
return
}
switch r.Method {
case "GET":
switch path {
case "favicon.ico":
return
case "":
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, lackofstyle+index+endofstyle)
case "add":
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, lackofstyle+inputtextform+endofstyle)
case "addfile":
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, lackofstyle+inputfileform+endofstyle)
default:
sec, ok := popSecret(secrets, path)
if ok {
defer sec.Wipe()
//If this is a file, set to octet-stream to force download
//Otherwise just print the data
if sec.Type == "file" {
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", sec.Name))
w.Header().Set("Content-Type", "application/octet-stream")
w.Write(sec.Data)
} else {
fmt.Fprintf(w, "%s", sec.Data)
}
} else {
http.NotFound(w, r)
fmt.Fprintf(w, "You are likely to be eaten by a grue")
}
}
case "POST":
//I could lock on adding things to the map, but i'm not gonna.
//If randomString() collides, sorry...?
switch path {
case "add":
for k, v := range r.Form {
if k == "secret" {
v := strings.Join(v, "")
secret := NewSecret()
secrets[secret.Id] = secret
secrets[secret.Id].Data = []byte(v)
shareable(secret.Id, w, r)
} else {
fmt.Fprintf(w, "no secret provided.")
}
}
case "addfile":
f, h, _ := r.FormFile("file")
defer f.Close()
d := new(bytes.Buffer)
//Limit the size of uploads. We aren't made of money.
mb := http.MaxBytesReader(w, f, MAXUPLOADSIZE)
_, err := io.Copy(d, mb)
if err != nil {
fmt.Fprintf(w, lackofstyle+uploaderror+endofstyle)
return
}
secret := NewSecret()
secrets[secret.Id] = secret
secrets[secret.Id].Type = "file"
secrets[secret.Id].Data = d.Bytes()
secrets[secret.Id].Name = h.Filename
shareable(secret.Id, w, r)
}
default:
http.NotFound(w, r)
}
}
func shareable(id string, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
proto := "https"
if r.TLS == nil {
proto = "http"
}
ret := fmt.Sprintf(lackofstyle+shareform+endofstyle, proto, r.Host, id, MAXHOURSTOKEEP)
fmt.Fprintf(w, ret)
}
//This generates a (crypto) random 32 byte string for the path
func randPathString() (string, error) {
rb := make([]byte, 32)
_, err := rand.Read(rb)
s := fmt.Sprintf("%x", rb)
if err == nil {
return s, nil
}
return "", errors.New("could not generate random string")
}
func popSecret(secrets smap, sec string) (secret, bool) {
//It is important to use the mutex here, otherwise a race condition
//could lead to the ability to read the secret twice.
//This would defeat the whole purpose of flashpaper...
mu.Lock()
defer mu.Unlock()
val, ok := secrets[sec]
if ok {
delete(secrets, sec)
} else {
return secret{}, false
}
return *val, ok
}
//Runs every 1 second(s) to remove things that haven't been read and are expired
func janitor(secrets smap) {
for {
for k, v := range secrets {
duration := time.Since(v.time)
if duration.Hours() > MAXHOURSTOKEEP {
sec, _ := popSecret(secrets, k)
sec.Wipe()
}
}
//Sleep one second
time.Sleep(1000000000)
}
}
func contextify(fn func(w http.ResponseWriter, r *http.Request), secrets smap) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
c := context.WithValue(r.Context(), SECRETSTOREKEY, secrets)
fn(w, r.WithContext(c))
}
}
//ya ya ya, globals are bad, but this is a lock.
var mu = &sync.Mutex{}
func main() {
//set up the map that stores secrets
secrets := smap{}
//launch the janitor to remove secrets that haven't been retrieved
go janitor(secrets)
//this handles all http requests. secretHandler uses a big stupid case statement.
http.HandleFunc("/", contextify(secretHandler, secrets))
//You can uncomment the non TLS version of ListenAndServe and
//run this without TLS if you have taken leave of your senses.
//err := http.ListenAndServe(":8080", nil)
err := http.ListenAndServeTLS(":8443", "server.crt", "server.key", nil)
if err != nil {
fmt.Printf("main(): %s\n", err)
fmt.Printf("Errors usually mean you don't have the required server.crt or server.key files.\n")
}
}
//That's right friend, all the terrible HTML is right here in the source.
const lackofstyle = `
<html><head></head>
<style>
* { font-family: "Raleway", "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, sans-serif; }
</style>
<body><br>
<div style="text-align:center; top: 25px;">
`
const endofstyle = `
</div></body></html>
`
const index = `
<a href=/add>Share a TEXT secret.</add><br><br>
<a href=/addfile>Share a secret FILE.</add><br>
`
const inputtextform = `
<form action="/add" method="POST">
<textarea name="secret" rows="20" cols="80"></textarea>
<br>
<input type=submit>
</form>
`
const inputfileform = `
<form action="/addfile" method="POST" enctype="multipart/form-data">
<!-- <label for="file">Filename: </label><br> -->
<input type="file" name="file" id="file">
<input type=submit>
</form>
`
const shareform = `
share this link (do not click!):<br><br>
<h2>%s://%s/%s</h2>
<br><br>THIS LINK WILL EXPIRE IN %f HOURS<br><br>
<a href="/">Share Another Secret</a>
`
const uploaderror = `
<h2>Upload too large.</h2>
<a href="/">Share Another Secret</a>
`