diff --git a/README.md b/README.md index 7267dd7..6165ddd 100644 --- a/README.md +++ b/README.md @@ -10,12 +10,15 @@ > 选择对应的版本进行下载,进入到解压目录执行 ```bash ./webdav -rt="your refreshToken" +# 或者 +echo "your refreshToken" > /path/to/save/refreshToken +./webdav -rt /path/to/save/refreshToken ``` # 参数说明 ```bash -rt - 阿里云盘的refreshToken,获取方式见下文 + 阿里云盘的refreshToken,获取方式见下文。或者包含refreshToken的文件路径。 -port 非必填,服务器端口号,默认为8085 -user diff --git a/aliyun/api.go b/aliyun/api.go index 18bfebe..7ae7c18 100644 --- a/aliyun/api.go +++ b/aliyun/api.go @@ -3,6 +3,8 @@ package aliyun import ( "encoding/json" "fmt" + "os" + "io/ioutil" "go-aliyun-webdav/aliyun/cache" "go-aliyun-webdav/aliyun/model" "go-aliyun-webdav/aliyun/net" @@ -111,6 +113,14 @@ func GetFile(w http.ResponseWriter, url string, token string, rangeStr string, i } func RefreshToken(refreshToken string) model.RefreshTokenModel { + path := refreshToken + if _, err := os.Stat(path); err == nil { + buf, _ := ioutil.ReadFile(path) + refreshToken = string(buf) + if(len(refreshToken) >= 32){ + refreshToken = refreshToken[:32] // refreshToken is only 32 bit?? FIXME + } + } rs := net.Post(model.APIREFRESHTOKENURL, "", []byte(`{"refresh_token":"`+refreshToken+`"}`)) var refresh model.RefreshTokenModel if len(rs) > 0 { @@ -122,6 +132,13 @@ func RefreshToken(refreshToken string) model.RefreshTokenModel { } else { fmt.Println("刷新token失败") } + + if _, err := os.Stat(path); err == nil { + if(refreshToken != refresh.RefreshToken){ + content := []byte(refresh.RefreshToken) + ioutil.WriteFile(path, content, 0600) + } + } return refresh }