copyright updated; imports renamed; misc fixes

This commit is contained in:
Jimmy Zelinskie 2013-06-21 21:43:11 -04:00
parent eee2810da6
commit 1bc42063ab
9 changed files with 176 additions and 129 deletions

View file

@ -1,7 +1,7 @@
chihaya chihaya
======= =======
[![Build Status](https://travis-ci.org/jzelinskie/chihaya.png?branch=master)](https://travis-ci.org/jzelinskie/chihaya) [![Build Status](https://travis-ci.org/pushrax/chihaya.png?branch=master)](https://travis-ci.org/pushrax/chihaya)
chihaya is a high-performance [BitTorrent tracker](http://en.wikipedia.org/wiki/BitTorrent_tracker) written in the Go programming language. chihaya is a high-performance [BitTorrent tracker](http://en.wikipedia.org/wiki/BitTorrent_tracker) written in the Go programming language.
It isn't quite ready for prime-time just yet, but these are the features that it'll have: It isn't quite ready for prime-time just yet, but these are the features that it'll have:
@ -15,7 +15,7 @@ It isn't quite ready for prime-time just yet, but these are the features that it
Installing Installing
---------- ----------
$ go install github.com/jzelinskie/chihaya $ go install github.com/pushrax/chihaya
Configuration Configuration

View file

@ -1,3 +1,7 @@
// 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 config package config
import ( import (
@ -14,12 +18,10 @@ type Config struct {
Announce Duration `json:"announce"` Announce Duration `json:"announce"`
MinAnnounce Duration `json:"min_announce"` MinAnnounce Duration `json:"min_announce"`
BufferPoolSize int `json:"bufferpool_size"`
Whitelist []string `json:"whitelist"` Whitelist []string `json:"whitelist"`
} }
// StorageConfig represents the settings used for storage or cache.
type Storage struct { type Storage struct {
Driver string `json:"driver"` Driver string `json:"driver"`
Protocol string `json:"protocol"` Protocol string `json:"protocol"`

View file

@ -1,11 +1,10 @@
{ {
"addr": ":34000" "addr": ":34000",
"announce": "30m", "announce": "30m",
"min_announce": "15m", "min_announce": "15m",
"freelech": false, "freelech": false,
"private": true, "private": true,
"bufferpool_size": 500,
"storage": { "storage": {
"driver": "redis", "driver": "redis",

View file

@ -12,8 +12,8 @@ import (
"runtime" "runtime"
"runtime/pprof" "runtime/pprof"
"github.com/jzelinskie/chihaya/config" "github.com/pushrax/chihaya/config"
"github.com/jzelinskie/chihaya/server" "github.com/pushrax/chihaya/server"
) )
var ( var (
@ -31,7 +31,7 @@ func main() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
if configFile != "" { if configFile != "" {
conf, err := config.Parse(configFile) conf, err := config.New(configFile)
if err != nil { if err != nil {
log.Fatalf("Failed to parse configuration file: %s\n", err) log.Fatalf("Failed to parse configuration file: %s\n", err)
} }

View file

@ -1,58 +1,59 @@
// 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 package server
import ( import (
"bytes"
"errors" "errors"
"log" "fmt"
"net/http"
"path"
"github.com/jzelinskie/chihaya/config" "github.com/pushrax/chihaya/config"
"github.com/jzelinskie/chihaya/storage"
) )
func (h *handler) serveAnnounce(w *http.ResponseWriter, r *http.Request) { func (h *handler) serveAnnounce(w http.ResponseWriter, r *http.Request) {
buf := h.bufferpool.Take() passkey, action := path.Split(r.URL.Path)
defer h.bufferpool.Give(buf) user, err := validatePasskey(passkey, h.storage)
defer h.writeResponse(&w, r, buf)
user, err := validatePasskey(dir, h.storage)
if err != nil { if err != nil {
fail(err, buf) fail(err, w)
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"), buf) fail(errors.New("Error parsing query"), w)
return return
} }
ip, err := determineIP(r, pq) ip, err := pq.determineIP(r)
if err != nil { if err != nil {
fail(err, buf) fail(err, w)
return return
} }
err := validateParsedQuery(pq) err = validateParsedQuery(pq)
if err != nil { if err != nil {
fail(errors.New("Malformed request"), buf) fail(errors.New("Malformed request"), w)
return return
} }
if !whitelisted(peerId, h.conf) { if !whitelisted(pq.params["peerId"], h.conf) {
fail(errors.New("Your client is not approved"), buf) fail(errors.New("Your client is not approved"), w)
return return
} }
torrent, exists, err := h.storage.FindTorrent(infohash) torrent, exists, err := h.storage.FindTorrent(pq.params["infohash"])
if err != nil { if err != nil {
panic("server: failed to find torrent") panic("server: failed to find torrent")
} }
if !exists { if !exists {
fail(errors.New("This torrent does not exist"), buf) fail(errors.New("This torrent does not exist"), w)
return return
} }
if torrent.Status == 1 && left == 0 { if left, _ := pq.getUint64("left"); torrent.Status == 1 && left == 0 {
err := h.storage.UnpruneTorrent(torrent) err := h.storage.UnpruneTorrent(torrent)
if err != nil { if err != nil {
panic("server: failed to unprune torrent") panic("server: failed to unprune torrent")
@ -65,17 +66,15 @@ func (h *handler) serveAnnounce(w *http.ResponseWriter, r *http.Request) {
torrent.Status, torrent.Status,
left, left,
), ),
buf, w,
) )
return return
} }
//go // TODO
} }
func whitelisted(peerId string, conf config.Config) bool { func whitelisted(peerId string, conf *config.Config) bool {
// TODO Decide if whitelist should be in storage or config // TODO
} return false
func newPeer() {
} }

View file

@ -1,7 +1,13 @@
// 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 package server
import ( import (
"errors" "errors"
"net/http"
"net/url"
"strconv" "strconv"
) )
@ -11,11 +17,11 @@ type parsedQuery struct {
} }
func (pq *parsedQuery) getUint64(key string) (uint64, bool) { func (pq *parsedQuery) getUint64(key string) (uint64, bool) {
str, exists := pq[key] str, exists := pq.params[key]
if !exists { if !exists {
return 0, false return 0, false
} }
val, err := strconv.Uint64(str, 10, 64) val, err := strconv.ParseUint(str, 10, 64)
if err != nil { if err != nil {
return 0, false return 0, false
} }
@ -54,11 +60,11 @@ func parseQuery(query string) (*parsedQuery, error) {
keyStr, err := url.QueryUnescape(query[keyStart : keyEnd+1]) keyStr, err := url.QueryUnescape(query[keyStart : keyEnd+1])
if err != nil { if err != nil {
return err return nil, err
} }
valStr, err := url.QueryUnescape(query[valStart : valEnd+1]) valStr, err := url.QueryUnescape(query[valStart : valEnd+1])
if err != nil { if err != nil {
return err return nil, err
} }
pq.params[keyStr] = valStr pq.params[keyStr] = valStr
@ -67,7 +73,7 @@ func parseQuery(query string) (*parsedQuery, error) {
if hasInfohash { if hasInfohash {
// Multiple infohashes // Multiple infohashes
if pq.infohashes == nil { if pq.infohashes == nil {
pq.infohashes = []string{firstInfoHash} pq.infohashes = []string{firstInfohash}
} }
pq.infohashes = append(pq.infohashes, valStr) pq.infohashes = append(pq.infohashes, valStr)
} else { } else {
@ -87,15 +93,15 @@ func parseQuery(query string) (*parsedQuery, error) {
valEnd = i valEnd = i
} }
} }
return return pq, nil
} }
func validateParsedQuery(pq *parsedQuery) error { func validateParsedQuery(pq *parsedQuery) error {
infohash, ok := pq["info_hash"] infohash, ok := pq.params["info_hash"]
if infohash == "" { if infohash == "" {
return errors.New("infohash does not exist") return errors.New("infohash does not exist")
} }
peerId, ok := pq["peer_id"] peerId, ok := pq.params["peer_id"]
if peerId == "" { if peerId == "" {
return errors.New("peerId does not exist") return errors.New("peerId does not exist")
} }
@ -117,3 +123,29 @@ func validateParsedQuery(pq *parsedQuery) error {
} }
return nil return nil
} }
func (pq *parsedQuery) determineIP(r *http.Request) (string, error) {
ip, ok := pq.params["ip"]
if !ok {
ip, ok = pq.params["ipv4"]
if !ok {
ips, ok := r.Header["X-Real-Ip"]
if ok && len(ips) > 0 {
ip = ips[0]
} else {
portIndex := len(r.RemoteAddr) - 1
for ; portIndex >= 0; portIndex-- {
if r.RemoteAddr[portIndex] == ':' {
break
}
}
if portIndex != -1 {
ip = r.RemoteAddr[0:portIndex]
} else {
return "", errors.New("Failed to parse IP address")
}
}
}
}
return ip, nil
}

View file

@ -1,8 +1,13 @@
// 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 package server
import ( import (
"bytes"
"errors" "errors"
"fmt"
"io"
"net" "net"
"net/http" "net/http"
"path" "path"
@ -10,61 +15,81 @@ import (
"sync" "sync"
"sync/atomic" "sync/atomic"
"github.com/jzelinskie/bufferpool" "github.com/pushrax/chihaya/config"
"github.com/pushrax/chihaya/storage"
"github.com/jzelinskie/chihaya/config"
"github.com/jzelinskie/chihaya/storage"
) )
type Server struct { type Server struct {
conf *config.Config
listener net.Listener
storage storage.Storage
terminated *bool
waitgroup *sync.WaitGroup
http.Server http.Server
listener *net.Listener
} }
func New(conf *config.Config) { func New(conf *config.Config) (*Server, error) {
return &Server{ var (
Addr: conf.Addr, wg sync.WaitGroup
Handler: newHandler(conf), terminated bool
)
store, err := storage.New(&conf.Storage)
if err != nil {
return nil, err
} }
handler := &handler{
conf: conf,
storage: store,
terminated: &terminated,
waitgroup: &wg,
}
s := &Server{
conf: conf,
storage: store,
terminated: &terminated,
waitgroup: &wg,
}
s.Server.Addr = conf.Addr
s.Server.Handler = handler
return s, nil
} }
func (s *Server) Start() error { func (s *Server) Start() error {
s.listener, err = net.Listen("tcp", config.Addr) listener, err := net.Listen("tcp", s.conf.Addr)
if err != nil { if err != nil {
return err return err
} }
s.Handler.terminated = false *s.terminated = false
s.Serve(s.listener) s.Serve(s.listener)
s.Handler.waitgroup.Wait() s.waitgroup.Wait()
s.Handler.storage.Shutdown()
return nil return nil
} }
func (s *Server) Stop() error { func (s *Server) Stop() error {
s.Handler.waitgroup.Wait() *s.terminated = true
s.Handler.terminated = true s.waitgroup.Wait()
return s.Handler.listener.Close() err := s.storage.Shutdown()
if err != nil {
return err
}
return s.listener.Close()
} }
type handler struct { type handler struct {
bufferpool *bufferpool.BufferPool
conf *config.Config conf *config.Config
deltaRequests int64 deltaRequests int64
storage *storage.Storage storage storage.Storage
terminated bool terminated *bool
waitgroup sync.WaitGroup waitgroup *sync.WaitGroup
}
func newHandler(conf *config.Config) {
return &Handler{
bufferpool: bufferpool.New(conf.BufferPoolSize, 500),
conf: conf,
storage: storage.New(&conf.Storage),
}
} }
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if h.terminated { if *h.terminated {
return return
} }
@ -72,46 +97,54 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer h.waitgroup.Done() defer h.waitgroup.Done()
if r.URL.Path == "/stats" { if r.URL.Path == "/stats" {
h.serveStats(&w, r) h.serveStats(w, r)
return return
} }
dir, action := path.Split(requestPath) passkey, action := path.Split(r.URL.Path)
switch action { switch action {
case "announce": case "announce":
h.serveAnnounce(&w, r) h.serveAnnounce(w, r)
return return
case "scrape": case "scrape":
// TODO // TODO
h.serveScrape(&w, r) h.serveScrape(w, r)
return return
default: default:
buf := h.bufferpool.Take() written := fail(errors.New("Unknown action"), w)
fail(errors.New("Unknown action"), buf) h.finalizeResponse(w, r, written)
h.writeResponse(&w, r, buf)
return return
} }
} }
func writeResponse(w *http.ResponseWriter, r *http.Request, buf *bytes.Buffer) { func (h *handler) finalizeResponse(
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(buf.Len())) w.Header().Add("Content-Length", strconv.Itoa(written))
w.Write(buf.Bytes())
w.(http.Flusher).Flush() w.(http.Flusher).Flush()
atomic.AddInt64(h.deltaRequests, 1) atomic.AddInt64(&h.deltaRequests, 1)
} }
func fail(err error, buf *bytes.Buffer) { func fail(err error, w http.ResponseWriter) int {
buf.WriteString("d14:failure reason") e := err.Error()
buf.WriteString(strconv.Itoa(len(err))) message := fmt.Sprintf(
buf.WriteRune(':') "%s%s%s%s%s",
buf.WriteString(err) "d14:failure reason",
buf.WriteRune('e') strconv.Itoa(len(e)),
':',
e,
'e',
)
written, _ := 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) {
if len(dir) != 34 { if len(dir) != 34 {
return nil, errors.New("Your passkey is invalid") return nil, errors.New("Your passkey is invalid")
} }
@ -127,29 +160,3 @@ func validatePasskey(dir string, s *storage.Storage) (storage.User, error) {
return user, nil return user, nil
} }
func determineIP(r *http.Request, pq *parsedQuery) (string, error) {
ip, ok := pq.params["ip"]
if !ok {
ip, ok = pq.params["ipv4"]
if !ok {
ips, ok := r.Header["X-Real-Ip"]
if ok && len(ips) > 0 {
ip = ips[0]
} else {
portIndex := len(r.RemoteAddr) - 1
for ; portIndex >= 0; portIndex-- {
if r.RemoteAddr[portIndex] == ':' {
break
}
}
if portIndex != -1 {
ip = r.RemoteAddr[0:portIndex]
} else {
return "", errors.New("Failed to parse IP address")
}
}
}
}
return &ip, nil
}

View file

@ -1,5 +1,9 @@
// 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 redis package redis
import ( import (
"github.com/jzelinskie/chihaya/storage" "github.com/pushrax/chihaya/storage"
) )

View file

@ -1,15 +1,19 @@
// 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 storage package storage
import ( import (
"fmt" "fmt"
"github.com/jzelinskie/chihaya/config" "github.com/pushrax/chihaya/config"
) )
var drivers = make(map[string]StorageDriver) var drivers = make(map[string]StorageDriver)
type StorageDriver interface { type StorageDriver interface {
New(*config.StorageConfig) (Storage, error) New(*config.Storage) (Storage, error)
} }
func Register(name string, driver StorageDriver) { func Register(name string, driver StorageDriver) {
@ -22,12 +26,12 @@ func Register(name string, driver StorageDriver) {
drivers[name] = driver drivers[name] = driver
} }
func New(name string, conf *config.Storage) (Storage, error) { func New(conf *config.Storage) (Storage, error) {
driver, ok := drivers[name] driver, ok := drivers[conf.Driver]
if !ok { if !ok {
return nil, fmt.Errorf( return nil, fmt.Errorf(
"storage: unknown driver %q (forgotten import?)", "storage: unknown driver %q (forgotten import?)",
name, conf.Driver,
) )
} }
store, err := driver.New(conf) store, err := driver.New(conf)
@ -40,8 +44,8 @@ func New(name string, conf *config.Storage) (Storage, error) {
type Storage interface { type Storage interface {
Shutdown() error Shutdown() error
FindUser(passkey []byte) (*User, bool, error) FindUser(passkey string) (*User, bool, error)
FindTorrent(infohash []byte) (*Torrent, bool, error) FindTorrent(infohash string) (*Torrent, bool, error)
UnpruneTorrent(torrent *Torrent) error UnpruneTorrent(torrent *Torrent) error
RecordUser( RecordUser(