From ae8223023617df2f41d99182b95361ef49f2beaf Mon Sep 17 00:00:00 2001
From: qjfoidnh <qjfoidnh@126.com>
Date: Mon, 22 Feb 2021 17:00:46 +0800
Subject: [PATCH] v3.7.6

---
 README.md                                      |  6 ++++++
 internal/pcscommand/download.go                |  9 +++++++--
 .../pcsdownload/download_task_unit.go          | 11 +++++++++--
 main.go                                        |  7 ++++++-
 requester/downloader/worker.go                 |  4 ++++
 requester/http_client.go                       | 18 +++++++++---------
 6 files changed, 41 insertions(+), 14 deletions(-)

diff --git a/README.md b/README.md
index 9ae745e..41dfbd8 100644
--- a/README.md
+++ b/README.md
@@ -100,6 +100,12 @@ iikira/BaiduPCS-Go was largely inspired by [GangZhuo/BaiduPCS](https://github.co
 
 # 版本更新
 
+**2021.2.23** v3.7.6:
+
+- fix 下载文件报```x509: certificate is valid```错误
+- 完善了下载错误的捕获种类
+- download增加--fullpath参数,本地目录保留网盘从根目录开始的完整结构
+
 **2021.2.8** v3.7.5:
 
 - fix 某些时候误报stoken缺失
diff --git a/internal/pcscommand/download.go b/internal/pcscommand/download.go
index 17d9198..9996456 100644
--- a/internal/pcscommand/download.go
+++ b/internal/pcscommand/download.go
@@ -30,6 +30,7 @@ type (
 		Load                 int
 		MaxRetry             int
 		NoCheck              bool
+		FullPath             bool
 	}
 
 	// LocateDownloadOption 获取下载链接可选参数
@@ -157,11 +158,15 @@ func RunDownload(paths []string, options *DownloadOptions) {
 		// 设置下载并发数
 		executor.SetParallel(loadCount)
 		// 设置储存的路径
+		vPath := v.Path
+		if !options.FullPath {
+			vPath = filepath.Join(v.PreBase, filepath.Base(v.Path))
+		}
 		if options.SaveTo != "" {
-			unit.SavePath = filepath.Join(options.SaveTo, filepath.Join(v.PreBase, filepath.Base(v.Path)))
+			unit.SavePath = filepath.Join(options.SaveTo, vPath)
 		} else {
 			// 使用默认的保存路径
-			unit.SavePath = GetActiveUser().GetSavePath(filepath.Join(v.PreBase, filepath.Base(v.Path)))
+			unit.SavePath = GetActiveUser().GetSavePath(vPath)
 		}
 		info := executor.Append(&unit, options.MaxRetry)
 		fmt.Printf("[%s] 加入下载队列: %s\n", info.Id(), v.Path)
diff --git a/internal/pcsfunctions/pcsdownload/download_task_unit.go b/internal/pcsfunctions/pcsdownload/download_task_unit.go
index 8168dcb..2a3cfe2 100644
--- a/internal/pcsfunctions/pcsdownload/download_task_unit.go
+++ b/internal/pcsfunctions/pcsdownload/download_task_unit.go
@@ -253,6 +253,8 @@ func (dtu *DownloadTaskUnit) handleError(result *taskframework.TaskUnitRunResult
 			fallthrough
 		case 31066: // file does not exist
 			result.NeedRetry = false
+		case 31297: // file does not exist
+			result.NeedRetry = false
 		case 31626: // user is not authorized
 			//可能是User-Agent不对
 			//重试
@@ -293,8 +295,13 @@ func (dtu *DownloadTaskUnit) locateDownload(result *taskframework.TaskUnitRunRes
 	}
 
 	// 更新链接的协议
-	FixHTTPLinkURL(rawDlinks[0])
-	dlink := rawDlinks[0].String()
+	// 跳过nb.cache这种还没有证书的
+	raw_dlink := rawDlinks[0]
+	if strings.HasPrefix(rawDlinks[0].Host, "nb.cache") && len(rawDlinks) > 1 {
+		raw_dlink = rawDlinks[1]
+	}
+	FixHTTPLinkURL(raw_dlink)
+	dlink := raw_dlink.String()
 
 	dtu.execPanDownload(dlink, result, &ok)
 	return
diff --git a/main.go b/main.go
index c6a1231..ea755d8 100644
--- a/main.go
+++ b/main.go
@@ -55,7 +55,7 @@ const (
 
 var (
 	// Version 版本号
-	Version = "v3.7.5-devel"
+	Version = "v3.7.6-devel"
 
 	historyFilePath = filepath.Join(pcsconfig.GetConfigDir(), "pcs_command_history.txt")
 	reloadFn        = func(c *cli.Context) error {
@@ -1066,6 +1066,7 @@ func main() {
 					Load:                 c.Int("l"),
 					MaxRetry:             c.Int("retry"),
 					NoCheck:              c.Bool("nocheck"),
+					FullPath:             c.Bool("fullpath"),
 				}
 
 				pcscommand.RunDownload(c.Args(), do)
@@ -1119,6 +1120,10 @@ func main() {
 					Name:  "nocheck",
 					Usage: "下载文件完成后不校验文件",
 				},
+				cli.BoolFlag{
+					Name:        "fullpath",
+					Usage:       "以网盘完整路径保存到本地",
+				},
 			},
 		},
 		{
diff --git a/requester/downloader/worker.go b/requester/downloader/worker.go
index a3f0ff8..ffede85 100644
--- a/requester/downloader/worker.go
+++ b/requester/downloader/worker.go
@@ -294,6 +294,10 @@ func (wer *Worker) Execute() {
 		fallthrough
 	case 403: // Forbidden
 		fallthrough
+	case 404: // file block not exists
+		wer.status.statusCode = StatusCodeInternalError
+		wer.err = errors.New(resp.Status)
+		return
 	case 406: // Not Acceptable
 		wer.status.statusCode = StatusCodeNetError
 		wer.err = errors.New(resp.Status)
diff --git a/requester/http_client.go b/requester/http_client.go
index 376c180..54dea29 100644
--- a/requester/http_client.go
+++ b/requester/http_client.go
@@ -36,7 +36,7 @@ func (h *HTTPClient) lazyInit() {
 			Dial:        dial,
 			// DialTLS:     h.dialTLSFunc(),
 			TLSClientConfig: &tls.Config{
-				InsecureSkipVerify: !h.https,
+				InsecureSkipVerify: true,
 			},
 			TLSHandshakeTimeout:   10 * time.Second,
 			DisableKeepAlives:     false,
@@ -77,17 +77,17 @@ func (h *HTTPClient) ResetCookiejar() {
 	h.Jar, _ = cookiejar.New(nil)
 }
 
-// SetHTTPSecure 是否启用 https 安全检查, 默认不检查
+// SetHTTPSecure 是否启用 https 安全检查, 强制不检查
 func (h *HTTPClient) SetHTTPSecure(b bool) {
 	h.https = b
 	h.lazyInit()
-	if b {
-		h.transport.TLSClientConfig = nil
-	} else {
-		h.transport.TLSClientConfig = &tls.Config{
-			InsecureSkipVerify: !b,
-		}
-	}
+// 	if b {
+// 		h.transport.TLSClientConfig = nil
+// 	} else {
+    h.transport.TLSClientConfig = &tls.Config{
+        InsecureSkipVerify: true,
+    }
+// 	}
 }
 
 // SetKeepAlive 设置 Keep-Alive