swarm.go 17.4 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4
package commands

import (
	"bytes"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
	"errors"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6
	"fmt"
7
	"io"
8
	"path"
9
	"sort"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10

11
	cmds "github.com/ipfs/go-ipfs/commands"
12 13 14
	repo "github.com/ipfs/go-ipfs/repo"
	config "github.com/ipfs/go-ipfs/repo/config"
	"github.com/ipfs/go-ipfs/repo/fsrepo"
15
	iaddr "github.com/ipfs/go-ipfs/thirdparty/ipfsaddr"
16 17
	swarm "gx/ipfs/QmVkDnNm71vYyY6s6rXwtmyDYis3WkKyrEhMECwT6R12uJ/go-libp2p-swarm"
	pstore "gx/ipfs/QmXZSd1qR5BxZkPyuwfT5jpqQFScZccoZvDneXsKzCNHWX/go-libp2p-peerstore"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18

Jeromy's avatar
Jeromy committed
19
	mafilter "gx/ipfs/QmSMZwvs3n4GBikZ7hKzT17c3bk65FmyZo2JqtJ16swqCv/multiaddr-filter"
20
	ma "gx/ipfs/QmcyqRMCAXVtYPS4DiBrA7sezL9rRGfW8Ctx7cywL4TXJj/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21 22 23 24 25 26
)

type stringList struct {
	Strings []string
}

27 28 29 30
type addrMap struct {
	Addrs map[string][]string
}

31
var SwarmCmd = &cmds.Command{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32
	Helptext: cmds.HelpText{
33
		Tagline: "Interact with the swarm.",
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34
		ShortDescription: `
35
'ipfs swarm' is a tool to manipulate the network swarm. The swarm is the
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36 37 38 39 40
component that opens, listens for, and maintains connections to other
ipfs peers in the internet.
`,
	},
	Subcommands: map[string]*cmds.Command{
41
		"addrs":      swarmAddrsCmd,
42 43
		"connect":    swarmConnectCmd,
		"disconnect": swarmDisconnectCmd,
44
		"filters":    swarmFiltersCmd,
Richard Littauer's avatar
Richard Littauer committed
45
		"peers":      swarmPeersCmd,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46 47 48 49 50
	},
}

var swarmPeersCmd = &cmds.Command{
	Helptext: cmds.HelpText{
rht's avatar
rht committed
51
		Tagline: "List peers with open connections.",
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
52
		ShortDescription: `
53
'ipfs swarm peers' lists the set of peers this node is connected to.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54 55
`,
	},
56
	Options: []cmds.Option{
57 58 59
		cmds.BoolOption("verbose", "v", "display all extra information"),
		cmds.BoolOption("streams", "Also list information about open streams for each peer"),
		cmds.BoolOption("latency", "Also list information about latency to each peer"),
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
60
	},
61
	Run: func(req cmds.Request, res cmds.Response) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62 63

		log.Debug("ipfs swarm peers")
Jeromy's avatar
Jeromy committed
64
		n, err := req.InvocContext().GetNode()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65
		if err != nil {
66 67
			res.SetError(err, cmds.ErrNormal)
			return
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
68 69
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
70
		if n.PeerHost == nil {
71 72
			res.SetError(errNotOnline, cmds.ErrClient)
			return
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73 74
		}

75
		verbose, _, _ := req.Option("verbose").Bool()
76 77 78
		latency, _, _ := req.Option("latency").Bool()
		streams, _, _ := req.Option("streams").Bool()

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
79
		conns := n.PeerHost.Network().Conns()
80

81 82
		var out connInfos
		for _, c := range conns {
83
			pid := c.RemotePeer()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84
			addr := c.RemoteMultiaddr()
85

86 87 88 89 90
			ci := connInfo{
				Addr: addr.String(),
				Peer: pid.Pretty(),
			}

Jeromy's avatar
Jeromy committed
91 92 93 94 95
			swcon, ok := c.(*swarm.Conn)
			if ok {
				ci.Muxer = fmt.Sprintf("%T", swcon.StreamConn().Conn())
			}

96
			if verbose || latency {
97 98 99 100 101 102
				lat := n.Peerstore.LatencyEWMA(pid)
				if lat == 0 {
					ci.Latency = "n/a"
				} else {
					ci.Latency = lat.String()
				}
103 104 105 106 107 108 109 110 111 112 113
			}
			if verbose || streams {
				strs, err := c.GetStreams()
				if err != nil {
					res.SetError(err, cmds.ErrNormal)
					return
				}

				for _, s := range strs {
					ci.Streams = append(ci.Streams, streamInfo{Protocol: string(s.Protocol())})
				}
114
			}
115 116
			sort.Sort(&ci)
			out.Peers = append(out.Peers, ci)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
117 118
		}

119 120
		sort.Sort(&out)
		res.SetOutput(&out)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
121 122
	},
	Marshalers: cmds.MarshalerMap{
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
			ci, ok := res.Output().(*connInfos)
			if !ok {
				return nil, fmt.Errorf("expected output type to be connInfos")
			}

			buf := new(bytes.Buffer)
			for _, info := range ci.Peers {
				fmt.Fprintf(buf, "%s/ipfs/%s", info.Addr, info.Peer)
				if info.Latency != "" {
					fmt.Fprintf(buf, " %s", info.Latency)
				}
				fmt.Fprintln(buf)

				for _, s := range info.Streams {
					if s.Protocol == "" {
						s.Protocol = "<no protocol name>"
					}

					fmt.Fprintf(buf, "  %s\n", s.Protocol)
				}
			}

			return buf, nil
		},
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
148
	},
