add helper methods to get IPv4 and IPv6 peer
This commit is contained in:
parent
468eefee57
commit
9f229c4ab6
2 changed files with 24 additions and 4 deletions
22
chihaya.go
22
chihaya.go
|
@ -34,6 +34,28 @@ type AnnounceRequest struct {
|
|||
Params Params
|
||||
}
|
||||
|
||||
// Peer4 returns a Peer using the IPv4 endpoint of the Announce.
|
||||
// Note that, if the Announce does not contain an IPv4 address, the IP field of
|
||||
// the returned Peer can be nil.
|
||||
func (r *AnnounceRequest) Peer4() Peer {
|
||||
return Peer{
|
||||
IP: r.IPv4,
|
||||
Port: r.Port,
|
||||
ID: r.PeerID,
|
||||
}
|
||||
}
|
||||
|
||||
// Peer6 returns a Peer using the IPv6 endpoint of the Announce.
|
||||
// Note that, if the Announce does not contain an IPv6 address, the IP field of
|
||||
// the returned Peer can be nil.
|
||||
func (r *AnnounceRequest) Peer6() Peer {
|
||||
return Peer{
|
||||
IP: r.IPv6,
|
||||
Port: r.Port,
|
||||
ID: r.PeerID,
|
||||
}
|
||||
}
|
||||
|
||||
// AnnounceResponse represents the parameters used to create an announce
|
||||
// response.
|
||||
type AnnounceResponse struct {
|
||||
|
|
|
@ -27,16 +27,14 @@ func (f FailedToRetrievePeers) Error() string { return string(f) }
|
|||
func responseAnnounceClient(next tracker.AnnounceHandler) tracker.AnnounceHandler {
|
||||
return func(cfg *chihaya.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) (err error) {
|
||||
storage := store.MustGetStore()
|
||||
peer4 := chihaya.Peer{ID: req.PeerID, IP: req.IPv4, Port: req.Port}
|
||||
peer6 := chihaya.Peer{ID: req.PeerID, IP: req.IPv6, Port: req.Port}
|
||||
|
||||
resp.MinInterval = cfg.MinAnnounceInterval
|
||||
resp.Compact = req.Compact
|
||||
resp.Complete = int32(storage.NumSeeders(req.InfoHash))
|
||||
resp.Incomplete = int32(storage.NumLeechers(req.InfoHash))
|
||||
resp.IPv4Peers, resp.IPv6Peers, err = storage.AnnouncePeers(req.InfoHash, req.Left == 0, int(req.NumWant), peer4, peer6)
|
||||
resp.IPv4Peers, resp.IPv6Peers, err = storage.AnnouncePeers(req.InfoHash, req.Left == 0, int(req.NumWant), req.Peer4(), req.Peer6())
|
||||
if err != nil {
|
||||
return err.(FailedToRetrievePeers)
|
||||
return FailedToRetrievePeers(err.Error())
|
||||
}
|
||||
|
||||
return next(cfg, req, resp)
|
||||
|
|
Loading…
Add table
Reference in a new issue