tracker/server/server.go

148 lines
2.6 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"
"fmt"
"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-06-22 01:31:32 +02:00
)
type Server struct {
2013-06-23 09:56:28 +02:00
conf *config.Config
listener net.Listener
storage storage.Conn
2013-06-23 09:56:28 +02:00
serving bool
startTime time.Time
deltaRequests int64
rpm int64
waitgroup sync.WaitGroup
2013-06-22 01:31:32 +02:00
http.Server
}
func New(conf *config.Config) (*Server, error) {
store, err := storage.Open(&conf.Storage)
if err != nil {
return nil, err
}
s := &Server{
2013-06-23 09:56:28 +02:00
conf: conf,
storage: store,
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()
go s.updateRPM()
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()
2013-06-23 09:56:28 +02:00
err := s.storage.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) {
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-06-23 12:21:59 +02:00
if r.URL.Path == "/stats" {
s.serveStats(w, r)
return
}
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()
message := fmt.Sprintf(
"%s%s%s%s%s",
"d14:failure reason",
2013-06-23 09:56:28 +02:00
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 validatePasskey(dir string, s storage.Conn) (*storage.User, error) {
2013-06-22 01:31:32 +02:00
if len(dir) != 34 {
return nil, errors.New("Your passkey is invalid")
}
passkey := dir[1:33]
user, exists, err := s.FindUser(passkey)
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 {
return nil, errors.New("Passkey not found")
}
return user, nil
}