149 150 151 152 153 154 155 156 157 158 159
	Type: connInfos{},
}

type streamInfo struct {
	Protocol string
}

type connInfo struct {
	Addr    string
	Peer    string
	Latency string
Jeromy's avatar
Jeromy committed
160
	Muxer   string
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
	Streams []streamInfo
}

func (ci *connInfo) Less(i, j int) bool {
	return ci.Streams[i].Protocol < ci.Streams[j].Protocol
}

func (ci *connInfo) Len() int {
	return len(ci.Streams)
}

func (ci *connInfo) Swap(i, j int) {
	ci.Streams[i], ci.Streams[j] = ci.Streams[j], ci.Streams[i]
}

type connInfos struct {
	Peers []connInfo
}

func (ci connInfos) Less(i, j int) bool {
	return ci.Peers[i].Addr < ci.Peers[j].Addr
}

func (ci connInfos) Len() int {
	return len(ci.Peers)
}

func (ci connInfos) Swap(i, j int) {
	ci.Peers[i], ci.Peers[j] = ci.Peers[j], ci.Peers[i]
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
190 191
}

192 193
var swarmAddrsCmd = &cmds.Command{
	Helptext: cmds.HelpText{
194
		Tagline: "List known addresses. Useful for debugging.",
195
		ShortDescription: `
196
'ipfs swarm addrs' lists all addresses this node is aware of.
197 198
`,
	},
199 200 201
	Subcommands: map[string]*cmds.Command{
		"local": swarmAddrsLocalCmd,
	},
202 203
	Run: func(req cmds.Request, res cmds.Response) {

Jeromy's avatar
Jeromy committed
204
		n, err := req.InvocContext().GetNode()
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		if n.PeerHost == nil {
			res.SetError(errNotOnline, cmds.ErrClient)
			return
		}

		addrs := make(map[string][]string)
		ps := n.PeerHost.Network().Peerstore()
		for _, p := range ps.Peers() {
			s := p.Pretty()
			for _, a := range ps.Addrs(p) {
				addrs[s] = append(addrs[s], a.String())
			}
			sort.Sort(sort.StringSlice(addrs[s]))
		}

		res.SetOutput(&addrMap{Addrs: addrs})
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
			m, ok := res.Output().(*addrMap)
			if !ok {
				return nil, errors.New("failed to cast map[string]string")
			}

			// sort the ids first
			ids := make([]string, 0, len(m.Addrs))
			for p := range m.Addrs {
				ids = append(ids, p)
			}
			sort.Sort(sort.StringSlice(ids))

241
			buf := new(bytes.Buffer)
242 243 244 245 246 247 248
			for _, p := range ids {
				paddrs := m.Addrs[p]
				buf.WriteString(fmt.Sprintf("%s (%d)\n", p, len(paddrs)))
				for _, addr := range paddrs {
					buf.WriteString("\t" + addr + "\n")
				}
			}
249
			return buf, nil
250 251 252 253 254
		},
	},
	Type: addrMap{},
}

