Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
iikira committed May 13, 2018
1 parent 7f0a4d1 commit 459319c
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 92 deletions.
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions internal/pcscommand/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ func download(id int, downloadURL, savePath string, client *requester.HTTPClient
}
}

if !cfg.IsTest {
fmt.Printf("\n\n[%d] 下载完成, 保存位置: %s\n\n", id, savePath)
} else {
fmt.Printf("\n\n[%d] 测试下载结束\n\n", id)
}

return nil
}

Expand All @@ -166,12 +172,6 @@ func getDownloadFunc(id int, savePath string, cfg *downloader.Config, isPrintSta
return err
}

if !cfg.IsTest {
fmt.Printf("\n\n[%d] 下载完成, 保存位置: %s\n\n", id, savePath)
} else {
fmt.Printf("\n\n[%d] 测试下载结束\n\n", id)
}

return nil
}
}
Expand Down
1 change: 0 additions & 1 deletion internal/pcscommand/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func getShareDLink(pcspath string) (dlink string) {
defer pcs.ShareCancel([]int64{s.ShareID})

sInfo := pan.NewSharedInfo(s.Link)
sInfo.SetHTTPS(pcsconfig.Config.EnableHTTPS())
sInfo.Client = requester.NewHTTPClient()
sInfo.Client.SetHTTPSecure(pcsconfig.Config.EnableHTTPS())

Expand Down
5 changes: 5 additions & 0 deletions internal/pcsupdate/pcsupdate.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// Package pcsupdate 更新包
package pcsupdate

import (
"archive/zip"
"bytes"
"fmt"
"github.com/iikira/BaiduPCS-Go/internal/pcsconfig"
"github.com/iikira/BaiduPCS-Go/pcsliner"
"github.com/iikira/BaiduPCS-Go/pcsutil"
"github.com/iikira/BaiduPCS-Go/pcsutil/converter"
"github.com/iikira/BaiduPCS-Go/requester"
"github.com/iikira/BaiduPCS-Go/requester/downloader"
"github.com/iikira/BaiduPCS-Go/requester/rio"
"github.com/iikira/baidu-tools/pan"
Expand Down Expand Up @@ -36,6 +39,8 @@ func CheckUpdate(version string, yes bool) {
}
fmt.Println("检测更新中, 稍候...")
sharedInfo := pan.NewSharedInfo("https://pan.baidu.com/s/100qbYyjlpSNNkrUWq8MKng")
sharedInfo.Client = requester.NewHTTPClient()
sharedInfo.Client.SetHTTPSecure(pcsconfig.Config.EnableHTTPS())

err := sharedInfo.Auth("7vgf")
if err != nil {
Expand Down
13 changes: 0 additions & 13 deletions requester/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package requester

import (
"context"
"crypto/tls"
"net"
"time"
)
Expand Down Expand Up @@ -65,15 +64,3 @@ func dialContext(ctx context.Context, network, address string) (conn net.Conn, e
func dial(network, address string) (conn net.Conn, err error) {
return dialContext(context.Background(), network, address)
}

func dialTLS(network, address string) (tlsConn net.Conn, err error) {
conn, err := dial(network, address)
if err != nil {
return nil, err
}

return tls.Client(conn, &tls.Config{
ServerName: getServerName(address),
InsecureSkipVerify: TLSConfig.InsecureSkipVerify,
}), nil
}
80 changes: 40 additions & 40 deletions requester/downloader/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Monitor struct {
completed <-chan struct{}
err error
dymanicMu sync.Mutex
isReloadWorker bool //是否重载worker
isReloadWorker bool //是否重载worker, 单线程模式不重载
}

//NewMonitor 初始化Monitor
Expand Down Expand Up @@ -375,57 +375,57 @@ func (mt *Monitor) Execute(cancelCtx context.Context) {
worker.Reset()
}(worker)
}
}

//下载快完成了, 动态分配线程
if float64(mt.status.Downloaded()) > float64(mt.status.totalSize)*0.8 {
pcsverbose.Verbosef("DEBUG: monitor: start duplicate.\n")
for k := range mt.workers {
if mt.workers[k] == nil {
continue
//下载快完成了, 动态分配线程
if float64(mt.status.Downloaded()) > float64(mt.status.TotalSize())*0.7 {
pcsverbose.Verbosef("DEBUG: monitor: start duplicate.\n")
for k := range mt.workers {
if mt.workers[k] == nil {
continue
}
//动态分配线程
go func(worker *Worker) {
//过滤速度为0的worker
if worker.GetSpeedsPerSecond() == 0 {
return
}
//动态分配线程
go func(worker *Worker) {
//过滤速度为0的worker
if worker.GetSpeedsPerSecond() == 0 {
return
}

mt.dymanicMu.Lock()
defer mt.dymanicMu.Unlock()
mt.dymanicMu.Lock()
defer mt.dymanicMu.Unlock()

// 筛选空闲的Worker
avaliableWorker := mt.GetAvaliableWorker()
if avaliableWorker == nil || worker == avaliableWorker { // 没有空的
return
}
// 筛选空闲的Worker
avaliableWorker := mt.GetAvaliableWorker()
if avaliableWorker == nil || worker == avaliableWorker { // 没有空的
return
}

workerRange := worker.GetRange()
workerRange := worker.GetRange()

end := workerRange.LoadEnd()
middle := (workerRange.LoadBegin() + end) / 2
end := workerRange.LoadEnd()
middle := (workerRange.LoadBegin() + end) / 2

if end-middle < MinParallelSize/5 { // 如果线程剩余的下载量太少, 不分配空闲线程
return
}
if end-middle < MinParallelSize/5 { // 如果线程剩余的下载量太少, 不分配空闲线程
return
}

// 折半
// 折半

avaliableWorkerRange := avaliableWorker.GetRange()
avaliableWorkerRange.StoreBegin(middle + 1)
avaliableWorkerRange.StoreEnd(end)
avaliableWorkerRange := avaliableWorker.GetRange()
avaliableWorkerRange.StoreBegin(middle + 1)
avaliableWorkerRange.StoreEnd(end)

avaliableWorker.CleanStatus()
avaliableWorker.CleanStatus()

workerRange.StoreEnd(middle)
workerRange.StoreEnd(middle)

pcsverbose.Verbosef("MONITER: worker duplicated: %d <- %d\n", avaliableWorker.ID(), worker.ID())
go avaliableWorker.Execute()
time.Sleep(10 * time.Microsecond)
}(mt.workers[k])
} //end for
} // end if 2
} //end if 1
} //end selete
pcsverbose.Verbosef("MONITER: worker duplicated: %d <- %d\n", avaliableWorker.ID(), worker.ID())
go avaliableWorker.Execute()
time.Sleep(10 * time.Microsecond)
}(mt.workers[k])
} //end for
} // end if
} //end select
} //end for
}

