@cryptotaxi247 / kubo / commits / aff1d8bdc

ipfs swarm disconnect (opposite of connect)

Juan Batiz-Benet committed Feb 1, 2015 at 06:37 UTC aff1d8bdcfbf96fecde5f784d146dcb268112292
1 file changed +73 -4
core/commands/swarm.go
+73 -4
@@ -22,8 +22,9 @@ var SwarmCmd = &cmds.Command{
22 Helptext: cmds.HelpText{
23 Tagline: "swarm inspection tool",
24 Synopsis: `
25 -ipfs swarm peers - List peers with open connections
26 -ipfs swarm connect <address> - Open connection to a given peer
25 +ipfs swarm peers - List peers with open connections
26 +ipfs swarm connect <address> - Open connection to a given address
27 +ipfs swarm disconnect <address> - Close connection to a given address
28 `,
29 ShortDescription: `
30 ipfs swarm is a tool to manipulate the network swarm. The swarm is the
@@ -32,8 +33,9 @@ ipfs peers in the internet.
33 `,
34 },
35 Subcommands: map[string]*cmds.Command{
35 - "peers": swarmPeersCmd,
36 - "connect": swarmConnectCmd,
36 + "peers": swarmPeersCmd,
37 + "connect": swarmConnectCmd,
38 + "disconnect": swarmDisconnectCmd,
39 },
40 }
41
@@ -130,6 +132,73 @@ ipfs swarm connect /ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3
132 Type: stringList{},
133 }
134
135 +var swarmDisconnectCmd = &cmds.Command{
136 + Helptext: cmds.HelpText{
137 + Tagline: "Close connection to a given address",
138 + ShortDescription: `
139 +'ipfs swarm disconnect' closes a connection to a peer address. The address format
140 +is an ipfs multiaddr:
141 +
142 +ipfs swarm disconnect /ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
143 +`,
144 + },
145 + Arguments: []cmds.Argument{
146 + cmds.StringArg("address", true, true, "address of peer to connect to").EnableStdin(),
147 + },
148 + Run: func(req cmds.Request, res cmds.Response) {
149 + n, err := req.Context().GetNode()
150 + if err != nil {
151 + res.SetError(err, cmds.ErrNormal)
152 + return
153 + }
154 +
155 + addrs := req.Arguments()
156 +
157 + if n.PeerHost == nil {
158 + res.SetError(errNotOnline, cmds.ErrClient)
159 + return
160 + }
161 +
162 + iaddrs, err := parseAddresses(addrs)
163 + if err != nil {
164 + res.SetError(err, cmds.ErrNormal)
165 + return
166 + }
167 +
168 + output := make([]string, len(iaddrs))
169 + for i, addr := range iaddrs {
170 + taddr := addr.Transport()
171 + output[i] = "disconnect " + addr.ID().Pretty()
172 +
173 + found := false
174 + conns := n.PeerHost.Network().ConnsToPeer(addr.ID())
175 + for _, conn := range conns {
176 + if !conn.RemoteMultiaddr().Equal(taddr) {
177 + log.Error("it's not", conn.RemoteMultiaddr(), taddr)
178 + continue
179 + }
180 +
181 + if err := conn.Close(); err != nil {
182 + output[i] += " failure: " + err.Error()
183 + } else {
184 + output[i] += " success"
185 + }
186 + found = true
187 + break
188 + }
189 +
190 + if !found {
191 + output[i] += " failure: conn not found"
192 + }
193 + }
194 + res.SetOutput(&stringList{output})
195 + },
196 + Marshalers: cmds.MarshalerMap{
197 + cmds.Text: stringListMarshaler,
198 + },
199 + Type: stringList{},
200 +}
201 +
202 func stringListMarshaler(res cmds.Response) (io.Reader, error) {
203 list, ok := res.Output().(*stringList)
204 if !ok {