Respect a clients AF Optional Support - config driven

This commit is contained in:
Cooper Lees 2014-08-20 16:24:33 -07:00
parent 1d9b2bc322
commit 26de6d2e9c
2 changed files with 23 additions and 14 deletions

View file

@ -45,6 +45,7 @@ type NetConfig struct {
AllowIPSpoofing bool `json:"allow_ip_spoofing"`
DualStackedPeers bool `json:"dual_stacked_peers"`
RealIPHeader string `json:"real_ip_header"`
RespectAF bool `json:"respect_af"`
PreferredSubnet bool `json:"preferred_subnet,omitempty"`
PreferredIPv4Subnet int `json:"preferred_ipv4_subnet,omitempty"`
@ -113,6 +114,7 @@ var DefaultConfig = Config{
NetConfig: NetConfig{
AllowIPSpoofing: true,
DualStackedPeers: true,
RespectAF: false,
},
ClientWhitelistEnabled: false,

View file

@ -129,13 +129,8 @@ func (pm *PeerMap) AppendPeers(ipv4s, ipv6s PeerList, ann *Announce, wanted int)
continue
}
if ann.HasIPv6() && peer.HasIPv6() {
ipv6s = append(ipv6s, peer)
count++
} else if peer.HasIPv4() {
ipv4s = append(ipv4s, peer)
count++
}
// Add the peers optionally respecting AF
appendPeers(&ipv4s, &ipv6s, ann, &peer, &count)
}
return ipv4s, ipv6s
@ -174,19 +169,31 @@ func (pm *PeerMap) AppendSubnetPeers(ipv4s, ipv6s PeerList, ann *Announce, wante
continue
}
if ann.HasIPv6() && peer.HasIPv6() {
ipv6s = append(ipv6s, peer)
count++
} else if peer.HasIPv4() {
ipv4s = append(ipv4s, peer)
count++
}
// Add the peers optionally respecting AF
appendPeers(&ipv4s, &ipv6s, ann, &peer, &count)
}
}
return ipv4s, ipv6s
}
// Optionally Respect AF for peers returned and avoid copy-pasta
func appendPeers(ipv4s, ipv6s *PeerList, ann *Announce, peer *Peer, count *int) {
// v6 to only v6 announcements
if ann.HasIPv6() && peer.HasIPv6() {
*ipv6s = append(*ipv6s, *peer)
*count++
// v4 to only dual stacked requests if we are respecting AF of annoucement
} else if ann.Config.RespectAF && ann.HasIPv4() && peer.HasIPv4() {
*ipv4s = append(*ipv4s, *peer)
*count++
// Default everything else to get IPv4 if we are not respecting AF
} else if !ann.Config.RespectAF && peer.HasIPv4() {
*ipv4s = append(*ipv4s, *peer)
*count++
}
}
// peersEquivalent checks if two peers represent the same entity.
func peersEquivalent(a, b *Peer) bool {
return a.ID == b.ID || a.UserID != 0 && a.UserID == b.UserID