-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
12 changed files
with
559 additions
and
2 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 |
---|---|---|
|
@@ -121,3 +121,5 @@ tmp | |
|
||
# build files | ||
/nuxbt | ||
/module/torrent/test_save.torrent | ||
/module/torrent/test_torrents/test_save.torrent |
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
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
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 @@ | ||
package torrent | ||
|
||
import ( | ||
"github.com/anacrolix/torrent/metainfo" | ||
"github.com/anacrolix/torrent/types/infohash" | ||
) | ||
|
||
// GetMagnet returns a magnet link for the given torrent hash and trackers. | ||
func GetMagnet(torrentHash string, trackers []string) string { | ||
mag := metainfo.Magnet{ | ||
InfoHash: infohash.FromHexString(torrentHash), | ||
Trackers: trackers, | ||
} | ||
|
||
return mag.String() | ||
} |
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 torrent | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestGetMagnet(t *testing.T) { | ||
mag := GetMagnet("7f3956e5a15b34b62159727c08f944c7e433ad1e", []string{}) | ||
assert.Equal(t, "magnet:?xt=urn:btih:7f3956e5a15b34b62159727c08f944c7e433ad1e", mag) | ||
} | ||
|
||
func TestGetMagnet2(t *testing.T) { | ||
mag := GetMagnet("7f3956e5a15b34b62159727c08f944c7e433ad1e", | ||
[]string{"http://tracker1.com", "http://tracker2.com"}) | ||
assert.Equal(t, "magnet:?xt=urn:btih:7f3956e5a15b34b62159727c08f944c7e433ad1e"+ | ||
"&tr=http%3A%2F%2Ftracker1.com&tr=http%3A%2F%2Ftracker2.com", mag) | ||
t.Log(GetMagnet("7f3956e5a15b34b62159727c08f944c7e433ad1e", TRACKER_LIST)) | ||
} |
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 @@ | ||
d10:created by21:qBittorrent v4.6.5.1013:creation datei1721224953e4:infod6:lengthi8211e4:name9:lenna.jpg12:piece lengthi16384e6:pieces20:w��N|�OaO��+U>Ʃee |
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
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,111 @@ | ||
package torrent | ||
|
||
import ( | ||
"crypto/sha1" | ||
"fmt" | ||
"mime/multipart" | ||
"os" | ||
|
||
"github.com/anacrolix/torrent/bencode" | ||
) | ||
|
||
func NewBitTorrentFileFromMultipart(fh *multipart.FileHeader) (*BitTorrentFile, error) { | ||
fileReader, err := fh.Open() | ||
if err != nil { | ||
return nil, err | ||
} | ||
decoder := bencode.NewDecoder(fileReader) | ||
bencodeTorrent := &BitTorrentFile{} | ||
decodeErr := decoder.Decode(bencodeTorrent) | ||
if decodeErr != nil { | ||
return nil, decodeErr | ||
} | ||
|
||
return bencodeTorrent, nil | ||
} | ||
|
||
// NewBitTorrentFilePath 通过文件路径创建 BitTorrentFile | ||
func NewBitTorrentFilePath(torrentFilePath string) (*BitTorrentFile, error) { | ||
// io.Reader | ||
fileHeader, err := os.Open(torrentFilePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return NewBitTorrentFile(fileHeader) | ||
} | ||
|
||
// NewBitTorrentFile 通过文件创建 BitTorrentFile | ||
func NewBitTorrentFile(fileReader *os.File) (*BitTorrentFile, error) { | ||
decoder := bencode.NewDecoder(fileReader) | ||
|
||
bencodeTorrent := &BitTorrentFile{} | ||
decodeErr := decoder.Decode(bencodeTorrent) | ||
if decodeErr != nil { | ||
return nil, decodeErr | ||
} | ||
return bencodeTorrent, nil | ||
} | ||
|
||
// GetHash 获取 torrent 文件的 hash | ||
func (bencodeTorrent *BitTorrentFile) GetHash() string { | ||
// marshal info part and calculate SHA1 | ||
marshaledInfo, marshalErr := bencode.Marshal(bencodeTorrent.Info) | ||
if marshalErr != nil { | ||
return "" | ||
} | ||
return fmt.Sprintf("%x", sha1.Sum(marshaledInfo)) | ||
} | ||
|
||
// Repack 重新打包 torrent 文件 | ||
func (bencodeTorrent *BitTorrentFile) Repack(editStrategy *BitTorrentFileEditStrategy) error { | ||
// Re-pack torrent | ||
if editStrategy.Comment != "" { | ||
bencodeTorrent.Comment = editStrategy.Comment | ||
} | ||
if editStrategy.InfoSource != "" { | ||
bencodeTorrent.Info.Source = editStrategy.InfoSource | ||
} | ||
|
||
bencodeTorrent.Info.Private = false | ||
|
||
return nil | ||
} | ||
|
||
// ConvertToBytes 将 torrent 文件转换为字节 | ||
func (bencodeTorrent *BitTorrentFile) ConvertToBytes() ([]byte, error) { | ||
// Marshal the entire torrent file | ||
marshaledTorrent, marshalErr := bencode.Marshal(bencodeTorrent) | ||
if marshalErr != nil { | ||
return nil, marshalErr | ||
} | ||
|
||
return marshaledTorrent, nil | ||
} | ||
|
||
// SaveTo 将 torrent 文件保存到指定路径 | ||
func (bencodeTorrent *BitTorrentFile) SaveTo(filePath string) error { | ||
// Marshal the entire torrent file | ||
marshaledTorrent, marshalErr := bencode.Marshal(bencodeTorrent) | ||
if marshalErr != nil { | ||
return marshalErr | ||
} | ||
|
||
// Write to file | ||
file, err := os.Create(filePath) | ||
if err != nil { | ||
return err | ||
} | ||
defer func(file *os.File) { | ||
err := file.Close() | ||
if err != nil { | ||
fmt.Println("Failed to close file", file.Name()) | ||
} | ||
}(file) | ||
|
||
_, err = file.Write(marshaledTorrent) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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,98 @@ | ||
package torrent | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const testTorrent = "test_torrents/test.torrent" | ||
const testTorrentFolder = "test_torrents/test_folder.torrent" | ||
const testTorrentSave = "test_torrents/test_save.torrent" | ||
|
||
func TestTorrentHash(t *testing.T) { | ||
torrentFilePath := testTorrent | ||
|
||
torrent, err := NewBitTorrentFilePath(torrentFilePath) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
err = torrent.Repack(&BitTorrentFileEditStrategy{}) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
assert.Equal(t, torrent.GetHash(), "7f3956e5a15b34b62159727c08f944c7e433ad1e") | ||
assert.Equal(t, torrent.Info.Name, "lenna.jpg") | ||
} | ||
|
||
func TestFolderTorrentHash(t *testing.T) { | ||
torrentFilePath := testTorrentFolder | ||
|
||
torrent, err := NewBitTorrentFilePath(torrentFilePath) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
err = torrent.Repack(&BitTorrentFileEditStrategy{}) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
assert.Equal(t, torrent.GetHash(), "0f8cd84ebb514a4d6975f217c1df129bba080a01") | ||
assert.Equal(t, torrent.Info.Name, "cxkcxk") | ||
} | ||
|
||
func TestRepackTorrent(t *testing.T) { | ||
torrentFilePath := testTorrent | ||
|
||
torrent, err := NewBitTorrentFilePath(torrentFilePath) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
comment := "TensoRaws" | ||
infoSource := "https://github.com/TensoRaws" | ||
|
||
err = torrent.Repack(&BitTorrentFileEditStrategy{ | ||
Comment: comment, | ||
InfoSource: infoSource, | ||
}) | ||
|
||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
assert.Equal(t, torrent.Comment, comment) | ||
assert.Equal(t, torrent.Info.Source, infoSource) | ||
} | ||
|
||
func TestSaveTorrent(t *testing.T) { | ||
torrentFilePath := testTorrent | ||
saveFilePath := testTorrentSave | ||
|
||
torrent, err := NewBitTorrentFilePath(torrentFilePath) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
err = torrent.SaveTo(saveFilePath) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
torrent, err = NewBitTorrentFilePath(saveFilePath) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
assert.Equal(t, torrent.GetHash(), "7f3956e5a15b34b62159727c08f944c7e433ad1e") | ||
assert.Equal(t, torrent.Info.Name, "lenna.jpg") | ||
} |
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,37 @@ | ||
package torrent | ||
|
||
var TRACKER_LIST = []string{ | ||
"http://1337.abcvg.info:80/announce", | ||
|
||
"http://bt.okmp3.ru:2710/announce", | ||
|
||
"http://ipv6.rer.lol:6969/announce", | ||
|
||
"http://nyaa.tracker.wf:7777/announce", | ||
|
||
"http://t.nyaatracker.com:80/announce", | ||
|
||
"https://tr.burnabyhighstar.com:443/announce", | ||
|
||
"https://tracker.gbitt.info:443/announce", | ||
|
||
"https://tracker.gcrenwp.top:443/announce", | ||
|
||
"https://tracker.kuroy.me:443/announce", | ||
|
||
"https://tracker.lilithraws.org:443/announce", | ||
|
||
"https://tracker.loligirl.cn:443/announce", | ||
|
||
"https://tracker1.520.jp:443/announce", | ||
|
||
"udp://amigacity.xyz:6969/announce", | ||
|
||
"udp://bt1.archive.org:6969/announce", | ||
|
||
"udp://bt2.archive.org:6969/announce", | ||
|
||
"udp://epider.me:6969/announce", | ||
|
||
"wss://tracker.openwebtorrent.com:443/announce", | ||
} |
Oops, something went wrong.