Compare commits

...

58 Commits

Author SHA1 Message Date
342729179d chore: Merge pull request #884 from Xhofe/dev
fix: some issues of webdav due to virtual path
2022-04-01 22:02:39 +08:00
0537449335 fix(webdav): virtual path no account 2022-04-01 21:57:55 +08:00
df90311453 fix(webdav): alist path not found 2022-04-01 20:40:57 +08:00
876579ea3b chore: Merge pull request #874 from Xhofe/dev
support mount to root path
2022-04-01 09:42:37 +08:00
e83081380e workflow: cancel build docker for pr 2022-04-01 09:40:53 +08:00
9daeaf7562 fix: virtual path, support mount to root path 2022-04-01 09:40:08 +08:00
e6be11c17f chore: Merge pull request #873 from Xhofe/dev
fix: load balance
2022-03-31 22:04:37 +08:00
b52e1e8be3 fix: load balance 2022-03-31 21:52:19 +08:00
948bbe9136 chore: Merge pull request #872 from Xhofe/dev
fix quark cookie, virtual path
2022-03-31 20:58:56 +08:00
ced61da33a feat: virtual path 2022-03-31 20:43:17 +08:00
a0f4383d41 feat(quark): set status 2022-03-30 14:06:50 +08:00
5a527dfa2c feat(xunlei): set timeout 2022-03-30 00:18:20 +08:00
49fc475f9f feat(s3): create placeholder file for mkdir 2022-03-30 00:18:00 +08:00
83c377270e fix(webdav): add sign for webdav proxy 2022-03-29 16:34:22 +08:00
7ffaef0de6 fix: audio and video types 2022-03-28 21:53:57 +08:00
dd151480a8 fix(alidrive): change response of move and copy 2022-03-28 21:51:24 +08:00
ad3121d367 fix(quark): __puus expired (close #830) 2022-03-28 21:38:05 +08:00
c1525ebc69 feat: cookie operate util 2022-03-28 21:10:20 +08:00
30277cd81f docs: change blog address [skip ci] 2022-03-27 20:23:18 +08:00
466ec27ffe chore: Merge pull request #825 from Xhofe/dev
docs: add disclaimer
2022-03-27 20:17:36 +08:00
85c757b035 docs: add disclaimer 2022-03-27 20:16:06 +08:00
712687370a chore: Merge pull request #823 from Xhofe/dev
fix some issues
2022-03-26 23:54:17 +08:00
b68ba22df3 workflow: issue invalid bot 2022-03-26 23:51:25 +08:00
d9652e2a0b fix(189cloud): link force https (close #821) 2022-03-26 21:59:18 +08:00
a5b757b251 feat: customize audio/video types (close #819) 2022-03-26 17:10:37 +08:00
0bc05a60b0 feat(189pc): override upload 2022-03-23 18:51:07 +08:00
db275f885a fix: DProxyTypes judge 2022-03-22 19:53:26 +08:00
9e483d902f feat: adapt postgres (close #740) 2022-03-22 16:41:38 +08:00
801f843f8a workflow: reproduction is required [skip ci] 2022-04-05 03:24:51 +08:00
77ffb93cbe feat: multiple down proxy urls (close #793) 2022-03-20 16:53:30 +08:00
bf73ea7f5d chore: Merge pull request #787 from Xhofe/dev
fix some issues of webdav
2022-03-19 14:57:38 +08:00
9b23d0ab29 docs: add sponsors [skip ci] 2022-03-18 17:08:43 +08:00
908cdd2c78 revert: undo delete upFileMap 2022-03-17 21:57:54 +08:00
f4f61a5787 fix(webdav): nil pointer error (close #749) 2022-03-17 21:23:10 +08:00
6db09a2736 fix: xunlei upload error (#749) 2022-03-17 21:13:13 +08:00
b21801d505 fix: clear cookie for 189 cloud login 2022-03-16 18:02:11 +08:00
2dbedc245c fix: 189 family cloud upload (#761) 2022-03-16 14:22:42 +08:00
58426613f6 feat: add tls config for mysql (fix #758) 2022-03-15 17:05:54 +08:00
ef19e851e3 fix: check local ip for 123pan 2022-03-15 14:48:39 +08:00
5a1b16a601 feat: set overwrite for aliyundrive upload 2022-03-14 22:43:27 +08:00
4eef9cd9bc fix: nil pointer while delete baidu account (close #751) 2022-03-14 20:40:42 +08:00
79b5c018ea workflow: add translation for duplicate issue [skip ci] 2022-03-14 18:09:38 +08:00
15651a4356 feat: only show files (close #735) 2022-03-13 19:37:58 +08:00
7be476cce0 fix: wrong dockerfile 2022-03-13 19:00:19 +08:00
bb017c5f6d feat: remove env prefix for docker 2022-03-13 17:01:45 +08:00
c51dc4594d chore: add tips for announcement 2022-03-13 16:46:06 +08:00
8e30b02efc fix: cache config env typo 2022-03-13 16:38:12 +08:00
0aa438dce4 feat: add announcement setting 2022-03-12 21:09:33 +08:00
9c2fc8e860 feat: read config from environment 2022-03-12 20:38:22 +08:00
b1d7a980d9 feat: echo password while start every time 2022-03-12 00:24:55 +08:00
19d0a88b55 fix: cookie lanzou file with password 2022-03-11 19:48:32 +08:00
40567dee0e fix: lanzou url password 2022-03-11 19:16:21 +08:00
4b540a2297 feat: skip creating an existing folder 2022-03-11 18:12:13 +08:00
8a62d55efe feat(google): add default client 2022-03-10 20:08:10 +08:00
10fce6c0fe fix(xunlei): some issues about page turning(#716) 2022-03-09 22:48:15 +08:00
d31d49a9bb fix(189pc): some minor issues 2022-03-09 21:09:21 +08:00
2e91f5ffa5 feat: support 189 family cloud (close #612) 2022-03-09 20:30:56 +08:00
8f19c45a81 feat: pikpak video use media link 2022-03-09 15:11:12 +08:00
64 changed files with 2326 additions and 338 deletions

View File

@ -5,7 +5,8 @@ body:
- type: markdown
attributes:
value: |
Thanks for taking the time to fill out this bug report, please confirm that your issue is not a duplicate issue
Thanks for taking the time to fill out this bug report, please **confirm that your issue is not a duplicate issue and not because of your operation or version issues**
感谢您花时间填写此错误报告,请**务必确认您的issue不是重复的且不是因为您的操作或版本问题**
- type: input
id: version
attributes:
@ -28,11 +29,11 @@ body:
Please provide a link to a repo that can reproduce the problem you ran into.
请提供能复现此问题的链接
validations:
required: false
required: true
- type: textarea
id: logs
attributes:
label: 日志 / Logs
label: Logs / 日志
description: |
Please copy and paste any relevant log output.
请复制粘贴错误日志,或者截图

View File

@ -3,8 +3,6 @@ name: build_docker
on:
push:
branches: [ v2 ]
pull_request:
branches: [ v2 ]
jobs:
build_docker:

25
.github/workflows/issue_invalid.yml vendored Normal file
View File

@ -0,0 +1,25 @@
name: Issue Invalid
on:
issues:
types: [labeled]
jobs:
create-comment:
runs-on: ubuntu-latest
if: github.event.label.name == 'invalid'
steps:
- name: Create comment
uses: actions-cool/issues-helper@v2
with:
actions: 'create-comment'
token: ${{ secrets.GITHUB_TOKEN }}
issue-number: ${{ github.event.issue.number }}
body: |
Hello @${{ github.event.issue.user.login }}, your issue is invalid and will be closed.
你好 @${{ github.event.issue.user.login }}你的issue无效将被关闭。
- name: Close issue
uses: actions-cool/issues-helper@v2
with:
actions: 'close-issue'
token: ${{ secrets.GITHUB_TOKEN }}

View File

@ -17,4 +17,4 @@ jobs:
issue-number: ${{ github.event.issue.number }}
body: |
Hello @${{ github.event.issue.user.login }}, please input issue by template and add detail. Issues labeled by `question` will be closed if no activities in 7 days.
你好 @${{ github.event.issue.user.login }}请按照issue模板填写, 并详细说明问题/复现步骤/实现思路或提供更多信息等, 7天内未回复issue自动关闭。
你好 @${{ github.event.issue.user.login }}请按照issue模板填写, 并详细说明问题/复现步骤/复现链接/实现思路或提供更多信息等, 7天内未回复issue自动关闭。

View File

@ -11,4 +11,4 @@ VOLUME /opt/alist/data/
WORKDIR /opt/alist/
COPY --from=builder /app/bin/alist ./
EXPOSE 5244
CMD [ "./alist" ]
CMD [ "./alist", "-docker" ]

View File

@ -21,7 +21,7 @@ English | [中文](./README_cn.md) | [Contributors](./CONTRIBUTORS.md) | [Contri
- [x] Local storage
- [x] [Aliyundrive](https://www.aliyundrive.com/)
- [x] OneDrive / Sharepoint ([global](https://www.office.com/), [cn](https://portal.partner.microsoftonline.cn),de,us)
- [x] [189cloud](https://cloud.189.cn)
- [x] [189cloud](https://cloud.189.cn) (Personal, Family)
- [x] [GoogleDrive](https://drive.google.com/)
- [x] [123pan](https://www.123pan.com/)
- [x] [Lanzou](https://pc.woozooo.com/)
@ -69,10 +69,21 @@ Available at: <https://alist.nn.ci>.
<https://alist-doc.nn.ci/en/>
## Special sponsors
- [Find Resources - Aliyundrive Resource Search Engine](https://zhaoziyuan.la/)
- [JetBrains: Essential tools for software developers and teams](https://www.jetbrains.com/)
## License
The `AList` is open-source software licensed under the AGPL-3.0 license.
## Disclaimer
- This program is a free and open source project. It is designed to share files on the network disk, which is convenient for downloading and learning golang. Please abide by relevant laws and regulations when using it, and do not abuse it;
- This program is implemented by calling the official sdk/interface, without destroying the official interface behavior;
- This program only does 302 redirect/traffic forwarding, and does not intercept, store, or tamper with any user data;
- Before using this program, you should understand and bear the corresponding risks, including but not limited to account ban, download speed limit, etc., which is none of this program's business;
- If there is any infringement, please contact me by [email](mailto:i@nn.ci), and it will be dealt with in time.
---
> [@Blog](https://www.nn.ci/) · [@GitHub](https://github.com/Xhofe) · [@TelegramGroup](https://t.me/alist_chat) · [@QQGroup](https://jq.qq.com/?_wv=1027&k=OVPJcv2b)
> [@Blog](https://nn.ci/) · [@GitHub](https://github.com/Xhofe) · [@TelegramGroup](https://t.me/alist_chat) · [@QQGroup](https://jq.qq.com/?_wv=1027&k=OVPJcv2b)

View File

@ -21,7 +21,7 @@
- [x] 本地存储
- [x] [阿里云盘](https://www.aliyundrive.com/)
- [x] OneDrive / Sharepoint[国际版](https://www.office.com/), [世纪互联](https://portal.partner.microsoftonline.cn),de,us
- [x] [天翼云盘](https://cloud.189.cn)
- [x] [天翼云盘](https://cloud.189.cn) (个人云, 家庭云)
- [x] [GoogleDrive](https://drive.google.com/)
- [x] [123云盘](https://www.123pan.com/)
- [x] [蓝奏云](https://pc.woozooo.com/)
@ -69,10 +69,21 @@
<https://alist-doc.nn.ci/>
## 特别赞助
- [找资源 - 阿里云盘资源搜索引擎](https://zhaoziyuan.la/)
- [JetBrains: Essential tools for software developers and teams](https://www.jetbrains.com/)
## 许可
`AList` 是在 AGPL-3.0 许可下许可的开源软件。
## 免责声明
- 本程序为免费开源项目旨在分享网盘文件方便下载以及学习golang使用时请遵守相关法律法规请勿滥用
- 本程序通过调用官方sdk/接口实现,无破坏官方接口行为;
- 本程序仅做302重定向/流量转发,不拦截、存储、篡改任何用户数据;
- 在使用本程序之前你应了解并承担相应的风险包括但不限于账号被ban下载限速等与本程序无关
- 如有侵权,请通过[邮件](mailto:i@nn.ci)与我联系,会及时处理。
---
> [@Blog](https://www.nn.ci/) · [@GitHub](https://github.com/Xhofe) · [@TelegramGroup](https://t.me/alist_chat) · [@QQGroup](https://jq.qq.com/?_wv=1027&k=OVPJcv2b)
> [@Blog](https://nn.ci/) · [@GitHub](https://github.com/Xhofe) · [@TelegramGroup](https://t.me/alist_chat) · [@QQGroup](https://jq.qq.com/?_wv=1027&k=OVPJcv2b)

View File

@ -3,6 +3,7 @@ package bootstrap
import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/model"
log "github.com/sirupsen/logrus"
)
@ -20,7 +21,8 @@ func InitAccounts() {
log.Errorf("no [%s] driver", account.Type)
} else {
log.Infof("start init account: [%s], type: [%s]", account.Name, account.Type)
err := driver.Save(&accounts[i], nil)
//err := driver.Save(&accounts[i], nil)
err := operate.Save(driver, &accounts[i], nil)
if err != nil {
log.Errorf("init account [%s] error:[%s]", account.Name, err.Error())
} else {

View File

@ -3,6 +3,7 @@ package bootstrap
import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/utils"
"github.com/caarlos0/env/v6"
log "github.com/sirupsen/logrus"
"io/ioutil"
"os"
@ -42,8 +43,24 @@ func InitConf() {
log.Fatalf("update config struct error: %s", err.Error())
}
}
if !conf.Conf.Force {
confFromEnv()
}
err := os.MkdirAll(conf.Conf.TempDir, 0700)
if err != nil {
log.Fatalf("create temp dir error: %s", err.Error())
}
log.Debugf("config: %+v", conf.Conf)
}
func confFromEnv() {
prefix := "ALIST_"
if conf.Docker {
prefix = ""
}
if err := env.Parse(conf.Conf, env.Options{
Prefix: prefix,
}); err != nil {
log.Fatalf("load config from env error: %s", err.Error())
}
}

View File

@ -30,6 +30,7 @@ func init() {
flag.BoolVar(&conf.Debug, "debug", false, "start with debug mode")
flag.BoolVar(&conf.Version, "version", false, "print version info")
flag.BoolVar(&conf.Password, "password", false, "print current password")
flag.BoolVar(&conf.Docker, "docker", false, "is using docker")
flag.Parse()
InitLog()
}

View File

@ -50,8 +50,8 @@ func InitModel() {
}
case "mysql":
{
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
databaseConfig.User, databaseConfig.Password, databaseConfig.Host, databaseConfig.Port, databaseConfig.Name)
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local&tls=%s",
databaseConfig.User, databaseConfig.Password, databaseConfig.Host, databaseConfig.Port, databaseConfig.Name, databaseConfig.SslMode)
db, err := gorm.Open(mysql.Open(dsn), gormConfig)
if err != nil {
log.Fatalf("failed to connect database:%s", err.Error())

View File

@ -58,6 +58,14 @@ func InitSettings() {
Access: model.PUBLIC,
Group: model.FRONT,
},
{
Key: "announcement",
Value: "This is a test announcement.",
Description: "announcement message (support markdown)",
Type: "text",
Access: model.PUBLIC,
Group: model.FRONT,
},
{
Key: "text types",
Value: strings.Join(conf.TextTypes, ","),
@ -65,6 +73,20 @@ func InitSettings() {
Description: "text type extensions",
Group: model.FRONT,
},
{
Key: "audio types",
Value: strings.Join(conf.AudioTypes, ","),
Type: "string",
Description: "audio type extensions",
Group: model.FRONT,
},
{
Key: "video types",
Value: strings.Join(conf.VideoTypes, ","),
Type: "string",
Description: "video type extensions",
Group: model.FRONT,
},
{
Key: "d_proxy types",
Value: strings.Join(conf.DProxyTypes, ","),
@ -245,7 +267,7 @@ func InitSettings() {
if err == gorm.ErrRecordNotFound {
err = model.SaveSetting(v)
if v.Key == "password" {
log.Infof("Initial password: %s", v.Value)
log.Infof("Initial password: %s", conf.C.Sprintf(v.Value))
}
if err != nil {
log.Fatalf("failed write setting: %s", err.Error())
@ -261,6 +283,9 @@ func InitSettings() {
if err != nil {
log.Fatalf("failed write setting: %s", err.Error())
}
if v.Key == "password" {
log.Infof("Your password: %s", conf.C.Sprintf(v.Value))
}
}
}
model.LoadSettings()

View File

@ -1,36 +1,37 @@
package conf
type Database struct {
Type string `json:"type"`
User string `json:"user"`
Password string `json:"password"`
Host string `json:"host"`
Port int `json:"port"`
Name string `json:"name"`
TablePrefix string `json:"table_prefix"`
DBFile string `json:"db_file"`
SslMode string `json:"ssl_mode"`
Type string `json:"type" env:"DB_TYPE"`
Host string `json:"host" env:"DB_HOST"`
Port int `json:"port" env:"DB_PORT"`
User string `json:"user" env:"DB_USER"`
Password string `json:"password" env:"DB_PASS"`
Name string `json:"name" env:"DB_NAME"`
DBFile string `json:"db_file" env:"DB_FILE"`
TablePrefix string `json:"table_prefix" env:"DB_TABLE_PREFIX"`
SslMode string `json:"ssl_mode" env:"DB_SLL_MODE"`
}
type Scheme struct {
Https bool `json:"https"`
CertFile string `json:"cert_file"`
KeyFile string `json:"key_file"`
Https bool `json:"https" env:"HTTPS"`
CertFile string `json:"cert_file" env:"CERT_FILE"`
KeyFile string `json:"key_file" env:"KEY_FILE"`
}
type CacheConfig struct {
Expiration int64 `json:"expiration"`
CleanupInterval int64 `json:"cleanup_interval"`
Expiration int64 `json:"expiration" env:"CACHE_EXPIRATION"`
CleanupInterval int64 `json:"cleanup_interval" env:"CLEANUP_INTERVAL"`
}
type Config struct {
Address string `json:"address"`
Port int `json:"port"`
Assets string `json:"assets"`
Force bool `json:"force"`
Address string `json:"address" env:"ADDR"`
Port int `json:"port" env:"PORT"`
Assets string `json:"assets" env:"ASSETS"`
Database Database `json:"database"`
Scheme Scheme `json:"scheme"`
Cache CacheConfig `json:"cache"`
TempDir string `json:"temp_dir"`
TempDir string `json:"temp_dir" env:"TEMP_DIR"`
}
func DefaultConfig() *Config {
@ -44,7 +45,6 @@ func DefaultConfig() *Config {
Port: 0,
TablePrefix: "x_",
DBFile: "data/data.db",
SslMode: "disable",
},
Cache: CacheConfig{
Expiration: 60,

View File

@ -3,6 +3,7 @@ package conf
import (
"context"
"github.com/eko/gocache/v2/cache"
"github.com/fatih/color"
"github.com/robfig/cron/v3"
"gorm.io/gorm"
"strconv"
@ -23,11 +24,14 @@ var (
Debug bool
Version bool
Password bool
Docker bool
DB *gorm.DB
Cache *cache.Cache
Ctx = context.TODO()
Cron *cron.Cron
C = color.New(color.FgHiBlue, color.Bold, color.BgHiWhite, color.Underline)
)
var (

View File

@ -140,7 +140,7 @@ func (driver Pan123) Link(args base.Args, account *model.Account) (*base.Link, e
}
var resp Pan123DownResp
var headers map[string]string
if args.IP != "" && args.IP != "::1" {
if !utils.IsLocalIPAddr(args.IP) {
headers = map[string]string{
//"X-Real-IP": "1.1.1.1",
"X-Forwarded-For": args.IP,

View File

@ -17,6 +17,7 @@ import (
"io"
"math"
"net/http"
"net/http/cookiejar"
"path/filepath"
"regexp"
"strconv"
@ -105,6 +106,9 @@ func (driver Cloud189) Login(account *model.Account) error {
client.SetRetryCount(3)
client.SetHeader("Referer", "https://cloud.189.cn/")
}
// clear cookie
jar, _ := cookiejar.New(nil)
client.SetCookieJar(jar)
url := "https://cloud.189.cn/api/portal/loginUrl.action?redirectURL=https%3A%2F%2Fcloud.189.cn%2Fmain.action"
b := ""
lt := ""

View File

@ -10,6 +10,7 @@ import (
log "github.com/sirupsen/logrus"
"net/http"
"path/filepath"
"strings"
)
type Cloud189 struct{}
@ -198,6 +199,7 @@ func (driver Cloud189) Link(args base.Args, account *model.Account) (*base.Link,
} else {
link.Url = resp.FileDownloadUrl
}
link.Url = strings.Replace(link.Url, "http://", "https://", 1)
return &link, nil
}

330
drivers/189pc/189.go Normal file
View File

@ -0,0 +1,330 @@
package _189
import (
"bytes"
"errors"
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
"regexp"
"sync"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/go-resty/resty/v2"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
)
var userStateCache = struct {
sync.Mutex
States map[string]*State
}{States: make(map[string]*State)}
func GetState(account *model.Account) *State {
userStateCache.Lock()
defer userStateCache.Unlock()
if v, ok := userStateCache.States[account.Username]; ok && v != nil {
return v
}
state := &State{client: resty.New().
SetHeaders(map[string]string{
"Accept": "application/json;charset=UTF-8",
"User-Agent": base.UserAgent,
}),
}
userStateCache.States[account.Username] = state
return state
}
type State struct {
sync.Mutex
client *resty.Client
RsaPublicKey string
SessionKey string
SessionSecret string
FamilySessionKey string
FamilySessionSecret string
AccessToken string
//怎么刷新的???
RefreshToken string
}
func (s *State) login(account *model.Account) error {
// 清除cookie
jar, _ := cookiejar.New(nil)
s.client.SetCookieJar(jar)
var err error
var res *resty.Response
defer func() {
account.Status = "work"
if err != nil {
account.Status = err.Error()
}
model.SaveAccount(account)
if res != nil {
log.Debug(res.String())
}
}()
var param *LoginParam
param, err = s.getLoginParam()
if err != nil {
return err
}
// 提交登录
s.RsaPublicKey = fmt.Sprintf("-----BEGIN PUBLIC KEY-----\n%s\n-----END PUBLIC KEY-----", param.jRsaKey)
res, err = s.client.R().
SetHeaders(map[string]string{
"Referer": AUTH_URL,
"REQID": param.ReqId,
"lt": param.Lt,
}).
SetFormData(map[string]string{
"appKey": APP_ID,
"accountType": "02",
"userName": "{RSA}" + rsaEncrypt(s.RsaPublicKey, account.Username),
"password": "{RSA}" + rsaEncrypt(s.RsaPublicKey, account.Password),
"validateCode": param.vCodeRS,
"captchaToken": param.CaptchaToken,
"returnUrl": RETURN_URL,
"mailSuffix": "@189.cn",
"dynamicCheck": "FALSE",
"clientType": CLIENT_TYPE,
"cb_SaveName": "1",
"isOauth2": "false",
"state": "",
"paramId": param.ParamId,
}).
Post(AUTH_URL + "/api/logbox/oauth2/loginSubmit.do")
if err != nil {
return err
}
toUrl := utils.Json.Get(res.Body(), "toUrl").ToString()
if toUrl == "" {
log.Error(res.String())
return fmt.Errorf(res.String())
}
// 获取Session
var erron Erron
var sessionResp appSessionResp
res, err = s.client.R().
SetResult(&sessionResp).SetError(&erron).
SetQueryParams(clientSuffix()).
SetQueryParam("redirectURL", url.QueryEscape(toUrl)).
Post(API_URL + "/getSessionForPC.action")
if err != nil {
return err
}
if erron.ResCode != "" {
err = fmt.Errorf(erron.ResMessage)
return err
}
if sessionResp.ResCode != 0 {
err = fmt.Errorf(sessionResp.ResMessage)
return err
}
s.SessionKey = sessionResp.SessionKey
s.SessionSecret = sessionResp.SessionSecret
s.FamilySessionKey = sessionResp.FamilySessionKey
s.FamilySessionSecret = sessionResp.FamilySessionSecret
s.AccessToken = sessionResp.AccessToken
s.RefreshToken = sessionResp.RefreshToken
return err
}
func (s *State) getLoginParam() (*LoginParam, error) {
res, err := s.client.R().
SetQueryParams(map[string]string{
"appId": APP_ID,
"clientType": CLIENT_TYPE,
"returnURL": RETURN_URL,
"timeStamp": fmt.Sprint(timestamp()),
}).
Get(WEB_URL + "/api/portal/unifyLoginForPC.action")
if err != nil {
return nil, err
}
log.Debug(res.String())
param := &LoginParam{
CaptchaToken: regexp.MustCompile(`'captchaToken' value='(.+?)'`).FindStringSubmatch(res.String())[1],
Lt: regexp.MustCompile(`lt = "(.+?)"`).FindStringSubmatch(res.String())[1],
ParamId: regexp.MustCompile(`paramId = "(.+?)"`).FindStringSubmatch(res.String())[1],
ReqId: regexp.MustCompile(`reqId = "(.+?)"`).FindStringSubmatch(res.String())[1],
jRsaKey: regexp.MustCompile(`"j_rsaKey" value="(.+?)"`).FindStringSubmatch(res.String())[1],
vCodeID: regexp.MustCompile(`token=([A-Za-z0-9&=]+)`).FindStringSubmatch(res.String())[1],
}
imgRes, err := s.client.R().Get(fmt.Sprint(AUTH_URL, "/api/logbox/oauth2/picCaptcha.do?token=", param.vCodeID, timestamp()))
if err != nil {
return nil, err
}
if len(imgRes.Body()) > 0 {
vRes, err := resty.New().R().
SetMultipartField("image", "validateCode.png", "image/png", bytes.NewReader(imgRes.Body())).
Post(conf.GetStr("ocr api"))
if err != nil {
return nil, err
}
if utils.Json.Get(vRes.Body(), "status").ToInt() != 200 {
return nil, errors.New("ocr error:" + utils.Json.Get(vRes.Body(), "msg").ToString())
}
param.vCodeRS = utils.Json.Get(vRes.Body(), "result").ToString()
log.Debugln("code: ", param.vCodeRS)
}
return param, nil
}
func (s *State) refreshSession(account *model.Account) error {
var erron Erron
var userSessionResp UserSessionResp
res, err := s.client.R().
SetResult(&userSessionResp).SetError(&erron).
SetQueryParams(clientSuffix()).
SetQueryParams(map[string]string{
"appId": APP_ID,
"accessToken": s.AccessToken,
}).
SetHeader("X-Request-ID", uuid.NewString()).
Get("https://api.cloud.189.cn/getSessionForPC.action")
if err != nil {
return err
}
log.Debug(res.String())
if erron.ResCode != "" {
return fmt.Errorf(erron.ResMessage)
}
switch userSessionResp.ResCode {
case 0:
s.SessionKey = userSessionResp.SessionKey
s.SessionSecret = userSessionResp.SessionSecret
s.FamilySessionKey = userSessionResp.FamilySessionKey
s.FamilySessionSecret = userSessionResp.FamilySessionSecret
case 11, 18:
return s.login(account)
default:
account.Status = userSessionResp.ResMessage
_ = model.SaveAccount(account)
return fmt.Errorf(userSessionResp.ResMessage)
}
return nil
}
func (s *State) IsLogin() bool {
_, err := s.Request("GET", API_URL+"/getUserInfo.action", nil, func(r *resty.Request) {
r.SetQueryParams(clientSuffix())
}, nil)
return err == nil
}
func (s *State) Login(account *model.Account) error {
s.Lock()
defer s.Unlock()
return s.login(account)
}
func (s *State) RefreshSession(account *model.Account) error {
s.Lock()
defer s.Unlock()
return s.refreshSession(account)
}
func (s *State) Request(method string, fullUrl string, params url.Values, callback func(*resty.Request), account *model.Account) (*resty.Response, error) {
s.Lock()
dateOfGmt := getHttpDateStr()
sessionKey := s.SessionKey
sessionSecret := s.SessionSecret
if account != nil && isFamily(account) {
sessionKey = s.FamilySessionKey
sessionSecret = s.FamilySessionSecret
}
req := s.client.R()
req.SetHeaders(map[string]string{
"Date": dateOfGmt,
"SessionKey": sessionKey,
"X-Request-ID": uuid.NewString(),
})
// 设置params
var paramsData string
if params != nil {
paramsData = AesECBEncrypt(params.Encode(), s.SessionSecret[:16])
req.SetQueryParam("params", paramsData)
}
req.SetHeader("Signature", signatureOfHmac(sessionSecret, sessionKey, method, fullUrl, dateOfGmt, paramsData))
callback(req)
s.Unlock()
var err error
var res *resty.Response
switch method {
case "GET":
res, err = req.Get(fullUrl)
case "POST":
res, err = req.Post(fullUrl)
case "DELETE":
res, err = req.Delete(fullUrl)
case "PATCH":
res, err = req.Patch(fullUrl)
case "PUT":
res, err = req.Put(fullUrl)
default:
return nil, base.ErrNotSupport
}
if err != nil {
return nil, err
}
log.Debug(res.String())
var erron Erron
utils.Json.Unmarshal(res.Body(), &erron)
if erron.ResCode != "" {
return nil, fmt.Errorf(erron.ResMessage)
}
if erron.Code != "" && erron.Code != "SUCCESS" {
if erron.Msg == "" {
return nil, fmt.Errorf(erron.Message)
}
return nil, fmt.Errorf(erron.Msg)
}
if erron.ErrorCode != "" {
return nil, fmt.Errorf(erron.ErrorMsg)
}
if account != nil {
switch utils.Json.Get(res.Body(), "res_code").ToInt64() {
case 11, 18:
if err := s.RefreshSession(account); err != nil {
return nil, err
}
return s.Request(method, fullUrl, params, callback, account)
case 0:
if res.StatusCode() == http.StatusOK {
return res, nil
}
fallthrough
default:
return nil, fmt.Errorf(res.String())
}
}
if utils.Json.Get(res.Body(), "res_code").ToInt64() != 0 {
return res, fmt.Errorf(utils.Json.Get(res.Body(), "res_message").ToString())
}
return res, nil
}

831
drivers/189pc/driver.go Normal file
View File

@ -0,0 +1,831 @@
package _189
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
)
func init() {
base.RegisterDriver(new(Cloud189))
}
type Cloud189 struct {
}
func (driver Cloud189) Config() base.DriverConfig {
return base.DriverConfig{
Name: "189CloudPC",
}
}
func (driver Cloud189) Items() []base.Item {
return []base.Item{
{
Name: "username",
Label: "username",
Type: base.TypeString,
Required: true,
Description: "account username/phone number",
},
{
Name: "password",
Label: "password",
Type: base.TypeString,
Required: true,
Description: "account password",
},
{
Name: "root_folder",
Label: "root folder file_id",
Type: base.TypeString,
Required: true,
},
{
Name: "internal_type",
Label: "189cloud type",
Type: base.TypeSelect,
Required: true,
Values: "Personal,Family",
},
{
Name: "site_id",
Label: "family id",
Type: base.TypeString,
Required: true,
},
{
Name: "order_by",
Label: "order_by",
Type: base.TypeSelect,
Values: "filename,filesize,lastOpTime",
Required: true,
},
{
Name: "order_direction",
Label: "desc",
Type: base.TypeSelect,
Values: "true,false",
Required: true,
},
}
}
func (driver Cloud189) Save(account *model.Account, old *model.Account) error {
if account == nil {
return nil
}
if !isFamily(account) && account.RootFolder == "" {
account.RootFolder = "-11"
}
state := GetState(account)
if !state.IsLogin() {
if err := state.Login(account); err != nil {
return err
}
}
if isFamily(account) {
list, err := driver.getFamilyInfoList(account)
if err != nil {
return err
}
for _, l := range list {
if account.SiteId == "" {
account.SiteId = fmt.Sprint(l.FamilyID)
}
log.Infof("天翼家庭云 用户名:%s FamilyID %d\n", l.RemarkName, l.FamilyID)
}
}
account.Status = "work"
model.SaveAccount(account)
return nil
}
func (driver Cloud189) getFamilyInfoList(account *model.Account) ([]FamilyInfoResp, error) {
var resp FamilyInfoListResp
_, err := GetState(account).Request("GET", API_URL+"/family/manage/getFamilyList.action", nil, func(r *resty.Request) {
r.SetQueryParams(clientSuffix())
r.SetResult(&resp)
}, account)
if err != nil {
return nil, err
}
return resp.FamilyInfoResp, nil
}
func (driver Cloud189) 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, base.ErrPathNotFound
}
func (driver Cloud189) Files(path string, account *model.Account) ([]model.File, error) {
path = utils.ParsePath(path)
cache, err := base.GetCache(path, account)
if err == nil {
files, _ := cache.([]model.File)
return files, nil
}
file, err := driver.File(path, account)
if err != nil {
return nil, err
}
fullUrl := API_URL
if isFamily(account) {
fullUrl += "/family/file"
}
fullUrl += "/listFiles.action"
files := make([]model.File, 0)
client := GetState(account)
for pageNum := 1; ; pageNum++ {
var resp Cloud189FilesResp
queryparam := map[string]string{
"folderId": file.Id,
"fileType": "0",
"mediaAttr": "0",
"iconOption": "5",
"pageNum": fmt.Sprint(pageNum),
"pageSize": "130",
}
_, err = client.Request("GET", fullUrl, nil, func(r *resty.Request) {
r.SetQueryParams(clientSuffix()).SetQueryParams(queryparam)
if isFamily(account) {
r.SetQueryParams(map[string]string{
"familyId": account.SiteId,
"orderBy": toFamilyOrderBy(account.OrderBy),
"descending": account.OrderDirection,
})
} else {
r.SetQueryParams(map[string]string{
"recursive": "0",
"orderBy": account.OrderBy,
"descending": account.OrderDirection,
})
}
r.SetResult(&resp)
}, account)
if err != nil {
return nil, err
}
// 获取完毕跳出
if resp.FileListAO.Count == 0 {
break
}
mustTime := func(str string) *time.Time {
time, _ := http.ParseTime(str)
return &time
}
for _, folder := range resp.FileListAO.FolderList {
files = append(files, model.File{
Id: fmt.Sprint(folder.ID),
Name: folder.Name,
Size: 0,
Type: conf.FOLDER,
Driver: driver.Config().Name,
UpdatedAt: mustTime(folder.CreateDate),
})
}
for _, file := range resp.FileListAO.FileList {
files = append(files, model.File{
Id: fmt.Sprint(file.ID),
Name: file.Name,
Size: file.Size,
Type: utils.GetFileType(filepath.Ext(file.Name)),
Driver: driver.Config().Name,
UpdatedAt: mustTime(file.CreateDate),
Thumbnail: file.Icon.SmallUrl,
})
}
}
if len(files) > 0 {
_ = base.SetCache(path, files, account)
}
return files, nil
}
func (driver Cloud189) Path(path string, account *model.Account) (*model.File, []model.File, error) {
path = utils.ParsePath(path)
log.Debugf("189PC path: %s", path)
file, err := driver.File(path, account)
if err != nil {
return nil, nil, err
}
if !file.IsDir() {
return file, nil, nil
}
files, err := driver.Files(path, account)
if err != nil {
return nil, nil, err
}
return nil, files, nil
}
func (driver Cloud189) Link(args base.Args, account *model.Account) (*base.Link, error) {
file, err := driver.File(utils.ParsePath(args.Path), account)
if err != nil {
return nil, err
}
if file.Type == conf.FOLDER {
return nil, base.ErrNotFile
}
fullUrl := API_URL
if isFamily(account) {
fullUrl += "/family/file"
}
fullUrl += "/getFileDownloadUrl.action"
var downloadUrl struct {
URL string `json:"fileDownloadUrl"`
}
_, err = GetState(account).Request("GET", fullUrl, nil, func(r *resty.Request) {
r.SetQueryParams(clientSuffix()).SetQueryParam("fileId", file.Id)
if isFamily(account) {
r.SetQueryParams(map[string]string{
"familyId": account.SiteId,
})
} else {
r.SetQueryParams(map[string]string{
"dt": "3",
"flag": "1",
})
}
r.SetResult(&downloadUrl)
}, account)
if err != nil {
return nil, err
}
return &base.Link{
Headers: []base.Header{
{Name: "User-Agent", Value: base.UserAgent},
},
Url: strings.ReplaceAll(downloadUrl.URL, "&amp;", "&"),
}, nil
}
func (driver Cloud189) Preview(path string, account *model.Account) (interface{}, error) {
return nil, base.ErrNotSupport
}
func (driver Cloud189) MakeDir(path string, account *model.Account) error {
dir, name := filepath.Split(path)
parentFile, err := driver.File(dir, account)
if err != nil {
return err
}
if !parentFile.IsDir() {
return base.ErrNotFolder
}
fullUrl := API_URL
if isFamily(account) {
fullUrl += "/family/file"
}
fullUrl += "/createFolder.action"
_, err = GetState(account).Request("POST", fullUrl, nil, func(r *resty.Request) {
r.SetQueryParams(clientSuffix()).SetQueryParams(map[string]string{
"folderName": name,
"relativePath": "",
})
if isFamily(account) {
r.SetQueryParams(map[string]string{
"familyId": account.SiteId,
"parentId": parentFile.Id,
})
} else {
r.SetQueryParams(map[string]string{
"parentFolderId": parentFile.Id,
})
}
}, account)
return err
}
func (driver Cloud189) Move(src string, dst string, account *model.Account) error {
srcFile, err := driver.File(src, account)
if err != nil {
return err
}
dstDirFile, err := driver.File(filepath.Dir(dst), account)
if err != nil {
return err
}
_, err = GetState(account).Request("POST", API_URL+"/batch/createBatchTask.action", nil, func(r *resty.Request) {
r.SetFormData(clientSuffix()).SetFormData(map[string]string{
"type": "MOVE",
"taskInfos": string(MustToBytes(utils.Json.Marshal(
[]*BatchTaskInfo{
{
FileId: srcFile.Id,
FileName: srcFile.Name,
IsFolder: BoolToNumber(srcFile.IsDir()),
},
}))),
"targetFolderId": dstDirFile.Id,
})
if isFamily(account) {
r.SetFormData(map[string]string{
"familyId": account.SiteId,
})
}
}, account)
return err
}
/*
func (driver Cloud189) Move(src string, dst string, account *model.Account) error {
srcFile, err := driver.File(src, account)
if err != nil {
return err
}
dstDirFile, err := driver.File(filepath.Dir(dst), account)
if err != nil {
return err
}
var queryParam map[string]string
fullUrl := API_URL
method := "POST"
if isFamily(account) {
fullUrl += "/family/file"
method = "GET"
}
if srcFile.IsDir() {
fullUrl += "/moveFolder.action"
queryParam = map[string]string{
"folderId": srcFile.Id,
"destFolderName": srcFile.Name,
}
} else {
fullUrl += "/moveFile.action"
queryParam = map[string]string{
"fileId": srcFile.Id,
"destFileName": srcFile.Name,
}
}
_, err = GetState(account).Request(method, fullUrl, nil, func(r *resty.Request) {
r.SetQueryParams(queryParam).SetQueryParams(clientSuffix())
if isFamily(account) {
r.SetQueryParams(map[string]string{
"familyId": account.SiteId,
"destParentId": dstDirFile.Id,
})
} else {
r.SetQueryParam("destParentFolderId", dstDirFile.Id)
}
}, account)
return err
}*/
func (driver Cloud189) Rename(src string, dst string, account *model.Account) error {
srcFile, err := driver.File(src, account)
if err != nil {
return err
}
var queryParam map[string]string
fullUrl := API_URL
method := "POST"
if isFamily(account) {
fullUrl += "/family/file"
method = "GET"
}
if srcFile.IsDir() {
fullUrl += "/renameFolder.action"
queryParam = map[string]string{
"folderId": srcFile.Id,
"destFolderName": filepath.Base(dst),
}
} else {
fullUrl += "/renameFile.action"
queryParam = map[string]string{
"fileId": srcFile.Id,
"destFileName": filepath.Base(dst),
}
}
_, err = GetState(account).Request(method, fullUrl, nil, func(r *resty.Request) {
r.SetQueryParams(queryParam).SetQueryParams(clientSuffix())
if isFamily(account) {
r.SetQueryParam("familyId", account.SiteId)
}
}, account)
return err
}
func (driver Cloud189) Copy(src string, dst string, account *model.Account) error {
srcFile, err := driver.File(src, account)
if err != nil {
return err
}
dstDirFile, err := driver.File(filepath.Dir(dst), account)
if err != nil {
return err
}
_, err = GetState(account).Request("POST", API_URL+"/batch/createBatchTask.action", nil, func(r *resty.Request) {
r.SetFormData(clientSuffix()).SetFormData(map[string]string{
"type": "COPY",
"taskInfos": string(MustToBytes(utils.Json.Marshal(
[]*BatchTaskInfo{
{
FileId: srcFile.Id,
FileName: srcFile.Name,
IsFolder: BoolToNumber(srcFile.IsDir()),
},
}))),
"targetFolderId": dstDirFile.Id,
"targetFileName": filepath.Base(dst),
})
if isFamily(account) {
r.SetFormData(map[string]string{
"familyId": account.SiteId,
})
}
}, account)
return err
}
func (driver Cloud189) Delete(path string, account *model.Account) error {
path = utils.ParsePath(path)
srcFile, err := driver.File(path, account)
if err != nil {
return err
}
_, err = GetState(account).Request("POST", API_URL+"/batch/createBatchTask.action", nil, func(r *resty.Request) {
r.SetFormData(clientSuffix()).SetFormData(map[string]string{
"type": "DELETE",
"taskInfos": string(MustToBytes(utils.Json.Marshal(
[]*BatchTaskInfo{
{
FileId: srcFile.Id,
FileName: srcFile.Name,
IsFolder: BoolToNumber(srcFile.IsDir()),
},
}))),
})
if isFamily(account) {
r.SetFormData(map[string]string{
"familyId": account.SiteId,
})
}
}, account)
return err
}
func (driver Cloud189) Upload(file *model.FileStream, account *model.Account) error {
if file == nil {
return base.ErrEmptyFile
}
parentFile, err := driver.File(file.ParentPath, account)
if err != nil {
return err
}
if !parentFile.IsDir() {
return base.ErrNotFolder
}
if isFamily(account) {
return driver.uploadFamily(file, parentFile, account)
}
return driver.uploadPerson(file, parentFile, account)
}
func (driver Cloud189) uploadFamily(file *model.FileStream, parentFile *model.File, account *model.Account) error {
tempFile, err := ioutil.TempFile(conf.Conf.TempDir, "file-*")
if err != nil {
return err
}
defer tempFile.Close()
defer os.Remove(tempFile.Name())
fileMd5 := md5.New()
if _, err = io.Copy(io.MultiWriter(fileMd5, tempFile), file); err != nil {
return err
}
client := GetState(account)
var createUpload CreateUploadFileResult
_, err = client.Request("GET", API_URL+"/family/file/createFamilyFile.action", nil, func(r *resty.Request) {
r.SetQueryParams(map[string]string{
"fileMd5": hex.EncodeToString(fileMd5.Sum(nil)),
"fileName": file.Name,
"familyId": account.SiteId,
"parentId": parentFile.Id,
"resumePolicy": "1",
"fileSize": fmt.Sprint(file.Size),
})
r.SetQueryParams(clientSuffix())
r.SetResult(&createUpload)
}, account)
if err != nil {
return err
}
if createUpload.FileDataExists != 1 {
if createUpload.UploadFileId, err = driver.uploadFileData(file, tempFile, createUpload, account); err != nil {
return err
}
}
_, err = client.Request("GET", createUpload.FileCommitUrl, nil, func(r *resty.Request) {
r.SetQueryParams(clientSuffix())
r.SetHeaders(map[string]string{
"FamilyId": account.SiteId,
"uploadFileId": fmt.Sprint(createUpload.UploadFileId),
"ResumePolicy": "1",
})
}, account)
return err
}
func (driver Cloud189) uploadPerson(file *model.FileStream, parentFile *model.File, account *model.Account) error {
tempFile, err := ioutil.TempFile(conf.Conf.TempDir, "file-*")
if err != nil {
return err
}
defer tempFile.Close()
defer os.Remove(tempFile.Name())
fileMd5 := md5.New()
if _, err = io.Copy(io.MultiWriter(fileMd5, tempFile), file); err != nil {
return err
}
client := GetState(account)
var createUpload CreateUploadFileResult
_, err = client.Request("POST", API_URL+"/createUploadFile.action", nil, func(r *resty.Request) {
r.SetQueryParams(clientSuffix())
r.SetFormData(clientSuffix()).SetFormData(map[string]string{
"parentFolderId": parentFile.Id,
"baseFileId": "",
"fileName": file.Name,
"size": fmt.Sprint(file.Size),
"md5": hex.EncodeToString(fileMd5.Sum(nil)),
// "lastWrite": param.LastWrite,
// "localPath": strings.ReplaceAll(file.ParentPath, "\\", "/"),
"opertype": "1",
"flag": "1",
"resumePolicy": "1",
"isLog": "0",
"fileExt": "",
})
r.SetResult(&createUpload)
}, account)
if err != nil {
return err
}
if createUpload.FileDataExists != 1 {
if createUpload.UploadFileId, err = driver.uploadFileData(file, tempFile, createUpload, account); err != nil {
return err
}
}
_, err = client.Request("POST", createUpload.FileCommitUrl, nil, func(r *resty.Request) {
r.SetQueryParams(clientSuffix())
r.SetFormData(map[string]string{
"uploadFileId": fmt.Sprint(createUpload.UploadFileId),
"opertype": "5", //5 覆盖 1 重命名
"ResumePolicy": "1",
"isLog": "0",
})
}, account)
return err
}
func (driver Cloud189) uploadFileData(file *model.FileStream, tempFile *os.File, createUpload CreateUploadFileResult, account *model.Account) (int64, error) {
uploadFileState, err := driver.getUploadFileState(createUpload.UploadFileId, account)
if err != nil {
return 0, err
}
if uploadFileState.FileDataExists == 1 || uploadFileState.DataSize == int64(file.Size) {
return uploadFileState.UploadFileId, nil
}
if _, err = tempFile.Seek(uploadFileState.DataSize, io.SeekStart); err != nil {
return 0, err
}
_, err = GetState(account).Request("PUT", uploadFileState.FileUploadUrl, nil, func(r *resty.Request) {
r.SetQueryParams(clientSuffix())
r.SetHeaders(map[string]string{
"Content-Type": "application/octet-stream",
"ResumePolicy": "1",
"Edrive-UploadFileRange": fmt.Sprintf("bytes=%d-%d", uploadFileState.DataSize, file.Size),
"Expect": "100-continue",
})
if isFamily(account) {
r.SetHeaders(map[string]string{
"familyId": account.SiteId,
"UploadFileId": fmt.Sprint(uploadFileState.UploadFileId),
})
} else {
r.SetHeader("Edrive-UploadFileId", fmt.Sprint(uploadFileState.UploadFileId))
}
r.SetBody(tempFile)
}, account)
return uploadFileState.UploadFileId, err
}
func (driver Cloud189) getUploadFileState(uploadFileId int64, account *model.Account) (*UploadFileStatusResult, error) {
fullUrl := API_URL
if isFamily(account) {
fullUrl += "/family/file/getFamilyFileStatus.action"
} else {
fullUrl += "/getUploadFileStatus.action"
}
var uploadFileState UploadFileStatusResult
_, err := GetState(account).Request("GET", fullUrl, nil, func(r *resty.Request) {
r.SetQueryParams(clientSuffix())
r.SetQueryParams(map[string]string{
"uploadFileId": fmt.Sprint(uploadFileId),
"resumePolicy": "1",
})
if isFamily(account) {
r.SetQueryParam("familyId", account.SiteId)
}
r.SetResult(&uploadFileState)
}, account)
if err != nil {
return nil, err
}
return &uploadFileState, nil
}
/*
暂时未解决
func (driver Cloud189) Upload(file *model.FileStream, account *model.Account) error {
if file == nil {
return base.ErrEmptyFile
}
parentFile, err := driver.File(file.ParentPath, account)
if err != nil {
return err
}
if !parentFile.IsDir() {
return base.ErrNotFolder
}
fullUrl := UPLOAD_URL
if isFamily(account) {
fullUrl += "/family"
} else {
fullUrl += "/person"
}
tempFile, err := ioutil.TempFile(conf.Conf.TempDir, "file-*")
if err != nil {
return err
}
defer tempFile.Close()
defer os.Remove(tempFile.Name())
// 初始化上传
const DEFAULT int64 = 10485760
count := int64(math.Ceil(float64(file.Size) / float64(DEFAULT)))
fileMd5 := md5.New()
silceMd5 := md5.New()
silceMd5Hexs := make([]string, 0, count)
silceMd5Base64s := make([]string, 0, count)
for i := int64(1); i <= count; i++ {
if _, err := io.CopyN(io.MultiWriter(fileMd5, silceMd5, tempFile), file, DEFAULT); err != io.EOF {
return err
}
md5Byte := silceMd5.Sum(nil)
silceMd5Hexs = append(silceMd5Hexs, strings.ToUpper(hex.EncodeToString(md5Byte)))
silceMd5Base64s = append(silceMd5Base64s, fmt.Sprint(i, "-", base64.StdEncoding.EncodeToString(md5Byte)))
}
fileMd5Hex := strings.ToUpper(hex.EncodeToString(fileMd5.Sum(nil)))
sliceMd5Hex := fileMd5Hex
if int64(file.Size) > DEFAULT {
sliceMd5Hex = strings.ToUpper(utils.GetMD5Encode(strings.Join(silceMd5Hexs, "\n")))
}
qID := uuid.NewString()
client := GetState(account)
param := MapToUrlValues(map[string]interface{}{
"parentFolderId": parentFile.Id,
"fileName": url.QueryEscape(file.Name),
"fileMd5": fileMd5Hex,
"fileSize": fmt.Sprint(file.Size),
"sliceMd5": sliceMd5Hex,
"sliceSize": fmt.Sprint(DEFAULT),
})
if isFamily(account) {
param.Set("familyId", account.SiteId)
}
var uploadInfo InitMultiUploadResp
_, err = client.Request("GET", fullUrl+"/initMultiUpload", param, func(r *resty.Request) {
r.SetQueryParams(clientSuffix())
r.SetHeader("X-Request-ID", qID)
r.SetResult(&uploadInfo)
}, account)
if err != nil {
return err
}
if uploadInfo.Data.FileDataExists != 1 {
param = MapToUrlValues(map[string]interface{}{
"uploadFileId": uploadInfo.Data.UploadFileID,
"partInfo": strings.Join(silceMd5Base64s, ","),
})
if isFamily(account) {
param.Set("familyId", account.SiteId)
}
var uploadUrls UploadUrlsResp
_, err := client.Request("GET", fullUrl+"/getMultiUploadUrls", param, func(r *resty.Request) {
r.SetQueryParams(clientSuffix())
r.SetHeader("X-Request-ID", qID).SetHeader("content-type", "application/x-www-form-urlencoded")
r.SetResult(&uploadUrls)
}, account)
if err != nil {
return err
}
var i int64
for _, uploadurl := range uploadUrls.UploadUrls {
req := resty.New().SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}).SetProxy("http://192.168.0.30:8888").R()
for _, header := range strings.Split(decodeURIComponent(uploadurl.RequestHeader), "&") {
i := strings.Index(header, "=")
req.SetHeader(header[0:i], header[i+1:])
}
_, err := req.SetBody(io.NewSectionReader(tempFile, i*DEFAULT, DEFAULT)).Put(uploadurl.RequestURL)
if err != nil {
return err
}
}
}
param = MapToUrlValues(map[string]interface{}{
"uploadFileId": uploadInfo.Data.UploadFileID,
"isLog": "0",
"opertype": "1",
})
if isFamily(account) {
param.Set("familyId", account.SiteId)
}
_, err = client.Request("GET", fullUrl+"/commitMultiUploadFile", param, func(r *resty.Request) {
r.SetHeader("X-Request-ID", qID)
r.SetQueryParams(clientSuffix())
}, account)
return err
}
*/
var _ base.Driver = (*Cloud189)(nil)

180
drivers/189pc/type.go Normal file
View File

@ -0,0 +1,180 @@
package _189
import "encoding/xml"
type LoginParam struct {
CaptchaToken string
Lt string
ParamId string
ReqId string
jRsaKey string
vCodeID string
vCodeRS string
}
// 居然有四种返回方式
type Erron struct {
ResCode string `json:"res_code"`
ResMessage string `json:"res_message"`
XMLName xml.Name `xml:"error"`
Code string `json:"code" xml:"code"`
Message string `json:"message" xml:"message"`
// Code string `json:"code"`
Msg string `json:"msg"`
ErrorCode string `json:"errorCode"`
ErrorMsg string `json:"errorMsg"`
}
// 刷新session返回
type UserSessionResp struct {
ResCode int `json:"res_code"`
ResMessage string `json:"res_message"`
LoginName string `json:"loginName"`
KeepAlive int `json:"keepAlive"`
GetFileDiffSpan int `json:"getFileDiffSpan"`
GetUserInfoSpan int `json:"getUserInfoSpan"`
// 个人云
SessionKey string `json:"sessionKey"`
SessionSecret string `json:"sessionSecret"`
// 家庭云
FamilySessionKey string `json:"familySessionKey"`
FamilySessionSecret string `json:"familySessionSecret"`
}
//登录返回
type appSessionResp struct {
UserSessionResp
IsSaveName string `json:"isSaveName"`
// 会话刷新Token
AccessToken string `json:"accessToken"`
//Token刷新
RefreshToken string `json:"refreshToken"`
}
type FamilyInfoListResp struct {
FamilyInfoResp []FamilyInfoResp `json:"familyInfoResp"`
}
type FamilyInfoResp struct {
Count int `json:"count"`
CreateTime string `json:"createTime"`
FamilyID int `json:"familyId"`
RemarkName string `json:"remarkName"`
Type int `json:"type"`
UseFlag int `json:"useFlag"`
UserRole int `json:"userRole"`
}
/*文件部分*/
// 文件
type Cloud189File struct {
CreateDate string `json:"createDate"`
FileCata int64 `json:"fileCata"`
Icon struct {
//iconOption 5
SmallUrl string `json:"smallUrl"`
LargeUrl string `json:"largeUrl"`
// iconOption 10
Max600 string `json:"max600"`
MediumURL string `json:"mediumUrl"`
} `json:"icon"`
ID int64 `json:"id"`
LastOpTime string `json:"lastOpTime"`
Md5 string `json:"md5"`
MediaType int `json:"mediaType"`
Name string `json:"name"`
Orientation int64 `json:"orientation"`
Rev string `json:"rev"`
Size int64 `json:"size"`
StarLabel int64 `json:"starLabel"`
}
// 文件夹
type Cloud189Folder struct {
ID int64 `json:"id"`
ParentID int64 `json:"parentId"`
Name string `json:"name"`
FileCata int64 `json:"fileCata"`
FileCount int64 `json:"fileCount"`
LastOpTime string `json:"lastOpTime"`
CreateDate string `json:"createDate"`
FileListSize int64 `json:"fileListSize"`
Rev string `json:"rev"`
StarLabel int64 `json:"starLabel"`
}
type Cloud189FilesResp struct {
//ResCode int `json:"res_code"`
//ResMessage string `json:"res_message"`
FileListAO struct {
Count int `json:"count"`
FileList []Cloud189File `json:"fileList"`
FolderList []Cloud189Folder `json:"folderList"`
} `json:"fileListAO"`
}
// TaskInfo 任务信息
type BatchTaskInfo struct {
// FileId 文件ID
FileId string `json:"fileId"`
// FileName 文件名
FileName string `json:"fileName"`
// IsFolder 是否是文件夹0-否1-是
IsFolder int `json:"isFolder"`
// SrcParentId 文件所在父目录ID
//SrcParentId string `json:"srcParentId"`
}
type CreateUploadFileResult struct {
// UploadFileId 上传文件请求ID
UploadFileId int64 `json:"uploadFileId"`
// FileUploadUrl 上传文件数据的URL路径
FileUploadUrl string `json:"fileUploadUrl"`
// FileCommitUrl 上传文件完成后确认路径
FileCommitUrl string `json:"fileCommitUrl"`
// FileDataExists 文件是否已存在云盘中0-未存在1-已存在
FileDataExists int `json:"fileDataExists"`
}
type UploadFileStatusResult struct {
// 上传文件的ID
UploadFileId int64 `json:"uploadFileId"`
// 已上传的大小
DataSize int64 `json:"dataSize"`
FileUploadUrl string `json:"fileUploadUrl"`
FileCommitUrl string `json:"fileCommitUrl"`
FileDataExists int `json:"fileDataExists"`
}
/*
type InitMultiUploadResp struct {
//Code string `json:"code"`
Data struct {
UploadType int `json:"uploadType"`
UploadHost string `json:"uploadHost"`
UploadFileID string `json:"uploadFileId"`
FileDataExists int `json:"fileDataExists"`
} `json:"data"`
}
type UploadUrlsResp struct {
Code string `json:"code"`
UploadUrls map[string]Part `json:"uploadUrls"`
}
type Part struct {
RequestURL string `json:"requestURL"`
RequestHeader string `json:"requestHeader"`
}
*/

145
drivers/189pc/util.go Normal file
View File

@ -0,0 +1,145 @@
package _189
import (
"bytes"
"crypto/aes"
"crypto/hmac"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/pem"
"fmt"
rand2 "math/rand"
"net/http"
"net/url"
"strings"
"time"
"github.com/Xhofe/alist/model"
)
const (
APP_ID = "8025431004"
CLIENT_TYPE = "10020"
VERSION = "6.2"
WEB_URL = "https://cloud.189.cn"
AUTH_URL = "https://open.e.189.cn"
API_URL = "https://api.cloud.189.cn"
UPLOAD_URL = "https://upload.cloud.189.cn"
RETURN_URL = "https://m.cloud.189.cn/zhuanti/2020/loginErrorPc/index.html"
PC = "TELEPC"
MAC = "TELEMAC"
CHANNEL_ID = "web_cloud.189.cn"
)
func clientSuffix() map[string]string {
return map[string]string{
"clientType": PC,
"version": VERSION,
"channelId": CHANNEL_ID,
"rand": fmt.Sprintf("%d_%d", rand2.Int63n(1e5), rand2.Int63n(1e10)),
}
}
// 带params的SignatureOfHmac HMAC签名
func signatureOfHmac(sessionSecret, sessionKey, operate, fullUrl, dateOfGmt, param string) string {
u, _ := url.Parse(fullUrl)
mac := hmac.New(sha1.New, []byte(sessionSecret))
data := fmt.Sprintf("SessionKey=%s&Operate=%s&RequestURI=%s&Date=%s", sessionKey, operate, u.Path, dateOfGmt)
if param != "" {
data += fmt.Sprintf("&params=%s", param)
}
mac.Write([]byte(data))
return strings.ToUpper(hex.EncodeToString(mac.Sum(nil)))
}
// 获取http规范的时间
func getHttpDateStr() string {
return time.Now().UTC().Format(http.TimeFormat)
}
// RAS 加密用户名密码
func rsaEncrypt(publicKey, origData string) string {
block, _ := pem.Decode([]byte(publicKey))
pubInterface, _ := x509.ParsePKIXPublicKey(block.Bytes)
data, _ := rsa.EncryptPKCS1v15(rand.Reader, pubInterface.(*rsa.PublicKey), []byte(origData))
return base64ToHex(base64.StdEncoding.EncodeToString(data))
}
// aes 加密params
func AesECBEncrypt(data, key string) string {
block, _ := aes.NewCipher([]byte(key))
paddingData := PKCS7Padding([]byte(data), block.BlockSize())
decrypted := make([]byte, len(paddingData))
size := block.BlockSize()
for src, dst := paddingData, decrypted; len(src) > 0; src, dst = src[size:], dst[size:] {
block.Encrypt(dst[:size], src[:size])
}
return strings.ToUpper(hex.EncodeToString(decrypted))
}
func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
// 时间戳
func timestamp() int64 {
return time.Now().UTC().UnixNano() / 1e6
}
func base64ToHex(a string) string {
v, _ := base64.StdEncoding.DecodeString(a)
return strings.ToUpper(hex.EncodeToString(v))
}
func isFamily(account *model.Account) bool {
return account.InternalType == "Family"
}
func toFamilyOrderBy(o string) string {
switch o {
case "filename":
return "1"
case "filesize":
return "2"
case "lastOpTime":
return "3"
default:
return "1"
}
}
func MapToUrlValues(m map[string]interface{}) url.Values {
url := make(url.Values, len(m))
for k, v := range m {
url.Add(k, fmt.Sprint(v))
}
return url
}
func decodeURIComponent(str string) string {
r, _ := url.QueryUnescape(str)
//r, _ := url.PathUnescape(str)
//r = strings.ReplaceAll(r, " ", "+")
return r
}
func MustToBytes(b []byte, err error) []byte {
return b
}
func BoolToNumber(b bool) int {
if b {
return 1
}
return 0
}

View File

@ -228,7 +228,7 @@ func (driver AliDrive) batch(srcId, dstId string, url string, account *model.Acc
}
return fmt.Errorf("%s", e.Message)
}
status := jsoniter.Get(res.Body(), "status").ToInt()
status := jsoniter.Get(res.Body(), "responses", 0, "status").ToInt()
if status < 400 && status >= 100 {
return nil
}

View File

@ -94,15 +94,15 @@ func (driver AliDrive) Save(account *model.Account, old *model.Account) error {
log.Debugf("user info: %+v", resp)
account.DriveId = resp["default_drive_id"].(string)
cronId, err := conf.Cron.AddFunc("@every 2h", func() {
name := account.Name
log.Debugf("ali account name: %s", name)
newAccount, ok := model.GetAccount(name)
id := account.ID
log.Debugf("ali account id: %d", id)
newAccount, err := model.GetAccountById(id)
log.Debugf("ali account: %+v", newAccount)
if !ok {
if err != nil {
return
}
err = driver.RefreshToken(&newAccount)
_ = model.SaveAccount(&newAccount)
err = driver.RefreshToken(newAccount)
_ = model.SaveAccount(newAccount)
})
if err != nil {
return err
@ -412,7 +412,7 @@ func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) er
buf := make([]byte, 1024)
n, _ := file.Read(buf[:])
reqBody := base.Json{
"check_name_mode": "auto_rename",
"check_name_mode": "overwrite",
"drive_id": account.DriveId,
"name": file.GetFileName(),
"parent_file_id": parentFile.Id,

View File

@ -4,6 +4,7 @@ import (
_ "github.com/Xhofe/alist/drivers/123"
_ "github.com/Xhofe/alist/drivers/139"
_ "github.com/Xhofe/alist/drivers/189"
_ "github.com/Xhofe/alist/drivers/189pc"
_ "github.com/Xhofe/alist/drivers/alidrive"
_ "github.com/Xhofe/alist/drivers/alist"
_ "github.com/Xhofe/alist/drivers/baidu"

View File

@ -76,6 +76,9 @@ func (driver Baidu) Items() []base.Item {
}
func (driver Baidu) Save(account *model.Account, old *model.Account) error {
if account == nil {
return nil
}
return driver.RefreshToken(account)
}

View File

@ -121,7 +121,7 @@ func GetDrivers() map[string][]Item {
{
Name: "down_proxy_url",
Label: "down_proxy_url",
Type: TypeString,
Type: TypeText,
},
{
Name: "extract_folder",

View File

@ -21,6 +21,7 @@ const (
TypeSelect = "select"
TypeBool = "bool"
TypeNumber = "number"
TypeText = "text"
)
const (

View File

@ -29,12 +29,14 @@ func (driver GoogleDrive) Items() []base.Item {
Label: "client id",
Type: base.TypeString,
Required: true,
Default: "202264815644.apps.googleusercontent.com",
},
{
Name: "client_secret",
Label: "client secret",
Type: base.TypeString,
Required: true,
Default: "X4Z3ca8xfWDb1Voo-F9a7ZxJ",
},
{
Name: "refresh_token",

View File

@ -73,8 +73,8 @@ func (driver GoogleDrive) FormatFile(file *File, account *model.Account) *model.
f.Type = utils.GetFileType(filepath.Ext(file.Name))
}
if file.ThumbnailLink != "" {
if account.DownProxyUrl != "" {
f.Thumbnail = fmt.Sprintf("%s/%s", account.DownProxyUrl, file.ThumbnailLink)
if account.APIProxyUrl != "" {
f.Thumbnail = fmt.Sprintf("%s/%s", account.APIProxyUrl, file.ThumbnailLink)
} else {
f.Thumbnail = file.ThumbnailLink
}

View File

@ -125,13 +125,19 @@ func (driver Lanzou) Link(args base.Args, account *model.Account) (*base.Link, e
}
log.Debugf("down file: %+v", file)
downId := file.Id
pwd := ""
if account.InternalType == "cookie" {
downId, err = driver.GetDownPageId(file.Id, account)
downId, pwd, err = driver.GetDownPageId(file.Id, account)
if err != nil {
return nil, err
}
}
url, err := driver.GetLink(downId, account)
var url string
//if pwd != "" {
//url, err = driver.GetLinkWithPassword(downId, pwd, account)
//} else {
url, err = driver.GetLink(downId, pwd, account)
//}
if err != nil {
return nil, err
}

View File

@ -165,29 +165,21 @@ func (driver *Lanzou) GetFilesByUrl(account *model.Account) ([]LanZouFile, error
//}
// GetDownPageId 获取下载页面的ID
func (driver *Lanzou) GetDownPageId(fileId string, account *model.Account) (string, error) {
var resp LanZouFilesResp
func (driver *Lanzou) GetDownPageId(fileId string, account *model.Account) (string, string, error) {
var resp DownPageResp
res, err := base.RestyClient.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
return "", "", err
}
log.Debug(res.String())
if resp.Zt != 1 {
return "", fmt.Errorf("%v", resp.Info)
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
return resp.Info.FId, resp.Info.Pwd, nil
}
type LanzouLinkResp struct {
@ -196,19 +188,20 @@ type LanzouLinkResp struct {
Zt int `json:"zt"`
}
func (driver *Lanzou) GetLink(downId string, account *model.Account) (string, error) {
func (driver *Lanzou) GetLink(downId string, pwd string, account *model.Account) (string, error) {
shareUrl := account.SiteUrl
u, err := url.Parse(shareUrl)
if err != nil {
return "", err
}
log.Debugln(fmt.Sprintf("https://%s/%s", u.Host, downId))
res, err := base.RestyClient.R().Get(fmt.Sprintf("https://%s/%s", u.Host, 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")
return driver.GetLinkWithPassword(downId, pwd, res.String(), account)
}
iframeUrl := fmt.Sprintf("https://%s%s", u.Host, iframe[1])
res, err = base.RestyClient.R().Get(iframeUrl)
@ -224,8 +217,12 @@ func (driver *Lanzou) GetLink(downId string, account *model.Account) (string, er
//sign := regexp.MustCompile(`var ispostdowns = '(.+?)';`).FindStringSubmatch(res.String())[1]
sign := regexp.MustCompile(`'sign':'(.+?)',`).FindStringSubmatch(res.String())[1]
//websign := regexp.MustCompile(`'websign':'(.+?)'`).FindStringSubmatch(res.String())[1]
//websign := regexp.MustCompile(`var websign = '(.+?)'`).FindStringSubmatch(res.String())[1]
websign := ""
websignR := regexp.MustCompile(`var websign = '(.+?)'`).FindStringSubmatch(res.String())
if len(websignR) > 1 {
websign = websignR[1]
}
//websign := ""
//websignkey := regexp.MustCompile(`'websignkey':'(.+?)'`).FindStringSubmatch(res.String())[1]
websignkey := regexp.MustCompile(`var websignkey = '(.+?)';`).FindStringSubmatch(res.String())[1]
var resp LanzouLinkResp
@ -249,7 +246,38 @@ func (driver *Lanzou) GetLink(downId string, account *model.Account) (string, er
if resp.Zt == 1 {
return resp.Dom + "/file/" + resp.Url, nil
}
return "", fmt.Errorf("can't get link")
return "", fmt.Errorf("failed get link")
}
func (driver *Lanzou) GetLinkWithPassword(downId string, pwd string, html string, account *model.Account) (string, error) {
shareUrl := account.SiteUrl
u, err := url.Parse(shareUrl)
if err != nil {
return "", err
}
if html == "" {
log.Debugln(fmt.Sprintf("https://%s/%s", u.Host, downId))
res, err := base.RestyClient.R().Get(fmt.Sprintf("https://%s/%s", u.Host, downId))
if err != nil {
return "", err
}
html = res.String()
}
data := regexp.MustCompile(`data : '(.+?)'\+pwd,`).FindStringSubmatch(html)[1] + pwd
var resp LanzouLinkResp
_, err = base.RestyClient.R().SetResult(&resp).SetHeaders(map[string]string{
"Referer": fmt.Sprintf("https://%s/%s", u.Host, downId),
"Origin": "https://" + u.Host,
"content-type": "application/x-www-form-urlencoded",
}).SetBody(data).Post(fmt.Sprintf("https://%s/ajaxm.php", u.Host))
if err != nil {
return "", err
}
if resp.Zt == 1 {
return resp.Dom + "/file/" + resp.Url, nil
}
return "", fmt.Errorf("failed get link with password")
}
func init() {

13
drivers/lanzou/types.go Normal file
View File

@ -0,0 +1,13 @@
package lanzou
type DownPageResp struct {
Zt int `json:"zt"`
Info struct {
Pwd string `json:"pwd"`
Onof string `json:"onof"`
FId string `json:"f_id"`
Taoc string `json:"taoc"`
IsNewd string `json:"is_newd"`
} `json:"info"`
Text interface{} `json:"text"`
}

View File

@ -158,10 +158,6 @@ func (driver MediaTrack) Preview(path string, account *model.Account) (interface
}
func (driver MediaTrack) MakeDir(path string, account *model.Account) error {
_, err := driver.File(path, account)
if err != base.ErrPathNotFound {
return nil
}
parentFile, err := driver.File(utils.Dir(path), account)
if err != nil {
return err

View File

@ -8,6 +8,10 @@ import (
"runtime/debug"
)
func Save(driver base.Driver, account, old *model.Account) error {
return driver.Save(account, old)
}
func Path(driver base.Driver, account *model.Account, path string) (*model.File, []model.File, error) {
return driver.Path(path, account)
}
@ -29,7 +33,11 @@ func File(driver base.Driver, account *model.Account, path string) (*model.File,
func MakeDir(driver base.Driver, account *model.Account, path string, clearCache bool) error {
log.Debugf("mkdir: %s", path)
err := driver.MakeDir(path, account)
_, err := Files(driver, account, path)
if err != base.ErrPathNotFound {
return nil
}
err = driver.MakeDir(path, account)
if err == nil && clearCache {
_ = base.DeleteCache(utils.Dir(path), account)
}

View File

@ -119,9 +119,14 @@ func (driver PikPak) Link(args base.Args, account *model.Account) (*base.Link, e
if err != nil {
return nil, err
}
return &base.Link{
link := base.Link{
Url: resp.WebContentLink,
}, nil
}
if len(resp.Medias) > 0 && resp.Medias[0].Link.Url != "" {
log.Debugln("use media link")
link.Url = resp.Medias[0].Link.Url
}
return &link, nil
}
func (driver PikPak) Path(path string, account *model.Account) (*model.File, []model.File, error) {

View File

@ -144,6 +144,37 @@ type File struct {
Size string `json:"size"`
ThumbnailLink string `json:"thumbnail_link"`
WebContentLink string `json:"web_content_link"`
Medias []Media `json:"medias"`
}
type Media struct {
MediaId string `json:"media_id"`
MediaName string `json:"media_name"`
Video struct {
Height int `json:"height"`
Width int `json:"width"`
Duration int `json:"duration"`
BitRate int `json:"bit_rate"`
FrameRate int `json:"frame_rate"`
VideoCodec string `json:"video_codec"`
AudioCodec string `json:"audio_codec"`
VideoType string `json:"video_type"`
} `json:"video"`
Link struct {
Url string `json:"url"`
Token string `json:"token"`
Expire time.Time `json:"expire"`
} `json:"link"`
NeedMoreQuota bool `json:"need_more_quota"`
VipTypes []interface{} `json:"vip_types"`
RedirectLink string `json:"redirect_link"`
IconLink string `json:"icon_link"`
IsDefault bool `json:"is_default"`
Priority int `json:"priority"`
IsOrigin bool `json:"is_origin"`
ResolutionName string `json:"resolution_name"`
IsVisible bool `json:"is_visible"`
Category string `json:"category"`
}
func (driver PikPak) FormatFile(file *File) *model.File {

View File

@ -64,6 +64,12 @@ func (driver Quark) Save(account *model.Account, old *model.Account) error {
return nil
}
_, err := driver.Get("/config", nil, nil, account)
if err == nil {
account.Status = "work"
} else {
account.Status = err.Error()
}
_ = model.SaveAccount(account)
return err
}

View File

@ -8,6 +8,7 @@ import (
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/Xhofe/alist/utils/cookie"
"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
"net/http"
@ -62,7 +63,12 @@ func (driver Quark) Request(pathname string, method int, headers, query, form ma
if err != nil {
return nil, err
}
log.Debugf("%s response: %s", pathname, res.String())
__puus := cookie.GetCookie(res.Cookies(), "__puus")
if __puus != nil {
account.AccessToken = cookie.SetStr(account.AccessToken, "__puus", __puus.Value)
_ = model.SaveAccount(account)
}
//log.Debugf("%s response: %s", pathname, res.String())
if e.Status >= 400 || e.Code != 0 {
return nil, errors.New(e.Message)
}

View File

@ -1,6 +1,7 @@
package s3
import (
"bytes"
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base"
@ -9,6 +10,7 @@ import (
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/url"
"path/filepath"
"time"
@ -78,6 +80,7 @@ func (driver S3) Items() []base.Item {
Label: "placeholder filename",
Type: base.TypeString,
Description: "default empty string",
Default: defaultPlaceholderName,
},
{
Name: "bool_1",
@ -213,9 +216,20 @@ func (driver S3) Preview(path string, account *model.Account) (interface{}, erro
}
func (driver S3) MakeDir(path string, account *model.Account) error {
// not support, default as success
// not support, generate a placeholder file
_, err := driver.File(path, account)
// exist
if err != base.ErrPathNotFound {
return nil
}
return driver.Upload(&model.FileStream{
File: ioutil.NopCloser(bytes.NewReader([]byte{})),
Size: 0,
ParentPath: path,
Name: getPlaceholderName(account.Zone),
MIMEType: "application/octet-stream",
}, account)
}
func (driver S3) Move(src string, dst string, account *model.Account) error {
err := driver.Copy(src, dst, account)
@ -277,6 +291,7 @@ func (driver S3) Upload(file *model.FileStream, account *model.Account) error {
}
uploader := s3manager.NewUploader(s)
key := driver.GetKey(utils.Join(file.ParentPath, file.GetFileName()), account, false)
log.Debugln("key:", key)
input := &s3manager.UploadInput{
Bucket: &account.Bucket,
Key: &key,

View File

@ -88,7 +88,7 @@ func (driver S3) List(prefix string, account *model.Account) ([]model.File, erro
}
for _, object := range listObjectsResult.Contents {
name := utils.Base(*object.Key)
if name == account.Zone {
if name == getPlaceholderName(account.Zone) {
continue
}
file := model.File{
@ -152,7 +152,7 @@ func (driver S3) ListV2(prefix string, account *model.Account) ([]model.File, er
}
for _, object := range listObjectsResult.Contents {
name := utils.Base(*object.Key)
if name == account.Zone {
if name == getPlaceholderName(account.Zone) {
continue
}
file := model.File{

10
drivers/s3/util.go Normal file
View File

@ -0,0 +1,10 @@
package s3
var defaultPlaceholderName = ".placeholder"
func getPlaceholderName(placeholder string) string {
if placeholder == "" {
return defaultPlaceholderName
}
return placeholder
}

View File

@ -4,12 +4,12 @@ import (
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strconv"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/go-resty/resty/v2"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base"
@ -60,7 +60,13 @@ func (driver XunLeiCloud) Save(account *model.Account, old *model.Account) error
if account == nil {
return nil
}
return GetState(account).Login(account)
state := GetState(account)
if state.isTokensExpires() {
return state.Login(account)
}
account.Status = "work"
model.SaveAccount(account)
return nil
}
func (driver XunLeiCloud) File(path string, account *model.Account) (*model.File, error) {
@ -89,28 +95,41 @@ func (driver XunLeiCloud) File(path string, account *model.Account) (*model.File
}
func (driver XunLeiCloud) Files(path string, account *model.Account) ([]model.File, error) {
path = utils.ParsePath(path)
cache, err := base.GetCache(path, account)
if err == nil {
files, _ := cache.([]model.File)
return files, nil
}
file, err := driver.File(utils.ParsePath(path), account)
file, err := driver.File(path, account)
if err != nil {
return nil, err
}
files := make([]model.File, 0)
for {
var fileList FileList
u := fmt.Sprintf("https://api-pan.xunlei.com/drive/v1/files?parent_id=%s&page_token=%s&with_audit=true&filters=%s", file.Id, "", url.QueryEscape(`{"phase": {"eq": "PHASE_TYPE_COMPLETE"}, "trashed":{"eq":false}}`))
if err = GetState(account).Request("GET", u, nil, &fileList, account); err != nil {
_, err = GetState(account).Request("GET", FILE_API_URL, func(r *resty.Request) {
r.SetQueryParams(map[string]string{
"parent_id": file.Id,
"page_token": fileList.NextPageToken,
"with_audit": "true",
"filters": `{"phase": {"eq": "PHASE_TYPE_COMPLETE"}, "trashed":{"eq":false}}`,
})
r.SetResult(&fileList)
}, account)
if err != nil {
return nil, err
}
files := make([]model.File, 0, len(fileList.Files))
for _, file := range fileList.Files {
if file.Kind == FOLDER || (file.Kind == FILE && file.Audit.Status == "STATUS_OK") {
files = append(files, *driver.formatFile(&file))
}
}
if fileList.NextPageToken == "" {
break
}
}
if len(files) > 0 {
_ = base.SetCache(path, files, account)
}
@ -143,7 +162,12 @@ func (driver XunLeiCloud) Link(args base.Args, account *model.Account) (*base.Li
return nil, base.ErrNotFile
}
var lFile Files
if err = GetState(account).Request("GET", fmt.Sprintf("https://api-pan.xunlei.com/drive/v1/files/%s?&with_audit=true", file.Id), nil, &lFile, account); err != nil {
_, err = GetState(account).Request("GET", FILE_API_URL+"/{id}", func(r *resty.Request) {
r.SetPathParam("id", file.Id)
r.SetQueryParam("with_audit", "true")
r.SetResult(&lFile)
}, account)
if err != nil {
return nil, err
}
return &base.Link{
@ -184,7 +208,14 @@ func (driver XunLeiCloud) MakeDir(path string, account *model.Account) error {
if !parentFile.IsDir() {
return base.ErrNotFolder
}
return GetState(account).Request("POST", "https://api-pan.xunlei.com/drive/v1/files", &base.Json{"kind": FOLDER, "name": name, "parent_id": parentFile.Id}, nil, account)
_, err = GetState(account).Request("POST", FILE_API_URL, func(r *resty.Request) {
r.SetBody(&base.Json{
"kind": FOLDER,
"name": name,
"parent_id": parentFile.Id,
})
}, account)
return err
}
func (driver XunLeiCloud) Move(src string, dst string, account *model.Account) error {
@ -197,7 +228,14 @@ func (driver XunLeiCloud) Move(src string, dst string, account *model.Account) e
if err != nil {
return err
}
return GetState(account).Request("POST", "https://api-pan.xunlei.com/drive/v1/files:batchMove", &base.Json{"to": base.Json{"parent_id": dstDirFile.Id}, "ids": []string{srcFile.Id}}, nil, account)
_, err = GetState(account).Request("POST", FILE_API_URL+":batchMove", func(r *resty.Request) {
r.SetBody(&base.Json{
"to": base.Json{"parent_id": dstDirFile.Id},
"ids": []string{srcFile.Id},
})
}, account)
return err
}
func (driver XunLeiCloud) Copy(src string, dst string, account *model.Account) error {
@ -210,7 +248,13 @@ func (driver XunLeiCloud) Copy(src string, dst string, account *model.Account) e
if err != nil {
return err
}
return GetState(account).Request("POST", "https://api-pan.xunlei.com/drive/v1/files:batchCopy", &base.Json{"to": base.Json{"parent_id": dstDirFile.Id}, "ids": []string{srcFile.Id}}, nil, account)
_, err = GetState(account).Request("POST", FILE_API_URL+":batchCopy", func(r *resty.Request) {
r.SetBody(&base.Json{
"to": base.Json{"parent_id": dstDirFile.Id},
"ids": []string{srcFile.Id},
})
}, account)
return err
}
func (driver XunLeiCloud) Delete(path string, account *model.Account) error {
@ -218,7 +262,11 @@ func (driver XunLeiCloud) Delete(path string, account *model.Account) error {
if err != nil {
return err
}
return GetState(account).Request("PATCH", fmt.Sprintf("https://api-pan.xunlei.com/drive/v1/files/%s/trash", srcFile.Id), &base.Json{}, nil, account)
_, err = GetState(account).Request("PATCH", FILE_API_URL+"/{id}/trash", func(r *resty.Request) {
r.SetPathParam("id", srcFile.Id)
r.SetBody(&base.Json{})
}, account)
return err
}
func (driver XunLeiCloud) Upload(file *model.FileStream, account *model.Account) error {
@ -236,7 +284,6 @@ func (driver XunLeiCloud) Upload(file *model.FileStream, account *model.Account)
return err
}
defer tempFile.Close()
defer os.Remove(tempFile.Name())
gcid, err := getGcid(io.TeeReader(file, tempFile), int64(file.Size))
@ -244,21 +291,27 @@ func (driver XunLeiCloud) Upload(file *model.FileStream, account *model.Account)
return err
}
var rep UploadTaskResponse
err = GetState(account).Request("POST", "https://api-pan.xunlei.com/drive/v1/files", &base.Json{
tempFile.Close()
var resp UploadTaskResponse
_, err = GetState(account).Request("POST", FILE_API_URL, func(r *resty.Request) {
r.SetBody(&base.Json{
"kind": FILE,
"parent_id": parentFile.Id,
"name": file.Name,
"size": fmt.Sprint(file.Size),
"hash": gcid,
"upload_type": UPLOAD_TYPE_RESUMABLE,
}, &rep, account)
})
r.SetResult(&resp)
}, account)
if err != nil {
return err
}
param := rep.Resumable.Params
client, err := oss.New(param.Endpoint, param.AccessKeyID, param.AccessKeySecret, oss.SecurityToken(param.SecurityToken), oss.EnableMD5(true), oss.HTTPClient(xunleiClient.GetClient()))
param := resp.Resumable.Params
if resp.UploadType == UPLOAD_TYPE_RESUMABLE {
client, err := oss.New(param.Endpoint, param.AccessKeyID, param.AccessKeySecret, oss.SecurityToken(param.SecurityToken), oss.EnableMD5(true))
if err != nil {
return err
}
@ -266,7 +319,9 @@ func (driver XunLeiCloud) Upload(file *model.FileStream, account *model.Account)
if err != nil {
return err
}
return bucket.UploadFile(param.Key, tempFile.Name(), 4*1024*1024, oss.Routines(3), oss.Checkpoint(true, ""), oss.Expires(param.Expiration))
return bucket.UploadFile(param.Key, tempFile.Name(), 1<<22, oss.Routines(3), oss.Checkpoint(true, ""), oss.Expires(param.Expiration))
}
return nil
}
func (driver XunLeiCloud) Rename(src string, dst string, account *model.Account) error {
@ -275,8 +330,11 @@ func (driver XunLeiCloud) Rename(src string, dst string, account *model.Account)
if err != nil {
return err
}
return GetState(account).Request("PATCH", fmt.Sprintf("https://api-pan.xunlei.com/drive/v1/files/%s", srcFile.Id), &base.Json{"name": dstName}, nil, account)
_, err = GetState(account).Request("PATCH", FILE_API_URL+"/{id}", func(r *resty.Request) {
r.SetPathParam("id", srcFile.Id)
r.SetBody(&base.Json{"name": dstName})
}, account)
return err
}
var _ base.Driver = (*XunLeiCloud)(nil)

View File

@ -37,6 +37,12 @@ var Algorithms = []string{
"T78dnANexYRbiZy",
}
const (
API_URL = "https://api-pan.xunlei.com/drive/v1"
FILE_API_URL = API_URL + "/files"
XLUSER_API_URL = "https://xluser-ssl.xunlei.com/v1"
)
const (
FOLDER = "drive#folder"
FILE = "drive#file"

View File

@ -2,6 +2,7 @@ package xunlei
import (
"fmt"
"net/http"
"sync"
"time"
@ -12,7 +13,7 @@ import (
log "github.com/sirupsen/logrus"
)
var xunleiClient = resty.New().SetTimeout(120 * time.Second)
var xunleiClient = resty.New().SetHeaders(map[string]string{"Accept": "application/json;charset=UTF-8"}).SetTimeout(base.DefaultTimeout)
// 一个账户只允许登陆一次
var userStateCache = struct {
@ -102,16 +103,16 @@ func (s *State) newCaptchaToken(action string, meta map[string]string, account *
var e Erron
var resp CaptchaTokenResponse
_, err := xunleiClient.R().
SetHeader("X-Device-Id", driverID).
SetBody(&creq).
SetError(&e).
SetResult(&resp).
Post("https://xluser-ssl.xunlei.com/v1/shield/captcha/init?client_id=" + CLIENT_ID)
SetHeader("X-Device-Id", driverID).
SetQueryParam("client_id", CLIENT_ID).
Post(XLUSER_API_URL + "/shield/captcha/init")
if err != nil {
return "", err
}
if e.ErrorCode != 0 {
log.Debugf("%+v\n %+v", e, account)
return "", fmt.Errorf("%s : %s", e.Error, e.ErrorDescription)
}
if resp.Url != "" {
@ -120,7 +121,6 @@ func (s *State) newCaptchaToken(action string, meta map[string]string, account *
s.captchaTokenExpiresTime = (ctime + resp.ExpiresIn*1000) - 30000
s.captchaToken = resp.CaptchaToken
log.Debugf("%+v\n %+v", s.captchaToken, account)
return s.captchaToken, nil
}
@ -136,7 +136,7 @@ func (s *State) refreshToken_(account *model.Account) error {
"client_secret": CLIENT_SECRET,
}).
SetHeader("X-Device-Id", utils.GetMD5Encode(account.Username)).SetQueryParam("client_id", CLIENT_ID).
Post("https://xluser-ssl.xunlei.com/v1/auth/token")
Post(XLUSER_API_URL + "/auth/token")
if err != nil {
return err
}
@ -152,7 +152,6 @@ func (s *State) refreshToken_(account *model.Account) error {
s.userID = resp.UserID
return nil
default:
log.Debugf("%+v\n %+v", e, account)
return fmt.Errorf("%s : %s", e.Error, e.ErrorDescription)
}
}
@ -160,7 +159,7 @@ func (s *State) refreshToken_(account *model.Account) error {
func (s *State) login(account *model.Account) error {
s.init()
ctime := time.Now().UnixMilli()
url := "https://xluser-ssl.xunlei.com/v1/auth/signin"
url := XLUSER_API_URL + "/auth/signin"
captchaToken, err := s.newCaptchaToken(getAction("POST", url), map[string]string{"username": account.Username}, account)
if err != nil {
return err
@ -190,7 +189,6 @@ func (s *State) login(account *model.Account) error {
defer model.SaveAccount(account)
if e.ErrorCode != 0 {
account.Status = e.Error
log.Debugf("%+v\n %+v", e, account)
return fmt.Errorf("%s : %s", e.Error, e.ErrorDescription)
}
account.Status = "work"
@ -199,67 +197,68 @@ func (s *State) login(account *model.Account) error {
s.accessToken = resp.AccessToken
s.refreshToken = resp.RefreshToken
s.userID = resp.UserID
log.Debugf("%+v\n %+v", resp, account)
return nil
}
func (s *State) Request(method string, url string, body interface{}, resp interface{}, account *model.Account) error {
func (s *State) Request(method string, url string, callback func(*resty.Request), account *model.Account) (*resty.Response, error) {
s.Lock()
token, err := s.getToken(account)
if err != nil {
return err
return nil, err
}
captchaToken, err := s.getCaptchaToken(getAction(method, url), account)
if err != nil {
return err
return nil, err
}
s.Unlock()
var e Erron
req := xunleiClient.R().
SetError(&e).
SetHeader("X-Device-Id", utils.GetMD5Encode(account.Username)).
SetHeader("Authorization", token).
SetHeader("X-Captcha-Token", captchaToken).
SetHeaders(map[string]string{
"X-Device-Id": utils.GetMD5Encode(account.Username),
"Authorization": token,
"X-Captcha-Token": captchaToken,
}).
SetQueryParam("client_id", CLIENT_ID)
if body != nil {
req.SetBody(body)
}
if resp != nil {
req.SetResult(resp)
}
callback(req)
s.Unlock()
var res *resty.Response
switch method {
case "GET":
_, err = req.Get(url)
res, err = req.Get(url)
case "POST":
_, err = req.Post(url)
res, err = req.Post(url)
case "DELETE":
_, err = req.Delete(url)
res, err = req.Delete(url)
case "PATCH":
_, err = req.Patch(url)
res, err = req.Patch(url)
case "PUT":
_, err = req.Put(url)
res, err = req.Put(url)
default:
return base.ErrNotSupport
return nil, base.ErrNotSupport
}
if err != nil {
return err
return nil, err
}
log.Debug(res.String())
var e Erron
utils.Json.Unmarshal(res.Body(), &e)
switch e.ErrorCode {
case 0:
return nil
case 9:
s.newCaptchaToken(getAction(method, url), nil, account)
fallthrough
case 4122, 4121:
return s.Request(method, url, body, resp, account)
return s.Request(method, url, callback, account)
case 0:
if res.StatusCode() == http.StatusOK {
return res, nil
}
return nil, fmt.Errorf(res.String())
default:
log.Debugf("%+v\n %+v", e, account)
return fmt.Errorf("%s : %s", e.Error, e.ErrorDescription)
return nil, fmt.Errorf("%s : %s", e.Error, e.ErrorDescription)
}
}

3
go.mod
View File

@ -4,6 +4,7 @@ go 1.17
require (
github.com/aws/aws-sdk-go v1.27.0
github.com/caarlos0/env/v6 v6.9.1
github.com/eko/gocache/v2 v2.1.0
github.com/gin-contrib/cors v1.3.1
github.com/gin-gonic/gin v1.7.4
@ -25,6 +26,8 @@ require (
require (
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
github.com/fatih/color v1.13.0
github.com/mattn/go-colorable v0.1.9 // indirect
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
)

6
go.sum
View File

@ -43,6 +43,8 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b h1:L/QXpzIa3pOvUGt1D1lA5KjYhPBAN/3iWdP7xeFS9F0=
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA=
github.com/caarlos0/env/v6 v6.9.1 h1:zOkkjM0F6ltnQ5eBX6IPI41UP/KDGEK7rRPwGCNos8k=
github.com/caarlos0/env/v6 v6.9.1/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc=
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
@ -94,6 +96,8 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/evanphx/json-patch v4.2.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4=
@ -329,6 +333,8 @@ github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=

View File

@ -2,7 +2,9 @@ package model
import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/utils"
log "github.com/sirupsen/logrus"
"sort"
"strings"
"sync"
"time"
@ -97,6 +99,7 @@ func RegisterAccount(account Account) {
accountsMap[account.Name] = account
}
// GetAccount 根据名称获取账号(不包含负载均衡账号) 用于定时任务更新账号
func GetAccount(name string) (Account, bool) {
if len(accountsMap) == 1 {
for _, v := range accountsMap {
@ -107,25 +110,29 @@ func GetAccount(name string) (Account, bool) {
return account, ok
}
func GetAccountsByName(name string) []Account {
accounts := make([]Account, 0)
if AccountsCount() == 1 {
account, _ := GetAccount("")
accounts = append(accounts, account)
return accounts
}
for _, v := range accountsMap {
if v.Name == name || strings.HasPrefix(v.Name, name+balance) {
accounts = append(accounts, v)
}
}
return accounts
}
// GetAccountsByName 根据名称获取账号(包含负载均衡账号)
//func GetAccountsByName(name string) []Account {
// accounts := make([]Account, 0)
// if AccountsCount() == 1 {
// for _, v := range accountsMap {
// accounts = append(accounts, v)
// }
// return accounts
// }
// for _, v := range accountsMap {
// if v.Name == name || strings.HasPrefix(v.Name, name+balance) {
// accounts = append(accounts, v)
// }
// }
// return accounts
//}
var balanceMap sync.Map
// GetBalancedAccount 根据名称获取账号,负载均衡之后的
func GetBalancedAccount(name string) (Account, bool) {
accounts := GetAccountsByName(name)
accounts := GetAccountsByPath(name)
log.Debugf("accounts: %+v", accounts)
accountNum := len(accounts)
switch accountNum {
case 0:
@ -147,6 +154,7 @@ func GetBalancedAccount(name string) (Account, bool) {
}
}
// GetAccountById 根据id获取账号用于更新账号
func GetAccountById(id uint) (*Account, error) {
var account Account
account.ID = id
@ -156,31 +164,117 @@ func GetAccountById(id uint) (*Account, error) {
return &account, nil
}
func GetAccountFiles() ([]File, error) {
files := make([]File, 0)
// GetAccountFiles 获取账号虚拟文件(去除负载均衡)
//func GetAccountFiles() ([]File, error) {
// files := make([]File, 0)
// var accounts []Account
// if err := conf.DB.Order(columnName("index")).Find(&accounts).Error; err != nil {
// return nil, err
// }
// for _, v := range accounts {
// if strings.Contains(v.Name, balance) {
// continue
// }
// files = append(files, File{
// Name: v.Name,
// Size: 0,
// Driver: v.Type,
// Type: conf.FOLDER,
// UpdatedAt: v.UpdatedAt,
// })
// }
// return files, nil
//}
// GetAccounts 获取所有账号
func GetAccounts() ([]Account, error) {
var accounts []Account
if err := conf.DB.Order("`index`").Find(&accounts).Error; err != nil {
if err := conf.DB.Order(columnName("index")).Find(&accounts).Error; err != nil {
return nil, err
}
return accounts, nil
}
// GetAccountsByPath 根据路径获取账号,最长匹配,未负载均衡
// 如有账号: /a/b,/a/c,/a/d/e,/a/d/e.balance
// GetAccountsByPath(/a/d/e/f) => /a/d/e,/a/d/e.balance
func GetAccountsByPath(path string) []Account {
accounts := make([]Account, 0)
curSlashCount := 0
for _, v := range accountsMap {
name := utils.ParsePath(v.Name)
bIndex := strings.LastIndex(name, balance)
if bIndex != -1 {
name = name[:bIndex]
}
if name == "/" {
name = ""
}
// 不是这个账号
if path != name && !strings.HasPrefix(path, name+"/") {
continue
}
slashCount := strings.Count(name, "/")
// 不是最长匹配
if slashCount < curSlashCount {
continue
}
if slashCount > curSlashCount {
accounts = accounts[:0]
curSlashCount = slashCount
}
accounts = append(accounts, v)
}
sort.Slice(accounts, func(i, j int) bool {
return accounts[i].Name < accounts[j].Name
})
return accounts
}
// GetAccountFilesByPath 根据路径获取账号虚拟文件
// 如有账号: /a/b,/a/c,/a/d/e,/a/b.balance1,/av
// GetAccountFilesByPath(/a) => b,c,d
func GetAccountFilesByPath(prefix string) []File {
files := make([]File, 0)
accounts := make([]Account, AccountsCount())
i := 0
for _, v := range accountsMap {
accounts[i] = v
i += 1
}
sort.Slice(accounts, func(i, j int) bool {
if accounts[i].Index == accounts[j].Index {
return accounts[i].Name < accounts[j].Name
}
return accounts[i].Index < accounts[j].Index
})
prefix = utils.ParsePath(prefix)
set := make(map[string]interface{})
for _, v := range accounts {
// 负载均衡账号
if strings.Contains(v.Name, balance) {
continue
}
full := utils.ParsePath(v.Name)
if len(full) <= len(prefix) {
continue
}
// 不是以prefix为前缀
if !strings.HasPrefix(full, prefix+"/") && prefix != "/" {
continue
}
name := strings.Split(strings.TrimPrefix(strings.TrimPrefix(full, prefix), "/"), "/")[0]
if _, ok := set[name]; ok {
continue
}
files = append(files, File{
Name: v.Name,
Name: name,
Size: 0,
Driver: v.Type,
Type: conf.FOLDER,
UpdatedAt: v.UpdatedAt,
})
set[name] = nil
}
return files, nil
}
func GetAccounts() ([]Account, error) {
var accounts []Account
if err := conf.DB.Order("`index`").Find(&accounts).Error; err != nil {
return nil, err
}
return accounts, nil
return files
}

View File

@ -11,6 +11,7 @@ type Meta struct {
Password string `json:"password"`
Hide string `json:"hide"`
Upload bool `json:"upload"`
OnlyShows string `json:"only_shows"`
}
func GetMetaByPath(path string) (*Meta, error) {

View File

@ -50,7 +50,7 @@ func SaveSetting(item SettingItem) error {
func GetSettingsPublic() ([]SettingItem, error) {
var items []SettingItem
if err := conf.DB.Where("`access` <> ?", 1).Find(&items).Error; err != nil {
if err := conf.DB.Where(fmt.Sprintf("%s <> ?", columnName("access")), 1).Find(&items).Error; err != nil {
return nil, err
}
return items, nil
@ -58,7 +58,7 @@ func GetSettingsPublic() ([]SettingItem, error) {
func GetSettingsByGroup(group int) ([]SettingItem, error) {
var items []SettingItem
if err := conf.DB.Where("`group` = ?", group).Find(&items).Error; err != nil {
if err := conf.DB.Where(fmt.Sprintf("%s = ?", columnName("group")), group).Find(&items).Error; err != nil {
return nil, err
}
items = append([]SettingItem{Version}, items...)
@ -82,7 +82,7 @@ func DeleteSetting(key string) error {
func GetSettingByKey(key string) (*SettingItem, error) {
var items SettingItem
if err := conf.DB.Where("`key` = ?", key).First(&items).Error; err != nil {
if err := conf.DB.Where(fmt.Sprintf("%s = ?", columnName("key")), key).First(&items).Error; err != nil {
return nil, err
}
return &items, nil
@ -93,6 +93,14 @@ func LoadSettings() {
if err == nil {
conf.TextTypes = strings.Split(textTypes.Value, ",")
}
audioTypes, err := GetSettingByKey("audio types")
if err == nil {
conf.AudioTypes = strings.Split(audioTypes.Value, ",")
}
videoTypes, err := GetSettingByKey("video types")
if err == nil {
conf.VideoTypes = strings.Split(videoTypes.Value, ",")
}
dProxyTypes, err := GetSettingByKey("d_proxy types")
if err == nil {
conf.DProxyTypes = strings.Split(dProxyTypes.Value, ",")

13
model/util.go Normal file
View File

@ -0,0 +1,13 @@
package model
import (
"fmt"
"github.com/Xhofe/alist/conf"
)
func columnName(name string) string {
if conf.Conf.Database.Type == "postgres" {
return fmt.Sprintf(`"%s"`, name)
}
return fmt.Sprintf("`%s`", name)
}

View File

@ -1,7 +1,6 @@
package common
import (
"errors"
"fmt"
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/model"
@ -25,30 +24,24 @@ type PathReq struct {
}
func ParsePath(rawPath string) (*model.Account, string, base.Driver, error) {
var path, name string
switch model.AccountsCount() {
case 0:
return nil, "", nil, fmt.Errorf("no accounts,please add one first")
case 1:
path = rawPath
break
default:
if path == "/" {
return nil, "", nil, errors.New("can't operate root of multiple accounts")
}
paths := strings.Split(rawPath, "/")
path = "/" + strings.Join(paths[2:], "/")
name = paths[1]
}
account, ok := model.GetBalancedAccount(name)
rawPath = utils.ParsePath(rawPath)
account, ok := model.GetBalancedAccount(rawPath)
if !ok {
return nil, "", nil, fmt.Errorf("no [%s] account", name)
return nil, "", nil, fmt.Errorf("path not found")
}
driver, ok := base.GetDriver(account.Type)
if !ok {
return nil, "", nil, fmt.Errorf("no [%s] driver", account.Type)
}
return &account, path, driver, nil
name := utils.ParsePath(account.Name)
bIndex := strings.LastIndex(name, ".balance")
if bIndex != -1 {
name = name[:bIndex]
}
//if name == "/" {
// name = ""
//}
return &account, utils.ParsePath(strings.TrimPrefix(rawPath, name)), driver, nil
}
func ErrorResp(c *gin.Context, err error, code int) {
@ -88,8 +81,10 @@ func SuccessResp(c *gin.Context, data ...interface{}) {
}
func Hide(meta *model.Meta, files []model.File) []model.File {
//meta, _ := model.GetMetaByPath(path)
if meta != nil && meta.Hide != "" {
if meta == nil {
return files
}
if meta.Hide != "" {
tmpFiles := make([]model.File, 0)
hideFiles := strings.Split(meta.Hide, ",")
for _, item := range files {
@ -99,5 +94,15 @@ func Hide(meta *model.Meta, files []model.File) []model.File {
}
files = tmpFiles
}
if meta.OnlyShows != "" {
tmpFiles := make([]model.File, 0)
showFiles := strings.Split(meta.OnlyShows, ",")
for _, item := range files {
if utils.IsContain(showFiles, item.Name) {
tmpFiles = append(tmpFiles, item)
}
}
files = tmpFiles
}
return files
}

33
server/common/files.go Normal file
View File

@ -0,0 +1,33 @@
package common
import (
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/model"
log "github.com/sirupsen/logrus"
)
func Path(rawPath string) (*model.File, []model.File, *model.Account, base.Driver, string, error) {
account, path, driver, err := ParsePath(rawPath)
if err != nil {
if err.Error() == "path not found" {
accountFiles := model.GetAccountFilesByPath(rawPath)
if len(accountFiles) != 0 {
return nil, accountFiles, nil, nil, path, nil
}
}
return nil, nil, nil, nil, "", err
}
log.Debugln("use account: ", account.Name)
file, files, err := operate.Path(driver, account, path)
if err != nil {
return nil, nil, nil, nil, "", err
}
if file != nil {
return file, nil, account, driver, path, nil
} else {
accountFiles := model.GetAccountFilesByPath(rawPath)
files = append(files, accountFiles...)
return nil, files, account, driver, path, nil
}
}

View File

@ -3,6 +3,7 @@ package controllers
import (
"fmt"
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/gin-gonic/gin"
@ -37,7 +38,8 @@ func CreateAccount(c *gin.Context) {
common.ErrorResp(c, err, 500)
} else {
log.Debugf("new account: %+v", req)
err = driver.Save(&req, nil)
//err = driver.Save(&req, nil)
err = operate.Save(driver, &req, nil)
if err != nil {
common.ErrorResp(c, err, 500)
return
@ -71,7 +73,8 @@ func SaveAccount(c *gin.Context) {
common.ErrorResp(c, err, 500)
} else {
log.Debugf("save account: %+v", req)
err = driver.Save(&req, old)
//err = driver.Save(&req, old)
err = operate.Save(driver, &req, nil)
if err != nil {
common.ErrorResp(c, err, 500)
return
@ -93,7 +96,8 @@ func DeleteAccount(c *gin.Context) {
} else {
driver, ok := base.GetDriver(account.Type)
if ok {
_ = driver.Save(nil, account)
//_ = driver.Save(nil, account)
_ = operate.Save(driver, nil, account)
} else {
log.Errorf("no driver: %s", account.Type)
}

View File

@ -7,7 +7,6 @@ import (
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"path"
)
func Down(c *gin.Context) {
@ -19,7 +18,7 @@ func Down(c *gin.Context) {
common.ErrorResp(c, err, 500)
return
}
if driver.Config().OnlyProxy || account.Proxy || utils.IsContain(conf.DProxyTypes, path.Ext(rawPath)) {
if driver.Config().OnlyProxy || account.Proxy || utils.IsContain(conf.DProxyTypes, utils.Ext(rawPath)) {
Proxy(c)
return
}

View File

@ -1,7 +1,6 @@
package file
import (
"github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/gin-gonic/gin"
@ -18,33 +17,20 @@ func Folder(c *gin.Context) {
return
}
var files = make([]model.File, 0)
var err error
if model.AccountsCount() > 1 && (req.Path == "/" || req.Path == "") {
files, err = model.GetAccountFiles()
_, rawFiles, _, _, _, err := common.Path(req.Path)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
} else {
account, path, driver, err := common.ParsePath(req.Path)
if err != nil {
common.ErrorResp(c, err, 500)
if rawFiles == nil {
common.ErrorStrResp(c, "not a folder", 400)
return
}
file, rawFiles, err := operate.Path(driver, account, path)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if file != nil {
common.ErrorStrResp(c, "Not folder", 400)
}
for _, file := range rawFiles {
if file.IsDir() {
files = append(files, file)
}
}
}
c.JSON(200, common.Resp{
Code: 200,
Message: "success",

View File

@ -18,6 +18,10 @@ func RefreshFolder(c *gin.Context) {
}
account, path_, _, err := common.ParsePath(req.Path)
if err != nil {
if err.Error() == "path not found" && req.Path == "/" {
common.SuccessResp(c)
return
}
common.ErrorResp(c, err, 500)
return
}

View File

@ -5,12 +5,12 @@ import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"strings"
)
func Pagination(files []model.File, req *common.PathReq) (int, []model.File) {
@ -78,39 +78,12 @@ func Path(c *gin.Context) {
if meta != nil && meta.Upload {
upload = true
}
if model.AccountsCount() > 1 && (req.Path == "/" || req.Path == "") {
files, err := model.GetAccountFiles()
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if !ok {
files = common.Hide(meta, files)
}
c.JSON(200, common.Resp{
Code: 200,
Message: "success",
Data: PathResp{
Type: "folder",
Meta: Meta{
Driver: "root",
},
Files: files,
},
})
return
}
err := CheckPagination(&req)
if err != nil {
common.ErrorResp(c, err, 400)
return
}
account, path, driver, err := common.ParsePath(req.Path)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
file, files, err := operate.Path(driver, account, path)
file, files, account, driver, path, err := common.Path(req.Path)
if err != nil {
common.ErrorResp(c, err, 500)
return
@ -119,7 +92,7 @@ func Path(c *gin.Context) {
// 对于中转文件或只能中转,将链接修改为中转链接
if driver.Config().OnlyProxy || account.Proxy {
if account.DownProxyUrl != "" {
file.Url = fmt.Sprintf("%s%s?sign=%s", account.DownProxyUrl, req.Path, utils.SignWithToken(file.Name, conf.Token))
file.Url = fmt.Sprintf("%s%s?sign=%s", strings.Split(account.DownProxyUrl, "\n")[0], req.Path, utils.SignWithToken(file.Name, conf.Token))
} else {
file.Url = fmt.Sprintf("//%s/p%s?sign=%s", c.Request.Host, req.Path, utils.SignWithToken(file.Name, conf.Token))
}
@ -146,10 +119,14 @@ func Path(c *gin.Context) {
if !ok {
files = common.Hide(meta, files)
}
driverName := "root"
if driver != nil {
if driver.Config().LocalSort {
model.SortFiles(files, account)
}
model.ExtractFolder(files, account)
driverName = driver.Config().Name
}
total, files := Pagination(files, &req)
c.JSON(200, common.Resp{
Code: 200,
@ -157,7 +134,7 @@ func Path(c *gin.Context) {
Data: PathResp{
Type: "folder",
Meta: Meta{
Driver: driver.Config().Name,
Driver: driverName,
Upload: upload,
Total: total,
},

View File

@ -11,6 +11,7 @@ import (
"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
"path/filepath"
"strings"
)
func Proxy(c *gin.Context) {
@ -29,7 +30,7 @@ func Proxy(c *gin.Context) {
// 4. 开启webdav中转需要验证sign
if !account.Proxy && !driver.Config().OnlyProxy &&
utils.GetFileType(filepath.Ext(rawPath)) != conf.TEXT &&
!utils.IsContain(conf.DProxyTypes, filepath.Ext(rawPath)) {
!utils.IsContain(conf.DProxyTypes, utils.Ext(rawPath)) {
// 只开启了webdav中转验证sign
ok := false
if account.WebdavProxy {
@ -43,7 +44,7 @@ func Proxy(c *gin.Context) {
// 中转时有中转机器使用中转机器,若携带标志位则表明不能再走中转机器了
if account.DownProxyUrl != "" && c.Query("d") != "1" {
name := utils.Base(rawPath)
link := fmt.Sprintf("%s%s?sign=%s", account.DownProxyUrl, rawPath, utils.SignWithToken(name, conf.Token))
link := fmt.Sprintf("%s%s?sign=%s", strings.Split(account.DownProxyUrl, "\n")[0], rawPath, utils.SignWithToken(name, conf.Token))
c.Redirect(302, link)
return
}

View File

@ -31,18 +31,21 @@ func (fs *FileSystem) File(rawPath string) (*model.File, error) {
if f, ok := upFileMap[rawPath]; ok {
return f, nil
}
if model.AccountsCount() > 1 && rawPath == "/" {
account, path_, driver, err := common.ParsePath(rawPath)
log.Debugln(account, path_, driver, err)
if err != nil {
if err.Error() == "path not found" {
accountFiles := model.GetAccountFilesByPath(rawPath)
if len(accountFiles) != 0 {
now := time.Now()
return &model.File{
Name: "root",
Size: 0,
Type: conf.FOLDER,
Driver: "root",
UpdatedAt: &now,
}, nil
}
account, path_, driver, err := common.ParsePath(rawPath)
if err != nil {
}
return nil, err
}
return operate.File(driver, account, path_)
@ -50,17 +53,18 @@ func (fs *FileSystem) File(rawPath string) (*model.File, error) {
func (fs *FileSystem) Files(ctx context.Context, rawPath string) ([]model.File, error) {
rawPath = utils.ParsePath(rawPath)
var files []model.File
var err error
if model.AccountsCount() > 1 && rawPath == "/" {
files, err = model.GetAccountFiles()
} else {
account, path_, driver, err := common.ParsePath(rawPath)
if err != nil {
return nil, err
}
files, err = operate.Files(driver, account, path_)
}
//var files []model.File
//var err error
//if model.AccountsCount() > 1 && rawPath == "/" {
// files, err = model.GetAccountFilesByPath("/")
//} else {
// account, path_, driver, err := common.ParsePath(rawPath)
// if err != nil {
// return nil, err
// }
// files, err = operate.Files(driver, account, path_)
//}
_, files, _, _, _, err := common.Path(rawPath)
if err != nil {
return nil, err
}
@ -125,10 +129,10 @@ func (fs *FileSystem) Link(w http.ResponseWriter, r *http.Request, rawPath strin
}
if driver.Config().OnlyProxy || account.WebdavProxy {
link = fmt.Sprintf("%s://%s/p%s", protocol, r.Host, rawPath)
if conf.GetBool("check down link") {
//if conf.GetBool("check down link") {
sign := utils.SignWithToken(utils.Base(rawPath), conf.Token)
link += "?sign=" + sign
}
//}
} else {
link_, err := driver.Link(base.Args{Path: path_, IP: ClientIP(r)}, account)
if err != nil {
@ -156,29 +160,27 @@ func (fs *FileSystem) CreateDirectory(ctx context.Context, rawPath string) error
return operate.MakeDir(driver, account, path_, true)
}
func (fs *FileSystem) Upload(ctx context.Context, r *http.Request, rawPath string) error {
func (fs *FileSystem) Upload(ctx context.Context, r *http.Request, rawPath string) (FileInfo, error) {
rawPath = utils.ParsePath(rawPath)
if model.AccountsCount() > 1 && rawPath == "/" {
return ErrNotImplemented
return nil, ErrNotImplemented
}
account, path_, driver, err := common.ParsePath(rawPath)
if err != nil {
return err
return nil, err
}
//fileSize, err := strconv.ParseUint(r.Header.Get("Content-Length"), 10, 64)
fileSize := uint64(r.ContentLength)
//if err != nil {
// return err
//}
filePath, fileName := filepath.Split(path_)
now := time.Now()
if fileSize == 0 {
upFileMap[rawPath] = &model.File{
fi := &model.File{
Name: fileName,
Size: 0,
UpdatedAt: &now,
}
return nil
if fileSize == 0 {
// 如果文件大小为0默认成功
upFileMap[rawPath] = fi
return fi, nil
} else {
delete(upFileMap, rawPath)
}
@ -189,7 +191,7 @@ func (fs *FileSystem) Upload(ctx context.Context, r *http.Request, rawPath strin
Name: fileName,
ParentPath: filePath,
}
return operate.Upload(driver, account, &fileData, true)
return fi, operate.Upload(driver, account, &fileData, true)
}
func (fs *FileSystem) Delete(rawPath string) error {

View File

@ -94,7 +94,6 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, fs *FileSyst
}
}
// OK
func (h *Handler) lock(now time.Time, root string, fs *FileSystem) (token string, status int, err error) {
token, err = h.LockSystem.Create(now, LockDetails{
Root: root,
@ -110,7 +109,6 @@ func (h *Handler) lock(now time.Time, root string, fs *FileSystem) (token string
return token, 0, nil
}
// ok
func (h *Handler) confirmLocks(r *http.Request, src, dst string, fs *FileSystem) (release func(), status int, err error) {
hdr := r.Header.Get("If")
if hdr == "" {
@ -189,7 +187,6 @@ func (h *Handler) confirmLocks(r *http.Request, src, dst string, fs *FileSystem)
return nil, http.StatusPreconditionFailed, ErrLocked
}
//OK
func (h *Handler) handleOptions(w http.ResponseWriter, r *http.Request, fs *FileSystem) (status int, err error) {
reqPath, status, err := h.stripPrefix(r.URL.Path)
if err != nil {
@ -213,7 +210,6 @@ func (h *Handler) handleOptions(w http.ResponseWriter, r *http.Request, fs *File
return 0, nil
}
// OK
func (h *Handler) handleGetHeadPost(w http.ResponseWriter, r *http.Request, fs *FileSystem) (status int, err error) {
reqPath, status, err := h.stripPrefix(r.URL.Path)
@ -248,7 +244,6 @@ func (h *Handler) handleGetHeadPost(w http.ResponseWriter, r *http.Request, fs *
return 0, nil
}
// OK
func (h *Handler) handleDelete(w http.ResponseWriter, r *http.Request, fs *FileSystem) (status int, err error) {
reqPath, status, err := h.stripPrefix(r.URL.Path)
@ -267,7 +262,6 @@ func (h *Handler) handleDelete(w http.ResponseWriter, r *http.Request, fs *FileS
return http.StatusNoContent, nil
}
// OK
func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request, fs *FileSystem) (status int, err error) {
reqPath, status, err := h.stripPrefix(r.URL.Path)
if err != nil {
@ -283,12 +277,13 @@ func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request, fs *FileSyst
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err = fs.Upload(ctx, r, reqPath)
fi, err := fs.Upload(ctx, r, reqPath)
if err != nil {
return http.StatusMethodNotAllowed, err
}
_, fi := isPathExist(ctx, fs, reqPath)
//_, fi := isPathExist(ctx, fs, reqPath)
// 为防止有些网盘上传有延时,这里认为上传没有报错就已经成功了,不再去判断是否存在
etag, err := findETag(ctx, fs, h.LockSystem, reqPath, fi)
if err != nil {
return http.StatusInternalServerError, err
@ -297,7 +292,6 @@ func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request, fs *FileSyst
return http.StatusCreated, nil
}
// OK
func (h *Handler) handleMkcol(w http.ResponseWriter, r *http.Request, fs *FileSystem) (status int, err error) {
reqPath, status, err := h.stripPrefix(r.URL.Path)
if err != nil {
@ -325,7 +319,6 @@ func (h *Handler) handleMkcol(w http.ResponseWriter, r *http.Request, fs *FileSy
return http.StatusCreated, nil
}
// OK
func (h *Handler) handleCopyMove(w http.ResponseWriter, r *http.Request, fs *FileSystem) (status int, err error) {
hdr := r.Header.Get("Destination")
@ -410,7 +403,6 @@ func (h *Handler) handleCopyMove(w http.ResponseWriter, r *http.Request, fs *Fil
return moveFiles(ctx, fs, src, dst, r.Header.Get("Overwrite") == "T")
}
// OK
func (h *Handler) handleLock(w http.ResponseWriter, r *http.Request, fs *FileSystem) (retStatus int, retErr error) {
duration, err := parseTimeout(r.Header.Get("Timeout"))
@ -506,7 +498,6 @@ func (h *Handler) handleLock(w http.ResponseWriter, r *http.Request, fs *FileSys
return 0, nil
}
// OK
func (h *Handler) handleUnlock(w http.ResponseWriter, r *http.Request, fs *FileSystem) (status int, err error) {
// http://www.webdav.org/specs/rfc4918.html#HEADER_Lock-Token says that the
@ -531,7 +522,6 @@ func (h *Handler) handleUnlock(w http.ResponseWriter, r *http.Request, fs *FileS
}
}
// OK
func (h *Handler) handlePropfind(w http.ResponseWriter, r *http.Request, fs *FileSystem) (status int, err error) {
reqPath, status, err := h.stripPrefix(r.URL.Path)
if err != nil {

28
utils/check.go Normal file
View File

@ -0,0 +1,28 @@
package utils
import (
"net"
)
func IsLocalIPAddr(ip string) bool {
return IsLocalIP(net.ParseIP(ip))
}
func IsLocalIP(ip net.IP) bool {
if ip == nil {
return false
}
if ip.IsLoopback() {
return true
}
ip4 := ip.To4()
if ip4 == nil {
return false
}
return ip4[0] == 10 || // 10.0.0.0/8
(ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31) || // 172.16.0.0/12
(ip4[0] == 169 && ip4[1] == 254) || // 169.254.0.0/16
(ip4[0] == 192 && ip4[1] == 168) // 192.168.0.0/16
}

59
utils/cookie/cookie.go Normal file
View File

@ -0,0 +1,59 @@
package cookie
import (
"net/http"
"strings"
)
func Parse(str string) []*http.Cookie {
header := http.Header{}
header.Add("Cookie", str)
request := http.Request{Header: header}
return request.Cookies()
}
func ToString(cookies []*http.Cookie) string {
if cookies == nil {
return ""
}
cookieStrings := make([]string, len(cookies))
for i, cookie := range cookies {
cookieStrings[i] = cookie.String()
}
return strings.Join(cookieStrings, ";")
}
func SetCookie(cookies []*http.Cookie, name, value string) []*http.Cookie {
for i, cookie := range cookies {
if cookie.Name == name {
cookies[i].Value = value
return cookies
}
}
cookies = append(cookies, &http.Cookie{Name: name, Value: value})
return cookies
}
func GetCookie(cookies []*http.Cookie, name string) *http.Cookie {
for _, cookie := range cookies {
if cookie.Name == name {
return cookie
}
}
return nil
}
func SetStr(cookiesStr, name, value string) string {
cookies := Parse(cookiesStr)
cookies = SetCookie(cookies, name, value)
return ToString(cookies)
}
func GetStr(cookiesStr, name string) string {
cookies := Parse(cookiesStr)
cookie := GetCookie(cookies, name)
if cookie == nil {
return ""
}
return cookie.Value
}

View File

@ -35,7 +35,7 @@ func GetFileType(ext string) int {
if ext == "" {
return conf.UNKNOWN
}
ext = strings.ToLower(strings.TrimLeft(ext, "."))
ext = strings.ToLower(strings.TrimPrefix(ext, "."))
if IsContain(conf.OfficeTypes, ext) {
return conf.OFFICE
}
@ -128,10 +128,6 @@ func Split(p string) (string, string) {
return path.Split(p)
}
// FormatName TODO
func FormatName(name string) string {
name = strings.ReplaceAll(name, "/", " ")
name = strings.ReplaceAll(name, "#", " ")
name = strings.ReplaceAll(name, "?", " ")
return name
func Ext(name string) string {
return strings.TrimPrefix(path.Ext(name), ".")
}

View File

@ -13,14 +13,12 @@ func GetSHA1Encode(data string) string {
return hex.EncodeToString(h.Sum(nil))
}
// GetMD5Encode
func GetMD5Encode(data string) string {
h := md5.New()
h.Write([]byte(data))
return hex.EncodeToString(h.Sum(nil))
}
// Get16MD5Encode
func Get16MD5Encode(data string) string {
return GetMD5Encode(data)[8:24]
}