initial announce response

This commit is contained in:
Jimmy Zelinskie 2013-08-04 15:56:31 -04:00
parent 2a639c2f3b
commit a6d04f6790
4 changed files with 276 additions and 154 deletions

View file

@ -41,6 +41,7 @@ type Storage struct {
MaxIdleConn int `json:"max_idle_conn"`
IdleTimeout *Duration `json:"idle_timeout"`
ConnTimeout *Duration `json:"conn_timeout"`
TxRetries int `json:"tx_retries"`
}
// Config represents a configuration for a server.Server.

View file

@ -25,7 +25,8 @@ var exampleJson = `{
"max_idle_conn": 3,
"idle_timeout": "240s",
"conn_timeout": "5s"
"conn_timeout": "5s",
"tx_retries": 3
},
"private": true,
@ -83,7 +84,7 @@ func TestOpenCurDir(t *testing.T) {
}
func TestOpenAbsEnvPath(t *testing.T) {
if !testing.Short() {
writeAndOpenJsonTest(t, filepath.Join(os.TempDir(),"testConfig.json"))
writeAndOpenJsonTest(t, filepath.Join(os.TempDir(), "testConfig.json"))
} else {
t.Log("Write/Read file test skipped")
}

View file

@ -14,6 +14,7 @@ import (
"github.com/pushrax/chihaya/config"
"github.com/pushrax/chihaya/server"
_ "github.com/pushrax/chihaya/storage/redis"
)
var (

View file

@ -23,6 +23,9 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
return
}
// Retry failed transactions a specified number of times
for i := 0; i < s.conf.Storage.TxRetries; i++ {
// Start a transaction
tx, err := s.dbConnPool.Get()
if err != nil {
@ -189,7 +192,68 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
}
}
// TODO compact, response, etc...
if ip != peer.IP || port != peer.Port {
peer.Port = port
peer.IP = ip
}
// If the transaction failed, retry
err = tx.Commit()
if err != nil {
continue
}
// Generate the response
seedCount := len(torrent.Seeders)
leechCount := len(torrent.Leechers)
writeBencoded(w, "d")
writeBencoded(w, "complete")
writeBencoded(w, seedCount)
writeBencoded(w, "incomplete")
writeBencoded(w, leechCount)
writeBencoded(w, "interval")
writeBencoded(w, s.conf.Announce.Duration)
writeBencoded(w, "min interval")
writeBencoded(w, s.conf.MinAnnounce.Duration)
if numWant > 0 && event != "stopped" && event != "paused" {
writeBencoded(w, "peers")
var peerCount, count int
if compact {
if left > 0 {
peerCount = minInt(numWant, leechCount)
} else {
peerCount = minInt(numWant, leechCount+seedCount-1)
}
writeBencoded(w, strconv.Itoa(peerCount*6))
writeBencoded(w, ":")
} else {
writeBencoded(w, "l")
}
if left > 0 {
// If they're seeding, give them only leechers
writeLeechers(w, torrent, count, numWant, compact)
} else {
// If they're leeching, prioritize giving them seeders
writeSeeders(w, torrent, count, numWant, compact)
writeLeechers(w, torrent, count, numWant, compact)
}
if compact && peerCount != count {
log.Panicf("Calculated peer count (%d) != real count (%d)", peerCount, count)
}
if !compact {
writeBencoded(w, "e")
}
}
writeBencoded(w, "e")
return
}
}
func (s Server) validateAnnounceQuery(r *http.Request) (compact bool, numWant int, infohash, peerID, event, ip string, port, uploaded, downloaded, left uint64, err error) {
@ -228,19 +292,26 @@ func determineNumWant(fallback int, pq *parsedQuery) int {
return fallback
}
return numWant
} else {
return fallback
}
return fallback
}
func determineIP(r *http.Request, pq *parsedQuery) (string, error) {
if ip, ok := pq.Params["ip"]; ok {
ip, ok := pq.Params["ip"]
ipv4, okv4 := pq.Params["ipv4"]
xRealIPs, xRealOk := pq.Params["X-Real-Ip"]
switch {
case ok:
return ip, nil
} else if ip, ok := pq.Params["ipv4"]; ok {
return ip, nil
} else if ips, ok := pq.Params["X-Real-Ip"]; ok && len(ips) > 0 {
return string(ips[0]), nil
} else {
case okv4:
return ipv4, nil
case xRealOk && len(xRealIPs) > 0:
return string(xRealIPs[0]), nil
default:
portIndex := len(r.RemoteAddr) - 1
for ; portIndex >= 0; portIndex-- {
if r.RemoteAddr[portIndex] == ':' {
@ -249,8 +320,56 @@ func determineIP(r *http.Request, pq *parsedQuery) (string, error) {
}
if portIndex != -1 {
return r.RemoteAddr[0:portIndex], nil
} else {
}
return "", errors.New("Failed to parse IP address")
}
}
func minInt(a, b int) int {
if a < b {
return a
}
return b
}
func writeSeeders(w http.ResponseWriter, t *storage.Torrent, count, numWant int, compact bool) {
for _, seed := range t.Seeders {
if count >= numWant {
break
}
if compact {
// TODO writeBencoded(w, compactAddr)
} else {
writeBencoded(w, "d")
writeBencoded(w, "ip")
writeBencoded(w, seed.IP)
writeBencoded(w, "peer id")
writeBencoded(w, seed.ID)
writeBencoded(w, "port")
writeBencoded(w, seed.Port)
writeBencoded(w, "e")
}
count++
}
}
func writeLeechers(w http.ResponseWriter, t *storage.Torrent, count, numWant int, compact bool) {
for _, leech := range t.Leechers {
if count >= numWant {
break
}
if compact {
// TODO writeBencoded(w, compactAddr)
} else {
writeBencoded(w, "d")
writeBencoded(w, "ip")
writeBencoded(w, leech.IP)
writeBencoded(w, "peer id")
writeBencoded(w, leech.ID)
writeBencoded(w, "port")
writeBencoded(w, leech.Port)
writeBencoded(w, "e")
}
count++
}
}