addrs.go 2.87 KB
Newer Older
tavit ohanian's avatar
tavit ohanian committed
1
package dms3p2p
2 3 4 5 6

import (
	"fmt"

	mamask "github.com/whyrusleeping/multiaddr-filter"
tavit ohanian's avatar
tavit ohanian committed
7 8 9 10
	ma "gitlab.dms3.io/mf/go-multiaddr"
	"gitlab.dms3.io/p2p/go-p2p"
	host "gitlab.dms3.io/p2p/go-p2p-core/host"
	p2pbhost "gitlab.dms3.io/p2p/go-p2p/p2p/host/basic"
11 12
)

tavit ohanian's avatar
tavit ohanian committed
13 14
func AddrFilters(filters []string) func() (*ma.Filters, P2pOpts, error) {
	return func() (filter *ma.Filters, opts P2pOpts, err error) {
Steven Allen's avatar
Steven Allen committed
15
		filter = ma.NewFilters()
tavit ohanian's avatar
tavit ohanian committed
16
		opts.Opts = append(opts.Opts, p2p.Filters(filter)) //nolint
17 18 19
		for _, s := range filters {
			f, err := mamask.NewMask(s)
			if err != nil {
Steven Allen's avatar
Steven Allen committed
20
				return filter, opts, fmt.Errorf("incorrectly formatted address filter in config: %s", s)
21
			}
tavit ohanian's avatar
tavit ohanian committed
22
			opts.Opts = append(opts.Opts, p2p.FilterAddresses(f)) //nolint
23
		}
Steven Allen's avatar
Steven Allen committed
24
		return filter, opts, nil
25 26 27 28 29 30 31 32 33 34 35 36 37
	}
}

func makeAddrsFactory(announce []string, noAnnounce []string) (p2pbhost.AddrsFactory, error) {
	var annAddrs []ma.Multiaddr
	for _, addr := range announce {
		maddr, err := ma.NewMultiaddr(addr)
		if err != nil {
			return nil, err
		}
		annAddrs = append(annAddrs, maddr)
	}

Steven Allen's avatar
Steven Allen committed
38
	filters := ma.NewFilters()
39 40 41 42
	noAnnAddrs := map[string]bool{}
	for _, addr := range noAnnounce {
		f, err := mamask.NewMask(addr)
		if err == nil {
Steven Allen's avatar
Steven Allen committed
43
			filters.AddFilter(*f, ma.ActionDeny)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
			continue
		}
		maddr, err := ma.NewMultiaddr(addr)
		if err != nil {
			return nil, err
		}
		noAnnAddrs[string(maddr.Bytes())] = true
	}

	return func(allAddrs []ma.Multiaddr) []ma.Multiaddr {
		var addrs []ma.Multiaddr
		if len(annAddrs) > 0 {
			addrs = annAddrs
		} else {
			addrs = allAddrs
		}

		var out []ma.Multiaddr
		for _, maddr := range addrs {
			// check for exact matches
			ok := noAnnAddrs[string(maddr.Bytes())]
			// check for /ipcidr matches
			if !ok && !filters.AddrBlocked(maddr) {
				out = append(out, maddr)
			}
		}
		return out
	}, nil
}

tavit ohanian's avatar
tavit ohanian committed
74 75
func AddrsFactory(announce []string, noAnnounce []string) func() (opts P2pOpts, err error) {
	return func() (opts P2pOpts, err error) {
76 77 78 79
		addrsFactory, err := makeAddrsFactory(announce, noAnnounce)
		if err != nil {
			return opts, err
		}
tavit ohanian's avatar
tavit ohanian committed
80
		opts.Opts = append(opts.Opts, p2p.AddrsFactory(addrsFactory))
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
		return
	}
}

func listenAddresses(addresses []string) ([]ma.Multiaddr, error) {
	var listen []ma.Multiaddr
	for _, addr := range addresses {
		maddr, err := ma.NewMultiaddr(addr)
		if err != nil {
			return nil, fmt.Errorf("failure to parse config.Addresses.Swarm: %s", addresses)
		}
		listen = append(listen, maddr)
	}

	return listen, nil
}

func StartListening(addresses []string) func(host host.Host) error {
	return func(host host.Host) error {
		listenAddrs, err := listenAddresses(addresses)
		if err != nil {
			return err
		}

		// Actually start listening:
		if err := host.Network().Listen(listenAddrs...); err != nil {
			return err
		}

		// list out our addresses
		addrs, err := host.Network().InterfaceListenAddresses()
		if err != nil {
			return err
		}
tavit ohanian's avatar
tavit ohanian committed
115
		log.Info("Swarm listening at: %s", addrs)
116 117 118
		return nil
	}
}