Skip to content

Commit

Permalink
feat: support download directory white list (GopeedLab#752)
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie authored Sep 20, 2024
1 parent d375046 commit 2dc7048
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 19 deletions.
8 changes: 0 additions & 8 deletions pkg/base/error.go

This file was deleted.

14 changes: 14 additions & 0 deletions pkg/download/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,20 @@ func (d *Downloader) doCreate(f fetcher.Fetcher, opts *base.Options) (taskId str
opts.Path = storeConfig.DownloadDir
}

// if enable white download directory, check if the download directory is in the white list
if len(d.cfg.DownloadDirWhiteList) > 0 {
inWhiteList := false
for _, dir := range d.cfg.DownloadDirWhiteList {
if match, err := filepath.Match(dir, opts.Path); match && err == nil {
inWhiteList = true
break
}
}
if !inWhiteList {
return "", errors.New("download directory is not in white list")
}
}

fm, err := d.parseFm(f.Meta().Req.URL)
if err != nil {
return
Expand Down
40 changes: 39 additions & 1 deletion pkg/download/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,45 @@ func TestDownloader_Create(t *testing.T) {
want := test.FileMd5(test.BuildFile)
got := test.FileMd5(test.DownloadFile)
if want != got {
t.Errorf("Download() got = %v, want %v", got, want)
t.Errorf("Downloader_Create() got = %v, want %v", got, want)
}
}

func TestDownloader_CreateNotInWhite(t *testing.T) {
listener := test.StartTestFileServer()
defer listener.Close()

downloader := NewDownloader(&DownloaderConfig{
DownloadDirWhiteList: []string{"./downloads"},
})
if err := downloader.Setup(); err != nil {
t.Fatal(err)
}
defer downloader.Clear()
req := &base.Request{
URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
}
rr, err := downloader.Resolve(req)
if err != nil {
t.Fatal(err)
}

var wg sync.WaitGroup
wg.Add(1)
downloader.Listener(func(event *Event) {
if event.Key == EventKeyDone {
wg.Done()
}
})
_, err = downloader.Create(rr.ID, &base.Options{
Path: test.Dir,
Name: test.DownloadName,
Extra: http.OptsExtra{
Connections: 4,
},
})
if !strings.Contains(err.Error(), "white") {
t.Errorf("TestDownloader_CreateNotInWhite() got = %v, want %v", err.Error(), "not in white list")
}
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/download/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ type DownloaderConfig struct {
Controller *controller.Controller
FetchManagers []fetcher.FetcherManager

RefreshInterval int `json:"refreshInterval"` // RefreshInterval time duration to refresh task progress(ms)
Storage Storage
StorageDir string
RefreshInterval int `json:"refreshInterval"` // RefreshInterval time duration to refresh task progress(ms)
Storage Storage
StorageDir string
DownloadDirWhiteList []string `json:"downloadDirWhiteList"`

ProductionMode bool

Expand Down
15 changes: 8 additions & 7 deletions pkg/rest/model/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ const (
)

type StartConfig struct {
Network string `json:"network"`
Address string `json:"address"`
RefreshInterval int `json:"refreshInterval"`
Storage Storage `json:"storage"`
StorageDir string `json:"storageDir"`
ApiToken string `json:"apiToken"`
DownloadConfig *base.DownloaderStoreConfig `json:"downloadConfig"`
Network string `json:"network"`
Address string `json:"address"`
RefreshInterval int `json:"refreshInterval"`
Storage Storage `json:"storage"`
StorageDir string `json:"storageDir"`
WhiteDownloadDirs []string `json:"whiteDownloadDirs"`
ApiToken string `json:"apiToken"`
DownloadConfig *base.DownloaderStoreConfig `json:"downloadConfig"`

ProductionMode bool

Expand Down

0 comments on commit 2dc7048

Please sign in to comment.