tracker/server/server.go

146 lines
2.8 KiB
Go
Raw Normal View History

// 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.
// Package server implements a BitTorrent tracker
2013-06-22 01:31:32 +02:00
package server
import (
2013-11-24 00:33:35 +01:00
"errors"
"io"
"log"
"net"
"net/http"
"path"
"strconv"
"sync/atomic"
"time"
"github.com/etix/stoppableListener"
"github.com/pushrax/chihaya/config"
"github.com/pushrax/chihaya/storage"
"github.com/pushrax/chihaya/storage/tracker"
2013-06-22 01:31:32 +02:00
)
type Server struct {
2013-11-24 00:33:35 +01:00
conf *config.Config
listener *stoppableListener.StoppableListener
dbConnPool tracker.Pool
2013-06-23 09:56:28 +02:00
2013-11-24 00:33:35 +01:00
startTime time.Time
2013-06-23 09:56:28 +02:00
2013-11-24 00:33:35 +01:00
deltaRequests int64
rpm int64
2013-06-23 09:56:28 +02:00
2013-11-24 00:33:35 +01:00
http.Server
2013-06-22 01:31:32 +02:00
}
func New(conf *config.Config) (*Server, error) {
2013-11-24 00:33:35 +01:00
pool, err := tracker.Open(&conf.Cache)
if err != nil {
return nil, err
}
s := &Server{
conf: conf,
dbConnPool: pool,
Server: http.Server{
Addr: conf.Addr,
ReadTimeout: conf.ReadTimeout.Duration,
},
}
s.Server.Handler = s
return s, nil
2013-06-22 01:31:32 +02:00
}
2013-06-23 09:56:28 +02:00
func (s *Server) ListenAndServe() error {
2013-11-24 00:33:35 +01:00
l, err := net.Listen("tcp", s.Addr)
if err != nil {
return err
}
sl := stoppableListener.Handle(l)
s.listener = sl
s.startTime = time.Now()
go s.updateStats()
s.Serve(s.listener)
return nil
2013-06-22 01:31:32 +02:00
}
func (s *Server) Stop() error {
2013-11-24 00:33:35 +01:00
s.listener.Stop <- true
err := s.dbConnPool.Close()
if err != nil {
return err
}
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) {
2013-11-24 00:33:35 +01:00
defer atomic.AddInt64(&s.deltaRequests, 1)
r.Close = true
switch r.URL.Path {
case "/stats":
s.serveStats(w, r)
return
case "/add":
s.serveAdd(w, r)
return
case "/remove":
s.serveRemove(w, r)
return
}
_, action := path.Split(r.URL.Path)
switch action {
case "announce":
s.serveAnnounce(w, r)
return
case "scrape":
s.serveScrape(w, r)
return
default:
fail(errors.New("Unknown action"), w, r)
return
}
2013-06-22 01:31:32 +02:00
}
func fail(err error, w http.ResponseWriter, r *http.Request) {
2013-11-24 00:33:35 +01:00
errmsg := err.Error()
message := "d14:failure reason" + strconv.Itoa(len(errmsg)) + ":" + errmsg + "e"
length, _ := io.WriteString(w, message)
w.Header().Add("Content-Length", string(length))
w.(http.Flusher).Flush()
2013-06-22 01:31:32 +02:00
}
func validateUser(tx tracker.Conn, dir string) (*storage.User, error) {
2013-11-24 00:33:35 +01:00
if len(dir) != 34 {
return nil, errors.New("Passkey is invalid")
}
passkey := dir[1:33]
user, exists, err := tx.FindUser(passkey)
if err != nil {
log.Panicf("server: %s", err)
}
if !exists {
return nil, errors.New("User not found")
}
return user, nil
2013-06-22 01:31:32 +02:00
}
2013-11-05 06:31:20 +01:00
// Takes a peer_id and returns a ClientID
func parsePeerID(peerID string) (clientID string) {
2013-11-24 00:33:35 +01:00
if peerID[0] == '-' {
clientID = peerID[1:7]
} else {
clientID = peerID[0:6]
}
return
2013-11-05 06:31:20 +01:00
}