255 256 257 258
var swarmAddrsLocalCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "List local addresses.",
		ShortDescription: `
259
'ipfs swarm addrs local' lists all local addresses the node is listening on.
260 261 262
`,
	},
	Options: []cmds.Option{
263
		cmds.BoolOption("id", "Show peer ID in addresses.").Default(false),
264 265 266
	},
	Run: func(req cmds.Request, res cmds.Response) {

Jeromy's avatar
Jeromy committed
267
		n, err := req.InvocContext().GetNode()
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		if n.PeerHost == nil {
			res.SetError(errNotOnline, cmds.ErrClient)
			return
		}

		showid, _, _ := req.Option("id").Bool()
		id := n.Identity.Pretty()

		var addrs []string
		for _, addr := range n.PeerHost.Addrs() {
			saddr := addr.String()
			if showid {
				saddr = path.Join(saddr, "ipfs", id)
			}
			addrs = append(addrs, saddr)
		}
		sort.Sort(sort.StringSlice(addrs))

		res.SetOutput(&stringList{addrs})
	},
	Type: stringList{},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: stringListMarshaler,
	},
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
299 300
var swarmConnectCmd = &cmds.Command{
	Helptext: cmds.HelpText{
rht's avatar
rht committed
301
		Tagline: "Open connection to a given address.",
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
302
		ShortDescription: `
303 304
'ipfs swarm connect' opens a new direct connection to a peer address.

305
The address format is an IPFS multiaddr:
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
306

307
ipfs swarm connect /ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
308 309 310
`,
	},
	Arguments: []cmds.Argument{
Jeromy's avatar
Jeromy committed
311
		cmds.StringArg("address", true, true, "Address of peer to connect to.").EnableStdin(),
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
312
	},
313
	Run: func(req cmds.Request, res cmds.Response) {
314
		ctx := req.Context()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
315

Jeromy's avatar
Jeromy committed
316
		n, err := req.InvocContext().GetNode()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
317
		if err != nil {
318 319
			res.SetError(err, cmds.ErrNormal)
			return
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
320 321
		}

322
		addrs := req.Arguments()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
323

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
324
		if n.PeerHost == nil {
325 326
			res.SetError(errNotOnline, cmds.ErrClient)
			return
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
327 328
		}

Jeromy's avatar
Jeromy committed
329 330 331 332 333 334 335 336
		snet, ok := n.PeerHost.Network().(*swarm.Network)
		if !ok {
			res.SetError(fmt.Errorf("peerhost network was not swarm"), cmds.ErrNormal)
			return
		}

		swrm := snet.Swarm()

337
		pis, err := peersWithAddresses(addrs)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
338
		if err != nil {
339 340
			res.SetError(err, cmds.ErrNormal)
			return
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
341 342
		}

343 344
		output := make([]string, len(pis))
		for i, pi := range pis {
Jeromy's avatar
Jeromy committed
345 346
			swrm.Backoff().Clear(pi.ID)

347
			output[i] = "connect " + pi.ID.Pretty()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
348

349
			err := n.PeerHost.Connect(ctx, pi)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
350
			if err != nil {
351 352
				res.SetError(fmt.Errorf("%s failure: %s", output[i], err), cmds.ErrNormal)
				return
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
353
			}
354
			output[i] += " success"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
355 356
		}

357
		res.SetOutput(&stringList{output})
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
358 359 360 361
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: stringListMarshaler,
	},
362
	Type: stringList{},
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
363 364
}

