This commit is contained in:
YouROK
2020-11-10 22:57:16 +03:00
parent 6a11651cf5
commit 1ce27ef121
12 changed files with 63 additions and 32 deletions

View File

@@ -39,7 +39,7 @@ GOBIN="/usr/local/go/bin/go"
[ -d src/github.com/pion/webrtc/v2 ] && go get -v github.com/pion/webrtc/v2 [ -d src/github.com/pion/webrtc/v2 ] && go get -v github.com/pion/webrtc/v2
[ -d src/go.etcd.io/bbolt ] && go get -v go.etcd.io/bbolt [ -d src/go.etcd.io/bbolt ] && go get -v go.etcd.io/bbolt
#ln -s . src/github.com/pion/webrtc/v2 ln -s . src/github.com/pion/webrtc/v2
$GOBIN version $GOBIN version

16
deps.sh Executable file
View File

@@ -0,0 +1,16 @@
#!/bin/bash
export GOPATH="${PWD}"
go get -v github.com/alexflint/go-arg
go get -v github.com/anacrolix/dht
go get -v github.com/anacrolix/missinggo/httptoo
go get -v github.com/anacrolix/torrent
go get -v github.com/anacrolix/torrent/iplist
go get -v github.com/anacrolix/torrent/metainfo
go get -v github.com/anacrolix/utp
go get -u github.com/gin-gonic/gin
go get -v github.com/pion/webrtc/v2
go get -v go.etcd.io/bbolt
ln -s . src/github.com/pion/webrtc/v2

View File

@@ -9,8 +9,6 @@ import (
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"server"
) )
func Preconfig(kill bool) { func Preconfig(kill bool) {
@@ -27,8 +25,6 @@ func Preconfig(kill bool) {
if kill { if kill {
fmt.Println("Signal catched:", s) fmt.Println("Signal catched:", s)
fmt.Println("For stop server, close in web") fmt.Println("For stop server, close in web")
} else {
server.Stop()
} }
} }
}() }()

View File

