Skip to content

Commit

Permalink
feat: webdav handle
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 30, 2022
1 parent dd013ac commit 2b17266
Show file tree
Hide file tree
Showing 11 changed files with 344 additions and 3,451 deletions.
26 changes: 26 additions & 0 deletions pkg/utils/ip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package utils

import (
"net"
"net/http"
"strings"
)

func ClientIP(r *http.Request) string {
xForwardedFor := r.Header.Get("X-Forwarded-For")
ip := strings.TrimSpace(strings.Split(xForwardedFor, ",")[0])
if ip != "" {
return ip
}

ip = strings.TrimSpace(r.Header.Get("X-Real-Ip"))
if ip != "" {
return ip
}

if ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr)); err == nil {
return ip
}

return ""
}
6 changes: 5 additions & 1 deletion server/common/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ import (

func GetBaseUrl(r *http.Request) string {
baseUrl := setting.GetByKey(conf.BaseUrl)
protocol := "http"
if r.TLS != nil {
protocol = "https"
}
if baseUrl == "" {
baseUrl = fmt.Sprintf("//%s", r.Host)
baseUrl = fmt.Sprintf("%s//%s", protocol, r.Host)
}
strings.TrimSuffix(baseUrl, "/")
return baseUrl
Expand Down
1 change: 1 addition & 0 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
func Init(r *gin.Engine) {
common.SecretKey = []byte(conf.Conf.JwtSecret)
Cors(r)
WebDav(r)

r.GET("/d/*path", middlewares.Down, controllers.Down)
r.GET("/p/*path", middlewares.Down, controllers.Proxy)
Expand Down
94 changes: 94 additions & 0 deletions server/webdav.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package server

import (
"context"
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/server/webdav"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"net/http"
)

var handler *webdav.Handler

func init() {
handler = &webdav.Handler{
Prefix: "/dav",
LockSystem: webdav.NewMemLS(),
Logger: func(request *http.Request, err error) {
log.Errorf("%s %s %+v", request.Method, request.URL.Path, err)
},
}
}

func WebDav(r *gin.Engine) {
dav := r.Group("/dav")
dav.Use(WebDAVAuth)
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) {
user := c.MustGet("user").(*model.User)
ctx := context.WithValue(c.Request.Context(), "user", user)
handler.ServeHTTP(c.Writer, c.Request.WithContext(ctx))
}

func WebDAVAuth(c *gin.Context) {
guest, _ := db.GetGuest()
username, password, ok := c.Request.BasicAuth()
if !ok {
if c.Request.Method == "OPTIONS" {
c.Set("user", guest)
c.Next()
return
}
c.Writer.Header()["WWW-Authenticate"] = []string{`Basic realm="alist"`}
c.Status(http.StatusUnauthorized)
c.Abort()
return
}
user, err := db.GetUserByName(username)
if err != nil || user.ValidatePassword(password) != nil {
if c.Request.Method == "OPTIONS" {
c.Set("user", guest)
c.Next()
return
}
c.Status(http.StatusUnauthorized)
c.Abort()
return
}
if !user.CanWebdavRead() {
if c.Request.Method == "OPTIONS" {
c.Set("user", guest)
c.Next()
return
}
c.Status(http.StatusForbidden)
c.Abort()
return
}
if !user.CanWebdavWrite() && utils.SliceContains([]string{"PUT", "DELETE", "PROPPATCH", "MKCOL", "COPY", "MOVE"}, c.Request.Method) {
if c.Request.Method == "OPTIONS" {
c.Set("user", guest)
c.Next()
return
}
c.Status(http.StatusForbidden)
c.Abort()
return
}
c.Set("user", user)
c.Next()
}
Loading

0 comments on commit 2b17266

Please sign in to comment.