forked from 1290799223/trafficConsume
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 65b2201
Showing
18 changed files
with
1,284 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
.idea | ||
*.iml | ||
out | ||
gen | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
*.test | ||
*.out | ||
go.work | ||
*.tar.gz | ||
*.zip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
FROM golang:alpine AS builder | ||
|
||
WORKDIR /build | ||
COPY . . | ||
|
||
RUN go mod tidy | ||
RUN go build -trimpath -v -ldflags "-X main.date=$(date -Iseconds)" | ||
|
||
FROM alpine | ||
|
||
RUN apk update --no-cache && apk add --no-cache ca-certificates tzdata | ||
ENV TZ Asia/Shanghai | ||
|
||
WORKDIR /app | ||
COPY --from=builder /build/trafficConsume /app/trafficConsume | ||
ENTRYPOINT ["/app/trafficConsume"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Binary name | ||
BINARY=trafficConsume | ||
DATE=$(shell date -Iseconds) | ||
# Builds the project | ||
build: | ||
GO111MODULE=on go build -trimpath -o ${BINARY} -ldflags "-X main.date=${DATE}" | ||
release: | ||
# Clean | ||
rm -rf *.gz | ||
|
||
# Build for mac | ||
go clean | ||
GO111MODULE=on go build -trimpath -ldflags "-s -w -X main.version=${VERSION} -X main.date=${DATE}" | ||
tar czvf ${BINARY}-mac64-${VERSION}.tar.gz ./${BINARY} | ||
# Build for arm | ||
go clean | ||
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GO111MODULE=on go build -trimpath -ldflags "-s -w -X main.version=${VERSION} -X main.date=${DATE}" | ||
tar czvf ${BINARY}-arm64-${VERSION}.tar.gz ./${BINARY} | ||
# Build for linux | ||
go clean | ||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -trimpath -ldflags "-s -w -X main.version=${VERSION} -X main.date=${DATE}" | ||
tar czvf ${BINARY}-linux64-${VERSION}.tar.gz ./${BINARY} | ||
# Build for win | ||
go clean | ||
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 GO111MODULE=on go build -trimpath -ldflags "-s -w -X main.version=${VERSION} -X main.date=${DATE}" | ||
zip ${BINARY}-win64-${VERSION}.zip ./${BINARY}.exe | ||
|
||
go clean | ||
# Cleans our projects: deletes binaries | ||
clean: | ||
go clean | ||
rm -rf *.gz *.zip | ||
|
||
.PHONY: clean build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package client | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/anacrolix/torrent" | ||
"github.com/anacrolix/torrent/bencode" | ||
"github.com/anacrolix/torrent/metainfo" | ||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/thank243/trafficConsume/common/fakefile" | ||
"github.com/thank243/trafficConsume/infra" | ||
"github.com/thank243/trafficConsume/storage" | ||
) | ||
|
||
func New(cfg *torrent.ClientConfig) (*Client, error) { | ||
cl, err := torrent.NewClient(cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
now := time.Now() | ||
|
||
return &Client{ | ||
Client: cl, | ||
totalStats: stats{createdAt: now}, | ||
fakeUploadStats: stats{createdAt: now}, | ||
fakeDownloadStats: stats{createdAt: now}, | ||
}, nil | ||
} | ||
|
||
func (c *Client) AddTorrents(mhs []metainfo.Hash) { | ||
// default tracker servers | ||
trs := []string{ | ||
"http://nyaa.tracker.wf:7777/announce", | ||
"http://p4p.arenabg.com:1337/announce", | ||
"udp://tracker.opentrackr.org:1337/announce", | ||
} | ||
|
||
var wg sync.WaitGroup | ||
for i := range mhs { | ||
wg.Add(1) | ||
go func(i int) { | ||
t, _ := c.AddTorrentInfoHash(mhs[i]) | ||
t.AddTrackers([][]string{trs}) | ||
if t.Info() == nil { | ||
<-t.GotInfo() | ||
} | ||
t.DownloadAll() | ||
wg.Done() | ||
}(i) | ||
} | ||
|
||
wg.Wait() | ||
} | ||
|
||
func (c *Client) Monitor() { | ||
for now := range time.Tick(time.Second * 10) { | ||
totalBytes := c.ConnStats().BytesRead | ||
totalSpeed := c.speed(&c.totalStats, totalBytes, now) | ||
fakeDownSpeed, fakeUpSpeed, actPeers := c.torrentStats(now) | ||
|
||
log.Infof("Throughput: %s, Total: ↓ %s/s, Private: ↑ %s/s - ↓ %s/s, Pieces: %d, Peers: %d, Tasks: %d", | ||
infra.ByteCountIEC(totalBytes.Int64()), | ||
infra.ByteCountIEC(totalSpeed), infra.ByteCountIEC(fakeUpSpeed), infra.ByteCountIEC(fakeDownSpeed), | ||
storage.PieceCache().ItemCount(), actPeers, len(c.Torrents())) | ||
} | ||
} | ||
|
||
func (c *Client) torrentStats(now time.Time) (fakeDownSpeed int64, fakeUpSpeed int64, actPeers int) { | ||
for _, t := range c.Torrents() { | ||
actPeers += t.Stats().ActivePeers | ||
|
||
if t.InfoHash().String() == storage.FakeFileHash { | ||
fakeDownSpeed = c.speed(&c.fakeDownloadStats, t.Stats().BytesRead, now) | ||
fakeUpSpeed = c.speed(&c.fakeUploadStats, t.Stats().BytesWritten, now) | ||
} | ||
} | ||
return | ||
} | ||
|
||
func (c *Client) speed(s *stats, nowBytes torrent.Count, now time.Time) int64 { | ||
b := nowBytes.Int64() | ||
speed := (b - s.bytesCount) * 1000 / now.Sub(s.createdAt).Milliseconds() | ||
s.bytesCount = b | ||
s.createdAt = now | ||
return speed | ||
} | ||
|
||
func (c *Client) AddFakeTorrent() { | ||
f := &fakefile.FakeFile{ | ||
Size: 1<<30 + 114514, | ||
FillByte: 0xff, | ||
} | ||
|
||
t, _ := c.AddTorrent(&metainfo.MetaInfo{ | ||
InfoBytes: bencode.MustMarshal(f.BuildFakeFileInfo()), | ||
}) | ||
|
||
trs := []string{ | ||
"http://p4p.arenabg.com:1337/announce", | ||
"udp://tracker.opentrackr.org:1337/announce", | ||
} | ||
t.AddTrackers([][]string{trs}) | ||
|
||
t.DownloadAll() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package client | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/anacrolix/torrent" | ||
) | ||
|
||
type Client struct { | ||
*torrent.Client | ||
totalStats stats | ||
fakeUploadStats stats | ||
fakeDownloadStats stats | ||
} | ||
|
||
type stats struct { | ||
bytesCount int64 | ||
createdAt time.Time | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package fakefile | ||
|
||
import ( | ||
"crypto/sha1" | ||
"io" | ||
|
||
"github.com/anacrolix/torrent/metainfo" | ||
) | ||
|
||
type FakeFile struct { | ||
Size int | ||
FillByte byte | ||
} | ||
|
||
func (f *FakeFile) BuildFakePieces(pieceLen int64) []byte { | ||
var cumB []byte | ||
cycleNum := int64(f.Size) / pieceLen | ||
r := int64(f.Size) % pieceLen | ||
p := make([]byte, pieceLen) | ||
for i := range p { | ||
p[i] = f.FillByte | ||
} | ||
b := sha1.Sum(p) | ||
rb := sha1.Sum(p[:r]) | ||
for i := int64(0); i < cycleNum; i++ { | ||
cumB = append(cumB, b[:]...) | ||
} | ||
cumB = append(cumB, rb[:]...) | ||
|
||
return cumB | ||
} | ||
|
||
func (f *FakeFile) Read(p []byte) (n int, err error) { | ||
readSize := len(p) | ||
r := f.Size | ||
if r < readSize { | ||
readSize = r | ||
} | ||
|
||
for i := 0; i < readSize; i++ { | ||
p[i] = f.FillByte | ||
} | ||
|
||
r -= readSize | ||
if r <= 0 { | ||
err = io.EOF | ||
} | ||
|
||
return readSize, err | ||
} | ||
|
||
func (f *FakeFile) BuildFakeFileInfo() *metainfo.Info { | ||
info := &metainfo.Info{ | ||
Name: "fake.file", | ||
Length: int64(f.Size), | ||
PieceLength: metainfo.ChoosePieceLength(int64(f.Size)), | ||
} | ||
info.Pieces = f.BuildFakePieces(info.PieceLength) | ||
|
||
return info | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package fakefile | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
var R = rand.New(rand.NewSource(time.Now().UnixNano())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package metahash | ||
|
||
import ( | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/PuerkitoBio/goquery" | ||
"github.com/anacrolix/torrent/metainfo" | ||
"github.com/anacrolix/torrent/types/infohash" | ||
"github.com/imroc/req/v3" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func GetNyaaMetaHashes() []metainfo.Hash { | ||
client := req.C() | ||
resp, err := client.R().Get("https://sukebei.nyaa.si/?s=seeders&o=desc") | ||
if err != nil { | ||
log.Error(err) | ||
return nil | ||
} | ||
|
||
doc, err := goquery.NewDocumentFromReader(resp.Body) | ||
if err != nil { | ||
log.Error(err) | ||
return nil | ||
} | ||
|
||
var els []string | ||
doc.Find(`a[href^="magnet"]`).Each(func(i int, s *goquery.Selection) { | ||
if val, ok := s.Attr("href"); ok { | ||
u, err := url.Parse(val) | ||
if err != nil { | ||
log.Error(err) | ||
return | ||
} | ||
|
||
q := u.Query() | ||
els = append(els, strings.Split(q.Get("xt"), ":")[2]) | ||
} | ||
}) | ||
|
||
length := 5 | ||
if len(els) < length { | ||
length = len(els) | ||
} | ||
log.Infof("Get %d meta info hash, add the top %d to task", len(els), length) | ||
|
||
hs := make([]metainfo.Hash, length) | ||
for i := 0; i < length; i++ { | ||
hs[i] = infohash.FromHexString(els[i]) | ||
} | ||
|
||
return hs | ||
} | ||
|
||
// GetDefaultMetaHashes default torrents | ||
func GetDefaultMetaHashes() []metainfo.Hash { | ||
return []metainfo.Hash{ | ||
infohash.FromHexString("76c23875f5d146993d79170e78e2de43c3178cf1"), | ||
infohash.FromHexString("5a29a7e432691d8f590c1b8e05b6678eee72fe68"), | ||
infohash.FromHexString("6dc8bc544faa0216c044855ddc51cd9252da6a2b"), | ||
} | ||
} | ||
|
||
func NeedDropTorrents(old []metainfo.Hash, new []metainfo.Hash) []metainfo.Hash { | ||
if len(new) == 0 { | ||
return nil | ||
} | ||
|
||
newMap := make(map[metainfo.Hash]bool) | ||
for i := range new { | ||
newMap[new[i]] = true | ||
} | ||
|
||
var dropTorrents []metainfo.Hash | ||
for i := range old { | ||
if _, ok := newMap[old[i]]; !ok { | ||
dropTorrents = append(dropTorrents, old[i]) | ||
} | ||
} | ||
return dropTorrents | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package metahash | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestGetTorrents(t *testing.T) { | ||
GetNyaaMetaHashes() | ||
} |
Oops, something went wrong.