@@ -5,9 +5,10 @@ import (
"sort" "sort"
"sync" "sync"
"server/torr/state"
"github.com/anacrolix/torrent" "github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/metainfo"
"server/torr/state"
) )
type TorrentDB struct { type TorrentDB struct {

View File

@@ -31,6 +31,9 @@ const (
) )
type TorrentStats struct { type TorrentStats struct {
Title string `json:"title"`
Poster string `json:"poster"`
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Hash string `json:"hash,omitempty"` Hash string `json:"hash,omitempty"`

View File

@@ -12,10 +12,10 @@ import (
func (t *Torrent) Stream(fileIndex int, req *http.Request, resp http.ResponseWriter) error { func (t *Torrent) Stream(fileIndex int, req *http.Request, resp http.ResponseWriter) error {
files := t.Files() files := t.Files()
if fileIndex < 0 || fileIndex >= len(files) { if fileIndex < 1 || fileIndex > len(files) {
return errors.New("file index out of range") return errors.New("file index out of range")
} }
file := files[fileIndex] file := files[fileIndex-1]
reader := t.NewReader(file, 0) reader := t.NewReader(file, 0)
log.Println("Connect client") log.Println("Connect client")

View File

@@ -42,8 +42,6 @@ type Torrent struct {
PreloadSize int64 PreloadSize int64
PreloadedBytes int64 PreloadedBytes int64
hash metainfo.Hash
expiredTime time.Time expiredTime time.Time
closed <-chan struct{} closed <-chan struct{}
@@ -80,6 +78,7 @@ func NewTorrent(spec *torrent.TorrentSpec, bt *BTServer) (*Torrent, error) {
torr.bt = bt torr.bt = bt
torr.closed = goTorrent.Closed() torr.closed = goTorrent.Closed()
torr.TorrentSpec = spec torr.TorrentSpec = spec
torr.expiredTime = time.Now().Add(time.Minute)
go torr.watch() go torr.watch()
@@ -97,7 +96,7 @@ func (t *Torrent) WaitInfo() bool {
select { select {
case <-t.Torrent.GotInfo(): case <-t.Torrent.GotInfo():
t.cache = t.bt.storage.GetCache(t.hash) t.cache = t.bt.storage.GetCache(t.Hash())
return true return true
case <-t.closed: case <-t.closed:
return false return false
@@ -192,7 +191,13 @@ func (t *Torrent) Files() []*torrent.File {
} }
func (t *Torrent) Hash() metainfo.Hash { func (t *Torrent) Hash() metainfo.Hash {
return t.hash if t.Torrent != nil {
t.Torrent.InfoHash()
}
if t.TorrentSpec != nil {
return t.TorrentSpec.InfoHash
}
return [20]byte{}
} }
func (t *Torrent) Length() int64 { func (t *Torrent) Length() int64 {
@@ -329,8 +334,8 @@ func (t *Torrent) Close() {
t.bt.mu.Lock() t.bt.mu.Lock()
defer t.bt.mu.Unlock() defer t.bt.mu.Unlock()
if _, ok := t.bt.torrents[t.hash]; ok { if _, ok := t.bt.torrents[t.Hash()]; ok {
delete(t.bt.torrents, t.hash) delete(t.bt.torrents, t.Hash())
} }
t.drop() t.drop()
@@ -344,6 +349,9 @@ func (t *Torrent) Stats() *state.TorrentStats {
st.TorrentStatus = t.Status st.TorrentStatus = t.Status
st.TorrentStatusString = t.Status.String() st.TorrentStatusString = t.Status.String()
st.Title = t.Title
st.Poster = t.Poster
if t.TorrentSpec != nil { if t.TorrentSpec != nil {
st.Hash = t.TorrentSpec.InfoHash.HexString() st.Hash = t.TorrentSpec.InfoHash.HexString()
} }
@@ -383,7 +391,7 @@ func (t *Torrent) Stats() *state.TorrentStats {
for i, f := range files { for i, f := range files {
st.FileStats = append(st.FileStats, state.TorrentFileStat{ st.FileStats = append(st.FileStats, state.TorrentFileStat{
Id: i, Id: i + 1,
Path: f.Path(), Path: f.Path(),
Length: f.Length(), Length: f.Length(),
}) })

View File

@@ -8,11 +8,12 @@ import (
"path/filepath" "path/filepath"
"time" "time"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
sets "server/settings" sets "server/settings"
"server/torr/state" "server/torr/state"
"server/utils" "server/utils"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
) )
func allPlayList(c *gin.Context) { func allPlayList(c *gin.Context) {

View File

@@ -5,18 +5,19 @@ import (
"net/url" "net/url"
"strconv" "strconv"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"server/torr" "server/torr"
"server/web/api/utils" "server/web/api/utils"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
) )
// http://127.0.0.1:8090/stream/fname?link=...&index=0&stat // http://127.0.0.1:8090/stream/fname?link=...&index=1&stat
// http://127.0.0.1:8090/stream/fname?link=...&index=0&m3u // http://127.0.0.1:8090/stream/fname?link=...&index=1&m3u
// http://127.0.0.1:8090/stream/fname?link=...&index=0&play // http://127.0.0.1:8090/stream/fname?link=...&index=1&play
// http://127.0.0.1:8090/stream/fname?link=...&save&title=...&poster=... // http://127.0.0.1:8090/stream/fname?link=...&save&title=...&poster=...
// http://127.0.0.1:8090/stream/fname?link=...&index=0&play&save // http://127.0.0.1:8090/stream/fname?link=...&index=1&play&save
// http://127.0.0.1:8090/stream/fname?link=...&index=0&play&save&title=...&poster=... // http://127.0.0.1:8090/stream/fname?link=...&index=1&play&save&title=...&poster=...
func stream(c *gin.Context) { func stream(c *gin.Context) {
link := c.Query("link") link := c.Query("link")
@@ -82,7 +83,7 @@ func stream(c *gin.Context) {
c.Status(200) c.Status(200)
} }
// wait torrent info // wait torrent info
if !tor.WaitInfo() { if !tor.GotInfo() {
c.AbortWithError(http.StatusInternalServerError, errors.New("timeout torrent get info")) c.AbortWithError(http.StatusInternalServerError, errors.New("timeout torrent get info"))
return return
} }
@@ -90,7 +91,7 @@ func stream(c *gin.Context) {
// find file // find file
index := -1 index := -1
if len(tor.Files()) == 1 { if len(tor.Files()) == 1 {
index = 0 index = 1
} else { } else {
ind, err := strconv.Atoi(indexStr) ind, err := strconv.Atoi(indexStr)
if err == nil { if err == nil {

View File

@@ -3,13 +3,14 @@ package api
import ( import (
"net/http" "net/http"
"github.com/anacrolix/torrent/metainfo"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"server/log" "server/log"
"server/torr" "server/torr"
"server/torr/state" "server/torr/state"
"server/web/api/utils" "server/web/api/utils"
"github.com/anacrolix/torrent/metainfo"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
) )
//Action: add, get, rem, list, drop //Action: add, get, rem, list, drop
@@ -70,7 +71,7 @@ func addTorrent(req torrReqJS, c *gin.Context) {
return return
} }
if !torr.WaitInfo() { if !torr.GotInfo() {
log.TLogln("error add torrent:", "timeout connection torrent") log.TLogln("error add torrent:", "timeout connection torrent")
c.AbortWithError(http.StatusNotFound, errors.New("timeout connection torrent")) c.AbortWithError(http.StatusNotFound, errors.New("timeout connection torrent"))
return return

View File

@@ -3,9 +3,11 @@ package utils
import ( import (
"time" "time"
"github.com/anacrolix/torrent/metainfo"
"server/settings" "server/settings"
"server/torr" "server/torr"
"server/torr/state"
"github.com/anacrolix/torrent/metainfo"
) )
func AddTorrent(torr *torr.Torrent) { func AddTorrent(torr *torr.Torrent) {
@@ -26,6 +28,7 @@ func GetTorrent(hash metainfo.Hash) *torr.Torrent {
torr.TorrentSpec = db.TorrentSpec torr.TorrentSpec = db.TorrentSpec
torr.Title = db.Title torr.Title = db.Title
torr.Poster = db.Poster torr.Poster = db.Poster
torr.Status = state.TorrentInDB
return torr return torr
} }
} }
@@ -44,6 +47,7 @@ func ListTorrents() []*torr.Torrent {
torr.TorrentSpec = db.TorrentSpec torr.TorrentSpec = db.TorrentSpec
torr.Title = db.Title torr.Title = db.Title
torr.Poster = db.Poster torr.Poster = db.Poster
torr.Status = state.TorrentInDB
ret = append(ret, torr) ret = append(ret, torr)
} }
return ret return ret

View File

@@ -35,7 +35,7 @@ func ParseLink(link string) (*torrent.TorrentSpec, error) {
} }
func fromMagnet(link string) (*torrent.TorrentSpec, error) { func fromMagnet(link string) (*torrent.TorrentSpec, error) {
mag, err := metainfo.ParseMagnetURI(link) mag, err := metainfo.ParseMagnetUri(link)
if err != nil { if err != nil {
return nil, err return nil, err
} }