2024-07-23 10:23:43 +08:00
|
|
|
package downloader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-07-31 16:49:14 +08:00
|
|
|
"gitea.bvbej.com/bvbej/base-golang/pkg/downloader/base"
|
|
|
|
"gitea.bvbej.com/bvbej/base-golang/pkg/downloader/controller"
|
|
|
|
"gitea.bvbej.com/bvbej/base-golang/tool"
|
2024-07-23 10:23:43 +08:00
|
|
|
"golang.org/x/net/proxy"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewDownloader(t *testing.T) {
|
|
|
|
dialer, err := proxy.SOCKS5("tcp", "127.0.0.1:1080", nil, proxy.Direct)
|
|
|
|
parse, _ := url.Parse(`socks5://127.0.0.1:1080`)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err, dialer)
|
|
|
|
}
|
|
|
|
err = <-New(
|
|
|
|
controller.WithDialer(dialer), // todo: use dialer proxy
|
|
|
|
controller.WithCookie(nil),
|
|
|
|
controller.WithProxy(http.ProxyURL(parse)), // todo: use http client proxy
|
|
|
|
controller.WithTimeout(time.Second*3),
|
2024-09-06 15:37:13 +08:00
|
|
|
controller.WithInsecureSkipVerify(true),
|
2024-07-23 10:23:43 +08:00
|
|
|
).URL("http://10.0.1.34/com.tencent.tmgp.jxqy.apk").
|
|
|
|
Listener(func(event *Event) {
|
|
|
|
if event.Key == EventKeyFinally {
|
|
|
|
fmt.Println("下载完成!")
|
|
|
|
}
|
|
|
|
if event.Key == EventKeyProgress {
|
|
|
|
fmt.Printf("下载速度:%s/s 已下载:%s 已用时:%s \n",
|
|
|
|
tool.ByteFmt(event.Task.Progress.Speed),
|
|
|
|
tool.ByteFmt(event.Task.Progress.Downloaded),
|
|
|
|
time.Duration(event.Task.Progress.Used),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}).
|
|
|
|
Create(&base.Options{
|
|
|
|
Connections: runtime.NumCPU(),
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Log(err)
|
|
|
|
}
|