Compare commits

...

17 Commits

Author SHA1 Message Date
1d7d37e642 🐛 fix onedrive sort 2021-12-01 11:25:33 +08:00
b62a716267 remove onedrive empty cache 2021-12-01 11:25:04 +08:00
944941db10 🎇 support https 2021-12-01 00:19:06 +08:00
bd91acc5d0 🐛 fix lanzou webdav time error 2021-12-01 00:15:34 +08:00
cd50227835 🎇 support lanzou 2021-11-30 21:30:50 +08:00
50226f66e3 google default root folder 2021-11-30 19:10:27 +08:00
9dcaa9b07a 🎨 add types 2021-11-30 18:53:37 +08:00
fa6c0f78bc 🐛 fix windows sharepoint path error 2021-11-30 16:45:08 +08:00
7f35dc6ade add onedrive thumbnail 2021-11-30 16:27:23 +08:00
5d6463b75a 🎨 move code 2021-11-30 10:00:06 +08:00
733b38b435 🎨 improve code structure 2021-11-30 09:37:51 +08:00
50a02a7af7 🔥 remove useless code 2021-11-29 21:30:52 +08:00
71b1517de7 🐛 fix windows check parent password 2021-11-29 21:09:16 +08:00
ffdd88ec66 webdav proxy 2021-11-29 16:42:46 +08:00
4ff2756572 🐛 fix 123pan webdav can't get 2021-11-29 16:06:49 +08:00
d955038ebc delete useless path unescape 2021-11-29 16:05:58 +08:00
72d5e4e691 delete useless log 2021-11-29 16:05:14 +08:00
30 changed files with 652 additions and 200 deletions

View File

