ipfs-cluster/cmdutils/cmdutils.go
Kishan Sagathiya 7b499263af service init --randomports:
Allow initialize cluster with random ports that are ununsed (at the moment of initialization)
2019-10-24 17:54:50 +02:00

59 lines
1.2 KiB
Go

// Package cmdutils contains utilities to facilitate building of command line
// applications launching cluster peers.
package cmdutils
import (
"fmt"
"net"
ma "github.com/multiformats/go-multiaddr"
)
// RandomizePorts replaces TCP and UDP ports with random, but valid port
// values.
func RandomizePorts(m ma.Multiaddr) (ma.Multiaddr, error) {
var prev string
var err error
components := []ma.Multiaddr{}
ma.ForEach(m, func(c ma.Component) bool {
code := c.Protocol().Code
if code != ma.P_TCP && code != ma.P_UDP {
components = append(components, &c)
prev = c.Value()
return true
}
var ln net.Listener
ln, err = net.Listen(c.Protocol().Name, prev+":")
if err != nil {
return false
}
defer ln.Close()
var c1 *ma.Component
c1, err = ma.NewComponent(c.Protocol().Name, fmt.Sprintf("%d", getPort(ln, code)))
if err != nil {
return false
}
components = append(components, c1)
prev = c.Value()
return true
})
return ma.Join(components...), err
}
func getPort(ln net.Listener, code int) int {
if code == ma.P_TCP {
return ln.Addr().(*net.TCPAddr).Port
}
if code == ma.P_UDP {
return ln.Addr().(*net.UDPAddr).Port
}
return 0
}