From 12f16fb0c7717beb01c2046437ac0c0a92b435d6 Mon Sep 17 00:00:00 2001 From: Andrew Zhao Date: Sat, 6 May 2017 18:10:35 -0700 Subject: [PATCH] use util to generate magnets --- config/trackers.go | 13 ++++++++++--- model/torrent.go | 2 +- router/upload.go | 2 -- service/torrent/torrent.go | 8 +++++--- util/magnet.go | 5 ++--- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/config/trackers.go b/config/trackers.go index d89b476f..d045880b 100644 --- a/config/trackers.go +++ b/config/trackers.go @@ -1,5 +1,12 @@ package config -const ( - Trackers = "&tr=udp://tracker.coppersurfer.tk:6969&tr=udp://zer0day.to:1337/announce&tr=udp://tracker.leechers-paradise.org:6969&tr=udp://explodie.org:6969&tr=udp://tracker.opentrackr.org:1337&tr=udp://tracker.internetwarriors.net:1337/announce&tr=udp://eddie4.nl:6969/announce&tr=http://mgtracker.org:6969/announce&tr=http://tracker.baka-sub.cf/announce" -) +var Trackers = []string{ + "udp://tracker.coppersurfer.tk:6969", + "udp://zer0day.to:1337/announce", + "udp://tracker.leechers-paradise.org:6969", + "udp://explodie.org:6969", + "udp://tracker.opentrackr.org:1337", + "udp://tracker.internetwarriors.net:1337/announce", + "udp://eddie4.nl:6969/announce", + "http://mgtracker.org:6969/announce", + "http://tracker.baka-sub.cf/announce"} diff --git a/model/torrent.go b/model/torrent.go index 4399758b..ab0feaae 100644 --- a/model/torrent.go +++ b/model/torrent.go @@ -73,7 +73,7 @@ type TorrentsJson struct { /* Model Conversion to Json */ func (t *Torrents) ToJson() TorrentsJson { - magnet := "magnet:?xt=urn:btih:" + strings.TrimSpace(t.Hash) + "&dn=" + t.Name + config.Trackers + magnet := util.InfoHashToMagnet(strings.TrimSpace(t.Hash), t.Name, config.Trackers...) b := []CommentsJson{} _ = json.Unmarshal([]byte(util.UnZlib(t.Comments)), &b) res := TorrentsJson{ diff --git a/router/upload.go b/router/upload.go index d0993ad0..9eac7dd6 100644 --- a/router/upload.go +++ b/router/upload.go @@ -3,7 +3,6 @@ package router import ( "encoding/hex" "errors" - "fmt" "github.com/ewhal/nyaa/util" "github.com/ewhal/nyaa/util/metainfo" "github.com/zeebo/bencode" @@ -98,7 +97,6 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error { if parseErr != nil { return metainfo.ErrInvalidTorrentFile } - fmt.Println(magnetUrl) exactTopic := magnetUrl.Query().Get("xt") if !strings.HasPrefix(exactTopic, "urn:btih:") { return metainfo.ErrInvalidTorrentFile diff --git a/service/torrent/torrent.go b/service/torrent/torrent.go index 77444d51..8a2c437a 100644 --- a/service/torrent/torrent.go +++ b/service/torrent/torrent.go @@ -1,10 +1,12 @@ package torrentService + import ( + "errors" + "github.com/ewhal/nyaa/config" "github.com/ewhal/nyaa/db" "github.com/ewhal/nyaa/model" - "github.com/ewhal/nyaa/config" + "github.com/ewhal/nyaa/util" "github.com/jinzhu/gorm" - "errors" "strings" ) @@ -30,7 +32,7 @@ func GetFeeds() []model.Feed { for rows.Next() { item := model.Feed{} rows.Scan(&item.Id, &item.Name, &item.Hash, &item.Timestamp) - magnet := "magnet:?xt=urn:btih:" + strings.TrimSpace(item.Hash) + "&dn=" + item.Name + config.Trackers + magnet := util.InfoHashToMagnet(strings.TrimSpace(item.Hash), item.Name, config.Trackers...) item.Magnet = magnet // memory hog result = append(result, item) diff --git a/util/magnet.go b/util/magnet.go index 15dacf13..500278a0 100644 --- a/util/magnet.go +++ b/util/magnet.go @@ -2,17 +2,16 @@ package util import ( "fmt" - "net/url" ) // convert a binary infohash to a magnet uri given a display name and tracker urls -func InfoHashToMagnet(ih string, name string, trackers ...url.URL) (str string) { +func InfoHashToMagnet(ih string, name string, trackers ...string) (str string) { str = fmt.Sprintf("magnet:?xt=urn:btih:%s", ih) if len(name) > 0 { str += fmt.Sprintf("&dn=%s", name) } for idx := range trackers { - str += fmt.Sprintf("&tr=%s", trackers[idx].String()) + str += fmt.Sprintf("&tr=%s", trackers[idx]) } return }