-
Notifications
You must be signed in to change notification settings - Fork 0
/
torznab.go
178 lines (160 loc) · 4.52 KB
/
torznab.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package server
import (
"fmt"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/sp0x/torrentd/indexer"
"github.com/sp0x/torrentd/indexer/cache"
"github.com/sp0x/torrentd/indexer/search"
"github.com/sp0x/torrentd/torznab"
"net/http"
"net/url"
"strings"
"time"
)
func (s *Server) aggregatesStatus(c *gin.Context) {
aggregate, err := s.indexerFacade.Scope.Lookup(s.config, "all")
if err != nil {
_ = c.Error(err)
return
}
statusObj := struct {
ActiveIndexers []string `json:"latest"`
}{}
for _, ixr := range aggregate.(*indexer.Aggregate).Indexers {
ixrInfo := ixr.Info()
statusObj.ActiveIndexers = append(statusObj.ActiveIndexers, ixrInfo.GetTitle())
}
c.JSON(200, statusObj)
}
var searchCache, _ = cache.NewTTL(100, 1*time.Hour)
//Handle queries
func (s *Server) torznabHandler(c *gin.Context) {
_ = c.Params
indexerID := indexer.ResolveIndexId(s.indexerFacade.Scope, c.Param("searchIndex"))
t := c.Query("t")
searchIndex, err := s.indexerFacade.Scope.Lookup(s.config, indexerID)
if err != nil {
torznab.Error(c, err.Error(), torznab.ErrIncorrectParameter)
return
}
if t == "caps" {
searchIndex.Capabilities().ServeHTTP(c.Writer, c.Request)
return
}
apiKey := c.Query("apikey")
if !s.checkAPIKey(apiKey) {
torznab.Error(c, "Invalid apikey parameter", torznab.ErrInsufficientPrivs)
return
}
if t == "" {
http.Redirect(c.Writer, c.Request, c.Request.URL.Path+"?t=caps", http.StatusTemporaryRedirect)
return
}
switch t {
case "search", "tvsearch", "tv-search", "movie", "movie-search", "moviesearch":
query, err := torznab.ParseQuery(c.Request.URL.Query())
if err != nil {
torznab.Error(c, "Invalid query", torznab.ErrInsufficientPrivs)
return
}
var feed *torznab.ResultFeed
if cachedFeed, ok := searchCache.Get(query.UniqueKey()); ok {
feed = cachedFeed.(*torznab.ResultFeed)
} else {
feed, err = s.torznabSearch(c.Request, query, searchIndex)
searchCache.Add(query.UniqueKey(), feed)
}
if err != nil {
torznab.Error(c, err.Error(), torznab.ErrUnknownError)
return
}
switch c.Query("format") {
case "atom":
atomOutput(c, feed, searchIndex.GetEncoding())
case "", "xml":
xmlOutput(c, feed, searchIndex.GetEncoding())
case "json":
jsonOutput(c.Writer, feed, searchIndex.GetEncoding())
}
default:
torznab.Error(c, "Unknown type parameter", torznab.ErrIncorrectParameter)
}
}
func formatEncoding(nm string) string {
nm = strings.Replace(nm, "ows12", "ows-12", -1)
nm = strings.Title(nm)
return nm
}
func (s *Server) torznabSearch(r *http.Request, query *torznab.Query, indexer indexer.Indexer) (*torznab.ResultFeed, error) {
srch, err := indexer.Search(query, nil)
if err != nil {
return nil, err
}
nfo := indexer.Info()
feed := &torznab.ResultFeed{
Info: torznab.Info{
ID: nfo.GetId(),
Title: nfo.GetTitle(),
Description: "",
Link: nfo.GetLink(),
Language: nfo.GetLanguage(),
Category: "",
},
Items: srch.GetResults(),
}
feed.Info.Category = query.Type
rewritten, err := s.rewriteLinks(r, srch.GetResults())
if err != nil {
return nil, err
}
feed.Items = rewritten
return feed, err
}
//Rewrites the download links so that the download goes through us.
//This is required since only we can access the torrent ( the site might need authorization )
func (s *Server) rewriteLinks(r *http.Request, items []search.ResultItemBase) ([]search.ResultItemBase, error) {
baseURL, err := s.baseURL(r, "/d")
if err != nil {
return nil, err
}
apiKey := s.sharedKey()
// rewrite non-magnet links to use the server
for idx, item := range items {
scrapeItem := item.AsScrapeItem()
if strings.HasPrefix(scrapeItem.Link, "magnet:") {
continue
}
//itemTmp := item
tokenValue, err := getTokenValue(scrapeItem, apiKey)
if err != nil {
return nil, err
}
filename := strings.Replace(item.UUID(), "/", "-", -1)
items[idx].AsScrapeItem().Link = fmt.Sprintf("%s/%s/%s.torrent", baseURL.String(), tokenValue, url.QueryEscape(filename))
}
return items, nil
}
func getTokenValue(item *search.ScrapeResultItem, apiKey []byte) (string, error) {
sourceLink := item.SourceLink
if sourceLink == "" {
sourceLink = item.Link
}
indexerName := ""
if item.Indexer != nil {
indexerName = item.Indexer.Name
} else {
indexerName = item.Site
}
//Encode the site and source of the torrent as a JWT token
t := &token{
IndexName: indexerName,
Link: sourceLink,
}
te, err := t.Encode(apiKey)
if err != nil {
log.Debugf("Error encoding token: %v", err)
return "", err
}
return te, nil
}