Add ClientConfig.AcceptPeerConnections

This commit is contained in:
Matt Joiner 2021-06-18 14:59:20 +10:00
parent af1ca91e04
commit a01b451857
2 changed files with 14 additions and 7 deletions

View File

@ -248,9 +248,11 @@ func NewClient(cfg *ClientConfig) (cl *Client, err error) {
if peerNetworkEnabled(parseNetworkString(s.Addr().Network()), cl.config) { if peerNetworkEnabled(parseNetworkString(s.Addr().Network()), cl.config) {
cl.dialers = append(cl.dialers, s) cl.dialers = append(cl.dialers, s)
cl.listeners = append(cl.listeners, s) cl.listeners = append(cl.listeners, s)
if cl.config.AcceptPeerConnections {
go cl.acceptConnections(s) go cl.acceptConnections(s)
} }
} }
}
go cl.forwardPort() go cl.forwardPort()
if !cfg.NoDHT { if !cfg.NoDHT {
@ -322,12 +324,14 @@ func (cl *Client) Listeners() []Listener {
// yourself. // yourself.
func (cl *Client) AddListener(l Listener) { func (cl *Client) AddListener(l Listener) {
cl.listeners = append(cl.listeners, l) cl.listeners = append(cl.listeners, l)
if cl.config.AcceptPeerConnections {
go cl.acceptConnections(l) go cl.acceptConnections(l)
} }
}
func (cl *Client) firewallCallback(net.Addr) bool { func (cl *Client) firewallCallback(net.Addr) bool {
cl.rLock() cl.rLock()
block := !cl.wantConns() block := !cl.wantConns() || !cl.config.AcceptPeerConnections
cl.rUnlock() cl.rUnlock()
if block { if block {
torrent.Add("connections firewalled", 1) torrent.Add("connections firewalled", 1)

View File

@ -133,6 +133,8 @@ type ClientConfig struct {
// bit of a special case, since a peer could also be useless if they're just not interested, or // bit of a special case, since a peer could also be useless if they're just not interested, or
// we don't intend to obtain all of a torrent's data. // we don't intend to obtain all of a torrent's data.
DropMutuallyCompletePeers bool DropMutuallyCompletePeers bool
// Whether to accept peer connections at all.
AcceptPeerConnections bool
// OnQuery hook func // OnQuery hook func
DHTOnQuery func(query *krpc.Msg, source net.Addr) (propagate bool) DHTOnQuery func(query *krpc.Msg, source net.Addr) (propagate bool)
@ -184,6 +186,7 @@ func NewDefaultClientConfig() *ClientConfig {
CryptoProvides: mse.AllSupportedCrypto, CryptoProvides: mse.AllSupportedCrypto,
ListenPort: 42069, ListenPort: 42069,
Extensions: defaultPeerExtensionBytes(), Extensions: defaultPeerExtensionBytes(),
AcceptPeerConnections: true,
} }
//cc.ConnTracker.SetNoMaxEntries() //cc.ConnTracker.SetNoMaxEntries()
//cc.ConnTracker.Timeout = func(conntrack.Entry) time.Duration { return 0 } //cc.ConnTracker.Timeout = func(conntrack.Entry) time.Duration { return 0 }