-
Notifications
You must be signed in to change notification settings - Fork 19
/
prosody-filer.go
318 lines (277 loc) · 8.39 KB
/
prosody-filer.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
/*
* This module allows upload via mod_http_upload_external
* Also see: https://modules.prosody.im/mod_http_upload_external.html
*/
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"flag"
"fmt"
"io"
"mime"
"net"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"github.com/BurntSushi/toml"
"github.com/sirupsen/logrus"
)
/*
* Configuration of this server
*/
type Config struct {
ListenPort string
UnixSocket bool
Secret string
StoreDir string
UploadSubDir string
LogLevel string
}
var conf Config
var versionString string = "0.0.0"
var log = &logrus.Logger{
Out: os.Stdout,
Formatter: new(logrus.TextFormatter),
Hooks: make(logrus.LevelHooks),
Level: logrus.DebugLevel,
}
var ALLOWED_METHODS string = strings.Join(
[]string{
http.MethodOptions,
http.MethodHead,
http.MethodGet,
http.MethodPut,
},
", ",
)
/*
* Sets CORS headers
*/
func addCORSheaders(w http.ResponseWriter) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", ALLOWED_METHODS)
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Max-Age", "7200")
}
/*
* Request handler
* Is activated when a clients requests the file, file information or an upload
*/
func handleRequest(w http.ResponseWriter, r *http.Request) {
log.Info("Incoming request: ", r.Method, r.URL.String())
// Parse URL and args
p := r.URL.Path
a, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
log.Warn("Failed to parse query")
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
subDir := path.Join("/", conf.UploadSubDir)
fileStorePath := strings.TrimPrefix(p, subDir)
if fileStorePath == "" || fileStorePath == "/" {
log.Warn("Access to / forbidden")
http.Error(w, "Forbidden", http.StatusForbidden)
return
} else if fileStorePath[0] == '/' {
fileStorePath = fileStorePath[1:]
}
absFilename := filepath.Join(conf.StoreDir, fileStorePath)
// Add CORS headers
addCORSheaders(w)
if r.Method == http.MethodPut {
/*
* User client tries to upload file
*/
/*
Check if MAC is attached to URL and check protocol version.
Ejabberd: supports "v" and probably "v2" Doc: https://docs.ejabberd.im/archive/20_12/modules/#mod-http-upload
Prosody: supports "v" and "v2" Doc: https://modules.prosody.im/mod_http_upload_external.html
Metronome: supports: "token" (meaning "v2") Doc: https://archon.im/metronome-im/documentation/external-upload-protocol/)
*/
var protocolVersion string
if a["v2"] != nil {
protocolVersion = "v2"
} else if a["token"] != nil {
protocolVersion = "token"
} else if a["v"] != nil {
protocolVersion = "v"
} else {
log.Warn("No HMAC attached to URL. Expecting URL with \"v\", \"v2\" or \"token\" parameter as MAC")
http.Error(w, "No HMAC attached to URL. Expecting URL with \"v\", \"v2\" or \"token\" parameter as MAC", http.StatusForbidden)
return
}
// Init HMAC
mac := hmac.New(sha256.New, []byte(conf.Secret))
macString := ""
// Calculate MAC, depending on protocolVersion
if protocolVersion == "v" {
// use a space character (0x20) between components of MAC
mac.Write([]byte(fileStorePath + "\x20" + strconv.FormatInt(r.ContentLength, 10)))
macString = hex.EncodeToString(mac.Sum(nil))
} else if protocolVersion == "v2" || protocolVersion == "token" {
// Get content type (for v2 / token)
contentType := mime.TypeByExtension(filepath.Ext(fileStorePath))
if contentType == "" {
contentType = "application/octet-stream"
}
// use a null byte character (0x00) between components of MAC
mac.Write([]byte(fileStorePath + "\x00" + strconv.FormatInt(r.ContentLength, 10) + "\x00" + contentType))
macString = hex.EncodeToString(mac.Sum(nil))
}
/*
* Check whether calculated (expected) MAC is the MAC that client send in "v" URL parameter
*/
if hmac.Equal([]byte(macString), []byte(a[protocolVersion][0])) {
err = createFile(absFilename, fileStorePath, w, r)
if err != nil {
log.Error(err)
}
return
} else {
log.Warning("Invalid MAC.")
http.Error(w, "Invalid MAC", http.StatusForbidden)
return
}
} else if r.Method == http.MethodHead || r.Method == http.MethodGet {
/*
* User client tries to download a file
*/
fileInfo, err := os.Stat(absFilename)
if err != nil {
log.Error("Getting file information failed:", err)
http.Error(w, "Not Found", http.StatusNotFound)
return
} else if fileInfo.IsDir() {
log.Warning("Directory listing forbidden!")
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
/*
* Find out the content type to sent correct header. There is a Go function for retrieving the
* MIME content type, but this does not work with encrypted files (=> OMEMO). Therefore we're just
* relying on file extensions.
*/
contentType := mime.TypeByExtension(filepath.Ext(fileStorePath))
if contentType == "" {
contentType = "application/octet-stream"
}
w.Header().Set("Content-Type", contentType)
if r.Method == http.MethodHead {
w.Header().Set("Content-Length", strconv.FormatInt(fileInfo.Size(), 10))
} else {
http.ServeFile(w, r, absFilename)
}
return
} else if r.Method == http.MethodOptions {
// Client CORS request: Return allowed methods
w.Header().Set("Allow", ALLOWED_METHODS)
return
} else {
// Client is using a prohibited / unsupported method
log.Warn("Invalid method", r.Method, "for access to ", conf.UploadSubDir)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
}
func createFile(absFilename string, fileStorePath string, w http.ResponseWriter, r *http.Request) error {
// Make sure the directory path exists
absDirectory := filepath.Dir(absFilename)
err := os.MkdirAll(absDirectory, os.ModePerm)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return fmt.Errorf("failed to create directory %s: %s", absDirectory, err)
}
// Make sure the target file exists (MUST NOT exist before! -> O_EXCL)
targetFile, err := os.OpenFile(absFilename, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
if err != nil {
http.Error(w, "Conflict", http.StatusConflict)
return fmt.Errorf("failed to create file %s: %s", absFilename, err)
}
defer targetFile.Close()
// Copy file contents to file
_, err = io.Copy(targetFile, r.Body)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return fmt.Errorf("failed to copy file contents to %s: %s", absFilename, err)
}
w.WriteHeader(http.StatusCreated)
return nil
}
func readConfig(configFilename string, conf *Config) error {
configData, err := os.ReadFile(configFilename)
if err != nil {
log.Fatal("Configuration file config.toml cannot be read:", err, "...Exiting.")
return err
}
if _, err := toml.Decode(string(configData), conf); err != nil {
log.Fatal("Config file config.toml is invalid:", err)
return err
}
return nil
}
func setLogLevel() {
switch conf.LogLevel {
case "info":
log.SetLevel(logrus.InfoLevel)
case "warn":
log.SetLevel(logrus.WarnLevel)
case "error":
log.SetLevel(logrus.ErrorLevel)
default:
log.SetLevel(logrus.WarnLevel)
fmt.Print("Invalid log level set in config. Defaulting to \"warn\"")
}
}
/*
* Main function
*/
func main() {
var configFile string
var proto string
/*
* Read startup arguments
*/
flag.StringVar(&configFile, "config", "./config.toml", "Path to configuration file \"config.toml\".")
flag.Parse()
if !flag.Parsed() {
log.Fatalln("Could not parse flags")
}
/*
* Read config file
*/
err := readConfig(configFile, &conf)
if err != nil {
log.Fatalln("There was an error while reading the configuration file:", err)
}
// Select proto
if conf.UnixSocket {
proto = "unix"
} else {
proto = "tcp"
}
/*
* Start HTTP server
*/
log.Println("Starting prosody-filer", versionString, "...")
listener, err := net.Listen(proto, conf.ListenPort)
if err != nil {
log.Fatalln("Could not open listening socket:", err)
}
subpath := path.Join("/", conf.UploadSubDir)
subpath = strings.TrimRight(subpath, "/")
subpath += "/"
http.HandleFunc(subpath, handleRequest)
log.Printf("Server started on port %s. Waiting for requests.\n", conf.ListenPort)
// Set log level
setLogLevel()
http.Serve(listener, nil)
// This line will only be reached when quitting
}