Expand Down
1 change: 0 additions & 1 deletion requester/downloader/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ func (wer *Worker) Execute() {
wer.status.statusCode = StatusCodePending

var resp *http.Response

resp, wer.err = wer.client.Req("GET", wer.url, nil, header)
if resp != nil {
defer resp.Body.Close()
Expand Down
41 changes: 27 additions & 14 deletions requester/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@ package requester

import (
"crypto/tls"
"net"
"net/http"
"net/http/cookiejar"
"time"
)

var (
// TLSConfig tls连接配置
TLSConfig = &tls.Config{
InsecureSkipVerify: true,
}
)

// HTTPClient http client
type HTTPClient struct {
http.Client
jar *cookiejar.Jar
transport *http.Transport
https bool
UserAgent string
}

Expand All @@ -37,11 +32,13 @@ func NewHTTPClient() *HTTPClient {
func (h *HTTPClient) lazyInit() {
if h.transport == nil {
h.transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: dialContext,
Dial: dial,
DialTLS: dialTLS,
TLSClientConfig: TLSConfig,
Proxy: http.ProxyFromEnvironment,
// DialContext: dialContext,
// Dial: dial,
// DialTLS: h.dialTLSFunc(),
TLSClientConfig: &tls.Config{
InsecureSkipVerify: !h.https,
},
TLSHandshakeTimeout: 10 * time.Second,
DisableKeepAlives: false,
DisableCompression: false, // gzip
Expand Down Expand Up @@ -82,12 +79,14 @@ func (h *HTTPClient) ResetCookiejar() {

// SetHTTPSecure 是否启用 https 安全检查, 默认不检查
func (h *HTTPClient) SetHTTPSecure(b bool) {
h.https = b
h.lazyInit()
if b {
TLSConfig.InsecureSkipVerify = b
h.transport.TLSClientConfig = nil
} else {
h.transport.TLSClientConfig = TLSConfig
h.transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: !b,
}
}
}

Expand All @@ -113,3 +112,17 @@ func (h *HTTPClient) SetResponseHeaderTimeout(t time.Duration) {
func (h *HTTPClient) SetTimeout(t time.Duration) {
h.Client.Timeout = t
}

func (h *HTTPClient) dialTLSFunc() func(network, address string) (tlsConn net.Conn, err error) {
return func(network, address string) (tlsConn net.Conn, err error) {
conn, err := dial(network, address)
if err != nil {
return nil, err
}

return tls.Client(conn, &tls.Config{
ServerName: getServerName(address),
InsecureSkipVerify: !h.https,
}), nil
}
}
18 changes: 2 additions & 16 deletions vendor/github.com/iikira/baidu-tools/pan/shared.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 459319c

Please sign in to comment.