forked from TrueCloudLab/frostfs-node
Airat Arifullin
9b13a18aac
* Update version within go.mod; * Fix deprecated frostfs-api-go/v2 package and use frostfs-sdk-go/api instead. Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
127 lines
2.7 KiB
Go
127 lines
2.7 KiB
Go
package network
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/rpc/client"
|
|
"github.com/multiformats/go-multiaddr"
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
|
)
|
|
|
|
/*
|
|
HostAddr strings: "localhost:8080", ":8080", "192.168.0.1:8080"
|
|
MultiAddr strings: "/dns4/localhost/tcp/8080", "/ip4/192.168.0.1/tcp/8080"
|
|
IPAddr strings: "192.168.0.1:8080"
|
|
URIAddr strings: "<scheme://>127.0.0.1:8080"
|
|
*/
|
|
|
|
var errHostIsEmpty = errors.New("host is empty")
|
|
|
|
// Address represents the FrostFS node
|
|
// network address.
|
|
type Address struct {
|
|
ma multiaddr.Multiaddr
|
|
}
|
|
|
|
// String returns multiaddr string.
|
|
func (a Address) String() string {
|
|
return a.ma.String()
|
|
}
|
|
|
|
// equal compares Address's.
|
|
func (a Address) equal(addr Address) bool {
|
|
return a.ma.Equal(addr.ma)
|
|
}
|
|
|
|
// URIAddr returns Address as a URI.
|
|
//
|
|
// Panics if host address cannot be fetched from Address.
|
|
//
|
|
// See also FromString.
|
|
func (a Address) URIAddr() string {
|
|
_, host, err := manet.DialArgs(a.ma)
|
|
if err != nil {
|
|
// the only correct way to construct Address is AddressFromString
|
|
// which makes this error appear unexpected
|
|
panic(fmt.Errorf("could not get host addr: %w", err))
|
|
}
|
|
|
|
if !a.IsTLSEnabled() {
|
|
return host
|
|
}
|
|
|
|
return (&url.URL{
|
|
Scheme: "grpcs",
|
|
Host: host,
|
|
}).String()
|
|
}
|
|
|
|
// FromString restores Address from a string representation.
|
|
//
|
|
// Supports URIAddr, MultiAddr and HostAddr strings.
|
|
func (a *Address) FromString(s string) error {
|
|
var err error
|
|
|
|
a.ma, err = multiaddr.NewMultiaddr(s)
|
|
if err != nil {
|
|
var (
|
|
host string
|
|
hasTLS bool
|
|
)
|
|
host, hasTLS, err = client.ParseURI(s)
|
|
if err != nil {
|
|
host = s
|
|
}
|
|
|
|
s, err = multiaddrStringFromHostAddr(host)
|
|
if err == nil {
|
|
a.ma, err = multiaddr.NewMultiaddr(s)
|
|
if err == nil && hasTLS {
|
|
a.ma = a.ma.Encapsulate(tls)
|
|
}
|
|
}
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// multiaddrStringFromHostAddr converts "localhost:8080" to "/dns4/localhost/tcp/8080".
|
|
func multiaddrStringFromHostAddr(host string) (string, error) {
|
|
if len(host) == 0 {
|
|
return "", errHostIsEmpty
|
|
}
|
|
|
|
endpoint, port, err := net.SplitHostPort(host)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// Empty address in host `:8080` generates `/dns4//tcp/8080` multiaddr
|
|
// which is invalid. It could be `/tcp/8080` but this breaks
|
|
// `manet.DialArgs`. The solution is to manually parse it as 0.0.0.0
|
|
if endpoint == "" {
|
|
return "/ip4/0.0.0.0/tcp/" + port, nil
|
|
}
|
|
|
|
var (
|
|
prefix = "/dns4"
|
|
addr = endpoint
|
|
)
|
|
|
|
if ip := net.ParseIP(endpoint); ip != nil {
|
|
addr = ip.String()
|
|
if ip.To4() == nil {
|
|
prefix = "/ip6"
|
|
} else {
|
|
prefix = "/ip4"
|
|
}
|
|
}
|
|
|
|
const l4Protocol = "tcp"
|
|
|
|
return strings.Join([]string{prefix, addr, l4Protocol, port}, "/"), nil
|
|
}
|