2013-06-22 03:43:11 +02:00
|
|
|
// Copyright 2013 The Chihaya Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
2013-06-24 04:34:13 +02:00
|
|
|
// Package server implements a BitTorrent tracker
|
2013-06-22 01:31:32 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2013-06-22 03:43:11 +02:00
|
|
|
"io"
|
2013-06-23 12:21:59 +02:00
|
|
|
"log"
|
2013-06-22 01:31:32 +02:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2013-06-23 09:56:28 +02:00
|
|
|
"time"
|
2013-06-22 01:31:32 +02:00
|
|
|
|
2013-06-22 03:43:11 +02:00
|
|
|
"github.com/pushrax/chihaya/config"
|
2013-10-04 10:19:43 +02:00
|
|
|
"github.com/pushrax/chihaya/storage"
|
2013-10-04 04:10:46 +02:00
|
|
|
"github.com/pushrax/chihaya/storage/tracker"
|
2013-06-22 01:31:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2013-07-25 16:03:04 +02:00
|
|
|
conf *config.Config
|
|
|
|
listener net.Listener
|
2013-10-04 04:10:46 +02:00
|
|
|
dbConnPool tracker.Pool
|
2013-06-23 09:56:28 +02:00
|
|
|
|
|
|
|
serving bool
|
|
|
|
startTime time.Time
|
|
|
|
|
|
|
|
deltaRequests int64
|
|
|
|
rpm int64
|
|
|
|
|
|
|
|
waitgroup sync.WaitGroup
|
2013-06-22 03:43:11 +02:00
|
|
|
|
2013-08-05 05:01:25 +02:00
|
|
|
pubChan chan string
|
|
|
|
|
2013-06-22 01:31:32 +02:00
|
|
|
http.Server
|
|
|
|
}
|
|
|
|
|
2013-06-22 03:43:11 +02:00
|
|
|
func New(conf *config.Config) (*Server, error) {
|
2013-10-04 04:10:46 +02:00
|
|
|
pool, err := tracker.Open(&conf.Cache)
|
2013-06-22 03:43:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &Server{
|
2013-07-25 16:03:04 +02:00
|
|
|
conf: conf,
|
|
|
|
dbConnPool: pool,
|
2013-08-05 05:01:25 +02:00
|
|
|
pubChan: make(chan string),
|
2013-06-23 09:56:28 +02:00
|
|
|
Server: http.Server{
|
2013-06-23 12:21:59 +02:00
|
|
|
Addr: conf.Addr,
|
|
|
|
ReadTimeout: conf.ReadTimeout.Duration,
|
2013-06-23 09:56:28 +02:00
|
|
|
},
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
2013-06-23 09:56:28 +02:00
|
|
|
s.Server.Handler = s
|
2013-06-22 03:43:11 +02:00
|
|
|
|
|
|
|
return s, nil
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
2013-06-23 09:56:28 +02:00
|
|
|
func (s *Server) ListenAndServe() error {
|
|
|
|
listener, err := net.Listen("tcp", s.Addr)
|
|
|
|
s.listener = listener
|
2013-06-22 01:31:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-06-23 09:56:28 +02:00
|
|
|
s.serving = true
|
|
|
|
s.startTime = time.Now()
|
|
|
|
|
2013-08-13 08:38:00 +02:00
|
|
|
go s.updateStats()
|
2013-06-22 01:31:32 +02:00
|
|
|
s.Serve(s.listener)
|
2013-06-23 09:56:28 +02:00
|
|
|
|
2013-06-22 03:43:11 +02:00
|
|
|
s.waitgroup.Wait()
|
2013-06-22 01:31:32 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Stop() error {
|
2013-06-23 09:56:28 +02:00
|
|
|
s.serving = false
|
2013-06-22 03:43:11 +02:00
|
|
|
s.waitgroup.Wait()
|
2013-07-25 16:03:04 +02:00
|
|
|
err := s.dbConnPool.Close()
|
2013-06-22 03:43:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-08-05 05:01:25 +02:00
|
|
|
close(s.pubChan)
|
2013-06-22 03:43:11 +02:00
|
|
|
return s.listener.Close()
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
2013-06-23 09:56:28 +02:00
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !s.serving {
|
2013-06-22 01:31:32 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-23 09:56:28 +02:00
|
|
|
s.waitgroup.Add(1)
|
|
|
|
defer s.waitgroup.Done()
|
|
|
|
defer atomic.AddInt64(&s.deltaRequests, 1)
|
2013-06-22 01:31:32 +02:00
|
|
|
|
2013-08-29 05:44:45 +02:00
|
|
|
switch r.URL.Path {
|
|
|
|
case "/stats":
|
2013-06-23 12:21:59 +02:00
|
|
|
s.serveStats(w, r)
|
|
|
|
return
|
2013-08-29 05:44:45 +02:00
|
|
|
case "/add":
|
|
|
|
s.serveAdd(w, r)
|
|
|
|
return
|
|
|
|
case "/remove":
|
|
|
|
s.serveRemove(w, r)
|
|
|
|
return
|
2013-06-23 12:21:59 +02:00
|
|
|
}
|
|
|
|
|
2013-06-23 09:56:28 +02:00
|
|
|
_, action := path.Split(r.URL.Path)
|
2013-06-22 01:31:32 +02:00
|
|
|
switch action {
|
|
|
|
case "announce":
|
2013-06-23 09:56:28 +02:00
|
|
|
s.serveAnnounce(w, r)
|
2013-06-22 01:31:32 +02:00
|
|
|
return
|
|
|
|
case "scrape":
|
2013-06-23 09:56:28 +02:00
|
|
|
s.serveScrape(w, r)
|
2013-06-22 01:31:32 +02:00
|
|
|
return
|
|
|
|
default:
|
2013-06-24 04:34:13 +02:00
|
|
|
fail(errors.New("Unknown action"), w, r)
|
2013-06-22 01:31:32 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-24 04:34:13 +02:00
|
|
|
func fail(err error, w http.ResponseWriter, r *http.Request) {
|
2013-06-23 09:56:28 +02:00
|
|
|
errmsg := err.Error()
|
2013-07-12 06:36:24 +02:00
|
|
|
message := "d14:failure reason" + strconv.Itoa(len(errmsg)) + ":" + errmsg + "e"
|
2013-06-24 04:34:13 +02:00
|
|
|
length, _ := io.WriteString(w, message)
|
|
|
|
r.Close = true
|
|
|
|
w.Header().Add("Content-Type", "text/plain")
|
|
|
|
w.Header().Add("Content-Length", string(length))
|
|
|
|
w.Header().Add("Connection", "close")
|
|
|
|
w.(http.Flusher).Flush()
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 10:19:43 +02:00
|
|
|
func validateUser(tx tracker.Conn, dir string) (*storage.User, error) {
|
2013-06-22 01:31:32 +02:00
|
|
|
if len(dir) != 34 {
|
2013-07-05 12:50:52 +02:00
|
|
|
return nil, errors.New("Passkey is invalid")
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
passkey := dir[1:33]
|
|
|
|
|
2013-07-25 16:03:04 +02:00
|
|
|
user, exists, err := tx.FindUser(passkey)
|
2013-06-22 01:31:32 +02:00
|
|
|
if err != nil {
|
2013-06-23 12:21:59 +02:00
|
|
|
log.Panicf("server: %s", err)
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
if !exists {
|
2013-08-13 08:38:00 +02:00
|
|
|
return nil, errors.New("User not found")
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|