mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-02 14:58:57 -07:00
transactions, connpools, misc
This commit is contained in:
+24
-5
@@ -10,11 +10,15 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"path"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||
conn := s.connPool.Get()
|
||||
defer conn.Close()
|
||||
|
||||
passkey, _ := path.Split(r.URL.Path)
|
||||
_, err := validatePasskey(passkey, s.storage)
|
||||
_, err := validatePasskey(passkey, conn)
|
||||
if err != nil {
|
||||
fail(err, w, r)
|
||||
return
|
||||
@@ -32,18 +36,18 @@ func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
err = pq.validate()
|
||||
err = pq.validateAnnounceParams()
|
||||
if err != nil {
|
||||
fail(errors.New("Malformed request"), w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if !s.conf.Whitelisted(pq.params["peerId"]) {
|
||||
if !s.conf.ClientWhitelisted(pq.params["peer_id"]) {
|
||||
fail(errors.New("Your client is not approved"), w, r)
|
||||
return
|
||||
}
|
||||
|
||||
torrent, exists, err := s.storage.FindTorrent(pq.params["infohash"])
|
||||
torrent, exists, err := conn.FindTorrent(pq.params["infohash"])
|
||||
if err != nil {
|
||||
log.Panicf("server: %s", err)
|
||||
}
|
||||
@@ -52,8 +56,13 @@ func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
tx, err := conn.NewTx()
|
||||
if err != nil {
|
||||
log.Panicf("server: %s", err)
|
||||
}
|
||||
|
||||
if left, _ := pq.getUint64("left"); torrent.Status == 1 && left == 0 {
|
||||
err := s.storage.UnpruneTorrent(torrent)
|
||||
err := tx.UnpruneTorrent(torrent)
|
||||
if err != nil {
|
||||
log.Panicf("server: %s", err)
|
||||
}
|
||||
@@ -71,5 +80,15 @@ func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var numWant int
|
||||
if numWantStr, exists := pq.params["numWant"]; exists {
|
||||
numWant, err := strconv.Atoi(numWantStr)
|
||||
if err != nil {
|
||||
numWant = s.conf.DefaultNumWant
|
||||
}
|
||||
} else {
|
||||
numWant = s.conf.DefaultNumWant
|
||||
}
|
||||
|
||||
// TODO continue
|
||||
}
|
||||
|
||||
+18
-21
@@ -6,6 +6,7 @@ package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/pushrax/chihaya/config"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
@@ -96,7 +97,7 @@ func parseQuery(query string) (*parsedQuery, error) {
|
||||
return pq, nil
|
||||
}
|
||||
|
||||
func (pq *parsedQuery) validate() error {
|
||||
func (pq *parsedQuery) validateAnnounceParams() error {
|
||||
infohash, _ := pq.params["info_hash"]
|
||||
if infohash == "" {
|
||||
return errors.New("infohash does not exist")
|
||||
@@ -126,27 +127,23 @@ func (pq *parsedQuery) validate() error {
|
||||
|
||||
// TODO IPv6 support
|
||||
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")
|
||||
}
|
||||
if ip, ok := pq.params["ip"]; 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 {
|
||||
portIndex := len(r.RemoteAddr) - 1
|
||||
for ; portIndex >= 0; portIndex-- {
|
||||
if r.RemoteAddr[portIndex] == ':' {
|
||||
break
|
||||
}
|
||||
}
|
||||
if portIndex != -1 {
|
||||
return r.RemoteAddr[0:portIndex], nil
|
||||
} else {
|
||||
return "", errors.New("Failed to parse IP address")
|
||||
}
|
||||
}
|
||||
return ip, nil
|
||||
}
|
||||
|
||||
+3
-3
@@ -25,7 +25,7 @@ import (
|
||||
type Server struct {
|
||||
conf *config.Config
|
||||
listener net.Listener
|
||||
storage storage.Conn
|
||||
connPool storage.Pool
|
||||
|
||||
serving bool
|
||||
startTime time.Time
|
||||
@@ -39,14 +39,14 @@ type Server struct {
|
||||
}
|
||||
|
||||
func New(conf *config.Config) (*Server, error) {
|
||||
store, err := storage.Open(&conf.Storage)
|
||||
pool, err := storage.Open(&conf.Storage)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s := &Server{
|
||||
conf: conf,
|
||||
storage: store,
|
||||
storage: pool,
|
||||
Server: http.Server{
|
||||
Addr: conf.Addr,
|
||||
ReadTimeout: conf.ReadTimeout.Duration,
|
||||
|
||||
Reference in New Issue
Block a user