allow disconnecting from a peer by ID
License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
Steven Allen committed
Jun 12, 2018 at 14:50 UTC
78f82e8e79a85aa6f1e5a80b62663b7d63ed1f98
1 file changed
+26
-13
core/commands/swarm.go
+26
-13
@@ -19,6 +19,7 @@ import (
19
swarm "gx/ipfs/QmSvhbgtjQJKdT5avEeb7cvjYs7YrhebJyM1K6GAnkKgfd/go-libp2p-swarm"
20
ma "gx/ipfs/QmUxSEGbv2nmYNnfXi7839wwQqTN3kwQeUxe8dTjZWZs7J/go-multiaddr"
21
peer "gx/ipfs/QmVf8hTAsLLFtn4WPCRNdnaF2Eag2qTBS6uR8AiHPZARXy/go-libp2p-peer"
22
+ inet "gx/ipfs/QmXdgNhVEgjLxjUoMs5ViQL7pboAt3Y7V7eGHRiE4qrmTE/go-libp2p-net"
23
pstore "gx/ipfs/QmZhsmorLpD9kmQ4ynbAu4vbKv2goMUnXazwGA4gnWHDjB/go-libp2p-peerstore"
24
iaddr "gx/ipfs/QmaKviZCLQrpuyFdSjteik7kJFcQpcyZgb1VuuwaCBBaEa/go-ipfs-addr"
25
cmdkit "gx/ipfs/QmdE4gMduCKCGAcczM2F5ioYDfdeKuPix138wrES1YSr7f/go-ipfs-cmdkit"
@@ -458,26 +459,38 @@ it will reconnect.
459
output := make([]string, len(iaddrs))
460
for i, addr := range iaddrs {
461
taddr := addr.Transport()
461
- output[i] = "disconnect " + addr.ID().Pretty()
462
+ id := addr.ID()
463
+ output[i] = "disconnect " + id.Pretty()
464
463
- found := false
464
- conns := n.PeerHost.Network().ConnsToPeer(addr.ID())
465
- for _, conn := range conns {
466
- if !conn.RemoteMultiaddr().Equal(taddr) {
467
- continue
468
- }
465
+ net := n.PeerHost.Network()
466
470
- if err := conn.Close(); err != nil {
467
+ if taddr == nil {
468
+ if net.Connectedness(id) != inet.Connected {
469
+ output[i] += " failure: not connected"
470
+ } else if err := net.ClosePeer(id); err != nil {
471
output[i] += " failure: " + err.Error()
472
} else {
473
output[i] += " success"
474
}
475
- found = true
476
- break
477
- }
475
+ } else {
476
+ found := false
477
+ for _, conn := range net.ConnsToPeer(id) {
478
+ if !conn.RemoteMultiaddr().Equal(taddr) {
479
+ continue
480
+ }
481
479
- if !found {
480
- output[i] += " failure: conn not found"
482
+ if err := conn.Close(); err != nil {
483
+ output[i] += " failure: " + err.Error()
484
+ } else {
485
+ output[i] += " success"
486
+ }
487
+ found = true
488
+ break
489
+ }
490
+
491
+ if !found {
492
+ output[i] += " failure: conn not found"
493
+ }
494
}
495
}
496
res.SetOutput(&stringList{output})