-
Notifications
You must be signed in to change notification settings - Fork 12
/
server.go
320 lines (278 loc) · 8.57 KB
/
server.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package main
import (
"context"
"crypto/rand"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/buildkite/terminal"
"github.com/sirupsen/logrus"
"github.com/sourcegraph/syntaxhighlight"
)
const (
htmlBegin string = `<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<link rel="shortcut icon" href="/static/favicon.ico" />
<link rel="stylesheet" media="all" href="/static/main.css"/>
<link rel="stylesheet" media="all" href="/static/ansi.css"/>
</head>
<body>`
htmlEnd string = `</body>
</html>`
serverHelp = `Run the server.`
)
func (cmd *serverCommand) Name() string { return "server" }
func (cmd *serverCommand) Args() string { return "[OPTIONS]" }
func (cmd *serverCommand) ShortHelp() string { return serverHelp }
func (cmd *serverCommand) LongHelp() string { return serverHelp }
func (cmd *serverCommand) Hidden() bool { return false }
func (cmd *serverCommand) Register(fs *flag.FlagSet) {
fs.StringVar(&cmd.cert, "cert", "", "path to ssl cert")
fs.StringVar(&cmd.key, "key", "", "path to ssl key")
fs.StringVar(&cmd.port, "port", "8080", "port for server to run on")
fs.StringVar(&cmd.storage, "s", "/etc/pastebinit/files", "directory to store pastes")
fs.StringVar(&cmd.storage, "storage", "/etc/pastebinit/files", "directory to store pastes")
fs.StringVar(&cmd.assetPath, "asset-path", "/src/static", "Path to assets and templates")
}
type serverCommand struct {
cert string
key string
port string
storage string
assetPath string
}
// JSONResponse is a map[string]string
// response from the web server.
type JSONResponse map[string]string
// String returns the string representation of the
// JSONResponse object.
func (j JSONResponse) String() string {
str, err := json.MarshalIndent(j, "", " ")
if err != nil {
return fmt.Sprintf(`{
"error": "%v"
}`, err)
}
return string(str)
}
func (cmd *serverCommand) Run(ctx context.Context, args []string) error {
// create the storage directory
if err := os.MkdirAll(cmd.storage, 0755); err != nil {
logrus.Fatalf("creating storage directory %q failed: %v", cmd.storage, err)
}
// create mux server
mux := http.NewServeMux()
// static files handler
staticHandler := http.StripPrefix("/static/", http.FileServer(http.Dir(cmd.assetPath)))
mux.Handle("/static/", staticHandler)
// pastes & view handlers
mux.HandleFunc("/paste", cmd.pasteUploadHandler) // paste upload handler
mux.HandleFunc("/", cmd.pasteHandler) // index & paste server handler
// Set up the server.
server := &http.Server{
Addr: ":" + cmd.port,
Handler: mux,
}
logrus.Infof("Starting server on port %s", cmd.port)
if len(cmd.cert) > 0 && len(cmd.key) > 0 {
return server.ListenAndServeTLS(cmd.cert, cmd.key)
}
return server.ListenAndServe()
}
// generateIndexHTML generates the html for the index page
// to list all the pastes, via walking the storage directory.
func (cmd *serverCommand) generateIndexHTML() (string, error) {
var files string
// create the function to walk the pastes files
walkPastes := func(pth string, f os.FileInfo, err error) error {
base := filepath.Base(pth)
if base != cmd.storage {
files += fmt.Sprintf(`<tr>
<td><a href="%s%s">%s</a></td>
<td>%s</td>
<td>%d</td>
</tr>`, baseuri, base, base, f.ModTime().Format("2006-01-02T15:04:05Z07:00"), f.Size())
}
return nil
}
// walk the pastes
if err := filepath.Walk(cmd.storage, walkPastes); err != nil {
return "", fmt.Errorf("walking %s failed: %v", cmd.storage, err)
}
html := fmt.Sprintf(`%s
<table>
<thead>
<tr>
<th>name</th><th>modified</th><th>size</th>
</tr>
</thead>
<tbody>
%s
</tbody>
</table>
%s`, htmlBegin, files, htmlEnd)
return html, nil
}
// pasteHandler is the request handler for / and /{pasteid}
// it returns a list of all pastes to / if properly authed
// and returns the paste to the public if /{pasteid} exists.
func (cmd *serverCommand) pasteHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
// they want the root, make them auth
u, p, ok := r.BasicAuth()
if (u != username || p != password) || !ok {
w.Header().Set("WWW-Authenticate", `Basic realm="`+baseuri+`"`)
w.WriteHeader(401)
w.Write([]byte("401 Unauthorized\n"))
return
}
html, err := cmd.generateIndexHTML()
if err != nil {
writeError(w, fmt.Sprintf("generating index html failed: %v", err))
return
}
// write the html
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, html)
logrus.Info("index file rendered")
return
}
filename := filepath.Join(cmd.storage, filepath.FromSlash(path.Clean("/"+strings.Trim(r.URL.Path, "/"))))
var handler func(data []byte) (string, error)
if strings.HasSuffix(filename, "/raw") {
// if they want the raw file serve a text/plain Content-Type
w.Header().Set("Content-Type", "text/plain")
// trim '/raw' from the filename so we can get the right file
filename = strings.TrimSuffix(filename, "/raw")
handler = func(data []byte) (string, error) {
return string(data), nil
}
} else if strings.HasSuffix(filename, "/html") {
// check if they want html
w.Header().Set("Content-Type", "text/html")
filename = strings.TrimSuffix(filename, "/html")
handler = func(data []byte) (string, error) {
return string(data), nil
}
} else if strings.HasSuffix(filename, "/ansi") {
// check if they want ansi colored text
w.Header().Set("Content-Type", "text/html")
filename = strings.TrimSuffix(filename, "/ansi")
// try to syntax highlight the file
handler = func(data []byte) (string, error) {
return fmt.Sprintf("%s<pre><code>%s</code></pre>%s", htmlBegin, terminal.Render(data), htmlEnd), nil
}
} else {
// check if they want html
w.Header().Set("Content-Type", "text/html")
handler = func(data []byte) (string, error) {
highlighted, err := syntaxhighlight.AsHTML(data)
if err != nil {
return "", err
}
return fmt.Sprintf("%s<pre><code>%s</code></pre>%s", htmlBegin, string(highlighted), htmlEnd), nil
}
}
// check if the file exists
if _, err := os.Stat(filename); os.IsNotExist(err) {
writeError(w, fmt.Sprintf("No such file or directory: %s", r.URL.Path))
return
}
// read the file
src, err := ioutil.ReadFile(filename)
if err != nil {
writeError(w, fmt.Sprintf("Reading file %s failed: %v", filename, err))
return
}
data, err := handler(src)
if err != nil {
writeError(w, fmt.Sprintf("Processing file %s failed: %v", filename, err))
}
io.WriteString(w, data)
}
// pasteUploadHander is the request handler for /paste
// it creates a uuid for the paste and saves the contents of
// the paste to that file.
func (cmd *serverCommand) pasteUploadHandler(w http.ResponseWriter, r *http.Request) {
// check basic auth
u, p, ok := r.BasicAuth()
if (u != username || p != password) || !ok {
w.Header().Set("WWW-Authenticate", `Basic realm="`+baseuri+`"`)
w.WriteHeader(401)
w.Write([]byte("401 Unauthorized\n"))
return
}
// set the content type and check to make sure they are POST-ing a paste
w.Header().Set("Content-Type", "application/json")
if r.Method != "POST" {
writeError(w, "not a valid endpoint")
return
}
// read the body of the paste
content, err := ioutil.ReadAll(r.Body)
if err != nil {
writeError(w, fmt.Sprintf("reading from body failed: %v", err))
return
}
// create a unique id for the paste
id, err := uuid()
if err != nil {
writeError(w, fmt.Sprintf("uuid generation failed: %v", err))
return
}
// write to file
file := filepath.Join(cmd.storage, id)
if err := ioutil.WriteFile(file, content, 0755); err != nil {
writeError(w, fmt.Sprintf("writing file to %q failed: %v", file, err))
return
}
// serve the uri for the paste to the requester
fmt.Fprint(w, JSONResponse{
"uri": baseuri + id,
})
logrus.Infof("paste %q posted successfully", id)
}
// uuid generates a uuid for the paste.
// This really does not need to be perfect.
func uuid() (string, error) {
var chars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
length := 8
b := make([]byte, length)
r := make([]byte, length+(length/4))
maxrb := 256 - (256 % len(chars))
i := 0
for {
if _, err := io.ReadFull(rand.Reader, r); err != nil {
return "", err
}
for _, rb := range r {
c := int(rb)
if c > maxrb {
continue
}
b[i] = chars[c%len(chars)]
i++
if i == length {
return string(b), nil
}
}
}
}
// writeError sends an error back to the requester
// and also logs the error.
func writeError(w http.ResponseWriter, msg string) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, JSONResponse{
"error": msg,
})
logrus.Printf("writing error: %s", msg)
}