365 366
var swarmDisconnectCmd = &cmds.Command{
	Helptext: cmds.HelpText{
rht's avatar
rht committed
367
		Tagline: "Close connection to a given address.",
368
		ShortDescription: `
369
'ipfs swarm disconnect' closes a connection to a peer address. The address
370
format is an IPFS multiaddr:
371 372

ipfs swarm disconnect /ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
373

374 375
The disconnect is not permanent; if ipfs needs to talk to that address later,
it will reconnect.
376 377 378
`,
	},
	Arguments: []cmds.Argument{
Jeromy's avatar
Jeromy committed
379
		cmds.StringArg("address", true, true, "Address of peer to disconnect from.").EnableStdin(),
380 381
	},
	Run: func(req cmds.Request, res cmds.Response) {
Jeromy's avatar
Jeromy committed
382
		n, err := req.InvocContext().GetNode()
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		addrs := req.Arguments()

		if n.PeerHost == nil {
			res.SetError(errNotOnline, cmds.ErrClient)
			return
		}

		iaddrs, err := parseAddresses(addrs)
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		output := make([]string, len(iaddrs))
		for i, addr := range iaddrs {
			taddr := addr.Transport()
			output[i] = "disconnect " + addr.ID().Pretty()

			found := false
			conns := n.PeerHost.Network().ConnsToPeer(addr.ID())
			for _, conn := range conns {
				if !conn.RemoteMultiaddr().Equal(taddr) {
410
					log.Debug("it's not", conn.RemoteMultiaddr(), taddr)
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
					continue
				}

				if err := conn.Close(); err != nil {
					output[i] += " failure: " + err.Error()
				} else {
					output[i] += " success"
				}
				found = true
				break
			}

			if !found {
				output[i] += " failure: conn not found"
			}
		}
		res.SetOutput(&stringList{output})
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: stringListMarshaler,
	},
	Type: stringList{},
}

