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
|
package http
import (
"fmt"
"html/template"
"io/ioutil"
"net/http"
"strings"
"git.darknebu.la/emile/faila/src/structs"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
// Server defines and runs an HTTP server
func Server() {
r := mux.NewRouter()
r.Use(loggingMiddleware)
// static js / css hosting
assets := r.PathPrefix("/assets/").Subrouter()
fs := http.FileServer(http.Dir("./hosted/assets"))
assets.PathPrefix("/").Handler(http.StripPrefix("/assets/", fs))
r.HandleFunc("/download", downloadHandler).Methods("GET")
t := r.PathPrefix("/").Subrouter()
t.PathPrefix("/").HandlerFunc(pathHandler)
// get the ip and port from the config
bindIP := viper.GetString("server.bindip")
listenPort := viper.GetString("server.listenport")
// define the http server
httpServer := http.Server{
Addr: fmt.Sprintf("%s:%s", bindIP, listenPort),
Handler: r,
}
logrus.Warnf("HTTP server defined listening on %s:%s", bindIP, listenPort)
logrus.Fatal(httpServer.ListenAndServe())
}
func downloadHandler(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
file := query["file"][0]
root := viper.GetString("server.root")
logrus.Info(root)
strippedFile := strings.Replace(file, root, "", -1)
strippedFile = strings.Replace(strippedFile, "..", "", -1)
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", strippedFile))
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
actualFile := fmt.Sprintf("%s%s", root, strippedFile)
logrus.Infof("serving: %s", actualFile)
http.ServeFile(w, r, actualFile)
}
func pathHandler(w http.ResponseWriter, r *http.Request) {
var content map[string]interface{}
content = make(map[string]interface{})
root := viper.GetString("server.root")
requestURI := fmt.Sprintf("%s%s", root, r.RequestURI)
logrus.Tracef("root: %s", root)
logrus.Tracef("requestURI: %s", root)
query := r.URL.Query()
if query["download"] != nil {
// strip the file before and after the request
strippedFile := strings.Replace(requestURI, root, "", -1)
strippedFile = strings.Replace(strippedFile, "?download", "", -1)
path := fmt.Sprintf("/download?file=%s", strippedFile)
http.Redirect(w, r, path, http.StatusSeeOther)
return
}
breadcrumbsList := breadcrumbs(r)
content["Breadcrumbs"] = breadcrumbsList
// get all files in the request dir
files, err := ioutil.ReadDir(requestURI)
if err != nil {
logrus.Warn(err)
return
}
// define the items (files and dirs)
var items structs.Items
var dirCount int = 0
var fileCount int = 0
for _, f := range files {
if filterIsValid(f.Name()) == false {
continue
}
// get the file or dirs modtime and format it in a readable way as
// described in the config
modTime := f.ModTime()
if viper.GetString("time.format") == "" {
logrus.Fatalf("Please insert a format for the time in the config (time.format), see the README for more information.")
}
humanModTime := modTime.Format(viper.GetString("time.format"))
// define the file or dir's url
var url string
if r.RequestURI != "/" {
url = fmt.Sprintf("%s/%s", r.RequestURI, f.Name())
} else {
url = fmt.Sprintf("/%s", f.Name())
}
// define the file or dir
item := structs.Item{
Name: f.Name(),
HumanSize: f.Size(),
URL: url,
HumanModTime: humanModTime,
IsSymlink: false,
Size: "0",
}
// if it is a dir, say so
if f.IsDir() == true {
item.IsDir = true
dirCount++
} else {
item.Download = true
fileCount++
}
items = append(items, item)
}
// add the items to the content map
content["Items"] = items
// ad the file and dir count to the contents map
content["NumDirs"] = dirCount
content["NumFiles"] = fileCount
logrus.Tracef("")
logrus.Tracef("numDirs: %d", dirCount)
logrus.Tracef("numFiles: %d", fileCount)
// if there are more than one breadcrumb, define the uppath as the second
// last breadcrumb
// I did this, because somehow things broke when simply using ".." in
// combination with hidden folders
if len(breadcrumbsList) > 1 {
content["UpPath"] = breadcrumbsList[len(breadcrumbsList)-2].Link
} else {
content["UpPath"] = ".."
}
// In the caddy
content["ItemsLimitedTo"] = 100000000000
// define the sort order manually
// TODO: handle this correctly
content["Sort"] = "namedirfirst"
content["Order"] = "desc"
// Set the site's title to the title defined in the config
content["SiteTitle"] = viper.GetString("server.name")
// if we're not at the root, we can still go futher down
if r.RequestURI != "/" {
content["CanGoUp"] = "true"
logrus.Tracef("can go up")
}
// define a new template to render the challenges in
t := template.New("")
t, err = t.ParseGlob("./hosted/tmpl/*.html")
if err != nil {
logrus.Warn(err)
return
}
err = t.ExecuteTemplate(w, "index", content)
logrus.Warn(err)
return
}
// breadcrumbs get's the breadcrumbs from the request
func breadcrumbs(r *http.Request) structs.Breadcrumbs {
request := r.RequestURI
// mitigate path traversals
strippedRequest := strings.Replace(request, "..", "", -1)
if request != "/" {
strippedRequest = strings.TrimRight(strippedRequest, "/")
}
// continue without the first slash, as it produces an unused field that has
// no use
crumbs := strings.Split(strippedRequest[1:], "/")
// build the breadcrumbs from the split RequestURI
var breadcrumbs structs.Breadcrumbs
for i, crumb := range crumbs {
text := crumb
// the link is defined as the text until the given crumb
link := strings.Join(crumbs[:i+1], "/")
resultCrumb := structs.Crumb{
Text: text,
Link: fmt.Sprintf("/%s", link),
}
breadcrumbs = append(breadcrumbs, resultCrumb)
}
return breadcrumbs
}
// filter filters files returning true if they should be displayed and false if
// not. The descision is made using the hide config from the config file
func filterIsValid(name string) bool {
// hide files starting with a dot if the "hide.file" directive is set
if viper.GetBool("hide.files") == true {
if name[0] == '.' {
return false
}
}
extensions := viper.GetStringSlice("hide.extensions")
for _, extension := range extensions {
if strings.HasSuffix(name, extension) {
return false
}
}
return true
}
|