Skip to content

Commit

Permalink
feat: more config
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Nov 29, 2023
1 parent 7d6428c commit 717134b
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 82 deletions.
6 changes: 3 additions & 3 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type Controller struct {
GetConfig func(v any) (bool, error)
GetConfig func(v any) bool
FileController
//ContextDialer() (proxy.Dialer, error)
}
Expand All @@ -21,8 +21,8 @@ type DefaultFileController struct {
func NewController() *Controller {
return &Controller{
FileController: &DefaultFileController{},
GetConfig: func(v any) (bool, error) {
return false, nil
GetConfig: func(v any) bool {
return false
},
}
}
Expand Down
3 changes: 2 additions & 1 deletion internal/protocol/bt/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package bt

type config struct {
Trackers []string `json:"trackers"`
ListenPort int `json:"listenPort"`
Trackers []string `json:"trackers"`
}
21 changes: 12 additions & 9 deletions internal/protocol/bt/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ var (
)

type Fetcher struct {
ctl *controller.Controller
ctl *controller.Controller
config *config

torrent *torrent.Torrent
meta *fetcher.FetcherMeta
Expand All @@ -42,6 +43,13 @@ func (f *Fetcher) Setup(ctl *controller.Controller) {
if f.meta == nil {
f.meta = &fetcher.FetcherMeta{}
}
exist := f.ctl.GetConfig(&f.config)
if !exist {
f.config = &config{
ListenPort: 0,
Trackers: []string{},
}
}
return
}

Expand All @@ -54,7 +62,7 @@ func (f *Fetcher) initClient() (err error) {
}

cfg := torrent.NewDefaultClientConfig()
cfg.ListenPort = 0
cfg.ListenPort = f.config.ListenPort
cfg.DefaultStorage = newFileOpts(newFileClientOpts{
ClientBaseDir: cfg.DataDir,
HandleFileTorrent: func(infoHash metainfo.Hash, ft *fileTorrentImpl) {
Expand Down Expand Up @@ -231,11 +239,6 @@ func (f *Fetcher) addTorrent(req *base.Request) (err error) {
if err != nil {
return
}
var cfg config
exist, err := f.ctl.GetConfig(&cfg)
if err != nil {
return
}

// use map to deduplicate
trackers := make(map[string]bool)
Expand All @@ -247,8 +250,8 @@ func (f *Fetcher) addTorrent(req *base.Request) (err error) {
}
}
}
if exist && len(cfg.Trackers) > 0 {
for _, tracker := range cfg.Trackers {
if len(f.config.Trackers) > 0 {
for _, tracker := range f.config.Trackers {
trackers[tracker] = true
}
}
Expand Down
3 changes: 2 additions & 1 deletion internal/protocol/http/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package http

type config struct {
Connections int `json:"connections"`
UserAgent string `json:"userAgent"`
Connections int `json:"connections"`
}
100 changes: 50 additions & 50 deletions internal/protocol/http/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func newChunk(begin int64, end int64) *chunk {

type Fetcher struct {
ctl *controller.Controller
config *config
doneCh chan error

meta *fetcher.FetcherMeta
Expand All @@ -71,14 +72,21 @@ func (f *Fetcher) Setup(ctl *controller.Controller) {
if f.meta == nil {
f.meta = &fetcher.FetcherMeta{}
}
exist := f.ctl.GetConfig(&f.config)
if !exist {
f.config = &config{
UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
Connections: 1,
}
}
return
}

func (f *Fetcher) Resolve(req *base.Request) error {
if err := base.ParseReqExtra[fhttp.ReqExtra](req); err != nil {
return err
}
httpReq, err := buildRequest(nil, req)
httpReq, err := f.buildRequest(nil, req)
if err != nil {
return err
}
Expand Down Expand Up @@ -160,16 +168,7 @@ func (f *Fetcher) Create(opts *base.Options) error {
}
extra := opts.Extra.(*fhttp.OptsExtra)
if extra.Connections == 0 {
var cfg config
exist, err := f.ctl.GetConfig(&cfg)
if err != nil {
return err
}
if exist {
extra.Connections = cfg.Connections
} else {
extra.Connections = 1
}
extra.Connections = f.config.Connections
}
return nil
}
Expand Down Expand Up @@ -260,7 +259,7 @@ func (f *Fetcher) fetchChunk(index int, ctx context.Context) (err error) {
chunk := f.chunks[index]
chunk.retryTimes = 0

httpReq, err := buildRequest(ctx, f.meta.Req)
httpReq, err := f.buildRequest(ctx, f.meta.Req)
if err != nil {
return err
}
Expand Down Expand Up @@ -357,43 +356,7 @@ func (f *Fetcher) fetchChunk(index int, ctx context.Context) (err error) {
return
}

func (f *Fetcher) splitChunk() (chunks []*chunk) {
if f.meta.Res.Range {
connections := f.meta.Opts.Extra.(*fhttp.OptsExtra).Connections
// 每个连接平均需要下载的分块大小
chunkSize := f.meta.Res.Size / int64(connections)
chunks = make([]*chunk, connections)
for i := 0; i < connections; i++ {
var (
begin = chunkSize * int64(i)
end int64
)
if i == connections-1 {
// 最后一个分块需要保证把文件下载完
end = f.meta.Res.Size - 1
} else {
end = begin + chunkSize - 1
}
chunk := newChunk(begin, end)
chunks[i] = chunk
}
} else {
// 只支持单连接下载
chunks = make([]*chunk, 1)
chunks[0] = newChunk(0, 0)
}
return
}

func buildClient() *http.Client {
// Cookie handle
jar, _ := cookiejar.New(nil)
return &http.Client{
Jar: jar,
}
}

func buildRequest(ctx context.Context, req *base.Request) (httpReq *http.Request, err error) {
func (f *Fetcher) buildRequest(ctx context.Context, req *base.Request) (httpReq *http.Request, err error) {
url, err := url.Parse(req.URL)
if err != nil {
return
Expand Down Expand Up @@ -423,7 +386,8 @@ func buildRequest(ctx context.Context, req *base.Request) (httpReq *http.Request
}
}
if _, ok := headers[base.HttpHeaderUserAgent]; !ok {
headers[base.HttpHeaderUserAgent] = []string{base.AgentName}
// load user agent from config
headers[base.HttpHeaderUserAgent] = []string{f.config.UserAgent}
}

if ctx != nil {
Expand All @@ -438,6 +402,42 @@ func buildRequest(ctx context.Context, req *base.Request) (httpReq *http.Request
return httpReq, nil
}

func (f *Fetcher) splitChunk() (chunks []*chunk) {
if f.meta.Res.Range {
connections := f.meta.Opts.Extra.(*fhttp.OptsExtra).Connections
// 每个连接平均需要下载的分块大小
chunkSize := f.meta.Res.Size / int64(connections)
chunks = make([]*chunk, connections)
for i := 0; i < connections; i++ {
var (
begin = chunkSize * int64(i)
end int64
)
if i == connections-1 {
// 最后一个分块需要保证把文件下载完
end = f.meta.Res.Size - 1
} else {
end = begin + chunkSize - 1
}
chunk := newChunk(begin, end)
chunks[i] = chunk
}
} else {
// 只支持单连接下载
chunks = make([]*chunk, 1)
chunks[0] = newChunk(0, 0)
}
return
}

func buildClient() *http.Client {
// Cookie handle
jar, _ := cookiejar.New(nil)
return &http.Client{
Jar: jar,
}
}

type fetcherData struct {
Chunks []*chunk
}
Expand Down
6 changes: 3 additions & 3 deletions internal/protocol/http/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,11 @@ func buildConfigFetcher() fetcher.Fetcher {
mockCfg := config{
Connections: 16,
}
newController.GetConfig = func(v any) (bool, error) {
newController.GetConfig = func(v any) bool {
if err := json.Unmarshal([]byte(test.ToJson(mockCfg)), v); err != nil {
return false, err
return false
}
return true, nil
return true
}
fetcher.Setup(newController)
return fetcher
Expand Down
4 changes: 0 additions & 4 deletions pkg/base/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,3 @@ const (

HttpHeaderRangeFormat = "bytes=%d-%d"
)

const (
AgentName = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
)
13 changes: 7 additions & 6 deletions pkg/download/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (d *Downloader) parseFb(url string) (fetcher.FetcherBuilder, error) {

func (d *Downloader) setupFetcher(fetcher fetcher.Fetcher) {
ctl := controller.NewController()
ctl.GetConfig = func(v any) (bool, error) {
ctl.GetConfig = func(v any) bool {
return d.getProtocolConfig(fetcher.Name(), v)
}
fetcher.Setup(ctl)
Expand Down Expand Up @@ -563,18 +563,19 @@ func (d *Downloader) PutConfig(v *DownloaderStoreConfig) error {
return d.storage.Put(bucketConfig, "config", v)
}

func (d *Downloader) getProtocolConfig(name string, v any) (bool, error) {
func (d *Downloader) getProtocolConfig(name string, v any) bool {
cfg, err := d.GetConfig()
if err != nil {
return false, err
return false
}
if cfg.ProtocolConfig == nil || cfg.ProtocolConfig[name] == nil {
return false, nil
return false
}
if err := util.MapToStruct(cfg.ProtocolConfig[name], v); err != nil {
return false, err
d.Logger.Warn().Err(err).Msgf("get protocol config failed")
return false
}
return true, nil
return true
}

// wait task done
Expand Down
7 changes: 2 additions & 5 deletions pkg/download/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,7 @@ func TestDownloader_Protocol_Config(t *testing.T) {
defer downloader.Clear()

var httpCfg map[string]any
exits, err := downloader.getProtocolConfig("http", &httpCfg)
if err != nil {
t.Fatal(err)
}
exits := downloader.getProtocolConfig("http", &httpCfg)
if exits {
t.Errorf("getProtocolConfig() got = %v, want %v", exits, false)
}
Expand All @@ -234,7 +231,7 @@ func TestDownloader_Protocol_Config(t *testing.T) {
},
}

if err = downloader.PutConfig(storeCfg); err != nil {
if err := downloader.PutConfig(storeCfg); err != nil {
t.Fatal(err)
}

Expand Down

0 comments on commit 717134b

Please sign in to comment.