-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
207 lines (166 loc) · 4.51 KB
/
main.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"bytes"
"flag"
"fmt"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
bencode "github.com/jackpal/bencode-go"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"io/ioutil"
)
type Segment struct {
Index int `bencode:"index"`
Start int64 `bencode:"start"`
End int64 `bencode:"end"`
StartTime int64 `bencode:"start_time"`
EndTime int64 `bencode:"end_time"`
}
type Manifest struct {
Duration int64
Bitrate int64
Segments []Segment
}
type MetaInfo struct {
Info InfoDict
InfoHash string
Announce string
AnnounceList [][]string `bencode:"announce-list"`
CreationDate string `bencode:"creation date"`
Comment string
CreatedBy string `bencode:"created by"`
Encoding string
Manifest Manifest
}
type FileDict struct {
Length int64
Path []string
Md5sum string
}
type InfoDict struct {
PieceLength int64 `bencode:"piece length"`
Pieces string
Private int64
Name string
// Single File Mode
Length int64
Md5sum string
// Multiple File mode
Files []FileDict
}
type TorrentService struct {
client *torrent.Client
manifestMap map[string]Manifest
}
func NewTorrentService(c *torrent.Client) *TorrentService {
var ts TorrentService
ts.client = c
ts.manifestMap = make(map[string]Manifest)
return &ts
}
type PostTorrentRequest struct {
File string `json:"file"`
}
type PostTorrentResponse struct {
HexInfoHash string `json:"infoHash"`
}
func PostTorrentHandler(c echo.Context) error {
r := c.Request().Body
b, _ := ioutil.ReadAll(r)
mrs := bytes.NewBuffer(b)
mr := bytes.NewBuffer(b)
m := &MetaInfo{}
err := bencode.Unmarshal(mr, m)
if err != nil {
return c.String(400, fmt.Sprintf("Invalid torrent metadata: %s", err))
}
mi, err := metainfo.Load(mrs)
if err != nil {
return c.String(400, fmt.Sprintf("Invalid torrent passed: %s", err))
}
h := mi.HashInfoBytes().HexString()
ts := c.Get("TorrentService").(*TorrentService)
t, err := ts.client.AddTorrent(mi)
if err != nil {
return c.String(500, fmt.Sprintf("Erroring adding torrent %s", mi.HashInfoBytes().HexString()))
}
ts.manifestMap[h] = m.Manifest
<-t.GotInfo()
resp := PostTorrentResponse{
HexInfoHash: t.InfoHash().HexString(),
}
return c.JSON(200, resp)
}
func GetTorrentHandler(c echo.Context) error {
infoHash := c.Param("infohash")
rangeHeader := c.Request().Header.Get("Range")
var start, end int64 = -1, -1
_, err := fmt.Sscanf(rangeHeader, "bytes=%d-%d", &start, &end)
if err != nil || start < 0 || end < 0 {
return c.String(400, "Must set valid Range header")
}
hash := metainfo.NewHashFromHex(infoHash)
ts := c.Get("TorrentService").(*TorrentService)
t, ok := ts.client.Torrent(hash)
if !ok {
return c.String(500, fmt.Sprintf("Error getting torrent handle: %s", infoHash))
}
r := t.NewReader()
_, err = r.Seek(start, 0)
if err != nil {
return c.String(500, fmt.Sprintf("Error seeking to: %s", start))
}
size := end - start + 1
d := make([]byte, size)
n, err := r.Read(d)
if err != nil {
return c.String(500, fmt.Sprintf("Error reading %s bytes: %s", size))
}
fmt.Printf("Read %d bytes\n", n)
return c.HTMLBlob(200, d)
}
func GetTorrentInfoHandler(c echo.Context) error {
infoHash := c.Param("infohash")
hash := metainfo.NewHashFromHex(infoHash)
ts := c.Get("TorrentService").(*TorrentService)
t, ok := ts.client.Torrent(hash)
if !ok {
return c.String(500, fmt.Sprintf("Error getting torrent handle: %s", infoHash))
}
var b bytes.Buffer
t.Metainfo().Write(&b)
return c.Blob(200, "application/x-bittorrent", b.Bytes())
}
func GetTorrentManifestHandler(c echo.Context) error {
infoHash := c.Param("infohash")
ts := c.Get("TorrentService").(*TorrentService)
manifest := ts.manifestMap[infoHash]
return c.JSON(200, manifest)
}
func TorrentServiceMiddleware(ts *TorrentService) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Set("TorrentService", ts)
return next(c)
}
}
}
var Args struct {
Port int
}
func init() {
flag.IntVar(&Args.Port, "port", 8080, "Port to run HTTP server on")
}
func main() {
c, _ := torrent.NewClient(nil)
ts := NewTorrentService(c)
e := echo.New()
e.Use(TorrentServiceMiddleware(ts))
e.Use(middleware.Static("./static"))
e.GET("/torrent/:infohash/data", GetTorrentHandler)
e.GET("/torrent/:infohash/info", GetTorrentInfoHandler)
e.GET("/torrent/:infohash/manifest", GetTorrentManifestHandler)
e.POST("/torrent", PostTorrentHandler)
e.Logger.Fatal(e.Start(":8012"))
}