@ -54,7 +54,12 @@ func main() {
server.InitApiRouter(r)
base := fmt.Sprintf("%s:%d", conf.Conf.Address, conf.Conf.Port)
log.Infof("start server @ %s", base)
err := r.Run(base)
var err error
if conf.Conf.Https {
err = r.RunTLS(base, conf.Conf.CertFile, conf.Conf.KeyFile)
} else {
err = r.Run(base)
}
if err != nil {
log.Errorf("failed to start: %s", err.Error())
}

View File

@ -1,10 +0,0 @@
package bootstrap
import (
_ "github.com/Xhofe/alist/drivers/123pan"
_ "github.com/Xhofe/alist/drivers/189cloud"
_ "github.com/Xhofe/alist/drivers/alidrive"
_ "github.com/Xhofe/alist/drivers/googledrive"
_ "github.com/Xhofe/alist/drivers/native"
_ "github.com/Xhofe/alist/drivers/onedrive"
)

View File

@ -5,7 +5,7 @@ import (
log "github.com/sirupsen/logrus"
)
// initLog init log
// InitLog init log
func InitLog() {
if conf.Debug {
log.SetLevel(log.DebugLevel)
@ -18,4 +18,5 @@ func InitLog() {
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
})
log.Infof("init log...")
}

View File

@ -14,6 +14,9 @@ type Config struct {
Address string `json:"address"`
Port int `json:"port"`
Database Database `json:"database"`
Https bool `json:"https"`
CertFile string `json:"cert_file"`
KeyFile string `json:"key_file"`
}
func DefaultConfig() *Config {
@ -22,11 +25,7 @@ func DefaultConfig() *Config {
Port: 5244,
Database: Database{
Type: "sqlite3",
User: "",
Password: "",
Host: "",
Port: 0,
Name: "",
TablePrefix: "x_",
DBFile: "data/data.db",
},

View File

@ -1,13 +1,11 @@
package _23pan
package drivers
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
"path/filepath"
"strconv"
"time"
@ -54,7 +52,7 @@ func (driver Pan123) Login(account *model.Account) error {
var resp Pan123TokenResp
_, err := pan123Client.R().
SetResult(&resp).
SetBody(drivers.Json{
SetBody(Json{
"passport": account.Username,
"password": account.Password,
}).Post("https://www.123pan.com/api/user/sign_in")
@ -77,7 +75,7 @@ func (driver Pan123) FormatFile(file *Pan123File) *model.File {
Id: strconv.FormatInt(file.FileId, 10),
Name: file.FileName,
Size: file.Size,
Driver: driverName,
Driver: driver.Config().Name,
UpdatedAt: file.UpdateAt,
}
if file.Type == 1 {
@ -107,7 +105,6 @@ func (driver Pan123) GetFiles(parentId string, account *model.Account) ([]Pan123
if err != nil {
return nil, err
}
log.Debugf("%+v", resp)
if resp.Code != 0 {
if resp.Code == 401 {
err := driver.Login(account)
@ -138,14 +135,14 @@ func (driver Pan123) GetFile(path string, account *model.Account) (*Pan123File,
if file.Type != conf.FOLDER {
return &file, err
} else {
return nil, drivers.NotFile
return nil, NotFile
}
}
}
return nil, drivers.PathNotFound
return nil, PathNotFound
}
func init() {
drivers.RegisterDriver(driverName, &Pan123{})
RegisterDriver(&Pan123{})
pan123Client.SetRetryCount(3)
}

View File

@ -1,29 +1,27 @@
package _23pan
package drivers
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
url "net/url"
"path/filepath"
)
type Pan123 struct {}
var driverName = "123Pan"
func (driver Pan123) Config() DriverConfig {
return DriverConfig{
Name: "123Pan",
OnlyProxy: false,
}
}
func (driver Pan123) Items() []drivers.Item {
return []drivers.Item{
{
Name: "proxy",
Label: "proxy",
Type: "bool",
Required: true,
Description: "allow proxy",
},
func (driver Pan123) Items() []Item {
return []Item{
{
Name: "username",
Label: "username",
@ -77,7 +75,7 @@ func (driver Pan123) File(path string, account *model.Account) (*model.File, err
Name: account.Name,
Size: 0,
Type: conf.FOLDER,
Driver: driverName,
Driver: driver.Config().Name,
UpdatedAt: account.UpdatedAt,
}, nil
}
@ -91,7 +89,7 @@ func (driver Pan123) File(path string, account *model.Account) (*model.File, err
return &file, nil
}
}
return nil, drivers.PathNotFound
return nil, PathNotFound
}
func (driver Pan123) Files(path string, account *model.Account) ([]model.File, error) {
@ -127,7 +125,7 @@ func (driver Pan123) Link(path string, account *model.Account) (string, error) {
}
var resp Pan123DownResp
_, err = pan123Client.R().SetResult(&resp).SetHeader("authorization", "Bearer "+account.AccessToken).
SetBody(drivers.Json{
SetBody(Json{
"driveId": 0,
"etag": file.Etag,
"fileId": file.FileId,
@ -149,6 +147,19 @@ func (driver Pan123) Link(path string, account *model.Account) (string, error) {
}
return "", fmt.Errorf(resp.Message)
}
u,err := url.Parse(resp.Data.DownloadUrl)
if err != nil {
return "", err
}
u_ := fmt.Sprintf("https://%s%s",u.Host,u.Path)
res, err := NoRedirectClient.R().SetQueryParamsFromValues(u.Query()).Get(u_)
if err != nil {
return "", err
}
log.Debug(res.String())
if res.StatusCode() == 302 {
return res.Header().Get("location"), nil
}
return resp.Data.DownloadUrl, nil
}
@ -175,7 +186,7 @@ func (driver Pan123) Proxy(c *gin.Context, account *model.Account) {
}
func (driver Pan123) Preview(path string, account *model.Account) (interface{}, error) {
return nil, nil
return nil, NotSupport
}
var _ drivers.Driver = (*Pan123)(nil)
var _ Driver = (*Pan123)(nil)

View File

@ -1,4 +1,4 @@
package _89cloud
package drivers
import (
"crypto/rand"
@ -9,7 +9,6 @@ import (
"encoding/pem"
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/go-resty/resty/v2"
@ -30,7 +29,7 @@ func (driver Cloud189) FormatFile(file *Cloud189File) *model.File {
Id: strconv.FormatInt(file.Id, 10),
Name: file.Name,
Size: file.Size,
Driver: "189Cloud",
Driver: driver.Config().Name,
UpdatedAt: nil,
Thumbnail: file.Icon.SmallUrl,
Url: file.Url,
@ -313,6 +312,6 @@ func b64tohex(a string) string {
}
func init() {
drivers.RegisterDriver(driverName, &Cloud189{})
RegisterDriver(&Cloud189{})
client189Map = make(map[string]*resty.Client, 0)
}

View File

@ -1,9 +1,8 @@
package _89cloud
package drivers
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
@ -13,17 +12,15 @@ import (
type Cloud189 struct {}
var driverName = "189Cloud"
func (driver Cloud189) Config() DriverConfig {
return DriverConfig{
Name: "189Cloud",
OnlyProxy: false,
}
}
func (driver Cloud189) Items() []drivers.Item {
return []drivers.Item{
{
Name: "proxy",
Label: "proxy",
Type: "bool",
Required: true,
Description: "allow proxy",
},
func (driver Cloud189) Items() []Item {
return []Item{
{
Name: "username",
Label: "username",
@ -86,7 +83,7 @@ func (driver Cloud189) File(path string, account *model.Account) (*model.File, e
Name: account.Name,
Size: 0,
Type: conf.FOLDER,
Driver: driverName,
Driver: driver.Config().Name,
UpdatedAt: account.UpdatedAt,
}, nil
}
@ -100,7 +97,7 @@ func (driver Cloud189) File(path string, account *model.Account) (*model.File, e
return &file, nil
}
}
return nil, drivers.PathNotFound
return nil, PathNotFound
}
func (driver Cloud189) Files(path string, account *model.Account) ([]model.File, error) {
@ -135,7 +132,7 @@ func (driver Cloud189) Link(path string, account *model.Account) (string, error)
return "", err
}
if file.Type == conf.FOLDER {
return "", drivers.NotFile
return "", NotFile
}
client, ok := client189Map[account.Name]
if !ok {
@ -164,7 +161,7 @@ func (driver Cloud189) Link(path string, account *model.Account) (string, error)
if resp.ResCode != 0 {
return "", fmt.Errorf(resp.ResMessage)
}
res, err := drivers.NoRedirectClient.R().Get(resp.FileDownloadUrl)
res, err := NoRedirectClient.R().Get(resp.FileDownloadUrl)
if err != nil {
return "", err
}
@ -197,7 +194,7 @@ func (driver Cloud189) Proxy(ctx *gin.Context, account *model.Account) {
}
func (driver Cloud189) Preview(path string, account *model.Account) (interface{}, error) {
return nil, nil
return nil, NotSupport
}
var _ drivers.Driver = (*Cloud189)(nil)
var _ Driver = (*Cloud189)(nil)

View File

@ -1,9 +1,8 @@
package alidrive
package drivers
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
@ -14,17 +13,15 @@ import (
type AliDrive struct{}
var driverName = "AliDrive"
func (driver AliDrive) Config() DriverConfig {
return DriverConfig{
Name: "AliDrive",
OnlyProxy: false,
}
}
func (driver AliDrive) Items() []drivers.Item {
return []drivers.Item{
{
Name: "proxy",
Label: "proxy",
Type: "bool",
Required: true,
Description: "allow proxy",
},
func (driver AliDrive) Items() []Item {
return []Item{
{
Name: "order_by",
Label: "order_by",
@ -75,7 +72,7 @@ func (driver AliDrive) Save(account *model.Account, old *model.Account) error {
if err != nil {
return err
}
var resp drivers.Json
var resp Json
_, _ = aliClient.R().SetResult(&resp).
SetBody("{}").
SetHeader("authorization", "Bearer\t"+account.AccessToken).
@ -112,7 +109,7 @@ func (driver AliDrive) File(path string, account *model.Account) (*model.File, e
Name: account.Name,
Size: 0,
Type: conf.FOLDER,
Driver: driverName,
Driver: driver.Config().Name,
UpdatedAt: account.UpdatedAt,
}, nil
}
@ -126,7 +123,7 @@ func (driver AliDrive) File(path string, account *model.Account) (*model.File, e
return &file, nil
}
}
return nil, drivers.PathNotFound
return nil, PathNotFound
}
func (driver AliDrive) Files(path string, account *model.Account) ([]model.File, error) {
@ -160,12 +157,12 @@ func (driver AliDrive) Link(path string, account *model.Account) (string, error)
if err != nil {
return "", err
}
var resp drivers.Json
var resp Json
var e AliRespError
_, err = aliClient.R().SetResult(&resp).
SetError(&e).
SetHeader("authorization", "Bearer\t"+account.AccessToken).
SetBody(drivers.Json{
SetBody(Json{
"drive_id": account.DriveId,
"file_id": file.Id,
"expire_sec": 14400,
@ -217,10 +214,10 @@ func (driver AliDrive) Preview(path string, account *model.Account) (interface{}
return nil, err
}
// office
var resp drivers.Json
var resp Json
var e AliRespError
var url string
req := drivers.Json{
req := Json{
"drive_id": account.DriveId,
"file_id": file.FileId,
}
@ -236,7 +233,7 @@ func (driver AliDrive) Preview(path string, account *model.Account) (interface{}
req["category"] = "live_transcoding"
}
default:
return nil, fmt.Errorf("don't support")
return nil, NotSupport
}
_, err = aliClient.R().SetResult(&resp).SetError(&e).
SetHeader("authorization", "Bearer\t"+account.AccessToken).
@ -250,4 +247,4 @@ func (driver AliDrive) Preview(path string, account *model.Account) (interface{}
return resp, nil
}
var _ drivers.Driver = (*AliDrive)(nil)
var _ Driver = (*AliDrive)(nil)

View File

@ -1,9 +1,8 @@
package alidrive
package drivers
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/go-resty/resty/v2"
@ -46,7 +45,7 @@ func (driver AliDrive) FormatFile(file *AliFile) *model.File {
Size: file.Size,
UpdatedAt: file.UpdatedAt,
Thumbnail: file.Thumbnail,
Driver: driverName,
Driver: driver.Config().Name,
Url: file.Url,
}
if file.Type == "folder" {
@ -76,7 +75,7 @@ func (driver AliDrive) GetFiles(fileId string, account *model.Account) ([]AliFil
SetResult(&resp).
SetError(&e).
SetHeader("authorization", "Bearer\t"+account.AccessToken).
SetBody(drivers.Json{
SetBody(Json{
"drive_id": account.DriveId,
"fields": "*",
"image_thumbnail_process": "image/resize,w_400/format,jpeg",
@ -128,16 +127,16 @@ func (driver AliDrive) GetFile(path string, account *model.Account) (*AliFile, e
}
}
}
return nil, drivers.PathNotFound
return nil, PathNotFound
}
func (driver AliDrive) RefreshToken(account *model.Account) error {
url := "https://auth.aliyundrive.com/v2/account/token"
var resp drivers.TokenResp
var resp TokenResp
var e AliRespError
_, err := aliClient.R().
//ForceContentType("application/json").
SetBody(drivers.Json{"refresh_token": account.RefreshToken, "grant_type": "refresh_token"}).
SetBody(Json{"refresh_token": account.RefreshToken, "grant_type": "refresh_token"}).
SetResult(&resp).
SetError(&e).
Post(url)
@ -157,7 +156,7 @@ func (driver AliDrive) RefreshToken(account *model.Account) error {
}
func init() {
drivers.RegisterDriver(driverName, &AliDrive{})
RegisterDriver(&AliDrive{})
aliClient.
SetRetryCount(3).
SetHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36").

View File

@ -8,7 +8,13 @@ import (
"net/http"
)
type DriverConfig struct {
Name string
OnlyProxy bool
}
type Driver interface {
Config() DriverConfig
Items() []Item
Save(account *model.Account, old *model.Account) error
File(path string, account *model.Account) (*model.File, error)
@ -41,9 +47,9 @@ type TokenResp struct {
var driversMap = map[string]Driver{}
func RegisterDriver(name string, driver Driver) {
log.Infof("register driver: [%s]", name)
driversMap[name] = driver
func RegisterDriver(driver Driver) {
log.Infof("register driver: [%s]", driver.Config().Name)
driversMap[driver.Config().Name] = driver
}
func GetDriver(name string) (driver Driver, ok bool) {
@ -54,7 +60,26 @@ func GetDriver(name string) (driver Driver, ok bool) {
func GetDrivers() map[string][]Item {
res := make(map[string][]Item, 0)
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
}
@ -69,4 +94,5 @@ func init() {
return http.ErrUseLastResponse
}),
)
NoRedirectClient.SetHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
}

View File

@ -1,8 +0,0 @@
package drivers
import "fmt"
var (
PathNotFound = fmt.Errorf("path not found")
NotFile = fmt.Errorf("not file")
)

View File

@ -1,9 +1,8 @@
package googledrive
package drivers
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
@ -13,10 +12,15 @@ import (
type GoogleDrive struct{}
var driverName = "GoogleDrive"
func (driver GoogleDrive) Config() DriverConfig {
return DriverConfig{
Name: "GoogleDrive",
OnlyProxy: true,
}
}
func (driver GoogleDrive) Items() []drivers.Item {
return []drivers.Item{
func (driver GoogleDrive) Items() []Item {
return []Item{
{
Name: "client_id",
Label: "client id",
@ -39,7 +43,7 @@ func (driver GoogleDrive) Items() []drivers.Item {
Name: "root_folder",
Label: "root folder file_id",
Type: "string",
Required: true,
Required: false,
},
}
}
@ -52,6 +56,9 @@ func (driver GoogleDrive) Save(account *model.Account, old *model.Account) error
_ = model.SaveAccount(account)
return err
}
if account.RootFolder == "" {
account.RootFolder = "root"
}
account.Status = "work"
_ = model.SaveAccount(account)
return nil
@ -65,7 +72,7 @@ func (driver GoogleDrive) File(path string, account *model.Account) (*model.File
Name: account.Name,
Size: 0,
Type: conf.FOLDER,
Driver: driverName,
Driver: driver.Config().Name,
UpdatedAt: account.UpdatedAt,
}, nil
}
@ -79,7 +86,7 @@ func (driver GoogleDrive) File(path string, account *model.Account) (*model.File
return &file, nil
}
}
return nil, drivers.PathNotFound
return nil, PathNotFound
}
func (driver GoogleDrive) Files(path string, account *model.Account) ([]model.File, error) {
@ -114,7 +121,7 @@ func (driver GoogleDrive) Link(path string, account *model.Account) (string, err
return "", err
}
if file.Type == conf.FOLDER {
return "", drivers.NotFile
return "", NotFile
}
link := fmt.Sprintf("https://www.googleapis.com/drive/v3/files/%s?includeItemsFromAllDrives=true&supportsAllDrives=true", file.Id)
var e GoogleError
@ -158,5 +165,7 @@ func (driver GoogleDrive) Proxy(c *gin.Context, account *model.Account) {
}
func (driver GoogleDrive) Preview(path string, account *model.Account) (interface{}, error) {
return nil, nil
return nil, NotSupport
}
var _ Driver = (*GoogleDrive)(nil)

View File

@ -1,9 +1,8 @@
package googledrive
package drivers
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/go-resty/resty/v2"
@ -21,7 +20,7 @@ type GoogleTokenError struct {
func (driver GoogleDrive) RefreshToken(account *model.Account) error {
url := "https://www.googleapis.com/oauth2/v4/token"
var resp drivers.TokenResp
var resp TokenResp
var e GoogleTokenError
_, err := googleClient.R().SetResult(&resp).SetError(&e).
SetFormData(map[string]string{
@ -57,7 +56,7 @@ func (driver GoogleDrive) FormatFile(file *GoogleFile) *model.File {
f := &model.File{
Id: file.Id,
Name: file.Name,
Driver: driverName,
Driver: driver.Config().Name,
UpdatedAt: file.ModifiedTime,
Thumbnail: "",
Url: "",
@ -152,9 +151,7 @@ func (driver GoogleDrive) GetFiles(id string, account *model.Account) ([]GoogleF
// return nil, drivers.PathNotFound
//}
var _ drivers.Driver = (*GoogleDrive)(nil)
func init() {
drivers.RegisterDriver(driverName, &GoogleDrive{})
RegisterDriver(&GoogleDrive{})
googleClient.SetRetryCount(3)
}

238
drivers/lanzou.go Normal file
View File

@ -0,0 +1,238 @@
package drivers
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
"path/filepath"
"regexp"
"strconv"
"time"
)
var lanzouClient = resty.New()
type LanZouFile struct {
Name string `json:"name"`
NameAll string `json:"name_all"`
Id string `json:"id"`
FolId string `json:"fol_id"`
Size string `json:"size"`
Time string `json:"time"`
Folder bool
}
func (driver *Lanzou) FormatFile(file *LanZouFile) *model.File {
now := time.Now()
f := &model.File{
Id: file.Id,
Name: file.Name,
Driver: driver.Config().Name,
SizeStr: file.Size,
TimeStr: file.Time,
UpdatedAt: &now,
}
if file.Folder {
f.Type = conf.FOLDER
f.Id = file.FolId
} else {
f.Name = file.NameAll
f.Type = utils.GetFileType(filepath.Ext(file.NameAll))
}
return f
}
type LanZouFilesResp struct {
Zt int `json:"zt"`
Info interface{} `json:"info"`
Text []LanZouFile `json:"text"`
}
func (driver *Lanzou) GetFiles(folderId string, account *model.Account) ([]LanZouFile, error) {
if account.OnedriveType == "cookie" {
files := make([]LanZouFile, 0)
var resp LanZouFilesResp
// folders
res, err := lanzouClient.R().SetResult(&resp).SetHeader("Cookie", account.AccessToken).
SetFormData(map[string]string{
"task": "47",
"folder_id": folderId,
}).Post("https://pc.woozooo.com/doupload.php")
if err != nil {
return nil, err
}
log.Debug(res.String())
if resp.Zt != 1 && resp.Zt != 2 {
return nil, fmt.Errorf("%v", resp.Info)
}
for _, file := range resp.Text {
file.Folder = true
files = append(files, file)
}
// files
pg := 1
for {
_, err = lanzouClient.R().SetResult(&resp).SetHeader("Cookie", account.AccessToken).
SetFormData(map[string]string{
"task": "5",
"folder_id": folderId,
"pg": strconv.Itoa(pg),
}).Post("https://pc.woozooo.com/doupload.php")
if err != nil {
return nil, err
}
if resp.Zt != 1 {
return nil, fmt.Errorf("%v", resp.Info)
}
if len(resp.Text) == 0 {
break
}
files = append(files, resp.Text...)
pg++
}
return files, nil
} else {
return driver.GetFilesByUrl(account)
}
}
func (driver *Lanzou) GetFilesByUrl(account *model.Account) ([]LanZouFile, error) {
files := make([]LanZouFile, 0)
shareUrl := account.SiteUrl
res, err := lanzouClient.R().Get(shareUrl)
if err != nil {
return nil, err
}
lxArr := regexp.MustCompile(`'lx':(.+?),`).FindStringSubmatch(res.String())
if len(lxArr) == 0 {
return nil, fmt.Errorf("get empty page")
}
lx := lxArr[1]
fid := regexp.MustCompile(`'fid':(.+?),`).FindStringSubmatch(res.String())[1]
uid := regexp.MustCompile(`'uid':'(.+?)',`).FindStringSubmatch(res.String())[1]
rep := regexp.MustCompile(`'rep':'(.+?)',`).FindStringSubmatch(res.String())[1]
up := regexp.MustCompile(`'up':(.+?),`).FindStringSubmatch(res.String())[1]
ls := regexp.MustCompile(`'ls':(.+?),`).FindStringSubmatch(res.String())[1]
tName := regexp.MustCompile(`'t':(.+?),`).FindStringSubmatch(res.String())[1]
kName := regexp.MustCompile(`'k':(.+?),`).FindStringSubmatch(res.String())[1]
t := regexp.MustCompile(`var ` + tName + ` = '(.+?)';`).FindStringSubmatch(res.String())[1]
k := regexp.MustCompile(`var ` + kName + ` = '(.+?)';`).FindStringSubmatch(res.String())[1]
pg := 1
for {
var resp LanZouFilesResp
res, err = lanzouClient.R().SetResult(&resp).SetFormData(map[string]string{
"lx": lx,
"fid": fid,
"uid": uid,
"pg": strconv.Itoa(pg),
"rep": rep,
"t": t,
"k": k,
"up": up,
"ls": ls,
"pwd": account.Password,
}).Post("https://wwa.lanzouo.com/filemoreajax.php")
if err != nil {
log.Debug(err)
break
}
log.Debug(res.String())
if resp.Zt != 1 {
return nil, fmt.Errorf("%v", resp.Info)
}
if len(resp.Text) == 0 {
break
}
pg++
files = append(files, resp.Text...)
}
return files, nil
}
//type LanzouDownInfo struct {
// FId string `json:"f_id"`
// IsNewd string `json:"is_newd"`
//}
// 获取下载页面的ID
func (driver *Lanzou) GetDownPageId(fileId string, account *model.Account) (string, error) {
var resp LanZouFilesResp
res, err := lanzouClient.R().SetResult(&resp).SetHeader("Cookie", account.AccessToken).
SetFormData(map[string]string{
"task": "22",
"file_id": fileId,
}).Post("https://pc.woozooo.com/doupload.php")
if err != nil {
return "", err
}
log.Debug(res.String())
if resp.Zt != 1 {
return "", fmt.Errorf("%v", resp.Info)
}
info, ok := resp.Info.(map[string]interface{})
if !ok {
return "", fmt.Errorf("%v", resp.Info)
}
fid, ok := info["f_id"].(string)
if !ok {
return "", fmt.Errorf("%v", info["f_id"])
}
return fid, nil
}
type LanzouLinkResp struct {
Dom string `json:"dom"`
Url string `json:"url"`
Zt int `json:"zt"`
}
func (driver *Lanzou) GetLink(downId string) (string, error) {
res, err := lanzouClient.R().Get("https://wwa.lanzouo.com/" + downId)
if err != nil {
return "", err
}
iframe := regexp.MustCompile(`<iframe class="ifr2" name=".{2,20}" src="(.+?)"`).FindStringSubmatch(res.String())
if len(iframe) == 0 {
return "", fmt.Errorf("get down empty page")
}
iframeUrl := "https://wwa.lanzouo.com" + iframe[1]
res, err = lanzouClient.R().Get(iframeUrl)
if err != nil {
return "", err
}
ajaxdata := regexp.MustCompile(`var ajaxdata = '(.+?)'`).FindStringSubmatch(res.String())
if len(ajaxdata) == 0 {
return "", fmt.Errorf("get iframe empty page")
}
signs := ajaxdata[1]
sign := regexp.MustCompile(`var ispostdowns = '(.+?)';`).FindStringSubmatch(res.String())[1]
websignkey := regexp.MustCompile(`'websignkey':'(.+?)'`).FindStringSubmatch(res.String())[1]
var resp LanzouLinkResp
form := map[string]string{
"action": "downprocess",
"signs": signs,
"sign": sign,
"ves": "1",
"websign": "",
"websignkey": websignkey,
}
log.Debugf("form: %+v", form)
_, err = lanzouClient.R().SetResult(&resp).
SetHeader("origin", "https://wwa.lanzouo.com").
SetHeader("referer", iframeUrl).
SetFormData(form).Post("https://wwa.lanzouo.com/ajaxm.php")
if resp.Zt == 1 {
return resp.Dom + "/file/" + resp.Url, nil
}
return "", fmt.Errorf("can't get link")
}
func init() {
RegisterDriver(&Lanzou{})
lanzouClient.
SetRetryCount(3).
SetHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
}

163
drivers/lanzou_driver.go Normal file
View File

@ -0,0 +1,163 @@
package drivers
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"path/filepath"
)
type Lanzou struct{}
func (driver Lanzou) Config() DriverConfig {
return DriverConfig{
Name: "Lanzou",
OnlyProxy: false,
}
}
func (driver Lanzou) Items() []Item {
return []Item{
{
Name: "onedrive_type",
Label: "lanzou type",
Type: SELECT,
Required: true,
Values: "cookie,url",
},
{
Name: "access_token",
Label: "cookie",
Type: STRING,
Description: "about 15 days valid",
},
{
Name: "root_folder",
Label: "root folder file_id",
Type: STRING,
},
{
Name: "site_url",
Label: "share url",
Type: STRING,
},
{
Name: "password",
Label: "share password",
Type: STRING,
},
}
}
func (driver Lanzou) Save(account *model.Account, old *model.Account) error {
if account.OnedriveType == "cookie" {
if account.RootFolder == "" {
account.RootFolder = "-1"
}
}
account.Status = "work"
_ = model.SaveAccount(account)
return nil
}
func (driver Lanzou) File(path string, account *model.Account) (*model.File, error) {
path = utils.ParsePath(path)
if path == "/" {
return &model.File{
Id: account.RootFolder,
Name: account.Name,
Size: 0,
Type: conf.FOLDER,
Driver: driver.Config().Name,
UpdatedAt: account.UpdatedAt,
}, nil
}
dir, name := filepath.Split(path)
files, err := driver.Files(dir, account)
if err != nil {
return nil, err
}
for _, file := range files {
if file.Name == name {
return &file, nil
}
}
return nil, PathNotFound
}
func (driver Lanzou) Files(path string, account *model.Account) ([]model.File, error) {
path = utils.ParsePath(path)
var rawFiles []LanZouFile
cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path))
if err == nil {
rawFiles, _ = cache.([]LanZouFile)
} else {
file, err := driver.File(path, account)
if err != nil {
return nil, err
}
rawFiles, err = driver.GetFiles(file.Id, account)
if err != nil {
return nil, err
}
if len(rawFiles) > 0 {
_ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), rawFiles, nil)
}
}
files := make([]model.File, 0)
for _, file := range rawFiles {
files = append(files, *driver.FormatFile(&file))
}
return files, nil
}
func (driver Lanzou) Link(path string, account *model.Account) (string, error) {
file, err := driver.File(path, account)
if err != nil {
return "", err
}
log.Debugf("down file: %+v", file)
downId := file.Id
if account.OnedriveType == "cookie" {
downId, err = driver.GetDownPageId(file.Id, account)
if err != nil {
return "", err
}
}
link, err := driver.GetLink(downId)
if err != nil {
return "", err
}
return link, nil
}
func (driver Lanzou) Path(path string, account *model.Account) (*model.File, []model.File, error) {
path = utils.ParsePath(path)
log.Debugf("lanzou path: %s", path)
file, err := driver.File(path, account)
if err != nil {
return nil, nil, err
}
if file.Type != conf.FOLDER {
file.Url, _ = driver.Link(path, account)
return file, nil, nil
}
files, err := driver.Files(path, account)
if err != nil {
return nil, nil, err
}
return nil, files, nil
}
func (driver Lanzou) Proxy(c *gin.Context, account *model.Account) {
c.Request.Header.Del("Origin")
}
func (driver Lanzou) Preview(path string, account *model.Account) (interface{}, error) {
return nil, NotSupport
}
var _ Driver = (*Lanzou)(nil)

5
drivers/native.go Normal file
View File

@ -0,0 +1,5 @@
package drivers
func init() {
RegisterDriver(&Native{})
}

View File

@ -1,9 +0,0 @@
package native
import (
"github.com/Xhofe/alist/drivers"
)
func init() {
drivers.RegisterDriver(driverName, &Native{})
}

View File

@ -1,9 +1,8 @@
package native
package drivers
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
@ -16,10 +15,15 @@ import (
type Native struct{}
var driverName = "Native"
func (driver Native) Config() DriverConfig {
return DriverConfig{
Name: "Native",
OnlyProxy: true,
}
}
func (driver Native) Items() []drivers.Item {
return []drivers.Item{
func (driver Native) Items() []Item {
return []Item{
{
Name: "root_folder",
Label: "root folder path",
@ -62,7 +66,7 @@ func (driver Native) Save(account *model.Account, old *model.Account) error {
func (driver Native) File(path string, account *model.Account) (*model.File, error) {
fullPath := filepath.Join(account.RootFolder, path)
if !utils.Exists(fullPath) {
return nil, drivers.PathNotFound
return nil, PathNotFound
}
f, err := os.Stat(fullPath)
if err != nil {
@ -73,7 +77,7 @@ func (driver Native) File(path string, account *model.Account) (*model.File, err
Name: f.Name(),
Size: f.Size(),
UpdatedAt: &time,
Driver: driverName,
Driver: driver.Config().Name,
}
if f.IsDir() {
file.Type = conf.FOLDER
@ -86,7 +90,7 @@ func (driver Native) File(path string, account *model.Account) (*model.File, err
func (driver Native) Files(path string, account *model.Account) ([]model.File, error) {
fullPath := filepath.Join(account.RootFolder, path)
if !utils.Exists(fullPath) {
return nil, drivers.PathNotFound
return nil, PathNotFound
}
files := make([]model.File, 0)
rawFiles, err := ioutil.ReadDir(fullPath)
@ -103,7 +107,7 @@ func (driver Native) Files(path string, account *model.Account) ([]model.File, e
Size: f.Size(),
Type: 0,
UpdatedAt: &time,
Driver: driverName,
Driver: driver.Config().Name,
}
if f.IsDir() {
file.Type = conf.FOLDER
@ -151,7 +155,7 @@ func (driver Native) Proxy(c *gin.Context, account *model.Account) {
}
func (driver Native) Preview(path string, account *model.Account) (interface{}, error) {
return nil, fmt.Errorf("no need")
return nil, NotSupport
}
var _ drivers.Driver = (*Native)(nil)
var _ Driver = (*Native)(nil)

View File

@ -1,9 +1,8 @@
package onedrive
package drivers
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
@ -14,17 +13,15 @@ import (
type Onedrive struct{}
var driverName = "Onedrive"
func (driver Onedrive) Config() DriverConfig {
return DriverConfig{
Name: "Onedrive",
OnlyProxy: false,
}
}
func (driver Onedrive) Items() []drivers.Item {
return []drivers.Item{
{
Name: "proxy",
Label: "proxy",
Type: "bool",
Required: true,
Description: "allow proxy",
},
func (driver Onedrive) Items() []Item {
return []Item{
{
Name: "zone",
Label: "zone",
@ -136,7 +133,7 @@ func (driver Onedrive) File(path string, account *model.Account) (*model.File, e
Name: account.Name,
Size: 0,
Type: conf.FOLDER,
Driver: driverName,
Driver: driver.Config().Name,
UpdatedAt: account.UpdatedAt,
}, nil
}
@ -150,7 +147,7 @@ func (driver Onedrive) File(path string, account *model.Account) (*model.File, e
return &file, nil
}
}
return nil, drivers.PathNotFound
return nil, PathNotFound
}
func (driver Onedrive) Files(path string, account *model.Account) ([]model.File, error) {
@ -168,7 +165,9 @@ func (driver Onedrive) Files(path string, account *model.Account) ([]model.File,
for _, file := range rawFiles {
files = append(files, *driver.FormatFile(&file))
}
_ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), files, nil)
if len(files) > 0 {
_ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), files, nil)
}
return files, nil
}
@ -205,5 +204,7 @@ func (driver Onedrive) Proxy(c *gin.Context, account *model.Account) {
}
func (driver Onedrive) Preview(path string, account *model.Account) (interface{}, error) {
return nil, nil
return nil, NotSupport
}
var _ Driver = (*Onedrive)(nil)

View File

@ -1,9 +1,8 @@
package onedrive
package drivers
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/go-resty/resty/v2"
@ -56,7 +55,7 @@ func (driver Onedrive) GetMetaUrl(account *model.Account, auth bool, path string
}
case "sharepoint":
{
if path == "/" {
if path == "/" || path == "\\" {
return fmt.Sprintf("%s/v1.0/sites/%s/drive/root", host.Api, account.SiteId)
} else {
return fmt.Sprintf("%s/v1.0/sites/%s/drive/root:%s:", host.Api, account.SiteId, path)
@ -74,7 +73,7 @@ type OneTokenErr struct {
func (driver Onedrive) RefreshToken(account *model.Account) error {
url := driver.GetMetaUrl(account, true, "") + "/common/oauth2/v2.0/token"
var resp drivers.TokenResp
var resp TokenResp
var e OneTokenErr
_, err := oneClient.R().SetResult(&resp).SetError(&e).SetFormData(map[string]string{
"grant_type": "refresh_token",
@ -105,6 +104,11 @@ type OneFile struct {
File struct {
MimeType string `json:"mimeType"`
} `json:"file"`
Thumbnails []struct{
Medium struct{
Url string `json:"url"`
} `json:"medium"`
} `json:"thumbnails"`
}
type OneFiles struct {
@ -124,9 +128,12 @@ func (driver Onedrive) FormatFile(file *OneFile) *model.File {
Name: file.Name,
Size: file.Size,
UpdatedAt: file.LastModifiedDateTime,
Driver: driverName,
Driver: driver.Config().Name,
Url: file.Url,
}
if len(file.Thumbnails) > 0 {
f.Thumbnail = file.Thumbnails[0].Medium.Url
}
if file.File.MimeType == "" {
f.Type = conf.FOLDER
} else {
@ -137,11 +144,11 @@ func (driver Onedrive) FormatFile(file *OneFile) *model.File {
func (driver Onedrive) GetFiles(account *model.Account, path string) ([]OneFile, error) {
var res []OneFile
nextLink := driver.GetMetaUrl(account, false, path) + "/children"
nextLink := driver.GetMetaUrl(account, false, path) + "/children?$expand=thumbnails"
if account.OrderBy != "" {
nextLink += fmt.Sprintf("?orderby=%s", account.OrderBy)
nextLink += fmt.Sprintf("&orderby=%s", account.OrderBy)
if account.OrderDirection != "" {
nextLink += fmt.Sprintf(" %s", account.OrderDirection)
nextLink += fmt.Sprintf("%%20%s", account.OrderDirection)
}
}
for nextLink != "" {
@ -177,9 +184,7 @@ func (driver Onedrive) GetFile(account *model.Account, path string) (*OneFile, e
return &file, nil
}
var _ drivers.Driver = (*Onedrive)(nil)
func init() {
drivers.RegisterDriver(driverName, &Onedrive{})
RegisterDriver(&Onedrive{})
oneClient.SetRetryCount(3)
}

16
drivers/types.go Normal file
View File

@ -0,0 +1,16 @@
package drivers
import "fmt"
var (
PathNotFound = fmt.Errorf("path not found")
NotFile = fmt.Errorf("not file")
NotImplement = fmt.Errorf("not implement")
NotSupport = fmt.Errorf("not support")
)
const (
STRING = "string"
SELECT = "select"
BOOL = "bool"
)

View File

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

View File

@ -16,6 +16,8 @@ type File struct {
UpdatedAt *time.Time `json:"updated_at"`
Thumbnail string `json:"thumbnail"`
Url string `json:"url"`
SizeStr string `json:"size_str"`
TimeStr string `json:"time_str"`
}
func SortFiles(files []File, account *Account) {
@ -63,4 +65,4 @@ func (f File) ModTime() time.Time {
func (f File) IsDir() bool {
return f.Type == conf.FOLDER
}
}

View File

@ -8,7 +8,6 @@ import (
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
"path/filepath"
)
func Auth(c *gin.Context) {
@ -49,10 +48,10 @@ func CheckParent(path string, password string) bool {
}
return true
} else {
if path == "/" || path == "\\" {
if path == "/" {
return true
}
return CheckParent(filepath.Dir(path), password)
return CheckParent(utils.Dir(path), password)
}
}
@ -72,9 +71,9 @@ func CheckDownLink(path string, passwordMd5 string) bool {
if !conf.CheckParent {
return true
}
if path == "/" || path == "\\" {
if path == "/" {
return true
}
return CheckDownLink(filepath.Dir(path), passwordMd5)
return CheckDownLink(utils.Dir(path), passwordMd5)
}
}

View File

@ -14,15 +14,11 @@ import (
)
func Down(c *gin.Context) {
rawPath, err := url.PathUnescape(c.Param("path"))
if err != nil {
ErrorResp(c, err, 500)
return
}
rawPath := c.Param("path")
rawPath = utils.ParsePath(rawPath)
log.Debugf("down: %s", rawPath)
pw := c.Query("pw")
if !CheckDownLink(filepath.Dir(rawPath), pw) {
if !CheckDownLink(utils.Dir(rawPath), pw) {
ErrorResp(c, fmt.Errorf("wrong password"), 401)
return
}
@ -50,15 +46,11 @@ func Down(c *gin.Context) {
}
func Proxy(c *gin.Context) {
rawPath, err := url.PathUnescape(c.Param("path"))
if err != nil {
ErrorResp(c, err, 500)
return
}
rawPath := c.Param("path")
rawPath = utils.ParsePath(rawPath)
log.Debugf("proxy: %s", rawPath)
pw := c.Query("pw")
if !CheckDownLink(filepath.Dir(rawPath), pw) {
if !CheckDownLink(utils.Dir(rawPath), pw) {
ErrorResp(c, fmt.Errorf("wrong password"), 401)
return
}

View File

@ -7,7 +7,6 @@ import (
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"path/filepath"
"strings"
)
@ -32,7 +31,7 @@ func Path(c *gin.Context) {
}
// TODO hide or ignore?
} else if conf.CheckParent {
if !CheckParent(filepath.Dir(req.Path), req.Password) {
if !CheckParent(utils.Dir(req.Path), req.Password) {
ErrorResp(c, fmt.Errorf("wrong password"), 401)
return
}

View File

@ -94,15 +94,16 @@ func GetPW(path string) string {
if !conf.CheckParent {
return ""
}
if path == "/" || path == "\\" {
if path == "/" {
return ""
}
return GetPW(filepath.Dir(path))
return GetPW(utils.Dir(path))
}
}
func (fs *FileSystem) Link(host, rawPath string) (string, error) {
func (fs *FileSystem) Link(r *http.Request, rawPath string) (string, error) {
rawPath = utils.ParsePath(rawPath)
log.Debugf("get link path: %s", rawPath)
if model.AccountsCount() > 1 && rawPath == "/" {
// error
}
@ -110,16 +111,22 @@ func (fs *FileSystem) Link(host, rawPath string) (string, error) {
if err != nil {
return "", err
}
if account.Type == "Native" || account.Type == "GoogleDrive" {
link := fmt.Sprintf("//%s/p%s", host, rawPath)
link := ""
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 {
pw := GetPW(filepath.Dir(rawPath))
pw := GetPW(utils.Dir(rawPath))
link += "?pw" + pw
}
log.Debugf("proxy link: %s", link)
return link, nil
} else {
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) {

View File

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

View File

@ -94,4 +94,15 @@ func RemoveLastSlash(path string) string {
return strings.TrimSuffix(path, "/")
}
return path
}
func Dir(path string) string {
idx := strings.LastIndex(path, "/")
if idx == 0 {
return "/"
}
if idx == -1 {
return path
}
return path[:idx]
}