mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-09 10:00:27 +00:00
feat: support custom tracker url (#59)
This commit is contained in:
1 parent
9ef3405e43
commit
691c2f496e
71 files changed
+2209
-891
No files matched your search
@@ -0,0 +1,8 @@
|
||||
package base
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
NotFound = errors.New("not found")
|
||||
BadParams = errors.New("bad params")
|
||||
)
|
||||
+37
-5
@@ -1,11 +1,13 @@
|
||||
package base
|
||||
|
||||
import "github.com/monkeyWie/gopeed/pkg/util"
|
||||
|
||||
// 下载请求
|
||||
type Request struct {
|
||||
// 下载链接
|
||||
URL string `json:"url"`
|
||||
// 附加信息
|
||||
Extra interface{} `json:"extra"`
|
||||
Extra any `json:"extra"`
|
||||
}
|
||||
|
||||
// 资源信息
|
||||
@@ -19,8 +21,8 @@ type Resource struct {
|
||||
Range bool `json:"range"`
|
||||
// 资源所包含的文件列表
|
||||
Files []*FileInfo `json:"files"`
|
||||
// 附加信息
|
||||
Extra interface{} `json:"extra"`
|
||||
// 资源hash值
|
||||
Hash string `json:"hash"`
|
||||
}
|
||||
|
||||
type FileInfo struct {
|
||||
@@ -35,8 +37,38 @@ type Options struct {
|
||||
Name string `json:"name"`
|
||||
// 保存目录
|
||||
Path string `json:"path"`
|
||||
// 并发连接数
|
||||
Connections int `json:"connections"`
|
||||
// 选择下载的文件下标列表
|
||||
SelectFiles []int `json:"selectFiles"`
|
||||
// 附加信息
|
||||
Extra any `json:"extra"`
|
||||
}
|
||||
|
||||
func ParseReqExtra[E any](req *Request) error {
|
||||
if req.Extra == nil {
|
||||
return nil
|
||||
}
|
||||
if _, ok := req.Extra.(*E); ok {
|
||||
return nil
|
||||
}
|
||||
var t E
|
||||
if err := util.MapToStruct(req.Extra, &t); err != nil {
|
||||
return err
|
||||
}
|
||||
req.Extra = &t
|
||||
return nil
|
||||
}
|
||||
|
||||
func ParseOptsExtra[E any](opts *Options) error {
|
||||
if opts.Extra == nil {
|
||||
return nil
|
||||
}
|
||||
if _, ok := opts.Extra.(*E); ok {
|
||||
return nil
|
||||
}
|
||||
var t E
|
||||
if err := util.MapToStruct(opts.Extra, &t); err != nil {
|
||||
return err
|
||||
}
|
||||
opts.Extra = &t
|
||||
return nil
|
||||
}
|
||||
+45
-41
@@ -4,8 +4,6 @@ import (
|
||||
"errors"
|
||||
"github.com/monkeyWie/gopeed/internal/controller"
|
||||
"github.com/monkeyWie/gopeed/internal/fetcher"
|
||||
"github.com/monkeyWie/gopeed/internal/protocol/bt"
|
||||
"github.com/monkeyWie/gopeed/internal/protocol/http"
|
||||
"github.com/monkeyWie/gopeed/pkg/base"
|
||||
"github.com/monkeyWie/gopeed/pkg/util"
|
||||
"strings"
|
||||
@@ -32,7 +30,6 @@ type Progress struct {
|
||||
}
|
||||
|
||||
type Downloader struct {
|
||||
controller *controller.DefaultController
|
||||
fetchBuilders map[string]fetcher.FetcherBuilder
|
||||
storage Storage
|
||||
tasks []*Task
|
||||
@@ -43,34 +40,6 @@ type Downloader struct {
|
||||
closed atomic.Bool
|
||||
}
|
||||
|
||||
type DownloaderConfig struct {
|
||||
Controller *controller.DefaultController
|
||||
FetchBuilders []fetcher.FetcherBuilder
|
||||
Storage Storage
|
||||
|
||||
// RefreshInterval time duration to refresh task progress(ms)
|
||||
RefreshInterval int
|
||||
}
|
||||
|
||||
func (cfg *DownloaderConfig) Init() *DownloaderConfig {
|
||||
if cfg.Controller == nil {
|
||||
cfg.Controller = controller.NewController()
|
||||
}
|
||||
if len(cfg.FetchBuilders) == 0 {
|
||||
cfg.FetchBuilders = []fetcher.FetcherBuilder{
|
||||
new(http.FetcherBuilder),
|
||||
new(bt.FetcherBuilder),
|
||||
}
|
||||
}
|
||||
if cfg.Storage == nil {
|
||||
cfg.Storage = NewMemStorage()
|
||||
}
|
||||
if cfg.RefreshInterval == 0 {
|
||||
cfg.RefreshInterval = 1000
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
func NewDownloader(cfg *DownloaderConfig) *Downloader {
|
||||
if cfg == nil {
|
||||
cfg = &DownloaderConfig{}
|
||||
@@ -78,7 +47,6 @@ func NewDownloader(cfg *DownloaderConfig) *Downloader {
|
||||
cfg.Init()
|
||||
|
||||
d := &Downloader{
|
||||
controller: cfg.Controller,
|
||||
fetchBuilders: make(map[string]fetcher.FetcherBuilder),
|
||||
refreshInterval: cfg.RefreshInterval,
|
||||
storage: cfg.Storage,
|
||||
@@ -166,12 +134,20 @@ func (d *Downloader) buildFetcher(url string) (fetcher.FetcherBuilder, fetcher.F
|
||||
}
|
||||
if ok {
|
||||
fetcher := fetchBuilder.Build()
|
||||
fetcher.Setup(d.controller)
|
||||
d.setupFetcher(fetcher)
|
||||
return fetchBuilder, fetcher, nil
|
||||
}
|
||||
return nil, nil, errors.New("unsupported protocol")
|
||||
}
|
||||
|
||||
func (d *Downloader) setupFetcher(fetcher fetcher.Fetcher) {
|
||||
ctl := controller.NewController()
|
||||
ctl.GetConfig = func(v any) (bool, error) {
|
||||
return d.getProtocolConfig(fetcher.Name(), v)
|
||||
}
|
||||
fetcher.Setup(ctl)
|
||||
}
|
||||
|
||||
func (d *Downloader) Resolve(req *base.Request) (*base.Resource, error) {
|
||||
_, fetcher, err := d.buildFetcher(req.URL)
|
||||
if err != nil {
|
||||
@@ -189,15 +165,21 @@ func (d *Downloader) Create(res *base.Resource, opts *base.Options) (taskId stri
|
||||
if opts == nil {
|
||||
opts = &base.Options{}
|
||||
}
|
||||
if !res.Range || opts.Connections < 1 {
|
||||
opts.Connections = 1
|
||||
}
|
||||
if len(opts.SelectFiles) == 0 {
|
||||
opts.SelectFiles = make([]int, 0)
|
||||
for i := range res.Files {
|
||||
opts.SelectFiles = append(opts.SelectFiles, i)
|
||||
}
|
||||
}
|
||||
if opts.Path == "" {
|
||||
exist, storeConfig, err := d.GetConfig()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if exist {
|
||||
opts.Path = storeConfig.DownloadDir
|
||||
}
|
||||
}
|
||||
|
||||
fetcherBuilder, fetcher, err := d.buildFetcher(res.Req.URL)
|
||||
if err != nil {
|
||||
@@ -380,14 +362,36 @@ func (d *Downloader) GetTasks() []*Task {
|
||||
return d.tasks
|
||||
}
|
||||
|
||||
func (d *Downloader) GetConfig(v any) (bool, error) {
|
||||
return d.storage.Get(bucketConfig, "config", v)
|
||||
func (d *Downloader) GetConfig() (bool, *DownloaderStoreConfig, error) {
|
||||
var cfg DownloaderStoreConfig
|
||||
exist, err := d.storage.Get(bucketConfig, "config", &cfg)
|
||||
if err != nil {
|
||||
return false, nil, err
|
||||
}
|
||||
return exist, &cfg, nil
|
||||
}
|
||||
|
||||
func (d *Downloader) PutConfig(v any) error {
|
||||
func (d *Downloader) PutConfig(v *DownloaderStoreConfig) error {
|
||||
return d.storage.Put(bucketConfig, "config", v)
|
||||
}
|
||||
|
||||
func (d *Downloader) getProtocolConfig(name string, v any) (bool, error) {
|
||||
exist, cfg, err := d.GetConfig()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if !exist {
|
||||
return false, nil
|
||||
}
|
||||
if cfg.ProtocolConfig == nil || cfg.ProtocolConfig[name] == nil {
|
||||
return false, nil
|
||||
}
|
||||
if err := util.MapToStruct(cfg.ProtocolConfig[name], v); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (d *Downloader) watch(task *Task) {
|
||||
err := task.fetcher.Wait()
|
||||
if err != nil {
|
||||
@@ -435,9 +439,9 @@ func (d *Downloader) restoreFetcher(task *Task) error {
|
||||
}
|
||||
if task.fetcher == nil {
|
||||
task.fetcher = fetcherBuilder.Build()
|
||||
task.fetcher.Create(task.Res, task.Opts)
|
||||
}
|
||||
task.fetcher.Setup(d.controller)
|
||||
d.setupFetcher(task.fetcher)
|
||||
task.fetcher.Create(task.Res, task.Opts)
|
||||
go d.watch(task)
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -3,6 +3,7 @@ package download
|
||||
import (
|
||||
"github.com/monkeyWie/gopeed/internal/test"
|
||||
"github.com/monkeyWie/gopeed/pkg/base"
|
||||
"github.com/monkeyWie/gopeed/pkg/protocol/http"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -68,9 +69,11 @@ func TestDownloader_Create(t *testing.T) {
|
||||
}
|
||||
})
|
||||
_, err = downloader.Create(res, &base.Options{
|
||||
Path: test.Dir,
|
||||
Name: test.DownloadName,
|
||||
Connections: 4,
|
||||
Path: test.Dir,
|
||||
Name: test.DownloadName,
|
||||
Extra: http.OptsExtra{
|
||||
Connections: 4,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -103,9 +106,11 @@ func TestDownloader_StoreAndRestore(t *testing.T) {
|
||||
}
|
||||
|
||||
id, err := downloader.Create(res, &base.Options{
|
||||
Path: test.Dir,
|
||||
Name: test.DownloadName,
|
||||
Connections: 4,
|
||||
Path: test.Dir,
|
||||
Name: test.DownloadName,
|
||||
Extra: http.OptsExtra{
|
||||
Connections: 4,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -148,3 +153,88 @@ func TestDownloader_StoreAndRestore(t *testing.T) {
|
||||
|
||||
downloader.Clear()
|
||||
}
|
||||
|
||||
func TestDownloader_Handle(t *testing.T) {
|
||||
downloader := NewDownloader(nil)
|
||||
if err := downloader.Setup(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer downloader.Clear()
|
||||
|
||||
ret, err := downloader.Handle("bt", "resolve", "https://raw.githubusercontent.com/XIU2/TrackersListCollection/master/best.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
list, ok := ret.([][]string)
|
||||
if !ok {
|
||||
t.Errorf("Handle() got = %T, want %v", ret, "[][]string")
|
||||
}
|
||||
if len(list) == 0 {
|
||||
t.Errorf("Handle() got = %v, want %v", len(list), ">0")
|
||||
}
|
||||
|
||||
_, err = downloader.Handle("bt", "no", nil)
|
||||
if err != base.NotFound {
|
||||
t.Errorf("Handle() got = %v, want %v", err, base.NotFound)
|
||||
}
|
||||
|
||||
_, err = downloader.Handle("http", "no", nil)
|
||||
if err != base.NotFound {
|
||||
t.Errorf("Handle() got = %v, want %v", err, base.NotFound)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDownloader_Protocol_Config(t *testing.T) {
|
||||
downloader := NewDownloader(nil)
|
||||
if err := downloader.Setup(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer downloader.Clear()
|
||||
|
||||
var httpCfg map[string]any
|
||||
exits, err := downloader.getProtocolConfig("http", &httpCfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if exits {
|
||||
t.Errorf("getProtocolConfig() got = %v, want %v", exits, false)
|
||||
}
|
||||
|
||||
storeCfg := &DownloaderStoreConfig{
|
||||
RefreshInterval: 500,
|
||||
DownloadDir: "./downloads",
|
||||
ProtocolConfig: map[string]any{
|
||||
"http": map[string]any{
|
||||
"connections": 4,
|
||||
},
|
||||
"bt": map[string]any{
|
||||
"trackerSubscribeUrls": []string{
|
||||
"https://raw.githubusercontent.com/XIU2/TrackersListCollection/master/best.txt",
|
||||
},
|
||||
"trackers": []string{
|
||||
"udp://tracker.coppersurfer.tk:6969/announce",
|
||||
"udp://tracker.leechers-paradise.org:6969/announce",
|
||||
},
|
||||
},
|
||||
},
|
||||
Extra: map[string]any{
|
||||
"theme": "dark",
|
||||
},
|
||||
}
|
||||
|
||||
if err := downloader.PutConfig(storeCfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
exits, newStoreCfg, err := downloader.GetConfig()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !exits {
|
||||
t.Errorf("GetConfig() got = %v, want %v", exits, true)
|
||||
}
|
||||
|
||||
if !test.JsonEqual(storeCfg, newStoreCfg) {
|
||||
t.Errorf("GetConfig() got = %v, want %v", test.ToJson(storeCfg), test.ToJson(newStoreCfg))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package download
|
||||
|
||||
import (
|
||||
"github.com/monkeyWie/gopeed/internal/controller"
|
||||
"github.com/monkeyWie/gopeed/internal/fetcher"
|
||||
"github.com/monkeyWie/gopeed/internal/protocol/bt"
|
||||
"github.com/monkeyWie/gopeed/internal/protocol/http"
|
||||
)
|
||||
|
||||
type DownloaderConfig struct {
|
||||
Controller *controller.Controller
|
||||
FetchBuilders []fetcher.FetcherBuilder
|
||||
Storage Storage
|
||||
|
||||
*DownloaderStoreConfig
|
||||
}
|
||||
|
||||
func (cfg *DownloaderConfig) Init() *DownloaderConfig {
|
||||
if cfg.Controller == nil {
|
||||
cfg.Controller = controller.NewController()
|
||||
}
|
||||
if len(cfg.FetchBuilders) == 0 {
|
||||
cfg.FetchBuilders = []fetcher.FetcherBuilder{
|
||||
new(http.FetcherBuilder),
|
||||
new(bt.FetcherBuilder),
|
||||
}
|
||||
}
|
||||
if cfg.Storage == nil {
|
||||
cfg.Storage = NewMemStorage()
|
||||
}
|
||||
|
||||
if cfg.DownloaderStoreConfig == nil {
|
||||
cfg.DownloaderStoreConfig = &DownloaderStoreConfig{}
|
||||
}
|
||||
|
||||
if cfg.RefreshInterval == 0 {
|
||||
cfg.RefreshInterval = 1000
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
// DownloaderStoreConfig is the config that can restore the downloader.
|
||||
type DownloaderStoreConfig struct {
|
||||
RefreshInterval int `json:"refreshInterval"` // RefreshInterval time duration to refresh task progress(ms)
|
||||
DownloadDir string `json:"downloadDir"` // DownloadDir is the directory to save the downloaded files
|
||||
ProtocolConfig map[string]any `json:"protocolConfig"` // ProtocolConfig is special config for each protocol
|
||||
Extra map[string]any `json:"extra"` // Extra is the extra config
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package http
|
||||
|
||||
type ReqExtra struct {
|
||||
Method string `json:"method"`
|
||||
Header map[string]string `json:"header"`
|
||||
Body string `json:"body"`
|
||||
}
|
||||
|
||||
type OptsExtra struct {
|
||||
Connections int `json:"connections"`
|
||||
}
|
||||
+47
-11
@@ -1,12 +1,15 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/monkeyWie/gopeed/pkg/base"
|
||||
"github.com/monkeyWie/gopeed/pkg/download"
|
||||
"github.com/monkeyWie/gopeed/pkg/rest/model"
|
||||
"github.com/monkeyWie/gopeed/pkg/rest/util"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -25,12 +28,6 @@ func Resolve(w http.ResponseWriter, r *http.Request) {
|
||||
func CreateTask(w http.ResponseWriter, r *http.Request) {
|
||||
var req model.CreateTask
|
||||
if util.ReadJson(w, r, &req) {
|
||||
if req.Opts.Connections == 0 {
|
||||
req.Opts.Connections = getServerConfig().Connections
|
||||
}
|
||||
if req.Opts.Path == "" {
|
||||
req.Opts.Path = getServerConfig().DownloadDir
|
||||
}
|
||||
taskId, err := Downloader.Create(req.Res, req.Opts)
|
||||
if err != nil {
|
||||
util.WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
||||
@@ -106,7 +103,7 @@ func GetConfig(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func PutConfig(w http.ResponseWriter, r *http.Request) {
|
||||
var cfg model.ServerConfig
|
||||
var cfg download.DownloaderStoreConfig
|
||||
if util.ReadJson(w, r, &cfg) {
|
||||
if err := Downloader.PutConfig(&cfg); err != nil {
|
||||
util.WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
||||
@@ -116,8 +113,47 @@ func PutConfig(w http.ResponseWriter, r *http.Request) {
|
||||
util.WriteJsonOk(w, nil)
|
||||
}
|
||||
|
||||
func getServerConfig() *model.ServerConfig {
|
||||
var cfg model.ServerConfig
|
||||
Downloader.GetConfig(&cfg)
|
||||
return &cfg
|
||||
func DoProxy(w http.ResponseWriter, r *http.Request) {
|
||||
target := r.Header.Get("X-Target-Uri")
|
||||
if target == "" {
|
||||
util.WriteJson(w, http.StatusBadRequest, model.NewResultWithMsg("header is required: X-Target-Uri"))
|
||||
return
|
||||
}
|
||||
targetUrl, err := url.Parse(target)
|
||||
if err != nil {
|
||||
util.WriteJson(w, http.StatusBadRequest, model.NewResultWithMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
r.RequestURI = ""
|
||||
r.URL = targetUrl
|
||||
r.Host = targetUrl.Host
|
||||
resp, err := http.DefaultClient.Do(r)
|
||||
if err != nil {
|
||||
util.WriteJson(w, http.StatusBadRequest, model.NewResultWithMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
w.WriteHeader(resp.StatusCode)
|
||||
for k, vv := range resp.Header {
|
||||
for _, v := range vv {
|
||||
w.Header().Add(k, v)
|
||||
}
|
||||
}
|
||||
var reader io.ReadCloser
|
||||
switch resp.Header.Get("Content-Encoding") {
|
||||
case "gzip":
|
||||
reader, err = gzip.NewReader(resp.Body)
|
||||
defer reader.Close()
|
||||
default:
|
||||
reader = resp.Body
|
||||
}
|
||||
if _, err := io.Copy(w, reader); err != nil {
|
||||
util.WriteJson(w, http.StatusBadRequest, model.NewResultWithMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func getServerConfig() *download.DownloaderStoreConfig {
|
||||
_, cfg, _ := Downloader.GetConfig()
|
||||
return cfg
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package rest
|
||||
|
||||
type Config struct {
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package model
|
||||
|
||||
type Command struct {
|
||||
Protocol string `json:"protocol"`
|
||||
Action string `json:"action"`
|
||||
Params any `json:"params"`
|
||||
}
|
||||
@@ -15,6 +15,7 @@ type StartConfig struct {
|
||||
Storage Storage `json:"storage"`
|
||||
StorageDir string `json:"storageDir"`
|
||||
RefreshInterval int `json:"refreshInterval"`
|
||||
ApiToken string `json:"apiToken"`
|
||||
|
||||
WebEnable bool
|
||||
WebFS fs.FS
|
||||
@@ -24,6 +25,9 @@ func (cfg *StartConfig) Init() *StartConfig {
|
||||
if cfg.Network == "" {
|
||||
cfg.Network = "tcp"
|
||||
}
|
||||
if cfg.Address == "" {
|
||||
cfg.Address = "127.0.0.1:0"
|
||||
}
|
||||
if cfg.Storage == "" {
|
||||
cfg.Storage = StorageBolt
|
||||
}
|
||||
@@ -32,27 +36,3 @@ func (cfg *StartConfig) Init() *StartConfig {
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
// ServerConfig is present in the database
|
||||
type ServerConfig struct {
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
|
||||
Connections int `json:"connections"`
|
||||
DownloadDir string `json:"downloadDir"`
|
||||
|
||||
Extra map[string]any `json:"extra"`
|
||||
}
|
||||
|
||||
func (cfg *ServerConfig) Init() *ServerConfig {
|
||||
if cfg.Host == "" {
|
||||
cfg.Host = "127.0.0.1"
|
||||
}
|
||||
if cfg.Port < 0 {
|
||||
cfg.Port = 0
|
||||
}
|
||||
if cfg.Connections <= 0 {
|
||||
cfg.Connections = 16
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
+20
-30
@@ -6,10 +6,10 @@ import (
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/monkeyWie/gopeed/pkg/download"
|
||||
"github.com/monkeyWie/gopeed/pkg/rest/model"
|
||||
restUtil "github.com/monkeyWie/gopeed/pkg/rest/util"
|
||||
"github.com/monkeyWie/gopeed/pkg/util"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -63,22 +63,18 @@ func BuildServer(startCfg *model.StartConfig) (*http.Server, net.Listener, error
|
||||
} else {
|
||||
downloadCfg.Storage = download.NewMemStorage()
|
||||
}
|
||||
downloadCfg.Init()
|
||||
downloadCfg.RefreshInterval = startCfg.RefreshInterval
|
||||
Downloader = download.NewDownloader(downloadCfg.Init())
|
||||
Downloader = download.NewDownloader(downloadCfg)
|
||||
if err := Downloader.Setup(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
serverConfig := getAndPutServerConfig(startCfg)
|
||||
var address string
|
||||
if startCfg.Network == "unix" {
|
||||
address = startCfg.Address
|
||||
util.SafeRemove(address)
|
||||
} else {
|
||||
address = net.JoinHostPort(serverConfig.Host, strconv.Itoa(serverConfig.Port))
|
||||
util.SafeRemove(startCfg.Address)
|
||||
}
|
||||
|
||||
listener, err := net.Listen(startCfg.Network, address)
|
||||
listener, err := net.Listen(startCfg.Network, startCfg.Address)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@@ -93,33 +89,27 @@ func BuildServer(startCfg *model.StartConfig) (*http.Server, net.Listener, error
|
||||
r.Methods(http.MethodGet).Path("/api/v1/tasks").HandlerFunc(GetTasks)
|
||||
r.Methods(http.MethodGet).Path("/api/v1/config").HandlerFunc(GetConfig)
|
||||
r.Methods(http.MethodPut).Path("/api/v1/config").HandlerFunc(PutConfig)
|
||||
r.Path("/api/v1/proxy").HandlerFunc(DoProxy)
|
||||
if startCfg.WebEnable {
|
||||
r.PathPrefix("/").Handler(http.FileServer(http.FS(startCfg.WebFS)))
|
||||
}
|
||||
|
||||
if startCfg.ApiToken != "" {
|
||||
r.Use(func(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("X-Api-Token") != startCfg.ApiToken {
|
||||
restUtil.WriteJson(w, http.StatusUnauthorized, model.NewResultWithMsg("invalid token"))
|
||||
return
|
||||
}
|
||||
h.ServeHTTP(w, r)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
srv = &http.Server{Handler: handlers.CORS(
|
||||
handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"}),
|
||||
handlers.AllowedMethods([]string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}),
|
||||
handlers.AllowedHeaders([]string{"Content-Type", "X-Api-Token", "X-Target-Uri"}),
|
||||
handlers.AllowedMethods([]string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}),
|
||||
handlers.AllowedOrigins([]string{"*"}),
|
||||
)(r)}
|
||||
return srv, listener, nil
|
||||
}
|
||||
|
||||
func getAndPutServerConfig(startCfg *model.StartConfig) *model.ServerConfig {
|
||||
var serverConfig model.ServerConfig
|
||||
exist, err := Downloader.GetConfig(&serverConfig)
|
||||
if err != nil {
|
||||
// TODO log
|
||||
}
|
||||
// first start
|
||||
if !exist {
|
||||
if startCfg.Network == "tcp" {
|
||||
host, port, _ := net.SplitHostPort(startCfg.Address)
|
||||
serverConfig.Host = host
|
||||
serverConfig.Port, _ = strconv.Atoi(port)
|
||||
}
|
||||
serverConfig.Init()
|
||||
Downloader.PutConfig(serverConfig)
|
||||
}
|
||||
return &serverConfig
|
||||
}
|
||||
+99
-30
@@ -2,6 +2,7 @@ package rest
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -22,7 +23,15 @@ import (
|
||||
var (
|
||||
restPort int
|
||||
|
||||
taskReq = &base.Request{}
|
||||
taskReq = &base.Request{
|
||||
Extra: map[string]any{
|
||||
"method": "",
|
||||
"header": map[string]string{
|
||||
"User-Agent": "gopeed",
|
||||
},
|
||||
"body": "",
|
||||
},
|
||||
}
|
||||
taskRes = &base.Resource{
|
||||
Name: test.BuildName,
|
||||
Req: taskReq,
|
||||
@@ -39,9 +48,11 @@ var (
|
||||
createReq = &model.CreateTask{
|
||||
Res: taskRes,
|
||||
Opts: &base.Options{
|
||||
Path: test.Dir,
|
||||
Name: test.DownloadName,
|
||||
Connections: 2,
|
||||
Path: test.Dir,
|
||||
Name: test.DownloadName,
|
||||
Extra: map[string]any{
|
||||
"connections": 2,
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
@@ -49,8 +60,8 @@ var (
|
||||
func TestResolve(t *testing.T) {
|
||||
doTest(func() {
|
||||
resp := httpRequestCheckOk[*base.Resource](http.MethodPost, "/api/v1/resolve", taskReq)
|
||||
if !reflect.DeepEqual(taskRes, resp) {
|
||||
t.Errorf("Resolve() got = %v, want %v", resp, taskRes)
|
||||
if !test.JsonEqual(taskRes, resp) {
|
||||
t.Errorf("Resolve() got = %v, want %v", test.ToJson(resp), test.ToJson(taskRes))
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -187,26 +198,68 @@ func TestGetTasks(t *testing.T) {
|
||||
|
||||
func TestGetAndPutConfig(t *testing.T) {
|
||||
doTest(func() {
|
||||
cfg := httpRequestCheckOk[*model.ServerConfig](http.MethodGet, "/api/v1/config", nil)
|
||||
|
||||
cfg.Port = 8888
|
||||
cfg.Connections = 32
|
||||
cfg := httpRequestCheckOk[*download.DownloaderStoreConfig](http.MethodGet, "/api/v1/config", nil)
|
||||
cfg.DownloadDir = "./download"
|
||||
cfg.Extra = map[string]any{
|
||||
"serverConfig": &Config{
|
||||
Host: "127.0.0.1",
|
||||
Port: 8080,
|
||||
},
|
||||
"theme": "dark",
|
||||
}
|
||||
httpRequestCheckOk[any](http.MethodPut, "/api/v1/config", cfg)
|
||||
|
||||
newCfg := httpRequestCheckOk[*model.ServerConfig](http.MethodGet, "/api/v1/config", nil)
|
||||
if !reflect.DeepEqual(newCfg, cfg) {
|
||||
t.Errorf("GetAndPutConfig() got = %v, want %v", newCfg, cfg)
|
||||
newCfg := httpRequestCheckOk[*download.DownloaderStoreConfig](http.MethodGet, "/api/v1/config", nil)
|
||||
if !test.JsonEqual(cfg, newCfg) {
|
||||
t.Errorf("GetAndPutConfig() got = %v, want %v", test.ToJson(newCfg), test.ToJson(cfg))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestDoProxy(t *testing.T) {
|
||||
doTest(func() {
|
||||
code, respBody := doHttpRequest0(http.MethodGet, "/api/v1/proxy", map[string]string{
|
||||
"X-Target-Uri": "https://github.com/monkeyWie/gopeed/raw/695da7ea87d2b455552b709d3cb4d7879484d4d1/README.md",
|
||||
}, nil)
|
||||
if code != http.StatusOK {
|
||||
t.Errorf("DoProxy() got = %v, want %v", code, http.StatusOK)
|
||||
}
|
||||
want := "4ee193b676f1ebb2ad810e016350d52a"
|
||||
got := fmt.Sprintf("%x", md5.Sum(respBody))
|
||||
if got != want {
|
||||
t.Errorf("DoProxy() got = %v, want %v", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestAuthorization(t *testing.T) {
|
||||
var cfg = &model.StartConfig{}
|
||||
cfg.Init()
|
||||
cfg.ApiToken = "123456"
|
||||
fileListener := doStart(cfg)
|
||||
defer func() {
|
||||
if err := fileListener.Close(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
Stop()
|
||||
}()
|
||||
|
||||
code, _ := doHttpRequest[any](http.MethodGet, "/api/v1/config", nil, nil)
|
||||
checkCode(code, http.StatusUnauthorized)
|
||||
|
||||
code, _ = doHttpRequest[any](http.MethodGet, "/api/v1/config", map[string]string{
|
||||
"X-Api-Token": cfg.ApiToken,
|
||||
}, nil)
|
||||
checkCode(code, http.StatusOK)
|
||||
|
||||
}
|
||||
|
||||
func doTest(handler func()) {
|
||||
testFunc := func(storage model.Storage) {
|
||||
fileListener := doStart(storage)
|
||||
var cfg = &model.StartConfig{}
|
||||
cfg.Init()
|
||||
cfg.Storage = storage
|
||||
fileListener := doStart(cfg)
|
||||
defer func() {
|
||||
if err := fileListener.Close(); err != nil {
|
||||
panic(err)
|
||||
@@ -221,10 +274,8 @@ func doTest(handler func()) {
|
||||
testFunc(model.StorageBolt)
|
||||
}
|
||||
|
||||
func doStart(storage model.Storage) net.Listener {
|
||||
port, err := Start((&model.StartConfig{
|
||||
Storage: storage,
|
||||
}).Init())
|
||||
func doStart(cfg *model.StartConfig) net.Listener {
|
||||
port, err := Start(cfg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -232,32 +283,50 @@ func doStart(storage model.Storage) net.Listener {
|
||||
return test.StartTestFileServer()
|
||||
}
|
||||
|
||||
func httpRequest[T any](method string, path string, req any) (int, *model.Result[T]) {
|
||||
var body io.Reader
|
||||
if req != nil {
|
||||
buf, _ := json.Marshal(req)
|
||||
body = bytes.NewBuffer(buf)
|
||||
func doHttpRequest0(method string, path string, headers map[string]string, body any) (int, []byte) {
|
||||
var reader io.Reader
|
||||
if body != nil {
|
||||
buf, _ := json.Marshal(body)
|
||||
reader = bytes.NewBuffer(buf)
|
||||
}
|
||||
|
||||
request, err := http.NewRequest(method, fmt.Sprintf("http://127.0.0.1:%d%s", restPort, path), body)
|
||||
request, err := http.NewRequest(method, fmt.Sprintf("http://127.0.0.1:%d%s", restPort, path), reader)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if headers != nil {
|
||||
for k, v := range headers {
|
||||
request.Header.Set(k, v)
|
||||
}
|
||||
}
|
||||
response, err := http.DefaultClient.Do(request)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
var r model.Result[T]
|
||||
if err := json.NewDecoder(response.Body).Decode(&r); err != nil {
|
||||
respBody, err := io.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return response.StatusCode, &r
|
||||
return response.StatusCode, respBody
|
||||
}
|
||||
|
||||
func httpRequestCheckOk[T any](method string, path string, req any) T {
|
||||
code, result := httpRequest[T](method, path, req)
|
||||
func doHttpRequest[T any](method string, path string, headers map[string]string, body any) (int, *model.Result[T]) {
|
||||
code, respBody := doHttpRequest0(method, path, headers, body)
|
||||
|
||||
var r model.Result[T]
|
||||
if err := json.Unmarshal(respBody, &r); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return code, &r
|
||||
}
|
||||
|
||||
func httpRequest[T any](method string, path string, body any) (int, *model.Result[T]) {
|
||||
return doHttpRequest[T](method, path, nil, body)
|
||||
}
|
||||
|
||||
func httpRequestCheckOk[T any](method string, path string, body any) T {
|
||||
code, result := httpRequest[T](method, path, body)
|
||||
checkOk(code)
|
||||
return result.Data
|
||||
}
|
||||
|
||||
@@ -8,18 +8,18 @@ import (
|
||||
|
||||
func ReadJson(w http.ResponseWriter, r *http.Request, v any) bool {
|
||||
if err := json.NewDecoder(r.Body).Decode(v); err != nil {
|
||||
WriteJson(w, 500, model.NewResultWithMsg(err.Error()))
|
||||
WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func WriteJsonOk(w http.ResponseWriter, v any) {
|
||||
WriteJson(w, 200, v)
|
||||
WriteJson(w, http.StatusOK, v)
|
||||
}
|
||||
|
||||
func WriteJson(w http.ResponseWriter, code int, v any) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(code)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package util
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
func MapToStruct(s any, v any) error {
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
b, err := json.Marshal(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(b, v)
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package util
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestParseSchema(t *testing.T) {
|
||||
type args struct {
|
||||
url string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "http",
|
||||
args: args{
|
||||
url: "http://www.google.com",
|
||||
},
|
||||
want: "HTTP",
|
||||
},
|
||||
{
|
||||
name: "https",
|
||||
args: args{
|
||||
url: "https://www.google.com",
|
||||
},
|
||||
want: "HTTPS",
|
||||
},
|
||||
{
|
||||
name: "file",
|
||||
args: args{
|
||||
url: "file:///home/bt.torrent",
|
||||
},
|
||||
want: "FILE",
|
||||
},
|
||||
{
|
||||
name: "file-no-scheme",
|
||||
args: args{
|
||||
url: "/home/bt.torrent",
|
||||
},
|
||||
want: "FILE",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := ParseSchema(tt.args.url); got != tt.want {
|
||||
t.Errorf("ParseSchema() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user