webdav proxy

pull/548/head
微凉 2021-11-29 16:42:46 +08:00
parent 4ff2756572
commit ffdd88ec66
10 changed files with 76 additions and 37 deletions

View File

@ -16,15 +16,14 @@ type Pan123 struct {}
var driverName = "123Pan" var driverName = "123Pan"
func (driver Pan123) Config() drivers.DriverConfig {
return drivers.DriverConfig{
OnlyProxy: false,
}
}
func (driver Pan123) Items() []drivers.Item { func (driver Pan123) Items() []drivers.Item {
return []drivers.Item{ return []drivers.Item{
{
Name: "proxy",
Label: "proxy",
Type: "bool",
Required: true,
Description: "allow proxy",
},
{ {
Name: "username", Name: "username",
Label: "username", Label: "username",

View File

@ -15,15 +15,14 @@ type Cloud189 struct {}
var driverName = "189Cloud" var driverName = "189Cloud"
func (driver Cloud189) Config() drivers.DriverConfig {
return drivers.DriverConfig{
OnlyProxy: false,
}
}
func (driver Cloud189) Items() []drivers.Item { func (driver Cloud189) Items() []drivers.Item {
return []drivers.Item{ return []drivers.Item{
{
Name: "proxy",
Label: "proxy",
Type: "bool",
Required: true,
Description: "allow proxy",
},
{ {
Name: "username", Name: "username",
Label: "username", Label: "username",

View File

@ -16,15 +16,14 @@ type AliDrive struct{}
var driverName = "AliDrive" var driverName = "AliDrive"
func (driver AliDrive) Config() drivers.DriverConfig {
return drivers.DriverConfig{
OnlyProxy: false,
}
}
func (driver AliDrive) Items() []drivers.Item { func (driver AliDrive) Items() []drivers.Item {
return []drivers.Item{ return []drivers.Item{
{
Name: "proxy",
Label: "proxy",
Type: "bool",
Required: true,
Description: "allow proxy",
},
{ {
Name: "order_by", Name: "order_by",
Label: "order_by", Label: "order_by",

View File

@ -8,7 +8,12 @@ import (
"net/http" "net/http"
) )
type DriverConfig struct {
OnlyProxy bool
}
type Driver interface { type Driver interface {
Config() DriverConfig
Items() []Item Items() []Item
Save(account *model.Account, old *model.Account) error Save(account *model.Account, old *model.Account) error
File(path string, account *model.Account) (*model.File, error) File(path string, account *model.Account) (*model.File, error)
@ -54,7 +59,26 @@ func GetDriver(name string) (driver Driver, ok bool) {
func GetDrivers() map[string][]Item { func GetDrivers() map[string][]Item {
res := make(map[string][]Item, 0) res := make(map[string][]Item, 0)
for k, v := range driversMap { for k, v := range driversMap {
res[k] = v.Items() if v.Config().OnlyProxy {
res[k] = v.Items()
} else {
res[k] = append([]Item{
{
Name: "proxy",
Label: "proxy",
Type: "bool",
Required: true,
Description: "allow proxy",
},
{
Name: "webdav_proxy",
Label: "webdav proxy",
Type: "bool",
Required: true,
Description: "Transfer the WebDAV of this account through the server",
},
}, v.Items()...)
}
} }
return res return res
} }

View File

@ -15,6 +15,12 @@ type GoogleDrive struct{}
var driverName = "GoogleDrive" var driverName = "GoogleDrive"
func (driver GoogleDrive) Config() drivers.DriverConfig {
return drivers.DriverConfig{
OnlyProxy: true,
}
}
func (driver GoogleDrive) Items() []drivers.Item { func (driver GoogleDrive) Items() []drivers.Item {
return []drivers.Item{ return []drivers.Item{
{ {

View File

@ -18,6 +18,12 @@ type Native struct{}
var driverName = "Native" var driverName = "Native"
func (driver Native) Config() drivers.DriverConfig {
return drivers.DriverConfig{
OnlyProxy: true,
}
}
func (driver Native) Items() []drivers.Item { func (driver Native) Items() []drivers.Item {
return []drivers.Item{ return []drivers.Item{
{ {

View File

@ -16,15 +16,14 @@ type Onedrive struct{}
var driverName = "Onedrive" var driverName = "Onedrive"
func (driver Onedrive) Config() drivers.DriverConfig {
return drivers.DriverConfig{
OnlyProxy: false,
}
}
func (driver Onedrive) Items() []drivers.Item { func (driver Onedrive) Items() []drivers.Item {
return []drivers.Item{ return []drivers.Item{
{
Name: "proxy",
Label: "proxy",
Type: "bool",
Required: true,
Description: "allow proxy",
},
{ {
Name: "zone", Name: "zone",
Label: "zone", Label: "zone",

View File

@ -32,6 +32,7 @@ type Account struct {
SiteUrl string `json:"site_url"` SiteUrl string `json:"site_url"`
SiteId string `json:"site_id"` SiteId string `json:"site_id"`
OnedriveType string `json:"onedrive_type"` OnedriveType string `json:"onedrive_type"`
WebdavProxy bool `json:"webdav_proxy"`
} }
var accountsMap = map[string]Account{} var accountsMap = map[string]Account{}

View File

@ -101,8 +101,9 @@ func GetPW(path string) string {
} }
} }
func (fs *FileSystem) Link(host, rawPath string) (string, error) { func (fs *FileSystem) Link(r *http.Request, rawPath string) (string, error) {
rawPath = utils.ParsePath(rawPath) rawPath = utils.ParsePath(rawPath)
log.Debugf("get link path: %s", rawPath)
if model.AccountsCount() > 1 && rawPath == "/" { if model.AccountsCount() > 1 && rawPath == "/" {
// error // error
} }
@ -110,16 +111,22 @@ func (fs *FileSystem) Link(host, rawPath string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
if account.Type == "Native" || account.Type == "GoogleDrive" { link := ""
link := fmt.Sprintf("//%s/p%s", host, rawPath) protocol := "http"
if r.TLS != nil {
protocol = "https"
}
if driver.Config().OnlyProxy || account.WebdavProxy {
link = fmt.Sprintf("%s://%s/p%s", protocol, r.Host, rawPath)
if conf.CheckDown { if conf.CheckDown {
pw := GetPW(filepath.Dir(rawPath)) pw := GetPW(filepath.Dir(rawPath))
link += "?pw" + pw link += "?pw" + pw
} }
log.Debugf("proxy link: %s", link) } else {
return link, nil link, err = driver.Link(path_, account)
} }
return driver.Link(path_, account) log.Debugf("webdav get link: %s", link)
return link, err
} }
func (fs *FileSystem) CreateDirectory(ctx context.Context, reqPath string) (interface{}, error) { func (fs *FileSystem) CreateDirectory(ctx context.Context, reqPath string) (interface{}, error) {

View File

@ -233,8 +233,7 @@ func (h *Handler) handleGetHeadPost(w http.ResponseWriter, r *http.Request, fs *
} }
w.Header().Set("ETag", etag) w.Header().Set("ETag", etag)
log.Debugf("url: %+v", r.URL) log.Debugf("url: %+v", r.URL)
host := r.Host link, err := fs.Link(r, reqPath)
link, err := fs.Link(host, reqPath)
if err != nil { if err != nil {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
} }