Skip to content

Commit

Permalink
🚩 新增代理功能
Browse files Browse the repository at this point in the history
  • Loading branch information
aoaostar committed Apr 28, 2022
1 parent 4e5a9e0 commit 568942e
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 38 deletions.
42 changes: 35 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,36 @@ var data = JSON.parse(localStorage.getItem('token'));
console.log(`refresh_token => ${data.refresh_token}
default_drive_id => ${data.default_drive_id}`);
```
### Proxy 使用方法
> 由于阿里云盘限制了海外,海外机器根本无法上传
> 可以使用`--proxy`参数设置国内代理
> 使用`nginx`配置反向代理,配置参数如下
```shell
location /{
if ($request_uri ~ /){
add_header content-type "application/json";
return 200 "{\"usage\":\"Host/{URL}\"}";
}
if ($request_uri ~ ^/(.*)){
set $proxy_url $1;
}
proxy_pass $proxy_url;
resolver 8.8.8.8;
}
```
#### 使用示例
```shell
alidrive -p(--proxy) http://proxy.aoaostar.com
```
> 或者配置文件内配置
### config.yaml
```yaml
debug: false
transfers: 3
proxy:
ali_drive:
drive_id: xxxxxxx
refresh_token: xxxxxx
Expand All @@ -48,17 +72,21 @@ Usage:
alidrive.exe [OPTIONS] LocalPath RemotePath

Application Options:
-d, --debug Debug模式
-t, --transfers= 同时上传文件个数
-c, --config= 配置文件路径 (default: config.yaml)
-v, --version 输出版本信息
-d, --debug Debug模式
-t, --transfers= 同时上传文件个数
-c, --config= 配置文件路径 (default: config.yaml)
-p, --proxy= API代理
-v, --version 输出版本信息
--drive_id= 驱动id
--refresh_token= 刷新令牌
--root_path= 根目录路径

Help Options:
-h, --help Show this help message
-h, --help Show this help message

