-
Notifications
You must be signed in to change notification settings - Fork 717
/
index.go
488 lines (463 loc) 路 14.5 KB
/
index.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package plg_editor_onlyoffice
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
. "github.com/mickael-kerjean/filestash/server/common"
"github.com/mickael-kerjean/filestash/server/ctrl"
. "github.com/mickael-kerjean/filestash/server/middleware"
"github.com/mickael-kerjean/filestash/server/model"
"github.com/patrickmn/go-cache"
"io"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path/filepath"
"strings"
"time"
)
var (
SECRET_KEY_DERIVATE_FOR_ONLYOFFICE string
OnlyOfficeCache *cache.Cache
)
type OnlyOfficeCacheData struct {
Path string
Save func(path string, file io.Reader) error
Cat func(path string) (io.ReadCloser, error)
}
func init() {
plugin_enable := func() bool {
return Config.Get("features.office.enable").Schema(func(f *FormElement) *FormElement {
if f == nil {
f = &FormElement{}
}
f.Name = "enable"
f.Type = "enable"
f.Target = []string{"onlyoffice_server", "onlyoffice_can_download"}
f.Description = "Enable/Disable the office suite to manage word, excel and powerpoint documents. This setting requires a restart to comes into effect"
f.Default = false
if u := os.Getenv("ONLYOFFICE_URL"); u != "" {
f.Default = true
}
return f
}).Bool()
}()
Config.Get("features.office.onlyoffice_server").Schema(func(f *FormElement) *FormElement {
if f == nil {
f = &FormElement{}
}
f.Id = "onlyoffice_server"
f.Name = "onlyoffice_server"
f.Type = "text"
f.Description = "Location of your OnlyOffice server"
f.Default = "http://127.0.0.1:8080"
f.Placeholder = "Eg: http://127.0.0.1:8080"
if u := os.Getenv("ONLYOFFICE_URL"); u != "" {
f.Default = u
f.Placeholder = fmt.Sprintf("Default: '%s'", u)
}
return f
})
Config.Get("features.office.can_download").Schema(func(f *FormElement) *FormElement {
if f == nil {
f = &FormElement{}
}
f.Id = "onlyoffice_can_download"
f.Name = "can_download"
f.Type = "boolean"
f.Description = "Display Download button in onlyoffice"
f.Default = true
return f
})
if plugin_enable == false {
return
}
SECRET_KEY_DERIVATE_FOR_ONLYOFFICE = Hash("ONLYOFFICE_"+SECRET_KEY, len(SECRET_KEY))
Hooks.Register.HttpEndpoint(func(r *mux.Router, app *App) error {
oods := r.PathPrefix("/onlyoffice").Subrouter()
oods.PathPrefix("/static/").HandlerFunc(StaticHandler).Methods("GET", "POST")
oods.HandleFunc("/event", OnlyOfficeEventHandler).Methods("POST")
oods.HandleFunc("/content", FetchContentHandler).Methods("GET")
r.HandleFunc(
COOKIE_PATH+"onlyoffice/iframe",
NewMiddlewareChain(
IframeContentHandler,
[]Middleware{SessionStart, LoggedInOnly},
*app,
),
).Methods("GET")
return nil
})
Hooks.Register.XDGOpen(`
if(mime === "application/word" || mime === "application/msword" ||
mime === "application/vnd.oasis.opendocument.text" || mime === "application/vnd.oasis.opendocument.spreadsheet" ||
mime === "application/excel" || mime === "application/vnd.ms-excel" || mime === "application/powerpoint" ||
mime === "application/vnd.ms-powerpoint" || mime === "application/vnd.oasis.opendocument.presentation" ) {
return ["appframe", {"endpoint": "/api/onlyoffice/iframe"}];
}
`)
OnlyOfficeCache = cache.New(720*time.Minute, 720*time.Minute)
}
func StaticHandler(res http.ResponseWriter, req *http.Request) {
req.URL.Path = strings.TrimPrefix(req.URL.Path, "/onlyoffice/static")
oodsLocation := Config.Get("features.office.onlyoffice_server").String()
u, err := url.Parse(oodsLocation)
if err != nil {
SendErrorResult(res, err)
return
}
req.Header.Set("X-Forwarded-Host", req.Host+"/onlyoffice/static")
req.Header.Set("X-Forwarded-Proto", func() string {
if scheme := req.Header.Get("X-Forwarded-Proto"); scheme != "" {
return scheme
} else if req.TLS != nil {
return "https"
}
return "http"
}())
// This code is a copy and paste from httputil.NewSingleHostReverseProxy with 1 single change
// to do SSL termination.
reverseProxy := &httputil.ReverseProxy{
Director: func(rq *http.Request) {
rq.URL.Scheme = "http" // <- this is the only change from NewSingleHostReverseProxy
rq.URL.Host = u.Host
rq.URL.Path = func(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}(u.Path, rq.URL.Path)
if u.RawQuery == "" || rq.URL.RawQuery == "" {
rq.URL.RawQuery = u.RawQuery + rq.URL.RawQuery
} else {
rq.URL.RawQuery = u.RawQuery + "&" + rq.URL.RawQuery
}
},
}
reverseProxy.ErrorHandler = func(rw http.ResponseWriter, rq *http.Request, err error) {
Log.Warning("[onlyoffice] %s", err.Error())
SendErrorResult(rw, NewError(err.Error(), http.StatusBadGateway))
}
reverseProxy.ServeHTTP(res, req)
}
func IframeContentHandler(ctx *App, res http.ResponseWriter, req *http.Request) {
if model.CanRead(ctx) == false {
SendErrorResult(res, ErrPermissionDenied)
return
} else if oodsLocation := Config.Get("features.office.onlyoffice_server").String(); oodsLocation == "" {
res.WriteHeader(http.StatusServiceUnavailable)
res.Write([]byte("<p>The Onlyoffice server hasn't been configured</p>"))
res.Write([]byte("<style>p {color: white; text-align: center; margin-top: 50px; font-size: 20px; opacity: 0.6; font-family: monospace; } </style>"))
return
}
var (
path string // path of the file we want to open via onlyoffice
filestashServerLocation string // location from which the oods server can reach filestash
userId string // as seen by onlyoffice to distinguish different users
username string // username as displayed by only office
key string // unique identifier for a file as seen be only office
contentType string // name of the application in onlyoffice
filetype string // extension of the document
filename string // filename of the document
oodsMode string // edit mode
oodsDevice string // mobile, desktop of embedded
localip string
)
query := req.URL.Query()
path, err := ctrl.PathBuilder(ctx, query.Get("path"))
if err != nil {
SendErrorResult(res, err)
return
}
userId = GenerateID(ctx)
f, err := ctx.Backend.Cat(path)
if err != nil {
SendErrorResult(res, err)
return
}
key = HashStream(f, 20)
key = Hash(key+userId+path, 20)
filename = filepath.Base(path)
oodsMode = func() string {
if model.CanEdit(ctx) == false {
return "view"
}
return "edit"
}()
oodsDevice = func() string {
ua := req.Header.Get("User-Agent")
if ua == "" {
return "desktop"
} else if strings.Contains(ua, "iPhone") {
return "mobile"
} else if strings.Contains(ua, "iPad") {
return "mobile"
} else if strings.Contains(ua, "Android") {
return "mobile"
} else if strings.Contains(ua, "Mobile") {
return "mobile"
}
if oodsMode == "view" {
return "embedded"
}
return "desktop"
}()
username = func() string {
if ctx.Session["username"] != "" {
return ctx.Session["username"]
}
return "Me"
}()
if ctx.Share.Id != "" {
username = "Anonymous"
userId = RandomString(10)
}
localip = func() string { // https://stackoverflow.com/questions/23558425/how-do-i-get-the-local-ip-address-in-go#23558495
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
maybeips := []string{}
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
maybeips = append(maybeips, ipnet.IP.String())
}
}
}
// if there is just one interface, we can just pick that one
if len(maybeips) == 1 {
return maybeips[0]
}
// if not, fallback to capturing our outgoing local ip
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return ""
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP.String()
}()
filestashServerLocation = fmt.Sprintf(
"%s://%s:%d",
func() string { // proto
if req.TLS == nil {
return "http"
}
return "https"
}(),
localip,
Config.Get("general.port").Int(),
)
contentType = func(p string) string {
var (
word string = "text"
excel string = "spreadsheet"
powerpoint string = "presentation"
)
switch GetMimeType(p) {
case "application/word":
return word
case "application/msword":
return word
case "application/vnd.oasis.opendocument.text":
return word
case "application/vnd.oasis.opendocument.spreadsheet":
return excel
case "application/excel":
return excel
case "application/vnd.ms-excel":
return excel
case "application/powerpoint":
return powerpoint
case "application/vnd.ms-powerpoint":
return powerpoint
case "application/vnd.oasis.opendocument.presentation":
return powerpoint
}
return ""
}(path)
filetype = strings.TrimPrefix(filepath.Ext(filename), ".")
OnlyOfficeCache.Set(key, &OnlyOfficeCacheData{path, ctx.Backend.Save, ctx.Backend.Cat}, cache.DefaultExpiration)
res.Write([]byte(fmt.Sprintf(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<style> body { margin: 0; } body, html{ height: 100%%; } iframe { width: 100%%; height: 100%%; } </style>
<div id="placeholder"></div>
<script type="text/javascript" src="/onlyoffice/static/web-apps/apps/api/documents/api.js"></script>
<script>
if("DocsAPI" in window) loadApplication();
else sendError("[error] Can't reach the onlyoffice server");
function loadApplication() {
new DocsAPI.DocEditor("placeholder", {
"token": "foobar",
"documentType": "%s",
"type": "%s",
"document": {
"title": "%s",
"url": "%s/onlyoffice/content?key=%s",
"fileType": "%s",
"key": "%s",
"permissions": {
"download": %s
}
},
"editorConfig": {
"callbackUrl": "%s/onlyoffice/event",
"mode": "%s",
"customization": {
"autosave": false,
"forcesave": true,
"compactHeader": true
},
"user": {
"id": "%s",
"name": "%s"
}
}
});
}
function sendError(message){
let $el = document.createElement("p");
$el.innerHTML = message;
$el.setAttribute("style", "text-align: center; color: white; opacity: 0.8; font-size: 20px; font-family: monospace;");
document.body.appendChild($el);
}
</script>
</body>
</html>`,
contentType,
oodsDevice,
filename,
filestashServerLocation, key,
filetype,
key,
func() string {
if Config.Get("features.office.can_download").Bool() {
return "true"
}
return "false"
}(),
filestashServerLocation,
oodsMode,
userId,
username,
)))
}
func FetchContentHandler(res http.ResponseWriter, req *http.Request) {
var key string
if key = req.URL.Query().Get("key"); key == "" {
SendErrorResult(res, NewError("unspecified key", http.StatusBadRequest))
return
}
c, found := OnlyOfficeCache.Get(key)
if found == false {
res.WriteHeader(http.StatusInternalServerError)
res.Write([]byte(`{"error": 1, "message": "missing data fetcher handler"}`))
return
}
cData, valid := c.(*OnlyOfficeCacheData)
if valid == false {
res.WriteHeader(http.StatusInternalServerError)
res.Write([]byte(`{"error": 1, "message": "invalid cache"}`))
return
}
f, err := cData.Cat(cData.Path)
if err != nil {
res.WriteHeader(http.StatusNotFound)
res.Write([]byte(`{"error": 1, "message": "error while fetching data"}`))
return
}
io.Copy(res, f)
f.Close()
}
type OnlyOfficeEventObject struct {
Actions []struct {
Type int `json: "type"`
UserId string `json: "userid" `
} `json: "actions"`
ChangesURL string `json: "changesurl"`
Forcesavetype int `json: "forcesavetype"`
History struct {
ServerVersion string `json: "serverVersion"`
Changes []struct {
Created string `json: "created"`
User struct {
Id string `json: "id"`
Name string `json: "name"`
}
} `json: "changes"`
} `json: "history"`
Key string `json: "key"`
Status int `json: "status"`
Url string `json: "url"`
UserData string `json: "userdata"`
Lastsave string `json: "lastsave"`
Users []string `json: "users"`
}
func OnlyOfficeEventHandler(res http.ResponseWriter, req *http.Request) {
event := OnlyOfficeEventObject{}
if err := json.NewDecoder(req.Body).Decode(&event); err != nil {
SendErrorResult(res, err)
return
}
req.Body.Close()
switch event.Status {
case 0:
Log.Warning("[onlyoffice] no document with the key identifier could be found. %+v", event)
case 1:
// document is being edited
case 2:
// document is ready for saving
case 3:
// document saving error has occurred
Log.Warning("[onlyoffice] document saving error has occurred. %+v", event)
case 4:
// document is closed with no changes
case 5:
Log.Warning("[onlyoffice] undocumented status. %+v", event)
case 6: // document is being edited, but the current document state is saved
saveObject, found := OnlyOfficeCache.Get(event.Key)
if found == false {
res.WriteHeader(http.StatusInternalServerError)
res.Write([]byte(`{"error": 1, "message": "doens't know where to store the given data"}`))
return
}
cData, valid := saveObject.(*OnlyOfficeCacheData)
if valid == false {
res.WriteHeader(http.StatusInternalServerError)
res.Write([]byte(`{"error": 1, "message": "[internal error] invalid save handler"}`))
return
}
r, err := http.NewRequest("GET", event.Url, nil)
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
res.Write([]byte(`{"error": 1, "message": "couldn't fetch the document on the oods server"}`))
return
}
f, err := HTTPClient.Do(r)
if err = cData.Save(cData.Path, f.Body); err != nil {
res.WriteHeader(http.StatusInternalServerError)
res.Write([]byte(`{"error": 1, "message": "error while saving the document"}`))
return
}
f.Body.Close()
case 7:
Log.Warning("[onlyoffice] error has occurred while force saving the document. %+v", event)
default:
Log.Warning("[onlyoffice] undocumented status. %+v", event)
}
res.Write([]byte(`{"error": 0}`))
}