Skip to content

Commit

Permalink
refactor: package
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Mar 25, 2021
1 parent 359f28b commit ede6a98
Show file tree
Hide file tree
Showing 55 changed files with 276 additions and 234 deletions.
41 changes: 28 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
## gopeed
支持多协议(HTTP、BitTorrent)下载的客户端,提供命令行、RESTful API、WebSocket、Go类库方式来使用。

支持多协议(HTTP、BitTorrent)下载的客户端,提供命令行、RESTful API、WebSocket、Go 类库方式来使用。

## 安装

```sh
go install github.com/monkeyWie/gopeed-core
```

## 示例

```go

```

## TODO
- [x] HTTP下载实现
- [ ] BitTorrent下载实现
- [x] .torrent文件解析
- [x] tracker协议实现
- [x] peer wire protocol协议实现
- [ ] DHT协议实现
- [ ] 磁力链接支持
- [ ] uTP协议实现
- [ ] 下载接口抽象(不关心具体协议)

- [x] HTTP 下载实现
- [ ] BitTorrent 下载实现
- [x] .torrent 文件解析
- [x] tracker 协议实现
- [x] peer wire protocol 协议实现
- [ ] DHT 协议实现
- [ ] 磁力链接支持
- [ ] uTP 协议实现
- [x] 下载接口抽象(不关心具体协议)
- [ ] 支持自定义配置
- [ ] 限速功能实现
- [ ] 命令行工具提供
- [ ] RESTful服务提供
- [x] 命令行工具提供
- [ ] RESTful 服务提供

## 参与
由于项目目前还未定型,代码可能随时有大的调整,所以暂不接受PR,当然如果有什么好的想法可以在issue区提出来。

