clean-up before bed
This commit is contained in:
parent
ccc8897ca8
commit
51acd12101
7 changed files with 99 additions and 100 deletions
47
README.md
47
README.md
|
@ -1,43 +1,30 @@
|
||||||
chihaya
|
#chihaya [![Build Status](https://travis-ci.org/pushrax/chihaya.png?branch=master)](https://travis-ci.org/pushrax/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. It isn't quite ready for prime-time just yet, but these are the features that it targets:
|
||||||
|
|
||||||
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:
|
|
||||||
|
|
||||||
|
- Asynchronous and concurrent (multiplexes requests over all available threads)
|
||||||
- Low processing and memory footprint
|
- Low processing and memory footprint
|
||||||
- IPv6 support
|
- IPv6 support
|
||||||
- Support for multiple storage backends
|
- Storage interface that can be easily adapted to use any data store
|
||||||
- Linear horizontal scalability (depending on the backends)
|
- Scaling properties that directly correlate with the chosen data store's scaling properties
|
||||||
|
|
||||||
|
|
||||||
Installing
|
##installing
|
||||||
----------
|
|
||||||
|
|
||||||
$ go install github.com/pushrax/chihaya
|
```sh
|
||||||
|
$ go install github.com/pushrax/chihaya
|
||||||
|
```
|
||||||
|
|
||||||
|
##configuring
|
||||||
|
|
||||||
|
Configuration is done in a JSON formatted file specified with the `-config` flag. One can start with [`example/config.json`](https://github.com/pushrax/chihaya/blob/master/example/config.json) as a base.
|
||||||
|
|
||||||
|
|
||||||
Configuration
|
##implementing a new storage backend
|
||||||
-------------
|
|
||||||
|
|
||||||
Configuration is done in a JSON formatted file specified with the -config flag.
|
The [`storage`](http://godoc.org/github.com/pushrax/chihaya/storage) package works similar to the standard library's [`database/sql`](http://godoc.org/database/sql) package. To write a new storage backend, create a new Go package that has an implementation of both the [`Storage`](http://godoc.org/github.com/pushrax/chihaya/storage#Storage) and the [`StorageDriver`](http://godoc.org/github.com/pushrax/chihaya/storage#StorageDriver) interfaces. Within your package define an [`init()`](http://golang.org/ref/spec#Program_execution) function that calls [`storage.Register(driverName, yourStorageDriver)`](http://godoc.org/github.com/pushrax/chihaya/storage#Register). After that, all you have to do is remember to add `import _ path/to/your/library` to the top of `main.go` and now config files will recognize your driver by name.
|
||||||
One can start with `example/config.json`, as a base. Check out GoDoc for more info.
|
|
||||||
|
|
||||||
|
|
||||||
Contributing
|
##contributing
|
||||||
------------
|
|
||||||
|
|
||||||
If you want to make a smaller change, just go ahead and do it, and when you're
|
|
||||||
done send a pull request through GitHub. If there's a larger change you want to
|
|
||||||
make, it would be preferable to discuss it first via a GitHub issue or by
|
|
||||||
getting in touch on IRC. Always remember to gofmt your code!
|
|
||||||
|
|
||||||
|
|
||||||
Contact
|
|
||||||
-------
|
|
||||||
|
|
||||||
If you have any questions or want to contribute something, come say hi in the
|
|
||||||
IRC channel: **#chihaya on [freenode](http://freenode.net/)**
|
|
||||||
([webchat](http://webchat.freenode.net?channels=chihaya)).
|
|
||||||
|
|
||||||
|
If you're interested in contributing, please contact us in **#chihaya on [freenode](http://freenode.net/)**([webchat](http://webchat.freenode.net?channels=chihaya)) or post to the issue tracker. Please don't offer a pull request with no prior communication attempts (unless it's small), as it will most likely lead to confusion and time wasted for everyone. And remember: good gophers always use gofmt!asted for everyone. And remember: good gophers always use gofmt!
|
||||||
|
|
|
@ -10,28 +10,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
|
||||||
Addr string `json:"addr"`
|
|
||||||
Storage Storage `json:"storage"`
|
|
||||||
Private bool `json:"private"`
|
|
||||||
Freeleech bool `json:"freeleech"`
|
|
||||||
|
|
||||||
Announce Duration `json:"announce"`
|
|
||||||
MinAnnounce Duration `json:"min_announce"`
|
|
||||||
|
|
||||||
Whitelist []string `json:"whitelist"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Storage struct {
|
|
||||||
Driver string `json:"driver"`
|
|
||||||
Protocol string `json:"protocol"`
|
|
||||||
Addr string `json:"addr"`
|
|
||||||
Username string `json:"user"`
|
|
||||||
Password string `json:"pass"`
|
|
||||||
Schema string `json:"schema"`
|
|
||||||
Encoding string `json:"encoding"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Duration struct {
|
type Duration struct {
|
||||||
time.Duration
|
time.Duration
|
||||||
}
|
}
|
||||||
|
@ -47,6 +25,30 @@ func (d *Duration) UnmarshalJSON(b []byte) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Storage struct {
|
||||||
|
Driver string `json:"driver"`
|
||||||
|
Protocol string `json:"protocol"`
|
||||||
|
Addr string `json:"addr"`
|
||||||
|
Username string `json:"user"`
|
||||||
|
Password string `json:"pass"`
|
||||||
|
Schema string `json:"schema"`
|
||||||
|
Encoding string `json:"encoding"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Addr string `json:"addr"`
|
||||||
|
Storage Storage `json:"storage"`
|
||||||
|
|
||||||
|
Private bool `json:"private"`
|
||||||
|
Freeleech bool `json:"freeleech"`
|
||||||
|
|
||||||
|
Announce Duration `json:"announce"`
|
||||||
|
MinAnnounce Duration `json:"min_announce"`
|
||||||
|
ReadTimeout Duration `json:"read_timeout"`
|
||||||
|
|
||||||
|
Whitelist []string `json:"whitelist"`
|
||||||
|
}
|
||||||
|
|
||||||
func New(path string) (*Config, error) {
|
func New(path string) (*Config, error) {
|
||||||
expandedPath := os.ExpandEnv(path)
|
expandedPath := os.ExpandEnv(path)
|
||||||
f, err := os.Open(expandedPath)
|
f, err := os.Open(expandedPath)
|
||||||
|
@ -62,3 +64,27 @@ func New(path string) (*Config, error) {
|
||||||
}
|
}
|
||||||
return conf, nil
|
return conf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Config) Whitelisted(peerId string) bool {
|
||||||
|
var (
|
||||||
|
widLen int
|
||||||
|
matched bool
|
||||||
|
)
|
||||||
|
|
||||||
|
for _, whitelistedId := range c.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
|
||||||
|
}
|
||||||
|
|
|
@ -1,11 +1,6 @@
|
||||||
{
|
{
|
||||||
|
|
||||||
"addr": ":34000",
|
"addr": ":34000",
|
||||||
"announce": "30m",
|
|
||||||
"min_announce": "15m",
|
|
||||||
"freelech": false,
|
|
||||||
"private": true,
|
|
||||||
|
|
||||||
"storage": {
|
"storage": {
|
||||||
"driver": "redis",
|
"driver": "redis",
|
||||||
"addr": "127.0.0.1:6379",
|
"addr": "127.0.0.1:6379",
|
||||||
|
@ -13,6 +8,13 @@
|
||||||
"pass": "",
|
"pass": "",
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"private": true,
|
||||||
|
"freeleech": false,
|
||||||
|
|
||||||
|
"announce": "30m",
|
||||||
|
"min_announce": "15m",
|
||||||
|
"read_timeout": "20s",
|
||||||
|
|
||||||
"whitelist": [],
|
"whitelist": [],
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,38 +10,36 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
"github.com/pushrax/chihaya/config"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||||
passkey, _ := path.Split(r.URL.Path)
|
passkey, _ := path.Split(r.URL.Path)
|
||||||
user, err := validatePasskey(passkey, s.storage)
|
user, err := validatePasskey(passkey, s.storage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fail(err, w, r)
|
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"), w, r)
|
fail(errors.New("Error parsing query"), w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ip, err := pq.determineIP(r)
|
ip, err := pq.determineIP(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fail(err, w, r)
|
fail(err, w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = validateParsedQuery(pq)
|
err = pq.validate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fail(errors.New("Malformed request"), w, r)
|
fail(errors.New("Malformed request"), w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !whitelisted(pq.params["peerId"], s.conf) {
|
if !s.conf.Whitelisted(pq.params["peerId"]) {
|
||||||
fail(errors.New("Your client is not approved"), w, r)
|
fail(errors.New("Your client is not approved"), w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,7 +48,7 @@ func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
if !exists {
|
if !exists {
|
||||||
fail(errors.New("This torrent does not exist"), w, r)
|
fail(errors.New("This torrent does not exist"), w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,34 +66,9 @@ func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||||
left,
|
left,
|
||||||
),
|
),
|
||||||
w,
|
w,
|
||||||
r,
|
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO continue
|
// TODO continue
|
||||||
}
|
}
|
||||||
|
|
||||||
func whitelisted(peerId string, conf *config.Config) bool {
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -96,7 +96,7 @@ func parseQuery(query string) (*parsedQuery, error) {
|
||||||
return pq, nil
|
return pq, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateParsedQuery(pq *parsedQuery) error {
|
func (pq *parsedQuery) validate() error {
|
||||||
infohash, ok := pq.params["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")
|
||||||
|
|
|
@ -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 server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -14,15 +18,15 @@ import (
|
||||||
|
|
||||||
func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request) {
|
||||||
passkey, _ := path.Split(r.URL.Path)
|
passkey, _ := path.Split(r.URL.Path)
|
||||||
user, err := validatePasskey(passkey, s.storage)
|
_, err := validatePasskey(passkey, s.storage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fail(err, w, r)
|
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"), w, r)
|
fail(errors.New("Error parsing query"), w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
|
@ -46,7 +47,8 @@ func New(conf *config.Config) (*Server, error) {
|
||||||
conf: conf,
|
conf: conf,
|
||||||
storage: store,
|
storage: store,
|
||||||
Server: http.Server{
|
Server: http.Server{
|
||||||
Addr: conf.Addr,
|
Addr: conf.Addr,
|
||||||
|
ReadTimeout: conf.ReadTimeout.Duration,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
s.Server.Handler = s
|
s.Server.Handler = s
|
||||||
|
@ -90,6 +92,11 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
defer atomic.AddInt64(&s.deltaRequests, 1)
|
defer atomic.AddInt64(&s.deltaRequests, 1)
|
||||||
defer finalizeResponse(w, r)
|
defer finalizeResponse(w, r)
|
||||||
|
|
||||||
|
if r.URL.Path == "/stats" {
|
||||||
|
s.serveStats(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
_, action := path.Split(r.URL.Path)
|
_, action := path.Split(r.URL.Path)
|
||||||
switch action {
|
switch action {
|
||||||
case "announce":
|
case "announce":
|
||||||
|
@ -99,7 +106,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
s.serveScrape(w, r)
|
s.serveScrape(w, r)
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
fail(errors.New("Unknown action"), w, r)
|
fail(errors.New("Unknown action"), w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -111,7 +118,7 @@ func finalizeResponse(w http.ResponseWriter, r *http.Request) {
|
||||||
w.(http.Flusher).Flush()
|
w.(http.Flusher).Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
func fail(err error, w http.ResponseWriter, r *http.Request) {
|
func fail(err error, w http.ResponseWriter) {
|
||||||
errmsg := err.Error()
|
errmsg := err.Error()
|
||||||
message := fmt.Sprintf(
|
message := fmt.Sprintf(
|
||||||
"%s%s%s%s%s",
|
"%s%s%s%s%s",
|
||||||
|
@ -132,7 +139,7 @@ func validatePasskey(dir string, s storage.Storage) (*storage.User, error) {
|
||||||
|
|
||||||
user, exists, err := s.FindUser(passkey)
|
user, exists, err := s.FindUser(passkey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
if !exists {
|
if !exists {
|
||||||
return nil, errors.New("Passkey not found")
|
return nil, errors.New("Passkey not found")
|
||||||
|
|
Loading…
Add table
Reference in a new issue