tracker/server/server.go

152 lines
2.7 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 (
"errors"
"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
"github.com/pushrax/chihaya/config"
"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 {
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-08-05 05:01:25 +02:00
pubChan chan string
2013-06-22 01:31:32 +02:00
http.Server
}
func New(conf *config.Config) (*Server, error) {
2013-10-04 04:10:46 +02:00
pool, err := tracker.Open(&conf.Cache)
if err != nil {
return nil, err
}
s := &Server{
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
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
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
s.waitgroup.Wait()
err := s.dbConnPool.Close()
if err != nil {
return err
}
2013-08-05 05:01:25 +02:00
close(s.pubChan)
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
switch r.URL.Path {
case "/stats":
2013-06-23 12:21:59 +02:00
s.serveStats(w, r)
return
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:
fail(errors.New("Unknown action"), w, r)
2013-06-22 01:31:32 +02:00
return
}
}
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"
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
}
func validateUser(tx tracker.Conn, dir string) (*storage.User, error) {
2013-06-22 01:31:32 +02:00
if len(dir) != 34 {
return nil, errors.New("Passkey is invalid")
2013-06-22 01:31:32 +02:00
}
passkey := dir[1:33]
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
}