由于项目目前还未定型,代码可能随时有大的调整,所以暂不接受 PR,当然如果有什么好的想法可以在 issue 区提出来。
14 changes: 7 additions & 7 deletions cmd/cli/gopeed.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import (
"fmt"
"github.com/monkeyWie/gopeed-core/download"
"github.com/monkeyWie/gopeed-core/download/base"
"github.com/monkeyWie/gopeed-core/download/http"
"github.com/monkeyWie/gopeed-core/util"
"github.com/monkeyWie/gopeed-core/internal/protocol/http"
"github.com/monkeyWie/gopeed-core/pkg/base"
"github.com/monkeyWie/gopeed-core/pkg/download"
"github.com/monkeyWie/gopeed-core/pkg/util"
"path/filepath"
"sync"
)
Expand Down Expand Up @@ -33,11 +33,11 @@ func main() {

var wg sync.WaitGroup
wg.Add(1)
downloader.Listener(func(taskInfo *download.TaskInfo, eventKey base.EventKey) {
if eventKey == base.EventKeyProgress {
downloader.Listener(func(taskInfo *download.TaskInfo, eventKey download.EventKey) {
if eventKey == download.EventKeyProgress {
printProgress(taskInfo, "downloading...")
}
if eventKey == base.EventKeyDone {
if eventKey == download.EventKeyDone {
printProgress(taskInfo, "complete")
wg.Done()
}
Expand Down
5 changes: 0 additions & 5 deletions download/base/event.go

This file was deleted.

32 changes: 0 additions & 32 deletions download/base/fetcher.go

This file was deleted.

7 changes: 0 additions & 7 deletions download/http/model/extra.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package base
package controller

import (
"golang.org/x/net/proxy"
Expand All @@ -15,40 +15,6 @@ type Controller interface {
ContextDialer() (proxy.Dialer, error)
}

// 下载请求
type Request struct {
// 下载链接
URL string
// 附加信息
Extra interface{}
}

// 资源信息
type Resource struct {
Req *Request
// 资源总大小
TotalSize int64
// 是否支持断点下载
Range bool
// 资源所包含的文件列表
Files []*FileInfo
}

type Options struct {
// 保存文件名
Name string
// 保存目录
Path string
// 并发连接数
Connections int
}

type FileInfo struct {
Name string
Path string
Size int64
}

type DefaultController struct {
Files map[string]*os.File
}
Expand Down Expand Up @@ -142,15 +108,3 @@ func (d *DialerWarp) Dial(network, addr string) (c net.Conn, err error) {
}
return &ConnWarp{conn: conn}, nil
}

// 获取任务中各个文件的已下载字节数
type Progress []int64

// TotalDownloaded 获取任务总下载字节数
func (p Progress) TotalDownloaded() int64 {
total := int64(0)
for _, d := range p {
total += d
}
return total
}
49 changes: 49 additions & 0 deletions internal/fetcher/fetcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package fetcher

import (
"github.com/monkeyWie/gopeed-core/internal/controller"
"github.com/monkeyWie/gopeed-core/pkg/base"
)

// 对应协议的下载支持
type Fetcher interface {
Setup(ctl controller.Controller)
// 解析请求
Resolve(req *base.Request) (res *base.Resource, err error)
// 创建任务
Create(res *base.Resource, opts *base.Options) (err error)
Start() (err error)
Pause() (err error)
Continue() (err error)

// 获取任务各个文件下载进度
Progress() Progress
// 该方法会一直阻塞,直到任务下载结束
Wait() (err error)
}

type DefaultFetcher struct {
Ctl controller.Controller
DoneCh chan error
}

func (f *DefaultFetcher) Setup(ctl controller.Controller) {
f.Ctl = ctl
f.DoneCh = make(chan error, 1)
}

func (f *DefaultFetcher) Wait() (err error) {
return <-f.DoneCh
}

// 获取任务中各个文件的已下载字节数
type Progress []int64

// TotalDownloaded 获取任务总下载字节数
func (p Progress) TotalDownloaded() int64 {
total := int64(0)
for _, d := range p {
total += d
}
return total
}
28 changes: 14 additions & 14 deletions download/http/fetcher.go → internal/protocol/http/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"bytes"
"context"
"fmt"
"github.com/monkeyWie/gopeed-core/download/base"
"github.com/monkeyWie/gopeed-core/download/http/model"
"github.com/monkeyWie/gopeed-core/internal/fetcher"
"github.com/monkeyWie/gopeed-core/pkg/base"
"golang.org/x/sync/errgroup"
"io"
"io/ioutil"
Expand Down Expand Up @@ -34,13 +34,13 @@ func (re *RequestError) Error() string {
}

type Fetcher struct {
*base.DefaultFetcher
*fetcher.DefaultFetcher

res *base.Resource
opts *base.Options
status base.Status
clients []*http.Response
chunks []*model.Chunk
chunks []*Chunk

ctx context.Context
cancel context.CancelFunc
Expand All @@ -49,15 +49,15 @@ type Fetcher struct {

func NewFetcher() *Fetcher {
return &Fetcher{
DefaultFetcher: new(base.DefaultFetcher),
DefaultFetcher: new(fetcher.DefaultFetcher),
pauseCh: make(chan interface{}),
}
}

var protocols = []string{"HTTP", "HTTPS"}

func FetcherBuilder() ([]string, func() base.Fetcher) {
return protocols, func() base.Fetcher {
func FetcherBuilder() ([]string, func() fetcher.Fetcher) {
return protocols, func() fetcher.Fetcher {
return NewFetcher()
}
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func (f *Fetcher) Start() (err error) {
if f.res.Range {
// 每个连接平均需要下载的分块大小
chunkSize := f.res.TotalSize / int64(f.opts.Connections)
f.chunks = make([]*model.Chunk, f.opts.Connections)
f.chunks = make([]*Chunk, f.opts.Connections)
f.clients = make([]*http.Response, f.opts.Connections)
for i := 0; i < f.opts.Connections; i++ {
var (
Expand All @@ -163,14 +163,14 @@ func (f *Fetcher) Start() (err error) {
} else {
end = begin + chunkSize - 1
}
chunk := model.NewChunk(begin, end)
chunk := NewChunk(begin, end)
f.chunks[i] = chunk
}
} else {
// 只支持单连接下载
f.chunks = make([]*model.Chunk, 1)
f.chunks = make([]*Chunk, 1)
f.clients = make([]*http.Response, 1)
f.chunks[0] = model.NewChunk(0, 0)
f.chunks[0] = NewChunk(0, 0)
}
f.fetch()
return
Expand Down Expand Up @@ -200,8 +200,8 @@ func (f *Fetcher) Continue() (err error) {
return
}

func (f *Fetcher) Progress() base.Progress {
p := make(base.Progress, 0)
func (f *Fetcher) Progress() fetcher.Progress {
p := make(fetcher.Progress, 0)
if len(f.chunks) > 0 {
total := int64(0)
for _, chunk := range f.chunks {
Expand Down Expand Up @@ -361,7 +361,7 @@ func buildRequest(ctx context.Context, req *base.Request) (httpReq *http.Request
if req.Extra == nil {
method = http.MethodGet
} else {
extra := req.Extra.(model.Extra)
extra := req.Extra.(Extra)
if extra.Method != "" {
method = extra.Method
} else {
Expand Down
Loading

0 comments on commit ede6a98

Please sign in to comment.