scrape+stats foundation; restructuring

This commit is contained in:
Jimmy Zelinskie 2013-06-23 03:56:28 -04:00
parent 1bc42063ab
commit ccc8897ca8
7 changed files with 235 additions and 93 deletions

20
main.go
View file

@ -18,25 +18,18 @@ import (
var ( var (
profile bool profile bool
configFile string configPath string
) )
func init() { func init() {
flag.BoolVar(&profile, "profile", false, "Generate profiling data for pprof into chihaya.cpu") flag.BoolVar(&profile, "profile", false, "Generate profiling data for pprof into chihaya.cpu")
flag.StringVar(&configFile, "config", "", "The location of a valid configuration file.") flag.StringVar(&configPath, "config", "", "The location of a valid configuration file.")
} }
func main() { func main() {
flag.Parse() flag.Parse()
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
if configFile != "" {
conf, err := config.New(configFile)
if err != nil {
log.Fatalf("Failed to parse configuration file: %s\n", err)
}
}
if profile { if profile {
log.Println("Running with profiling enabled") log.Println("Running with profiling enabled")
f, err := os.Create("chihaya.cpu") f, err := os.Create("chihaya.cpu")
@ -47,6 +40,13 @@ func main() {
pprof.StartCPUProfile(f) pprof.StartCPUProfile(f)
} }
if configPath == "" {
log.Fatalf("Must specify a configuration file")
}
conf, err := config.New(configPath)
if err != nil {
log.Fatalf("Failed to parse configuration file: %s\n", err)
}
s := server.New(conf) s := server.New(conf)
go func() { go func() {
@ -68,7 +68,7 @@ func main() {
os.Exit(0) os.Exit(0)
}() }()
err = s.Start() err = s.ListenAndServe()
if err != nil { if err != nil {
log.Fatalf("Failed to start server: %s\n", err) log.Fatalf("Failed to start server: %s\n", err)
} }

View file

@ -7,56 +7,57 @@ package server
import ( import (
"errors" "errors"
"fmt" "fmt"
"log"
"net/http" "net/http"
"path" "path"
"github.com/pushrax/chihaya/config" "github.com/pushrax/chihaya/config"
) )
func (h *handler) serveAnnounce(w http.ResponseWriter, r *http.Request) { func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
passkey, action := path.Split(r.URL.Path) passkey, _ := path.Split(r.URL.Path)
user, err := validatePasskey(passkey, h.storage) user, err := validatePasskey(passkey, s.storage)
if err != nil { if err != nil {
fail(err, w) fail(err, w, r)
return return
} }
pq, err := parseQuery(r.URL.RawQuery) pq, err := parseQuery(r.URL.RawQuery)
if err != nil { if err != nil {
fail(errors.New("Error parsing query"), w) fail(errors.New("Error parsing query"), w, r)
return return
} }
ip, err := pq.determineIP(r) ip, err := pq.determineIP(r)
if err != nil { if err != nil {
fail(err, w) fail(err, w, r)
return return
} }
err = validateParsedQuery(pq) err = validateParsedQuery(pq)
if err != nil { if err != nil {
fail(errors.New("Malformed request"), w) fail(errors.New("Malformed request"), w, r)
return return
} }
if !whitelisted(pq.params["peerId"], h.conf) { if !whitelisted(pq.params["peerId"], s.conf) {
fail(errors.New("Your client is not approved"), w) fail(errors.New("Your client is not approved"), w, r)
return return
} }
torrent, exists, err := h.storage.FindTorrent(pq.params["infohash"]) torrent, exists, err := s.storage.FindTorrent(pq.params["infohash"])
if err != nil { if err != nil {
panic("server: failed to find torrent") log.Panicf("server: %s", err)
} }
if !exists { if !exists {
fail(errors.New("This torrent does not exist"), w) fail(errors.New("This torrent does not exist"), w, r)
return return
} }
if left, _ := pq.getUint64("left"); torrent.Status == 1 && left == 0 { if left, _ := pq.getUint64("left"); torrent.Status == 1 && left == 0 {
err := h.storage.UnpruneTorrent(torrent) err := s.storage.UnpruneTorrent(torrent)
if err != nil { if err != nil {
panic("server: failed to unprune torrent") log.Panicf("server: %s", err)
} }
torrent.Status = 0 torrent.Status = 0
} else if torrent.Status != 0 { } else if torrent.Status != 0 {
@ -67,14 +68,34 @@ func (h *handler) serveAnnounce(w http.ResponseWriter, r *http.Request) {
left, left,
), ),
w, w,
r,
) )
return return
} }
// TODO // TODO continue
} }
func whitelisted(peerId string, conf *config.Config) bool { func whitelisted(peerId string, conf *config.Config) bool {
// TODO var (
widLen int
matched bool
)
for _, whitelistedId := range conf.Whitelist {
widLen = len(whitelistedId)
if widLen <= len(peerId) {
matched = true
for i := 0; i < widLen; i++ {
if peerId[i] != whitelistedId[i] {
matched = false
break
}
}
if matched {
return true
}
}
}
return false return false
} }