435
func stringListMarshaler(res cmds.Response) (io.Reader, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
436 437 438 439 440
	list, ok := res.Output().(*stringList)
	if !ok {
		return nil, errors.New("failed to cast []string")
	}

441
	buf := new(bytes.Buffer)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
442
	for _, s := range list.Strings {
443 444
		buf.WriteString(s)
		buf.WriteString("\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
445
	}
446
	return buf, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
447
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
448

449
// parseAddresses is a function that takes in a slice of string peer addresses
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
450
// (multiaddr + peerid) and returns slices of multiaddrs and peerids.
451 452 453 454
func parseAddresses(addrs []string) (iaddrs []iaddr.IPFSAddr, err error) {
	iaddrs = make([]iaddr.IPFSAddr, len(addrs))
	for i, saddr := range addrs {
		iaddrs[i], err = iaddr.ParseString(saddr)
455
		if err != nil {
456
			return nil, cmds.ClientError("invalid peer address: " + err.Error())
457
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
458 459 460 461 462 463
	}
	return
}

// peersWithAddresses is a function that takes in a slice of string peer addresses
// (multiaddr + peerid) and returns a slice of properly constructed peers
Jeromy's avatar
Jeromy committed
464
func peersWithAddresses(addrs []string) (pis []pstore.PeerInfo, err error) {
465
	iaddrs, err := parseAddresses(addrs)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
466 467 468 469
	if err != nil {
		return nil, err
	}

470
	for _, iaddr := range iaddrs {
Jeromy's avatar
Jeromy committed
471
		pis = append(pis, pstore.PeerInfo{
472 473 474
			ID:    iaddr.ID(),
			Addrs: []ma.Multiaddr{iaddr.Transport()},
		})
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
475
	}
476
	return pis, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
477
}
478 479 480

var swarmFiltersCmd = &cmds.Command{
	Helptext: cmds.HelpText{
rht's avatar
rht committed
481
		Tagline: "Manipulate address filters.",
482
		ShortDescription: `
483 484 485
'ipfs swarm filters' will list out currently applied filters. Its subcommands
can be used to add or remove said filters. Filters are specified using the
multiaddr-filter format:
486

487
Example:
488 489 490 491 492 493 494

    /ip4/192.168.0.0/ipcidr/16

Where the above is equivalent to the standard CIDR:

    192.168.0.0/16

495
Filters default to those specified under the "Swarm.AddrFilters" config key.
496 497 498 499 500 501 502
`,
	},
	Subcommands: map[string]*cmds.Command{
		"add": swarmFiltersAddCmd,
		"rm":  swarmFiltersRmCmd,
	},
	Run: func(req cmds.Request, res cmds.Response) {
Jeromy's avatar
Jeromy committed
503
		n, err := req.InvocContext().GetNode()
504 505 506 507 508
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

509 510 511 512 513
		if n.PeerHost == nil {
			res.SetError(errNotOnline, cmds.ErrNormal)
			return
		}

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
		snet, ok := n.PeerHost.Network().(*swarm.Network)
		if !ok {
			res.SetError(errors.New("failed to cast network to swarm network"), cmds.ErrNormal)
			return
		}

		var output []string
		for _, f := range snet.Filters.Filters() {
			s, err := mafilter.ConvertIPNet(f)
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}
			output = append(output, s)
		}
		res.SetOutput(&stringList{output})
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: stringListMarshaler,
	},
	Type: stringList{},
}

var swarmFiltersAddCmd = &cmds.Command{
	Helptext: cmds.HelpText{
rht's avatar
rht committed
539
		Tagline: "Add an address filter.",
540
		ShortDescription: `
541
'ipfs swarm filters add' will add an address filter to the daemons swarm.
Richard Littauer's avatar
Richard Littauer committed
542
Filters applied this way will not persist daemon reboots, to achieve that,
543 544 545 546
add your filters to the ipfs config file.
`,
	},
	Arguments: []cmds.Argument{
Jeromy's avatar
Jeromy committed
547
		cmds.StringArg("address", true, true, "Multiaddr to filter.").EnableStdin(),
548 549
	},
	Run: func(req cmds.Request, res cmds.Response) {
Jeromy's avatar
Jeromy committed
550
		n, err := req.InvocContext().GetNode()
551 552 553 554 555
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

556 557 558 559 560
		if n.PeerHost == nil {
			res.SetError(errNotOnline, cmds.ErrNormal)
			return
		}

561 562 563 564 565 566
		snet, ok := n.PeerHost.Network().(*swarm.Network)
		if !ok {
			res.SetError(errors.New("failed to cast network to swarm network"), cmds.ErrNormal)
			return
		}

567 568 569 570 571
		if len(req.Arguments()) == 0 {
			res.SetError(errors.New("no filters to add"), cmds.ErrClient)
			return
		}

572 573 574 575 576 577 578 579 580 581 582 583
		r, err := fsrepo.Open(req.InvocContext().ConfigRoot)
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}
		defer r.Close()
		cfg, err := r.Config()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

584 585 586 587 588 589 590 591 592
		for _, arg := range req.Arguments() {
			mask, err := mafilter.NewMask(arg)
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}

			snet.Filters.AddDialFilter(mask)
		}
593 594 595 596 597 598 599 600 601

		added, err := filtersAdd(r, cfg, req.Arguments())
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return

		}

		res.SetOutput(&stringList{added})
602
	},
603 604 605 606
	Marshalers: cmds.MarshalerMap{
		cmds.Text: stringListMarshaler,
	},
	Type: stringList{},
607 608 609 610
}

