tracker/server/server.go

143 lines
2.4 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.
2013-06-22 01:31:32 +02:00
package server
import (
"errors"
"fmt"
"io"
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.Storage
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.New(&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{
Addr: conf.Addr,
},
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)
defer finalizeResponse(w, r)
2013-06-22 01:31:32 +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-23 09:56:28 +02:00
fail(errors.New("Unknown action"), w, r)
2013-06-22 01:31:32 +02:00
return
}
}
2013-06-23 09:56:28 +02:00
func finalizeResponse(w http.ResponseWriter, r *http.Request) {
2013-06-22 01:31:32 +02:00
r.Close = true
w.Header().Add("Content-Type", "text/plain")
w.Header().Add("Connection", "close")
w.(http.Flusher).Flush()
}
2013-06-23 09:56:28 +02:00
func fail(err error, w http.ResponseWriter, r *http.Request) {
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",
)
2013-06-23 09:56:28 +02:00
io.WriteString(w, message)
2013-06-22 01:31:32 +02:00
}
func validatePasskey(dir string, s storage.Storage) (*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 {
return nil, err
}
if !exists {
return nil, errors.New("Passkey not found")
}
return user, nil
}