Arguments:
LocalPath: 本地文件路径
RemotePath: 远程文件路径
LocalPath: 本地文件路径
RemotePath: 远程文件路径
```
## 编译
Expand Down
3 changes: 3 additions & 0 deletions bootstrap/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ func InitConfig() {
if conf.Opt.AliDrive.RootPath != "" {
conf.Conf.AliDrive.RootPath = conf.Opt.AliDrive.RootPath
}
if conf.Opt.Proxy != "" {
conf.Conf.Proxy = conf.Opt.Proxy
}
}
20 changes: 10 additions & 10 deletions bootstrap/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ func Run() {
conf.Opt.Positional.LocalPath = filepath.Dir(conf.Opt.Positional.LocalPath) + "/"
conf.Output.Infof("共计%d个文件", len(allFiles))

drive := alidrive.AliDrive{
Instance: alidrive.Instance{
RefreshToken: conf.Conf.AliDrive.RefreshToken,
DriveId: conf.Conf.AliDrive.DriveId,
AccessToken: "",
ParentPath: "root",
},
}
drive := alidrive.New(alidrive.Instance{
RefreshToken: conf.Conf.AliDrive.RefreshToken,
DriveId: conf.Conf.AliDrive.DriveId,
AccessToken: "",
ParentPath: "root",
Proxy: conf.Conf.Proxy,
})

conf.Output.Infof("正在获取AccessToken")
if err := drive.RefreshToken(); err != nil {
conf.Output.Panic(err)
Expand Down Expand Up @@ -81,7 +81,7 @@ func Run() {
return
}

TreeFolders(&drive, conf.Opt.Positional.RemotePath, dirs)
TreeFolders(drive, conf.Opt.Positional.RemotePath, dirs)

wg := sync.WaitGroup{}
p := mpb.New(mpb.WithWaitGroup(&wg), mpb.WithRefreshRate(300*time.Millisecond))
Expand All @@ -96,7 +96,7 @@ func Run() {
wg.Add(1)
go func() {
defer wg.Done()
transfer(jobs, taskBar, p, &drive, dirs)
transfer(jobs, taskBar, p, drive, dirs)
}()
}
for _, file := range files {
Expand Down
7 changes: 4 additions & 3 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import "github.com/sirupsen/logrus"

type (
Config struct {
Debug bool `json:"debug"`
Transfers uint64 `json:"transfers"`
AliDrive struct {
Debug bool `json:"debug" mapstructure:"debug"`
Transfers uint64 `json:"transfers" mapstructure:"transfers"`
Proxy string `json:"proxy" mapstructure:"proxy"`
AliDrive struct {
DriveId string `mapstructure:"drive_id"`
RefreshToken string `mapstructure:"refresh_token"`
RootPath string `mapstructure:"root_path"`
Expand Down
2 changes: 1 addition & 1 deletion conf/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

const (
VERSION = "v2.0.3"
VERSION = "v2.0.4"
)

var executable, _ = os.Executable()
Expand Down
1 change: 1 addition & 0 deletions example.config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
debug: false
transfers: 3
proxy:
ali_drive:
drive_id: xxxxxxx
refresh_token: xxxxxx
Expand Down
21 changes: 15 additions & 6 deletions pkg/alidrive/alidrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ type (

var client = resty.New()

func New(instance Instance) *AliDrive {
if instance.Proxy != "" {
instance.Proxy = strings.TrimRight(instance.Proxy, "/") + "/"
}
client.OnBeforeRequest(func(c *resty.Client, request *resty.Request) error {
request.URL = instance.Proxy + request.URL
return nil
})
return &AliDrive{instance}
}

func (drive *AliDrive) RefreshToken() error {
url := "https://auth.aliyundrive.com/v2/account/token"
var resp TokenResp
Expand All @@ -41,6 +52,7 @@ func (drive *AliDrive) RefreshToken() error {
return fmt.Errorf("刷新token失败: %s", e.Message)
}
drive.Instance.RefreshToken, drive.Instance.AccessToken = resp.RefreshToken, resp.AccessToken
client.SetAuthToken(drive.Instance.AccessToken)
return nil
}

Expand Down Expand Up @@ -76,7 +88,6 @@ func (drive *AliDrive) Upload(file util.FileStream) error {
}
//preHash
_, err := client.R().
SetAuthToken(drive.Instance.AccessToken).
SetBody(&createWithFoldersBody).
SetResult(&resp).
SetError(&e).
Expand Down Expand Up @@ -112,7 +123,6 @@ func (drive *AliDrive) Upload(file util.FileStream) error {
createWithFoldersBody["proof_version"] = "v1"
conf.Output.Debug("createWithFoldersBody", createWithFoldersBody)
_, err = client.R().
SetAuthToken(drive.Instance.AccessToken).
SetBody(&createWithFoldersBody).
SetResult(&resp).
SetError(&e).
Expand All @@ -137,7 +147,7 @@ func (drive *AliDrive) Upload(file util.FileStream) error {
return err
}
for i = 0; i < total; i++ {
req, err := http.NewRequest(http.MethodPut, resp.PartInfoList[i].UploadUrl, file.Bar.ProxyReader(io.LimitReader(file.File, CHUNKSIZE)))
req, err := http.NewRequest(http.MethodPut, drive.Instance.Proxy+resp.PartInfoList[i].UploadUrl, file.Bar.ProxyReader(io.LimitReader(file.File, CHUNKSIZE)))
if err != nil {
return err
}
Expand Down Expand Up @@ -174,7 +184,6 @@ func (drive *AliDrive) Upload(file util.FileStream) error {
"upload_id": resp.UploadId,
}
_, err := client.R().SetResult(&getUploadUrlResp).SetError(&e).SetBody(getUploadUrlBody).
SetAuthToken(drive.Instance.AccessToken).
Post("https://api.aliyundrive.com/v2/file/get_upload_url")
if err != nil {
return err
Expand All @@ -194,7 +203,7 @@ func (drive *AliDrive) Upload(file util.FileStream) error {
"drive_id": drive.Instance.DriveId,
"file_id": resp.FileId,
"upload_id": resp.UploadId,
}).SetAuthToken(drive.Instance.AccessToken).SetError(&e).
}).SetError(&e).
Post("https://api.aliyundrive.com/v2/file/complete")
if err != nil {
return err
Expand Down Expand Up @@ -227,7 +236,7 @@ func (drive *AliDrive) CreateFolders(path string, rootPath string) (string, erro
"check_name_mode": "refuse",
"type": "folder",
}
_, err := client.R().SetAuthToken(drive.Instance.AccessToken).SetError(&e).SetBody(&body).SetResult(&resp).
_, err := client.R().SetError(&e).SetBody(&body).SetResult(&resp).
Post("https://api.aliyundrive.com/adrive/v2/file/createWithFolders")
if err != nil {
return parentFileId, err
Expand Down
23 changes: 12 additions & 11 deletions pkg/alidrive/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type (
RefreshToken string
DriveId string
AccessToken string
ParentPath string
ParentPath string
Proxy string
}
PartInfo struct {
PartNumber uint64 `json:"part_number"`
Expand All @@ -27,17 +28,17 @@ type (
ContentType string `json:"content_type"`
}
CreateWithFoldersResp struct {
ParentFileId string `json:"parent_file_id"`
ParentFileId string `json:"parent_file_id"`
PartInfoList []PartInfoResp `json:"part_info_list"`
UploadId string `json:"upload_id"`
RapidUpload bool `json:"rapid_upload"`
Type string `json:"type"`
FileId string `json:"file_id"`
DomainId string `json:"domain_id"`
DriveId string `json:"drive_id"`
FileName string `json:"file_name"`
EncryptMode string `json:"encrypt_mode"`
Location string `json:"location"`
UploadId string `json:"upload_id"`
RapidUpload bool `json:"rapid_upload"`
Type string `json:"type"`
FileId string `json:"file_id"`
DomainId string `json:"domain_id"`
DriveId string `json:"drive_id"`
FileName string `json:"file_name"`
EncryptMode string `json:"encrypt_mode"`
Location string `json:"location"`
}
GetUploadUrlResp struct {
DomainId string `json:"domain_id"`
Expand Down
1 change: 1 addition & 0 deletions pkg/util/mpb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func NewMpb(p *mpb.Progress, name string, total int64) *mpb.Bar {
),
mpb.AppendDecorators(
decor.AverageSpeed(decor.UnitKiB, "% .2f", decor.WCSyncSpace),
decor.OnComplete(decor.Percentage(decor.WCSyncSpace), ""),
),
)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/util/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Option struct {
Debug *bool `short:"d" long:"debug" description:"Debug模式"`
Transfers *uint64 `short:"t" long:"transfers" description:"同时上传文件个数"`
Config string `short:"c" long:"config" description:"配置文件路径" default:"config.yaml"`
Proxy string `short:"p" long:"proxy" description:"API代理"`
Version func() `short:"v" long:"version" description:"输出版本信息"`
AliDrive struct {
DriveId string `long:"drive_id" description:"驱动id"`
Expand Down

0 comments on commit 568942e

Please sign in to comment.