var swarmFiltersRmCmd = &cmds.Command{
	Helptext: cmds.HelpText{
rht's avatar
rht committed
611
		Tagline: "Remove an address filter.",
612
		ShortDescription: `
613
'ipfs swarm filters rm' will remove an address filter from the daemons swarm.
Richard Littauer's avatar
Richard Littauer committed
614
Filters removed this way will not persist daemon reboots, to achieve that,
615 616 617 618
remove your filters from the ipfs config file.
`,
	},
	Arguments: []cmds.Argument{
Jeromy's avatar
Jeromy committed
619
		cmds.StringArg("address", true, true, "Multiaddr filter to remove.").EnableStdin(),
620 621
	},
	Run: func(req cmds.Request, res cmds.Response) {
Jeromy's avatar
Jeromy committed
622
		n, err := req.InvocContext().GetNode()
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		if n.PeerHost == nil {
			res.SetError(errNotOnline, cmds.ErrNormal)
			return
		}

		snet, ok := n.PeerHost.Network().(*swarm.Network)
		if !ok {
			res.SetError(errors.New("failed to cast network to swarm network"), cmds.ErrNormal)
			return
		}

639 640 641 642 643 644 645 646 647 648 649 650
		r, err := fsrepo.Open(req.InvocContext().ConfigRoot)
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}
		defer r.Close()
		cfg, err := r.Config()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

651 652 653 654 655
		if req.Arguments()[0] == "all" || req.Arguments()[0] == "*" {
			fs := snet.Filters.Filters()
			for _, f := range fs {
				snet.Filters.Remove(f)
			}
656 657 658 659 660 661 662 663 664

			removed, err := filtersRemoveAll(r, cfg)
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}

			res.SetOutput(&stringList{removed})

665 666 667 668 669 670 671 672 673 674 675 676
			return
		}

		for _, arg := range req.Arguments() {
			mask, err := mafilter.NewMask(arg)
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}

			snet.Filters.Remove(mask)
		}
677 678 679 680 681 682 683

		removed, err := filtersRemove(r, cfg, req.Arguments())

		res.SetOutput(&stringList{removed})
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: stringListMarshaler,
684
	},
685
	Type: stringList{},
686
}
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722

func filtersAdd(r repo.Repo, cfg *config.Config, filters []string) ([]string, error) {
	addedMap := map[string]struct{}{}
	addedList := make([]string, 0, len(filters))

	// re-add cfg swarm filters to rm dupes
	oldFilters := cfg.Swarm.AddrFilters
	cfg.Swarm.AddrFilters = nil

	// add new filters
	for _, filter := range filters {
		if _, found := addedMap[filter]; found {
			continue
		}

		cfg.Swarm.AddrFilters = append(cfg.Swarm.AddrFilters, filter)
		addedList = append(addedList, filter)
		addedMap[filter] = struct{}{}
	}

	// add back original filters. in this order so that we output them.
	for _, filter := range oldFilters {
		if _, found := addedMap[filter]; found {
			continue
		}

		cfg.Swarm.AddrFilters = append(cfg.Swarm.AddrFilters, filter)
		addedMap[filter] = struct{}{}
	}

	if err := r.SetConfig(cfg); err != nil {
		return nil, err
	}

	return addedList, nil
}
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762

func filtersRemoveAll(r repo.Repo, cfg *config.Config) ([]string, error) {
	removed := cfg.Swarm.AddrFilters
	cfg.Swarm.AddrFilters = nil

	if err := r.SetConfig(cfg); err != nil {
		return nil, err
	}

	return removed, nil
}

func filtersRemove(r repo.Repo, cfg *config.Config, toRemoveFilters []string) ([]string, error) {
	removed := make([]string, 0, len(toRemoveFilters))
	keep := make([]string, 0, len(cfg.Swarm.AddrFilters))

	oldFilters := cfg.Swarm.AddrFilters

	for _, oldFilter := range oldFilters {
		found := false
		for _, toRemoveFilter := range toRemoveFilters {
			if oldFilter == toRemoveFilter {
				found = true
				removed = append(removed, toRemoveFilter)
				break
			}
		}

		if !found {
			keep = append(keep, oldFilter)
		}
	}
	cfg.Swarm.AddrFilters = keep

	if err := r.SetConfig(cfg); err != nil {
		return nil, err
	}

	return removed, nil
}