/
web.go
414 lines (351 loc) · 9.93 KB
/
web.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// web.go
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"html"
"html/template"
"io/ioutil"
"mime"
"net/http"
"net/url"
"os"
"path"
"sort"
"strings"
)
//import "github.com/JamesDunne/go-util/base"
import "github.com/JamesDunne/go-util/web"
var uiTmpl *template.Template
func endSlash(s string) string {
if strings.HasSuffix(s, "/") {
return s
}
return s + "/"
}
func translateForProxy(s string) string {
return path.Join(proxyRoot, removeIfStartsWith(s, jailRoot))
}
// Logging+action functions
func doError(req *http.Request, rsp http.ResponseWriter, msg string, code int) {
http.Error(rsp, msg, code)
}
func doRedirect(req *http.Request, rsp http.ResponseWriter, url string, code int) {
http.Redirect(rsp, req, url, code)
}
func doOK(req *http.Request, msg string, code int) {
}
// Serves an index.html file for a directory or sends the requested file.
func processRequest(rsp http.ResponseWriter, req *http.Request) *web.Error {
// proxy sends us absolute path URLs
u, err := url.Parse(req.RequestURI)
if err != nil {
return web.AsError(err, 500)
}
if (jplayerPath != "") && strings.HasPrefix(u.Path, jplayerUrl) {
// URL is under the jPlayer path:
localPath := path.Join(jplayerPath, removeIfStartsWith(u.Path, jplayerUrl))
http.ServeFile(rsp, req, localPath)
return nil
} else if (mtPath != "") && strings.HasPrefix(u.Path, mtUrl) {
// URL is under the MT path:
localPath := path.Join(mtPath, removeIfStartsWith(u.Path, mtUrl))
http.ServeFile(rsp, req, localPath)
return nil
} else if strings.HasPrefix(u.Path, proxyRoot) {
// URL is under the proxy path:
return processProxiedRequest(rsp, req, u)
}
return nil
}
func processProxiedRequest(rsp http.ResponseWriter, req *http.Request, u *url.URL) *web.Error {
relPath := removeIfStartsWith(u.Path, proxyRoot)
localPath := path.Join(jailRoot, relPath)
// Check if the requested path is a symlink:
fi, err := os.Lstat(localPath)
if fi != nil && (fi.Mode()&os.ModeSymlink) != 0 {
localDir := path.Dir(localPath)
// Check if file is a symlink and do 302 redirect:
linkDest, err := os.Readlink(localPath)
if err != nil {
return web.AsError(err, http.StatusBadRequest)
}
// NOTE(jsd): Problem here for links outside the jail folder.
if path.IsAbs(linkDest) && !strings.HasPrefix(linkDest, jailRoot) {
return web.AsError(errors.New("Symlink points outside of jail"), http.StatusBadRequest)
}
linkDest = path.Join(localDir, linkDest)
tp := translateForProxy(linkDest)
doRedirect(req, rsp, tp, http.StatusFound)
return nil
}
// Regular stat
fi, err = os.Stat(localPath)
if err != nil {
return web.AsError(err, http.StatusNotFound)
}
// Serve the file if it is regular:
if fi.Mode().IsRegular() {
// Send file:
// NOTE(jsd): using `http.ServeFile` does not appear to handle range requests well. Lots of broken pipe errors
// that lead to a poor client experience. X-Accel-Redirect back to nginx is much better.
if accelRedirect != "" {
// Use X-Accel-Redirect if the cmdline option was given:
redirPath := path.Join(accelRedirect, relPath)
rsp.Header().Add("X-Accel-Redirect", redirPath)
rsp.Header().Add("Content-Type", mime.TypeByExtension(path.Ext(localPath)))
rsp.WriteHeader(200)
} else {
// Just serve the file directly from the filesystem:
http.ServeFile(rsp, req, localPath)
}
return nil
}
// Generate an index.html for directories:
if fi.Mode().IsDir() {
dl := u.Query().Get("dl")
if dl != "" {
switch dl {
default:
fallthrough
case "zip":
downloadZip(rsp, req, u, &fi, localPath)
//case "tar":
// downloadTar(rsp, req, u, &fi, localPath)
}
return nil
}
return generateIndexHtml(rsp, req, u)
}
return nil
}
type IndexTemplateAudioFileJSON struct {
Href string `json:"mp3"`
Name string `json:"title"`
Index int `json:"index"`
}
type IndexTemplateFile struct {
Href string
Name string
NameOnly string
IsAudio bool
IsMultitrack bool
IsFolder bool
Date string
SizeHumanReadable template.HTML
MimeType string
}
type IndexTemplate struct {
JplayerUrl string
MtUrl string
Path string
Files []*IndexTemplateFile
HasParent bool
ParentHref string
SortName string
SortDate string
SortSize string
HasAudio bool
AudioFiles template.JS
HasMultitrack bool
MultitrackMixJson template.JS
}
func generateIndexHtml(rsp http.ResponseWriter, req *http.Request, u *url.URL) *web.Error {
// Build index.html
relPath := removeIfStartsWith(u.Path, proxyRoot)
localPath := path.Join(jailRoot, relPath)
pathLink := path.Join(proxyRoot, relPath)
baseDir := path.Dir(localPath)
if localPath[len(localPath)-1] == '/' {
baseDir = path.Dir(localPath[0 : len(localPath)-1])
}
if baseDir == "" {
baseDir = "/"
}
// Determine what mode to sort by...
sortString := ""
// Check the .index-sort file:
if sf, err := os.Open(path.Join(localPath, ".index-sort")); err == nil {
defer sf.Close()
scanner := bufio.NewScanner(sf)
if scanner.Scan() {
sortString = scanner.Text()
}
}
// Use query-string 'sort' to override sorting:
sortStringQuery := u.Query().Get("sort")
if sortStringQuery != "" {
sortString = sortStringQuery
}
// default Sort mode for headers
nameSort := "name-asc"
dateSort := "date-asc"
sizeSort := "size-asc"
// Determine the sorting mode:
sortBy, sortDir := sortByName, sortAscending
switch sortString {
case "size-desc":
sortBy, sortDir = sortBySize, sortDescending
case "size-asc":
sortBy, sortDir = sortBySize, sortAscending
sizeSort = "size-desc"
case "date-desc":
sortBy, sortDir = sortByDate, sortDescending
case "date-asc":
sortBy, sortDir = sortByDate, sortAscending
dateSort = "date-desc"
case "name-desc":
sortBy, sortDir = sortByName, sortDescending
case "name-asc":
sortBy, sortDir = sortByName, sortAscending
nameSort = "name-desc"
default:
}
// Open the directory to read its contents:
f, err := os.Open(localPath)
if err != nil {
return web.AsError(err, http.StatusInternalServerError)
}
defer f.Close()
// Read the directory entries:
fis, err := f.Readdir(0)
if err != nil {
return web.AsError(err, http.StatusInternalServerError)
}
// Sort the entries by the desired mode:
switch sortBy {
default:
sort.Sort(ByName{fis, sortDir})
case sortByName:
sort.Sort(ByName{fis, sortDir})
case sortByDate:
sort.Sort(ByDate{fis, sortDir})
case sortBySize:
sort.Sort(BySize{fis, sortDir})
}
files := make([]*IndexTemplateFile, 0, len(fis))
audioFiles := make([]*IndexTemplateAudioFileJSON, 0, len(fis))
multitrackFiles := make([]*IndexTemplateAudioFileJSON, 0, len(fis))
// Check if there are MP3s in this directory:
hasMP3s := false
hasMultitrack := false
mixJsonPath := ""
i := 0
for _, dfi := range fis {
name := dfi.Name()
// No hidden files:
if len(name) > 0 && name[0] == '.' {
continue
}
// Follow symlink if applicable:
dfi = followSymlink(localPath, dfi)
dfiPath := path.Join(localPath, name)
// Folder has a mix.json file means we want a multitrack mixer:
if name == "mix.json" {
hasMultitrack = true
mixJsonPath = dfiPath
continue
}
href := translateForProxy(dfiPath)
ext := path.Ext(name)
onlyname := name
if ext != "" {
onlyname = name[0 : len(name)-len(ext)]
}
mt := mime.TypeByExtension(path.Ext(dfi.Name()))
sizeText := ""
if dfi.IsDir() {
sizeText = "-"
name += "/"
href += "/"
} else {
size := dfi.Size()
if size < 1024 {
sizeText = fmt.Sprintf("%d B", size)
} else if size < 1024*1024 {
sizeText = fmt.Sprintf("%.02f KB", float64(size)/1024.0)
} else if size < 1024*1024*1024 {
sizeText = fmt.Sprintf("%.02f MB", float64(size)/(1024.0*1024.0))
} else {
sizeText = fmt.Sprintf("%.02f GB", float64(size)/(1024.0*1024.0*1024.0))
}
}
file := &IndexTemplateFile{
Href: href,
Name: name,
NameOnly: onlyname,
Date: dfi.ModTime().Format("2006-01-02 15:04:05 -0700 MST"),
SizeHumanReadable: template.HTML(strings.Replace(html.EscapeString(sizeText), " ", " ", -1)),
MimeType: mt,
IsFolder: dfi.IsDir(),
}
files = append(files, file)
if !dfi.IsDir() {
if isMP3(dfi.Name()) {
hasMP3s = true
file.IsAudio = true
audioFiles = append(audioFiles, &IndexTemplateAudioFileJSON{
Href: href,
Name: onlyname,
Index: i,
})
i++
}
if isMultitrack(dfi.Name()) {
file.IsMultitrack = true
multitrackFiles = append(multitrackFiles, &IndexTemplateAudioFileJSON{
Href: href,
Name: onlyname,
})
}
}
}
// Disable extra features if we don't have the supporting code for them:
if !useJPlayer {
hasMP3s = false
}
if !useMT {
hasMultitrack = false
}
audioFilesJSON, err := json.Marshal(audioFiles)
if err != nil {
return web.AsError(err, http.StatusInternalServerError)
}
// Load mix.json file:
multitrackMixJson := []byte(nil)
if hasMultitrack && mixJsonPath != "" {
multitrackMixJson, err = ioutil.ReadFile(mixJsonPath)
if err != nil {
return web.AsError(err, http.StatusInternalServerError)
}
}
templateData := &IndexTemplate{
Path: pathLink,
Files: files,
SortName: nameSort,
SortDate: dateSort,
SortSize: sizeSort,
HasParent: strings.HasPrefix(baseDir, jailRoot),
ParentHref: endSlash(translateForProxy(baseDir)),
JplayerUrl: jplayerUrl,
HasAudio: hasMP3s,
AudioFiles: template.JS(audioFilesJSON),
MtUrl: mtUrl,
HasMultitrack: hasMultitrack,
MultitrackMixJson: template.JS(multitrackMixJson),
}
// TODO: check Accepts header to reply accordingly (i.e. add JSON support)
templateName := "index"
if hasMultitrack {
templateName = "index-mt"
}
// Render index template:
rsp.Header().Set("Content-Type", "text/html; charset=utf-8")
rsp.WriteHeader(200)
if werr := web.AsError(uiTmpl.ExecuteTemplate(rsp, templateName, templateData), http.StatusInternalServerError); werr != nil {
return werr.AsHTML()
}
return nil
}