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 (
|
2013-11-24 06:49:20 +01:00
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/etix/stoppableListener"
|
2014-07-01 05:21:08 +02:00
|
|
|
"github.com/golang/glog"
|
2013-11-24 06:49:20 +01:00
|
|
|
|
2013-12-01 04:39:02 +01:00
|
|
|
"github.com/chihaya/chihaya/config"
|
2014-06-24 04:47:43 +02:00
|
|
|
"github.com/chihaya/chihaya/drivers/backend"
|
|
|
|
"github.com/chihaya/chihaya/drivers/tracker"
|
2013-06-22 01:31:32 +02:00
|
|
|
)
|
|
|
|
|
2014-05-01 06:30:46 +02:00
|
|
|
// Server represents BitTorrent tracker server.
|
2013-06-22 01:31:32 +02:00
|
|
|
type Server struct {
|
2014-05-08 07:09:02 +02:00
|
|
|
conf *config.Config
|
|
|
|
|
2014-05-08 12:48:32 +02:00
|
|
|
// These are open connections/pools.
|
2014-02-23 05:47:11 +01:00
|
|
|
listener *stoppableListener.StoppableListener
|
|
|
|
trackerPool tracker.Pool
|
2014-04-20 08:25:21 +02:00
|
|
|
backendConn backend.Conn
|
2013-06-23 09:56:28 +02:00
|
|
|
|
2014-05-08 07:09:02 +02:00
|
|
|
// These are for collecting stats.
|
|
|
|
startTime time.Time
|
2013-11-24 06:49:20 +01:00
|
|
|
deltaRequests int64
|
|
|
|
rpm int64
|
2013-06-23 09:56:28 +02:00
|
|
|
|
2013-11-24 06:49:20 +01:00
|
|
|
http.Server
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
2014-05-01 06:30:46 +02:00
|
|
|
// New creates a new Server.
|
2013-06-22 03:43:11 +02:00
|
|
|
func New(conf *config.Config) (*Server, error) {
|
2014-02-23 05:47:11 +01:00
|
|
|
trackerPool, err := tracker.Open(&conf.Tracker)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-08 07:09:02 +02:00
|
|
|
|
2014-02-23 05:47:11 +01:00
|
|
|
backendConn, err := backend.Open(&conf.Backend)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-08 07:09:02 +02:00
|
|
|
|
2013-11-24 06:49:20 +01:00
|
|
|
s := &Server{
|
2014-02-23 05:47:11 +01:00
|
|
|
conf: conf,
|
|
|
|
trackerPool: trackerPool,
|
2014-04-20 08:25:21 +02:00
|
|
|
backendConn: backendConn,
|
2013-11-24 06:49:20 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-05-01 06:30:46 +02:00
|
|
|
// ListenAndServe starts listening and handling incoming HTTP requests.
|
2013-06-23 09:56:28 +02:00
|
|
|
func (s *Server) ListenAndServe() error {
|
2013-11-24 06:49:20 +01:00
|
|
|
l, err := net.Listen("tcp", s.Addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-05-08 07:09:02 +02:00
|
|
|
|
2013-11-24 06:49:20 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-05-01 06:30:46 +02:00
|
|
|
// Stop cleanly ends the handling of incoming HTTP requests.
|
2013-06-22 01:31:32 +02:00
|
|
|
func (s *Server) Stop() error {
|
2014-05-08 07:09:02 +02:00
|
|
|
// Wait for current requests to finish being handled.
|
2013-11-24 06:49:20 +01:00
|
|
|
s.listener.Stop <- true
|
2014-05-08 07:09:02 +02:00
|
|
|
|
2014-02-23 05:47:11 +01:00
|
|
|
err := s.trackerPool.Close()
|
2013-11-24 06:49:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-05-08 07:09:02 +02:00
|
|
|
|
|
|
|
err = s.backendConn.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-11-24 06:49:20 +01: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) {
|
2013-11-24 06:49:20 +01:00
|
|
|
defer atomic.AddInt64(&s.deltaRequests, 1)
|
|
|
|
r.Close = true
|
|
|
|
|
|
|
|
_, action := path.Split(r.URL.Path)
|
|
|
|
switch action {
|
|
|
|
case "announce":
|
|
|
|
s.serveAnnounce(w, r)
|
|
|
|
return
|
|
|
|
case "scrape":
|
|
|
|
s.serveScrape(w, r)
|
|
|
|
return
|
2013-12-06 02:24:54 +01:00
|
|
|
case "stats":
|
|
|
|
s.serveStats(w, r)
|
|
|
|
return
|
2013-11-24 06:49:20 +01:00
|
|
|
default:
|
2014-05-01 06:30:46 +02:00
|
|
|
fail(errors.New("unknown action"), w, r)
|
2013-11-24 06:49:20 +01:00
|
|
|
return
|
|
|
|
}
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
2013-06-24 04:34:13 +02:00
|
|
|
func fail(err error, w http.ResponseWriter, r *http.Request) {
|
2013-11-24 06:49:20 +01:00
|
|
|
errmsg := err.Error()
|
2014-05-08 07:09:02 +02:00
|
|
|
msg := "d14:failure reason" + strconv.Itoa(len(errmsg)) + ":" + errmsg + "e"
|
|
|
|
length, _ := io.WriteString(w, msg)
|
2013-11-24 06:49:20 +01:00
|
|
|
w.Header().Add("Content-Length", string(length))
|
|
|
|
|
2014-06-24 04:47:43 +02:00
|
|
|
w.(http.Flusher).Flush()
|
2014-06-26 23:10:39 +02:00
|
|
|
|
2014-07-01 05:21:08 +02:00
|
|
|
glog.V(5).Infof(
|
2014-06-26 23:10:39 +02:00
|
|
|
"failed request: ip: %s failure: %s",
|
|
|
|
r.RemoteAddr,
|
|
|
|
errmsg,
|
|
|
|
)
|
2013-11-05 06:31:20 +01:00
|
|
|
}
|