alist/server/webdav.go

60 lines
1.4 KiB
Go
Raw Normal View History

2021-11-27 16:12:04 +00:00
package server
import (
2021-11-28 08:18:09 +00:00
"github.com/Xhofe/alist/conf"
2021-11-27 16:12:04 +00:00
"github.com/Xhofe/alist/server/webdav"
"github.com/gin-gonic/gin"
2021-11-28 08:18:09 +00:00
"net/http"
2021-11-27 16:12:04 +00:00
)
var handler *webdav.Handler
func init() {
handler = &webdav.Handler{
Prefix: "/dav",
LockSystem: webdav.NewMemLS(),
}
}
func WebDav(r *gin.Engine) {
dav := r.Group("/dav")
2021-11-28 08:18:09 +00:00
dav.Use(WebDAVAuth)
2021-11-27 16:12:04 +00:00
dav.Any("/*path", ServeWebDAV)
dav.Any("", ServeWebDAV)
dav.Handle("PROPFIND", "/*path", ServeWebDAV)
dav.Handle("PROPFIND", "", ServeWebDAV)
dav.Handle("MKCOL", "/*path", ServeWebDAV)
dav.Handle("LOCK", "/*path", ServeWebDAV)
dav.Handle("UNLOCK", "/*path", ServeWebDAV)
dav.Handle("PROPPATCH", "/*path", ServeWebDAV)
dav.Handle("COPY", "/*path", ServeWebDAV)
dav.Handle("MOVE", "/*path", ServeWebDAV)
}
func ServeWebDAV(c *gin.Context) {
fs := webdav.FileSystem{}
handler.ServeHTTP(c.Writer,c.Request,&fs)
2021-11-28 08:18:09 +00:00
}
func WebDAVAuth(c *gin.Context) {
if c.Request.Method == "OPTIONS" {
c.Next()
return
}
username, password, ok := c.Request.BasicAuth()
if !ok {
c.Writer.Header()["WWW-Authenticate"] = []string{`Basic realm="alist"`}
c.Status(http.StatusUnauthorized)
c.Abort()
return
}
if conf.DavUsername != "" && conf.DavUsername != username {
c.Status(http.StatusUnauthorized)
c.Abort()
}
if conf.DavPassword != "" && conf.DavPassword != password {
c.Status(http.StatusUnauthorized)
c.Abort()
}
c.Next()
2021-11-27 16:12:04 +00:00
}