View file

@ -124,6 +124,7 @@ func validateParsedQuery(pq *parsedQuery) error {
return nil return nil
} }
// TODO IPv6 support
func (pq *parsedQuery) determineIP(r *http.Request) (string, error) { func (pq *parsedQuery) determineIP(r *http.Request) (string, error) {
ip, ok := pq.params["ip"] ip, ok := pq.params["ip"]
if !ok { if !ok {

106
server/scrape.go Normal file
View file

@ -0,0 +1,106 @@
package server
import (
"errors"
"fmt"
"io"
"net/http"
"path"
"strconv"
"time"
"github.com/pushrax/chihaya/storage"
)
func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request) {
passkey, _ := path.Split(r.URL.Path)
user, err := validatePasskey(passkey, s.storage)
if err != nil {
fail(err, w, r)
return
}
pq, err := parseQuery(r.URL.RawQuery)
if err != nil {
fail(errors.New("Error parsing query"), w, r)
return
}
io.WriteString(w, "d")
bencode(w, "files")
if pq.infohashes != nil {
for _, infohash := range pq.infohashes {
torrent, exists, err := s.storage.FindTorrent(infohash)
if err != nil {
panic("server: failed to find torrent")
}
if exists {
bencode(w, infohash)
writeScrapeInfo(w, torrent)
}
}
} else if infohash, exists := pq.params["info_hash"]; exists {
torrent, exists, err := s.storage.FindTorrent(infohash)
if err != nil {
panic("server: failed to find torrent")
}
if exists {
bencode(w, infohash)
writeScrapeInfo(w, torrent)
}
}
io.WriteString(w, "e")
finalizeResponse(w, r)
}
func writeScrapeInfo(w io.Writer, torrent *storage.Torrent) {
io.WriteString(w, "d")
bencode(w, "complete")
bencode(w, len(torrent.Seeders))
bencode(w, "downloaded")
bencode(w, torrent.Snatched)
bencode(w, "incomplete")
bencode(w, len(torrent.Leechers))
io.WriteString(w, "e")
}
func bencode(w io.Writer, data interface{}) {
switch v := data.(type) {
case string:
str := fmt.Sprintf("%s:%s", strconv.Itoa(len(v)), v)
io.WriteString(w, str)
case int:
str := fmt.Sprintf("i%se", strconv.Itoa(v))
io.WriteString(w, str)
case uint:
str := fmt.Sprintf("i%se", strconv.FormatUint(uint64(v), 10))
io.WriteString(w, str)
case int64:
str := fmt.Sprintf("i%se", strconv.FormatInt(v, 10))
io.WriteString(w, str)
case uint64:
str := fmt.Sprintf("i%se", strconv.FormatUint(v, 10))
io.WriteString(w, str)
case time.Duration: // Assume seconds
str := fmt.Sprintf("i%se", strconv.FormatInt(int64(v/time.Second), 10))
io.WriteString(w, str)
case map[string]interface{}:
io.WriteString(w, "d")
for key, val := range v {
str := fmt.Sprintf("%s:%s", strconv.Itoa(len(key)), key)
io.WriteString(w, str)
bencode(w, val)
}
io.WriteString(w, "e")
case []string:
io.WriteString(w, "l")
for _, val := range v {
bencode(w, val)
}
io.WriteString(w, "e")
default:
// Although not currently necessary,
// should handle []interface{} manually; Go can't do it implicitly
panic("Tried to bencode an unsupported type!")
}
}

View file

@ -14,6 +14,7 @@ import (
"strconv" "strconv"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time"
"github.com/pushrax/chihaya/config" "github.com/pushrax/chihaya/config"
"github.com/pushrax/chihaya/storage" "github.com/pushrax/chihaya/storage"
@ -23,125 +24,104 @@ type Server struct {
conf *config.Config conf *config.Config
listener net.Listener listener net.Listener
storage storage.Storage storage storage.Storage
terminated *bool
waitgroup *sync.WaitGroup serving bool
startTime time.Time
deltaRequests int64
rpm int64
waitgroup sync.WaitGroup
http.Server http.Server
} }
func New(conf *config.Config) (*Server, error) { func New(conf *config.Config) (*Server, error) {
var (
wg sync.WaitGroup
terminated bool
)
store, err := storage.New(&conf.Storage) store, err := storage.New(&conf.Storage)
if err != nil { if err != nil {
return nil, err return nil, err
} }
handler := &handler{
conf: conf,
storage: store,
terminated: &terminated,
waitgroup: &wg,
}
s := &Server{ s := &Server{
conf: conf, conf: conf,
storage: store, storage: store,
terminated: &terminated, Server: http.Server{
waitgroup: &wg, Addr: conf.Addr,
},
} }
s.Server.Handler = s
s.Server.Addr = conf.Addr
s.Server.Handler = handler
return s, nil return s, nil
} }
func (s *Server) Start() error { func (s *Server) ListenAndServe() error {
listener, err := net.Listen("tcp", s.conf.Addr) listener, err := net.Listen("tcp", s.Addr)
s.listener = listener
if err != nil { if err != nil {
return err return err
} }
*s.terminated = false s.serving = true
s.startTime = time.Now()
go s.updateRPM()
s.Serve(s.listener) s.Serve(s.listener)
s.waitgroup.Wait() s.waitgroup.Wait()
return nil return nil
} }
func (s *Server) Stop() error { func (s *Server) Stop() error {
*s.terminated = true s.serving = false
s.waitgroup.Wait() s.waitgroup.Wait()
err := s.storage.Shutdown() err := s.storage.Close()
if err != nil { if err != nil {
return err return err
} }
return s.listener.Close() return s.listener.Close()
} }
type handler struct { func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
conf *config.Config if !s.serving {
deltaRequests int64
storage storage.Storage
terminated *bool
waitgroup *sync.WaitGroup
}
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if *h.terminated {
return return
} }
h.waitgroup.Add(1) s.waitgroup.Add(1)
defer h.waitgroup.Done() defer s.waitgroup.Done()
defer atomic.AddInt64(&s.deltaRequests, 1)
defer finalizeResponse(w, r)
if r.URL.Path == "/stats" { _, action := path.Split(r.URL.Path)
h.serveStats(w, r)
return
}
passkey, action := path.Split(r.URL.Path)
switch action { switch action {
case "announce": case "announce":
h.serveAnnounce(w, r) s.serveAnnounce(w, r)
return return
case "scrape": case "scrape":
// TODO s.serveScrape(w, r)
h.serveScrape(w, r)
return return
default: default:
written := fail(errors.New("Unknown action"), w) fail(errors.New("Unknown action"), w, r)
h.finalizeResponse(w, r, written)
return return
} }
} }
func (h *handler) finalizeResponse( func finalizeResponse(w http.ResponseWriter, r *http.Request) {
w http.ResponseWriter,
r *http.Request,
written int,
) {
r.Close = true r.Close = true
w.Header().Add("Content-Type", "text/plain") w.Header().Add("Content-Type", "text/plain")
w.Header().Add("Connection", "close") w.Header().Add("Connection", "close")
w.Header().Add("Content-Length", strconv.Itoa(written))
w.(http.Flusher).Flush() w.(http.Flusher).Flush()
atomic.AddInt64(&h.deltaRequests, 1)
} }
func fail(err error, w http.ResponseWriter) int { func fail(err error, w http.ResponseWriter, r *http.Request) {
e := err.Error() errmsg := err.Error()
message := fmt.Sprintf( message := fmt.Sprintf(
"%s%s%s%s%s", "%s%s%s%s%s",
"d14:failure reason", "d14:failure reason",
strconv.Itoa(len(e)), strconv.Itoa(len(errmsg)),
':', ":",
e, errmsg,
'e', "e",
) )
written, _ := io.WriteString(w, message) io.WriteString(w, message)
return written
} }
func validatePasskey(dir string, s storage.Storage) (*storage.User, error) { func validatePasskey(dir string, s storage.Storage) (*storage.User, error) {

34
server/stats.go Normal file
View file

@ -0,0 +1,34 @@
// 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
import (
"encoding/json"
"net/http"
"sync/atomic"
"time"
"github.com/pushrax/chihaya/config"
)
type stats struct {
Uptime config.Duration `json:"uptime"`
RPM int64 `json:"req_per_min"`
}
func (s *Server) serveStats(w http.ResponseWriter, r *http.Request) {
stats, _ := json.Marshal(&stats{
config.Duration{time.Now().Sub(s.startTime)},
s.rpm,
})
w.Write(stats)
}
func (s *Server) updateRPM() {
for _ = range time.NewTicker(time.Minute).C {
s.rpm = s.deltaRequests
atomic.StoreInt64(&s.deltaRequests, 0)
}
}

View file

@ -42,7 +42,7 @@ func New(conf *config.Storage) (Storage, error) {
} }
type Storage interface { type Storage interface {
Shutdown() error Close() error
FindUser(passkey string) (*User, bool, error) FindUser(passkey string) (*User, bool, error)
FindTorrent(infohash string) (*Torrent, bool, error) FindTorrent(infohash string) (*Torrent, bool, error)