Global r.Close = true
This commit is contained in:
parent
86f8199bef
commit
31e618c8d9
3 changed files with 155 additions and 162 deletions
118
server/scrape.go
118
server/scrape.go
|
@ -5,76 +5,72 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
"github.com/pushrax/chihaya/storage"
|
"github.com/pushrax/chihaya/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request) {
|
||||||
// Parse the query
|
// Parse the query
|
||||||
pq, err := parseQuery(r.URL.RawQuery)
|
pq, err := parseQuery(r.URL.RawQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fail(errors.New("Error parsing query"), w, r)
|
fail(errors.New("Error parsing query"), w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start a transaction
|
// Start a transaction
|
||||||
tx, err := s.dbConnPool.Get()
|
tx, err := s.dbConnPool.Get()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find and validate the user
|
// Find and validate the user
|
||||||
passkey, _ := path.Split(r.URL.Path)
|
passkey, _ := path.Split(r.URL.Path)
|
||||||
_, err = validateUser(tx, passkey)
|
_, err = validateUser(tx, passkey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fail(err, w, r)
|
fail(err, w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
io.WriteString(w, "d")
|
io.WriteString(w, "d")
|
||||||
writeBencoded(w, "files")
|
writeBencoded(w, "files")
|
||||||
if pq.Infohashes != nil {
|
if pq.Infohashes != nil {
|
||||||
for _, infohash := range pq.Infohashes {
|
for _, infohash := range pq.Infohashes {
|
||||||
torrent, exists, err := tx.FindTorrent(infohash)
|
torrent, exists, err := tx.FindTorrent(infohash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
if exists {
|
if exists {
|
||||||
writeBencoded(w, infohash)
|
writeBencoded(w, infohash)
|
||||||
writeScrapeInfo(w, torrent)
|
writeScrapeInfo(w, torrent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if infohash, exists := pq.Params["info_hash"]; exists {
|
} else if infohash, exists := pq.Params["info_hash"]; exists {
|
||||||
torrent, exists, err := tx.FindTorrent(infohash)
|
torrent, exists, err := tx.FindTorrent(infohash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
if exists {
|
if exists {
|
||||||
writeBencoded(w, infohash)
|
writeBencoded(w, infohash)
|
||||||
writeScrapeInfo(w, torrent)
|
writeScrapeInfo(w, torrent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
io.WriteString(w, "e")
|
io.WriteString(w, "e")
|
||||||
|
|
||||||
// Finish up and write headers
|
w.(http.Flusher).Flush()
|
||||||
r.Close = true
|
|
||||||
w.Header().Add("Content-Type", "text/plain")
|
|
||||||
w.Header().Add("Connection", "close")
|
|
||||||
w.(http.Flusher).Flush()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeScrapeInfo(w io.Writer, torrent *storage.Torrent) {
|
func writeScrapeInfo(w io.Writer, torrent *storage.Torrent) {
|
||||||
io.WriteString(w, "d")
|
io.WriteString(w, "d")
|
||||||
writeBencoded(w, "complete")
|
writeBencoded(w, "complete")
|
||||||
writeBencoded(w, len(torrent.Seeders))
|
writeBencoded(w, len(torrent.Seeders))
|
||||||
writeBencoded(w, "downloaded")
|
writeBencoded(w, "downloaded")
|
||||||
writeBencoded(w, torrent.Snatches)
|
writeBencoded(w, torrent.Snatches)
|
||||||
writeBencoded(w, "incomplete")
|
writeBencoded(w, "incomplete")
|
||||||
writeBencoded(w, len(torrent.Leechers))
|
writeBencoded(w, len(torrent.Leechers))
|
||||||
io.WriteString(w, "e")
|
io.WriteString(w, "e")
|
||||||
}
|
}
|
||||||
|
|
198
server/server.go
198
server/server.go
|
@ -6,142 +6,140 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/etix/stoppableListener"
|
"github.com/etix/stoppableListener"
|
||||||
|
|
||||||
"github.com/pushrax/chihaya/config"
|
"github.com/pushrax/chihaya/config"
|
||||||
"github.com/pushrax/chihaya/storage"
|
"github.com/pushrax/chihaya/storage"
|
||||||
"github.com/pushrax/chihaya/storage/tracker"
|
"github.com/pushrax/chihaya/storage/tracker"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
conf *config.Config
|
conf *config.Config
|
||||||
listener *stoppableListener.StoppableListener
|
listener *stoppableListener.StoppableListener
|
||||||
dbConnPool tracker.Pool
|
dbConnPool tracker.Pool
|
||||||
|
|
||||||
startTime time.Time
|
startTime time.Time
|
||||||
|
|
||||||
deltaRequests int64
|
deltaRequests int64
|
||||||
rpm int64
|
rpm int64
|
||||||
|
|
||||||
http.Server
|
http.Server
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(conf *config.Config) (*Server, error) {
|
func New(conf *config.Config) (*Server, error) {
|
||||||
pool, err := tracker.Open(&conf.Cache)
|
pool, err := tracker.Open(&conf.Cache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
s := &Server{
|
s := &Server{
|
||||||
conf: conf,
|
conf: conf,
|
||||||
dbConnPool: pool,
|
dbConnPool: pool,
|
||||||
Server: http.Server{
|
Server: http.Server{
|
||||||
Addr: conf.Addr,
|
Addr: conf.Addr,
|
||||||
ReadTimeout: conf.ReadTimeout.Duration,
|
ReadTimeout: conf.ReadTimeout.Duration,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
s.Server.Handler = s
|
s.Server.Handler = s
|
||||||
|
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) ListenAndServe() error {
|
func (s *Server) ListenAndServe() error {
|
||||||
l, err := net.Listen("tcp", s.Addr)
|
l, err := net.Listen("tcp", s.Addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
sl := stoppableListener.Handle(l)
|
sl := stoppableListener.Handle(l)
|
||||||
s.listener = sl
|
s.listener = sl
|
||||||
s.startTime = time.Now()
|
s.startTime = time.Now()
|
||||||
|
|
||||||
go s.updateStats()
|
go s.updateStats()
|
||||||
s.Serve(s.listener)
|
s.Serve(s.listener)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Stop() error {
|
func (s *Server) Stop() error {
|
||||||
s.listener.Stop <- true
|
s.listener.Stop <- true
|
||||||
err := s.dbConnPool.Close()
|
err := s.dbConnPool.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return s.listener.Close()
|
return s.listener.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
defer atomic.AddInt64(&s.deltaRequests, 1)
|
defer atomic.AddInt64(&s.deltaRequests, 1)
|
||||||
|
r.Close = true
|
||||||
|
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/stats":
|
case "/stats":
|
||||||
s.serveStats(w, r)
|
s.serveStats(w, r)
|
||||||
return
|
return
|
||||||
case "/add":
|
case "/add":
|
||||||
s.serveAdd(w, r)
|
s.serveAdd(w, r)
|
||||||
return
|
return
|
||||||
case "/remove":
|
case "/remove":
|
||||||
s.serveRemove(w, r)
|
s.serveRemove(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, action := path.Split(r.URL.Path)
|
_, action := path.Split(r.URL.Path)
|
||||||
switch action {
|
switch action {
|
||||||
case "announce":
|
case "announce":
|
||||||
s.serveAnnounce(w, r)
|
s.serveAnnounce(w, r)
|
||||||
return
|
return
|
||||||
case "scrape":
|
case "scrape":
|
||||||
s.serveScrape(w, r)
|
s.serveScrape(w, r)
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
fail(errors.New("Unknown action"), w, r)
|
fail(errors.New("Unknown action"), w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func fail(err error, w http.ResponseWriter, r *http.Request) {
|
func fail(err error, w http.ResponseWriter, r *http.Request) {
|
||||||
errmsg := err.Error()
|
errmsg := err.Error()
|
||||||
message := "d14:failure reason" + strconv.Itoa(len(errmsg)) + ":" + errmsg + "e"
|
message := "d14:failure reason" + strconv.Itoa(len(errmsg)) + ":" + errmsg + "e"
|
||||||
length, _ := io.WriteString(w, message)
|
length, _ := io.WriteString(w, message)
|
||||||
r.Close = true
|
w.Header().Add("Content-Length", string(length))
|
||||||
w.Header().Add("Content-Type", "text/plain")
|
w.(http.Flusher).Flush()
|
||||||
w.Header().Add("Content-Length", string(length))
|
|
||||||
w.Header().Add("Connection", "close")
|
|
||||||
w.(http.Flusher).Flush()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateUser(tx tracker.Conn, dir string) (*storage.User, error) {
|
func validateUser(tx tracker.Conn, dir string) (*storage.User, error) {
|
||||||
if len(dir) != 34 {
|
if len(dir) != 34 {
|
||||||
return nil, errors.New("Passkey is invalid")
|
return nil, errors.New("Passkey is invalid")
|
||||||
}
|
}
|
||||||
passkey := dir[1:33]
|
passkey := dir[1:33]
|
||||||
|
|
||||||
user, exists, err := tx.FindUser(passkey)
|
user, exists, err := tx.FindUser(passkey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
if !exists {
|
if !exists {
|
||||||
return nil, errors.New("User not found")
|
return nil, errors.New("User not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Takes a peer_id and returns a ClientID
|
// Takes a peer_id and returns a ClientID
|
||||||
func parsePeerID(peerID string) (clientID string) {
|
func parsePeerID(peerID string) (clientID string) {
|
||||||
if peerID[0] == '-' {
|
if peerID[0] == '-' {
|
||||||
clientID = peerID[1:7]
|
clientID = peerID[1:7]
|
||||||
} else {
|
} else {
|
||||||
clientID = peerID[0:6]
|
clientID = peerID[0:6]
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,6 @@ type stats struct {
|
||||||
|
|
||||||
func (s *Server) serveStats(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) serveStats(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.Header().Set("Connection", "close")
|
|
||||||
|
|
||||||
stats, _ := json.Marshal(&stats{
|
stats, _ := json.Marshal(&stats{
|
||||||
config.Duration{time.Now().Sub(s.startTime)},
|
config.Duration{time.Now().Sub(s.startTime)},
|
||||||
|
|
Loading…
Reference in a new issue