mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-18 14:23:42 +00:00
feat: new ui (#1455)
This commit is contained in:
742 files changed
+59951
-5934
No files matched your search
@@ -133,6 +133,8 @@ type Options struct {
|
||||
Name string `json:"name"`
|
||||
// Download file path
|
||||
Path string `json:"path"`
|
||||
// Use this path as the default download directory after creation succeeds
|
||||
AsDefaultPath bool `json:"asDefaultPath,omitempty"`
|
||||
// Select file indexes to download
|
||||
SelectFiles []int `json:"selectFiles"`
|
||||
// Extra info for specific fetcher
|
||||
@@ -206,6 +208,7 @@ type DownloaderStoreConfig struct {
|
||||
Script *ScriptConfig `json:"script"` // Script is the script execution configuration
|
||||
AutoTorrent *AutoTorrentConfig `json:"autoTorrent"` // AutoTorrent is the auto torrent task creation configuration
|
||||
Archive *ArchiveConfig `json:"archive"` // Archive is the archive extraction configuration
|
||||
AutoStartTasks bool `json:"autoStartTasks"` // AutoStartTasks continues all unfinished tasks when the backend starts
|
||||
AutoDeleteMissingFileTasks bool `json:"autoDeleteMissingFileTasks"` // AutoDeleteMissingFileTasks enables automatic deletion of tasks with missing files
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,198 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/bits"
|
||||
)
|
||||
|
||||
const (
|
||||
// PieceMapEncoding identifies the one-bit wire format used by PieceMap.
|
||||
// One byte stores eight ordered pieces, least-significant bit first: piece
|
||||
// 0 uses bit 0, piece 1 uses bit 1, and so on. A set bit means the piece is
|
||||
// locally complete and verified; a clear bit means it is not complete.
|
||||
PieceMapEncoding = "bitset-v1"
|
||||
|
||||
piecesPerByte = 8
|
||||
)
|
||||
|
||||
var ErrPieceIndexOutOfRange = errors.New("piece index out of range")
|
||||
|
||||
// PieceMap stores the verified completion state of ordered pieces as a bitset.
|
||||
// It deliberately excludes scheduler states such as downloading, hashing and
|
||||
// failed: those states are transient and are not stable across pause/restart.
|
||||
//
|
||||
// Fields are private so PieceCount, CompletedPieces and packed bytes remain
|
||||
// consistent. Use Add, Delete, Update and Get for mutation, and Bytes when a
|
||||
// binary copy is required. JSON serializes Data as Base64.
|
||||
type PieceMap struct {
|
||||
pieceCount int
|
||||
pieceSize int64
|
||||
completedPieces int
|
||||
data []byte
|
||||
}
|
||||
|
||||
// NewPieceMap creates a map containing pieceCount incomplete pieces.
|
||||
func NewPieceMap(pieceCount int, pieceSize int64) *PieceMap {
|
||||
if pieceCount < 0 {
|
||||
pieceCount = 0
|
||||
}
|
||||
return &PieceMap{
|
||||
pieceCount: pieceCount,
|
||||
pieceSize: pieceSize,
|
||||
data: make([]byte, packedPieceByteCount(pieceCount)),
|
||||
}
|
||||
}
|
||||
|
||||
// NewPieceMapFromBytes restores a bitset-v1 map. Input bytes are copied and
|
||||
// unused high bits in the last byte are cleared.
|
||||
func NewPieceMapFromBytes(pieceCount int, pieceSize int64, data []byte) (*PieceMap, error) {
|
||||
if pieceCount < 0 {
|
||||
return nil, fmt.Errorf("%w: negative piece count", ErrPieceIndexOutOfRange)
|
||||
}
|
||||
expectedBytes := packedPieceByteCount(pieceCount)
|
||||
if len(data) != expectedBytes {
|
||||
return nil, fmt.Errorf("invalid piece data length: got %d, want %d", len(data), expectedBytes)
|
||||
}
|
||||
|
||||
pieceMap := &PieceMap{
|
||||
pieceCount: pieceCount,
|
||||
pieceSize: pieceSize,
|
||||
data: append([]byte(nil), data...),
|
||||
}
|
||||
pieceMap.clearUnusedBits()
|
||||
for _, value := range pieceMap.data {
|
||||
pieceMap.completedPieces += bits.OnesCount8(value)
|
||||
}
|
||||
return pieceMap, nil
|
||||
}
|
||||
|
||||
// Clone returns an independent ordered snapshot.
|
||||
func (m *PieceMap) Clone() *PieceMap {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
return &PieceMap{
|
||||
pieceCount: m.pieceCount,
|
||||
pieceSize: m.pieceSize,
|
||||
completedPieces: m.completedPieces,
|
||||
data: append([]byte(nil), m.data...),
|
||||
}
|
||||
}
|
||||
|
||||
// Add appends a piece while preserving all existing indexes.
|
||||
func (m *PieceMap) Add(completed bool) {
|
||||
if m.pieceCount%piecesPerByte == 0 {
|
||||
m.data = append(m.data, 0)
|
||||
}
|
||||
m.pieceCount++
|
||||
if completed {
|
||||
m.setCompleted(m.pieceCount-1, true)
|
||||
m.completedPieces++
|
||||
}
|
||||
}
|
||||
|
||||
// Delete removes a piece and shifts every following piece one index left.
|
||||
func (m *PieceMap) Delete(index int) error {
|
||||
if !m.validIndex(index) {
|
||||
return ErrPieceIndexOutOfRange
|
||||
}
|
||||
if m.completedAt(index) {
|
||||
m.completedPieces--
|
||||
}
|
||||
for current := index; current < m.pieceCount-1; current++ {
|
||||
m.setCompleted(current, m.completedAt(current+1))
|
||||
}
|
||||
m.pieceCount--
|
||||
m.data = m.data[:packedPieceByteCount(m.pieceCount)]
|
||||
m.clearUnusedBits()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update changes whether the piece at index is complete and verified.
|
||||
func (m *PieceMap) Update(index int, completed bool) error {
|
||||
if !m.validIndex(index) {
|
||||
return ErrPieceIndexOutOfRange
|
||||
}
|
||||
previous := m.completedAt(index)
|
||||
if previous == completed {
|
||||
return nil
|
||||
}
|
||||
if completed {
|
||||
m.completedPieces++
|
||||
} else {
|
||||
m.completedPieces--
|
||||
}
|
||||
m.setCompleted(index, completed)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get reports whether the exact piece index is complete and verified.
|
||||
func (m *PieceMap) Get(index int) (bool, error) {
|
||||
if !m.validIndex(index) {
|
||||
return false, ErrPieceIndexOutOfRange
|
||||
}
|
||||
return m.completedAt(index), nil
|
||||
}
|
||||
|
||||
// Bytes returns an independent copy of the packed bitset-v1 bytes.
|
||||
func (m *PieceMap) Bytes() []byte {
|
||||
return append([]byte(nil), m.data...)
|
||||
}
|
||||
|
||||
func (m *PieceMap) PieceCount() int { return m.pieceCount }
|
||||
func (m *PieceMap) PieceSize() int64 { return m.pieceSize }
|
||||
func (m *PieceMap) CompletedPieces() int { return m.completedPieces }
|
||||
|
||||
// MarshalJSON publishes the compact bytes without exposing mutable storage.
|
||||
func (m PieceMap) MarshalJSON() ([]byte, error) {
|
||||
type pieceMapJSON struct {
|
||||
Encoding string `json:"encoding"`
|
||||
PieceCount int `json:"pieceCount"`
|
||||
PieceSize int64 `json:"pieceSize"`
|
||||
CompletedPieces int `json:"completedPieces"`
|
||||
Data []byte `json:"data"`
|
||||
}
|
||||
return json.Marshal(pieceMapJSON{
|
||||
Encoding: PieceMapEncoding,
|
||||
PieceCount: m.pieceCount,
|
||||
PieceSize: m.pieceSize,
|
||||
CompletedPieces: m.completedPieces,
|
||||
Data: m.data,
|
||||
})
|
||||
}
|
||||
|
||||
func (m *PieceMap) validIndex(index int) bool {
|
||||
return m != nil && index >= 0 && index < m.pieceCount
|
||||
}
|
||||
|
||||
func (m *PieceMap) completedAt(index int) bool {
|
||||
byteIndex, mask := piecePosition(index)
|
||||
return m.data[byteIndex]&mask != 0
|
||||
}
|
||||
|
||||
func (m *PieceMap) setCompleted(index int, completed bool) {
|
||||
byteIndex, mask := piecePosition(index)
|
||||
if completed {
|
||||
m.data[byteIndex] |= mask
|
||||
} else {
|
||||
m.data[byteIndex] &^= mask
|
||||
}
|
||||
}
|
||||
|
||||
func (m *PieceMap) clearUnusedBits() {
|
||||
if m.pieceCount == 0 || m.pieceCount%piecesPerByte == 0 {
|
||||
return
|
||||
}
|
||||
usedBits := uint(m.pieceCount % piecesPerByte)
|
||||
m.data[len(m.data)-1] &= byte(1<<usedBits) - 1
|
||||
}
|
||||
|
||||
func piecePosition(index int) (byteIndex int, mask byte) {
|
||||
return index / piecesPerByte, byte(1 << uint(index%piecesPerByte))
|
||||
}
|
||||
|
||||
func packedPieceByteCount(pieceCount int) int {
|
||||
return (pieceCount + piecesPerByte - 1) / piecesPerByte
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPieceMapCRUDAndBytes(t *testing.T) {
|
||||
pieceMap := NewPieceMap(4, 16*1024)
|
||||
_ = pieceMap.Update(0, true)
|
||||
_ = pieceMap.Update(2, true)
|
||||
|
||||
if got, want := pieceMap.Bytes(), []byte{0b00000101}; len(got) != 1 || got[0] != want[0] {
|
||||
t.Fatalf("Bytes() = %08b, want %08b", got, want)
|
||||
}
|
||||
if got := pieceMap.CompletedPieces(); got != 2 {
|
||||
t.Fatalf("CompletedPieces() = %d, want 2", got)
|
||||
}
|
||||
|
||||
pieceMap.Add(true)
|
||||
if got := pieceMap.PieceCount(); got != 5 {
|
||||
t.Fatalf("PieceCount() = %d, want 5", got)
|
||||
}
|
||||
if err := pieceMap.Delete(1); err != nil {
|
||||
t.Fatalf("Delete: %v", err)
|
||||
}
|
||||
wantAfterDelete := []bool{true, true, false, true}
|
||||
for index, want := range wantAfterDelete {
|
||||
got, err := pieceMap.Get(index)
|
||||
if err != nil || got != want {
|
||||
t.Fatalf("Get(%d) = %v, %v; want %v", index, got, err, want)
|
||||
}
|
||||
}
|
||||
if got := pieceMap.CompletedPieces(); got != 3 {
|
||||
t.Fatalf("CompletedPieces() after delete = %d, want 3", got)
|
||||
}
|
||||
|
||||
bytesCopy := pieceMap.Bytes()
|
||||
bytesCopy[0] = 0
|
||||
got, _ := pieceMap.Get(0)
|
||||
if !got {
|
||||
t.Fatal("Bytes returned mutable internal storage")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPieceMapValidationAndRestore(t *testing.T) {
|
||||
pieceMap, err := NewPieceMapFromBytes(10, 1024, []byte{0b10010001, 0xff})
|
||||
if err != nil {
|
||||
t.Fatalf("NewPieceMapFromBytes: %v", err)
|
||||
}
|
||||
// Only the low positions for pieces 8 and 9 belong to the map.
|
||||
if got, want := pieceMap.Bytes()[1], byte(0b00000011); got != want {
|
||||
t.Fatalf("last byte = %08b, want %08b", got, want)
|
||||
}
|
||||
if pieceMap.CompletedPieces() != 5 {
|
||||
t.Fatalf("CompletedPieces() = %d, want 5", pieceMap.CompletedPieces())
|
||||
}
|
||||
|
||||
if _, err := NewPieceMapFromBytes(10, 1024, []byte{0}); err == nil {
|
||||
t.Fatal("expected invalid byte length error")
|
||||
}
|
||||
if err := pieceMap.Update(10, true); !errors.Is(err, ErrPieceIndexOutOfRange) {
|
||||
t.Fatalf("Update out of range error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPieceMapJSON(t *testing.T) {
|
||||
pieceMap := NewPieceMap(4, 4096)
|
||||
_ = pieceMap.Update(0, true)
|
||||
_ = pieceMap.Update(2, true)
|
||||
|
||||
data, err := json.Marshal(pieceMap)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal: %v", err)
|
||||
}
|
||||
var got map[string]any
|
||||
if err := json.Unmarshal(data, &got); err != nil {
|
||||
t.Fatalf("Unmarshal JSON: %v", err)
|
||||
}
|
||||
if got["encoding"] != PieceMapEncoding || got["data"] != "BQ==" {
|
||||
t.Fatalf("unexpected JSON: %s", data)
|
||||
}
|
||||
if got["pieceCount"] != float64(4) || got["pieceSize"] != float64(4096) || got["completedPieces"] != float64(2) {
|
||||
t.Fatalf("unexpected metadata: %s", data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPieceMapCloneIsIndependent(t *testing.T) {
|
||||
pieceMap := NewPieceMap(4, 4096)
|
||||
_ = pieceMap.Update(0, true)
|
||||
snapshot := pieceMap.Clone()
|
||||
_ = snapshot.Update(1, true)
|
||||
|
||||
if got, _ := pieceMap.Get(1); got {
|
||||
t.Fatal("Clone mutated the source map")
|
||||
}
|
||||
if snapshot.CompletedPieces() != 2 {
|
||||
t.Fatalf("clone completed pieces = %d, want 2", snapshot.CompletedPieces())
|
||||
}
|
||||
if (*PieceMap)(nil).Clone() != nil {
|
||||
t.Fatal("nil map clone must stay nil")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package base
|
||||
|
||||
// PeerStats is the common row returned by protocols that expose connected
|
||||
// peers. Speeds contain payload bytes per second rather than protocol overhead.
|
||||
type PeerStats struct {
|
||||
Address string `json:"address"`
|
||||
Client string `json:"client"`
|
||||
DownloadSpeed int64 `json:"downloadSpeed"`
|
||||
UploadSpeed int64 `json:"uploadSpeed"`
|
||||
PieceCount int `json:"pieceCount"`
|
||||
// Completion is the known remote data completion ratio in the range 0–1.
|
||||
// It is omitted when a protocol cannot determine the remote total.
|
||||
Completion *float64 `json:"completion,omitempty"`
|
||||
// Relevance is the ratio of locally wanted, missing pieces that this peer
|
||||
// claims to have. It is omitted when the local missing set is unavailable or empty.
|
||||
Relevance *float64 `json:"relevance,omitempty"`
|
||||
Source string `json:"source"`
|
||||
// Transport is the active connection transport, for example tcp or utp.
|
||||
// It is intentionally separate from Source, which describes peer discovery.
|
||||
Transport string `json:"transport"`
|
||||
}
|
||||
+366
-28
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
+31
-2
@@ -36,6 +36,10 @@ type Task struct {
|
||||
timer *util.Timer
|
||||
statusLock *sync.Mutex
|
||||
lock *sync.Mutex
|
||||
statsLock *sync.Mutex
|
||||
statsLoaded bool
|
||||
stats *fetcher.Stats
|
||||
statsSnapshot json.RawMessage
|
||||
blobRefLock *sync.Mutex
|
||||
blobURL string
|
||||
runGeneration uint64
|
||||
@@ -43,6 +47,31 @@ type Task struct {
|
||||
uploadSpeedArr []int64
|
||||
}
|
||||
|
||||
// TaskRuntimeStatus is the lightweight, frequently refreshed view of a task.
|
||||
// It intentionally excludes request, resource and option metadata returned by
|
||||
// Task so detail pages can poll progress without repeatedly transferring the
|
||||
// full task object.
|
||||
type TaskRuntimeStatus struct {
|
||||
Status base.Status `json:"status"`
|
||||
Used int64 `json:"used"`
|
||||
Speed int64 `json:"speed"`
|
||||
Downloaded int64 `json:"downloaded"`
|
||||
Total int64 `json:"total"`
|
||||
UploadSpeed int64 `json:"uploadSpeed"`
|
||||
Uploaded int64 `json:"uploaded"`
|
||||
ExtractStatus ExtractStatus `json:"extractStatus"`
|
||||
ExtractProgress int `json:"extractProgress"`
|
||||
Files []FileRuntimeStatus `json:"files"`
|
||||
}
|
||||
|
||||
// FileRuntimeStatus maps one value returned by fetcher.Progress to its
|
||||
// original resource index. Fetchers return progress in SelectFiles order.
|
||||
type FileRuntimeStatus struct {
|
||||
Index int `json:"index"`
|
||||
Size int64 `json:"size"`
|
||||
Downloaded int64 `json:"downloaded"`
|
||||
}
|
||||
|
||||
func NewTask() *Task {
|
||||
id, err := gonanoid.New()
|
||||
if err != nil {
|
||||
@@ -88,10 +117,10 @@ func (t *Task) Name() string {
|
||||
func (t *Task) MarshalJSON() ([]byte, error) {
|
||||
type rawTaskType Task
|
||||
jsonTask := struct {
|
||||
rawTaskType
|
||||
*rawTaskType
|
||||
Name string `json:"name"`
|
||||
}{
|
||||
rawTaskType(*t),
|
||||
(*rawTaskType)(t),
|
||||
t.Name(),
|
||||
}
|
||||
return json.Marshal(jsonTask)
|
||||
|
||||
@@ -1,19 +1,35 @@
|
||||
package bt
|
||||
|
||||
import "github.com/GopeedLab/gopeed/pkg/base"
|
||||
|
||||
type ReqExtra struct {
|
||||
Trackers []string `json:"trackers"`
|
||||
}
|
||||
|
||||
// Stats for torrent
|
||||
type Stats struct {
|
||||
// health indicators of torrents, from large to small, ConnectedSeeders are also the key to the health of seed resources
|
||||
TotalPeers int `json:"totalPeers"`
|
||||
ActivePeers int `json:"activePeers"`
|
||||
ConnectedSeeders int `json:"connectedSeeders"`
|
||||
// StatsSnapshot contains cumulative BitTorrent data that remains meaningful
|
||||
// after the torrent is stopped or the application restarts.
|
||||
type StatsSnapshot struct {
|
||||
// Total seed bytes
|
||||
SeedBytes int64 `json:"seedBytes"`
|
||||
// Seed ratio
|
||||
SeedRatio float64 `json:"seedRatio"`
|
||||
// Total seed time
|
||||
SeedTime int64 `json:"seedTime"`
|
||||
// PieceMap contains verified local piece completion in compact bitset-v1
|
||||
// format. It is nil while metadata or storage completion is unavailable.
|
||||
PieceMap *base.PieceMap `json:"pieceMap"`
|
||||
}
|
||||
|
||||
// StatsRuntime contains live swarm telemetry. It is never persisted because
|
||||
// peer connections and live swarm counters cease to be valid once the fetcher
|
||||
// stops.
|
||||
type StatsRuntime struct {
|
||||
// Health indicators of torrents, from large to small. ConnectedSeeders are
|
||||
// also the key to the health of seed resources.
|
||||
TotalPeers int `json:"totalPeers"`
|
||||
ActivePeers int `json:"activePeers"`
|
||||
ConnectedSeeders int `json:"connectedSeeders"`
|
||||
ConnectedLeechers int `json:"connectedLeechers"`
|
||||
// Peers contains currently connected BitTorrent peers.
|
||||
Peers []*base.PeerStats `json:"peers"`
|
||||
}
|
||||
+18
-11
@@ -1,14 +1,21 @@
|
||||
package ed2k
|
||||
|
||||
type Stats struct {
|
||||
State string `json:"state"`
|
||||
Paused bool `json:"paused"`
|
||||
ActivePeers int `json:"activePeers"`
|
||||
TotalPeers int `json:"totalPeers"`
|
||||
DownloadRate int `json:"downloadRate"`
|
||||
Upload int64 `json:"upload"`
|
||||
UploadRate int `json:"uploadRate"`
|
||||
TotalDone int64 `json:"totalDone"`
|
||||
TotalReceived int64 `json:"totalReceived"`
|
||||
TotalWanted int64 `json:"totalWanted"`
|
||||
import "github.com/GopeedLab/gopeed/pkg/base"
|
||||
|
||||
type StatsSnapshot struct {
|
||||
Upload int64 `json:"upload"`
|
||||
TotalDone int64 `json:"totalDone"`
|
||||
TotalReceived int64 `json:"totalReceived"`
|
||||
TotalWanted int64 `json:"totalWanted"`
|
||||
PieceMap *base.PieceMap `json:"pieceMap"`
|
||||
}
|
||||
|
||||
type StatsRuntime struct {
|
||||
State string `json:"state"`
|
||||
Paused bool `json:"paused"`
|
||||
ActivePeers int `json:"activePeers"`
|
||||
TotalPeers int `json:"totalPeers"`
|
||||
DownloadRate int `json:"downloadRate"`
|
||||
UploadRate int `json:"uploadRate"`
|
||||
Peers []*base.PeerStats `json:"peers"`
|
||||
}
|
||||
@@ -30,6 +30,11 @@ type Stats struct {
|
||||
|
||||
type StatsConnection struct {
|
||||
Downloaded int64 `json:"downloaded"`
|
||||
// Total is the resource size divided by the current number of connections,
|
||||
// rounded up to a whole byte. Every connection in one snapshot uses the same
|
||||
// denominator so clients can compare their downloaded contributions. It is
|
||||
// zero when the resource size is unknown.
|
||||
Total int64 `json:"total"`
|
||||
Completed bool `json:"completed"`
|
||||
Failed bool `json:"failed"`
|
||||
RetryTimes int `json:"retryTimes"`
|
||||
|
||||
@@ -197,6 +197,25 @@ func GetTask(w http.ResponseWriter, r *http.Request) {
|
||||
WriteJson(w, model.NewOkResult(task))
|
||||
}
|
||||
|
||||
func GetTaskStatus(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
taskId := vars["id"]
|
||||
if taskId == "" {
|
||||
WriteJson(w, model.NewErrorResult("param invalid: id", model.CodeInvalidParam))
|
||||
return
|
||||
}
|
||||
status, err := Downloader.RuntimeStatus(taskId)
|
||||
if err != nil {
|
||||
if err == download.ErrTaskNotFound {
|
||||
WriteJson(w, model.NewErrorResult("task not found", model.CodeTaskNotFound))
|
||||
return
|
||||
}
|
||||
WriteJson(w, model.NewErrorResult(err.Error()))
|
||||
return
|
||||
}
|
||||
WriteJson(w, model.NewOkResult(status))
|
||||
}
|
||||
|
||||
func GetTasks(w http.ResponseWriter, r *http.Request) {
|
||||
filter, errResult := parseFilter(r)
|
||||
if errResult != nil {
|
||||
|
||||
+63
-25
@@ -35,6 +35,11 @@ var (
|
||||
Downloader *download.Downloader
|
||||
)
|
||||
|
||||
const (
|
||||
webAuthCookieName = "gopeed_web_auth"
|
||||
webAuthTokenTTL = 7 * 24 * time.Hour
|
||||
)
|
||||
|
||||
func Start(startCfg *model.StartConfig) (port int, err error) {
|
||||
// avoid repeat start
|
||||
if srv != nil {
|
||||
@@ -118,6 +123,9 @@ func BuildServer(startCfg *model.StartConfig) (*http.Server, net.Listener, error
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if err := Downloader.ContinueOnStartup(); err != nil {
|
||||
Downloader.Logger.Warn().Err(err).Msg("auto-start tasks failed")
|
||||
}
|
||||
|
||||
var r = mux.NewRouter()
|
||||
r.Methods(http.MethodGet).Path("/api/v1/info").HandlerFunc(Info)
|
||||
@@ -132,6 +140,7 @@ func BuildServer(startCfg *model.StartConfig) (*http.Server, net.Listener, error
|
||||
r.Methods(http.MethodDelete).Path("/api/v1/tasks/{id}").HandlerFunc(DeleteTask)
|
||||
r.Methods(http.MethodDelete).Path("/api/v1/tasks").HandlerFunc(DeleteTasks)
|
||||
r.Methods(http.MethodGet).Path("/api/v1/tasks/{id}").HandlerFunc(GetTask)
|
||||
r.Methods(http.MethodGet).Path("/api/v1/tasks/{id}/status").HandlerFunc(GetTaskStatus)
|
||||
r.Methods(http.MethodGet).Path("/api/v1/tasks").HandlerFunc(GetTasks)
|
||||
r.Methods(http.MethodGet).Path("/api/v1/tasks/{id}/stats").HandlerFunc(GetStats)
|
||||
r.Methods(http.MethodGet).Path("/api/v1/config").HandlerFunc(GetConfig)
|
||||
@@ -164,7 +173,17 @@ func BuildServer(startCfg *model.StartConfig) (*http.Server, net.Listener, error
|
||||
return
|
||||
}
|
||||
|
||||
WriteJson(w, model.NewOkResult(token))
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: webAuthCookieName,
|
||||
Value: token,
|
||||
Path: "/",
|
||||
Expires: time.Now().Add(webAuthTokenTTL),
|
||||
MaxAge: int(webAuthTokenTTL.Seconds()),
|
||||
HttpOnly: true,
|
||||
Secure: requestUsesHTTPS(r),
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
})
|
||||
WriteJson(w, model.NewNilResult())
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -182,6 +201,16 @@ func BuildServer(startCfg *model.StartConfig) (*http.Server, net.Listener, error
|
||||
|
||||
r.Use(func(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if enableWebAuth && isProtectedWebFilePath(r.URL.Path) {
|
||||
cookie, err := r.Cookie(webAuthCookieName)
|
||||
if err != nil || !validWebAuthToken(cookie.Value, startCfg.WebAuth) {
|
||||
writeUnauthorized(w, r)
|
||||
return
|
||||
}
|
||||
h.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if enableApiToken {
|
||||
apiTokenHeader := r.Header["X-Api-Token"]
|
||||
// If api token header is set, only check api token ignore basic auth
|
||||
@@ -202,30 +231,8 @@ func BuildServer(startCfg *model.StartConfig) (*http.Server, net.Listener, error
|
||||
return
|
||||
}
|
||||
|
||||
token := r.Header.Get("Authorization")
|
||||
if token == "" {
|
||||
writeUnauthorized(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
token = strings.TrimPrefix(token, "Bearer ")
|
||||
tokenData, err := aesDecrypt(aesKey, token)
|
||||
if err != nil {
|
||||
writeUnauthorized(w, r)
|
||||
return
|
||||
}
|
||||
parts := strings.SplitN(string(tokenData), ":", 3)
|
||||
username := parts[0]
|
||||
password := parts[1]
|
||||
timestamp, _ := strconv.Atoi(parts[2])
|
||||
|
||||
if username != startCfg.WebAuth.Username || password != startCfg.WebAuth.Password {
|
||||
writeUnauthorized(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// Check if the token is expired (7 days)
|
||||
if time.Now().Unix()-int64(timestamp) > 7*24*3600 {
|
||||
cookie, err := r.Cookie(webAuthCookieName)
|
||||
if err != nil || !validWebAuthToken(cookie.Value, startCfg.WebAuth) {
|
||||
writeUnauthorized(w, r)
|
||||
return
|
||||
}
|
||||
@@ -261,6 +268,37 @@ func BuildServer(startCfg *model.StartConfig) (*http.Server, net.Listener, error
|
||||
return srv, listener, nil
|
||||
}
|
||||
|
||||
func isProtectedWebFilePath(urlPath string) bool {
|
||||
return urlPath == "/fs" || strings.HasPrefix(urlPath, "/fs/")
|
||||
}
|
||||
|
||||
func validWebAuthToken(token string, auth *model.WebAuth) bool {
|
||||
if token == "" || auth == nil {
|
||||
return false
|
||||
}
|
||||
tokenData, err := aesDecrypt(aesKey, token)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
parts := strings.SplitN(string(tokenData), ":", 3)
|
||||
if len(parts) != 3 || parts[0] != auth.Username || parts[1] != auth.Password {
|
||||
return false
|
||||
}
|
||||
timestamp, err := strconv.ParseInt(parts[2], 10, 64)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return time.Now().Unix()-timestamp <= int64(webAuthTokenTTL.Seconds())
|
||||
}
|
||||
|
||||
func requestUsesHTTPS(r *http.Request) bool {
|
||||
if r.TLS != nil {
|
||||
return true
|
||||
}
|
||||
forwardedProto := strings.TrimSpace(strings.Split(r.Header.Get("X-Forwarded-Proto"), ",")[0])
|
||||
return strings.EqualFold(forwardedProto, "https")
|
||||
}
|
||||
|
||||
func resolvePath(urlPath string, prefix string) (identity string, path string, err error) {
|
||||
// remove prefix
|
||||
clearPath := strings.TrimPrefix(urlPath, prefix)
|
||||
|
||||
+92
-11
@@ -144,6 +144,23 @@ func TestCreateDirectTask(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetTaskStatus(t *testing.T) {
|
||||
doTest(func() {
|
||||
resolved := httpRequestCheckOk[*download.ResolveResult](http.MethodPost, "/api/v1/resolve", resolveReq)
|
||||
taskId := httpRequestCheckOk[string](http.MethodPost, "/api/v1/tasks", &model.CreateTask{Rid: resolved.ID})
|
||||
status := httpRequestCheckOk[*download.TaskRuntimeStatus](http.MethodGet, "/api/v1/tasks/"+taskId+"/status", nil)
|
||||
if status.Total != test.BuildSize {
|
||||
t.Fatalf("task status total = %d, want %d", status.Total, test.BuildSize)
|
||||
}
|
||||
if status.Files == nil {
|
||||
t.Fatal("task status files must be an empty array or contain file progress, not null")
|
||||
}
|
||||
|
||||
code, _ := httpRequest[*download.TaskRuntimeStatus](http.MethodGet, "/api/v1/tasks/missing/status", nil)
|
||||
checkCode(code, model.CodeTaskNotFound)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCreateDirectTaskBatch(t *testing.T) {
|
||||
doTest(func() {
|
||||
reqs := make([]*base.CreateTaskBatchItem, 0)
|
||||
@@ -438,6 +455,7 @@ func TestGetAndPutConfig(t *testing.T) {
|
||||
doTest(func() {
|
||||
cfg := httpRequestCheckOk[*base.DownloaderStoreConfig](http.MethodGet, "/api/v1/config", nil)
|
||||
cfg.DownloadDir = "./download"
|
||||
cfg.AutoStartTasks = true
|
||||
cfg.Extra = map[string]any{
|
||||
"serverConfig": &Config{
|
||||
Host: "127.0.0.1",
|
||||
@@ -809,16 +827,73 @@ func TestAuthorization(t *testing.T) {
|
||||
t.Errorf("TestAuthorization() got = %v, want %v", status, http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
token := httpRequestCheckOk[string](http.MethodPost, "/api/web/login", cfg.WebAuth)
|
||||
authToken := fmt.Sprintf("Bearer %s", token)
|
||||
authHeaders := map[string]string{
|
||||
"Authorization": authToken,
|
||||
status, loginHeaders, loginBody := doHttpRequest1(http.MethodPost, "/api/web/login", nil, cfg.WebAuth)
|
||||
if status != http.StatusOK {
|
||||
t.Fatalf("login status = %d, want %d", status, http.StatusOK)
|
||||
}
|
||||
var loginResult model.Result[any]
|
||||
if err := json.Unmarshal(loginBody, &loginResult); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if loginResult.Data != nil {
|
||||
t.Fatalf("login response exposed session data: %#v", loginResult.Data)
|
||||
}
|
||||
loginResponse := &http.Response{Header: http.Header{"Set-Cookie": []string{loginHeaders["Set-Cookie"]}}}
|
||||
cookies := loginResponse.Cookies()
|
||||
if len(cookies) != 1 {
|
||||
t.Fatalf("login cookies = %d, want 1", len(cookies))
|
||||
}
|
||||
authCookie := cookies[0]
|
||||
if authCookie.Name != webAuthCookieName || authCookie.Value == "" || authCookie.Path != "/" ||
|
||||
!authCookie.HttpOnly || authCookie.SameSite != http.SameSiteLaxMode || authCookie.MaxAge <= 0 {
|
||||
t.Fatalf("unexpected login cookie: %#v", authCookie)
|
||||
}
|
||||
bearerHeaders := map[string]string{
|
||||
"Authorization": "Bearer " + authCookie.Value,
|
||||
}
|
||||
authCookieHeader := map[string]string{
|
||||
"Cookie": fmt.Sprintf("%s=%s", authCookie.Name, authCookie.Value),
|
||||
}
|
||||
|
||||
for _, filePath := range []string{
|
||||
"/fs/extensions/not_exist/icon.png",
|
||||
"/fs/tasks/not_exist/file.bin",
|
||||
} {
|
||||
status, _ = doHttpRequest0(http.MethodGet, filePath, nil, nil)
|
||||
if status != http.StatusUnauthorized {
|
||||
t.Errorf("unauthenticated %s status = %d, want %d", filePath, status, http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
status, _ = doHttpRequest0(http.MethodGet, filePath, bearerHeaders, nil)
|
||||
if status != http.StatusUnauthorized {
|
||||
t.Errorf("Bearer-only %s status = %d, want %d", filePath, status, http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
status, _ = doHttpRequest0(http.MethodGet, filePath, authCookieHeader, nil)
|
||||
if status != http.StatusNotFound {
|
||||
t.Errorf("cookie-authenticated %s status = %d, want %d", filePath, status, http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
status, _ = doHttpRequest0(http.MethodGet, "/fs/future-resource", nil, nil)
|
||||
if status != http.StatusUnauthorized {
|
||||
t.Errorf("unauthenticated future /fs route status = %d, want %d", status, http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
status, _ = doHttpRequest0(http.MethodGet, "/fs/extensions/not_exist/icon.png", map[string]string{
|
||||
"Cookie": webAuthCookieName + "=invalid",
|
||||
}, nil)
|
||||
if status != http.StatusUnauthorized {
|
||||
t.Errorf("invalid-cookie file status = %d, want %d", status, http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
status, _ = doHttpRequest0(http.MethodGet, "/api/v1/config", nil, nil)
|
||||
if status != http.StatusUnauthorized {
|
||||
t.Errorf("TestAuthorization() got = %v, want %v", status, http.StatusUnauthorized)
|
||||
}
|
||||
status, _ = doHttpRequest0(http.MethodGet, "/api/v1/config", bearerHeaders, nil)
|
||||
if status != http.StatusUnauthorized {
|
||||
t.Errorf("Bearer-authenticated API status = %v, want %v", status, http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
status, _ = doHttpRequest0(http.MethodGet, "/api/v1/config", map[string]string{
|
||||
"Authorization": "xxx",
|
||||
@@ -841,7 +916,7 @@ func TestAuthorization(t *testing.T) {
|
||||
|
||||
fakeToken := buildToken("fake", "fake", time.Now().Unix())
|
||||
status, _ = doHttpRequest0(http.MethodGet, "/api/v1/config", map[string]string{
|
||||
"Authorization": fmt.Sprintf("Bearer %s", fakeToken),
|
||||
"Cookie": fmt.Sprintf("%s=%s", webAuthCookieName, fakeToken),
|
||||
}, nil)
|
||||
if status != http.StatusUnauthorized {
|
||||
t.Errorf("TestAuthorization() got = %v, want %v", status, http.StatusUnauthorized)
|
||||
@@ -849,13 +924,19 @@ func TestAuthorization(t *testing.T) {
|
||||
|
||||
expireToken := buildToken(cfg.WebAuth.Username, cfg.WebAuth.Password, time.Now().Add(-time.Hour*8*24).Unix())
|
||||
status, _ = doHttpRequest0(http.MethodGet, "/api/v1/config", map[string]string{
|
||||
"Authorization": fmt.Sprintf("Bearer %s", expireToken),
|
||||
"Cookie": fmt.Sprintf("%s=%s", webAuthCookieName, expireToken),
|
||||
}, nil)
|
||||
if status != http.StatusUnauthorized {
|
||||
t.Errorf("TestAuthorization() got = %v, want %v", status, http.StatusUnauthorized)
|
||||
}
|
||||
status, _ = doHttpRequest0(http.MethodGet, "/fs/extensions/not_exist/icon.png", map[string]string{
|
||||
"Cookie": fmt.Sprintf("%s=%s", webAuthCookieName, expireToken),
|
||||
}, nil)
|
||||
if status != http.StatusUnauthorized {
|
||||
t.Errorf("expired-cookie file status = %d, want %d", status, http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
status, _ = doHttpRequest0(http.MethodGet, "/api/v1/config", authHeaders, nil)
|
||||
status, _ = doHttpRequest0(http.MethodGet, "/api/v1/config", authCookieHeader, nil)
|
||||
if status != http.StatusOK {
|
||||
t.Errorf("TestAuthorization() got = %v, want %v", status, http.StatusOK)
|
||||
}
|
||||
@@ -868,16 +949,16 @@ func TestAuthorization(t *testing.T) {
|
||||
}
|
||||
|
||||
status, _ = doHttpRequest0(http.MethodGet, "/api/v1/config", map[string]string{
|
||||
"Authorization": authToken,
|
||||
"X-Api-Token": cfg.ApiToken,
|
||||
"Cookie": fmt.Sprintf("%s=%s", authCookie.Name, authCookie.Value),
|
||||
"X-Api-Token": cfg.ApiToken,
|
||||
}, nil)
|
||||
if status != http.StatusOK {
|
||||
t.Errorf("TestAuthorization() got = %v, want %v", status, http.StatusOK)
|
||||
}
|
||||
|
||||
status, _ = doHttpRequest0(http.MethodGet, "/api/v1/config", map[string]string{
|
||||
"Authorization": authToken,
|
||||
"X-Api-Token": "",
|
||||
"Cookie": fmt.Sprintf("%s=%s", authCookie.Name, authCookie.Value),
|
||||
"X-Api-Token": "",
|
||||
}, nil)
|
||||
if status != http.StatusUnauthorized {
|
||||
t.Errorf("TestAuthorization() got = %v, want %v", status, http.StatusUnauthorized)
|
||||
|
||||
Reference in new issue
Block a user