2020-07-24 13:54:03 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2020-09-24 08:27:14 +00:00
|
|
|
"net"
|
2022-05-04 12:34:26 +00:00
|
|
|
"net/url"
|
2020-09-24 08:27:14 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-api-go/v2/rpc/client"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2021-05-18 08:04:01 +00:00
|
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
|
2020-09-24 08:27:14 +00:00
|
|
|
/*
|
|
|
|
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"
|
2021-06-24 12:25:11 +00:00
|
|
|
URIAddr strings: "<scheme://>127.0.0.1:8080"
|
2020-09-24 08:27:14 +00:00
|
|
|
*/
|
|
|
|
|
2023-02-05 15:59:38 +00:00
|
|
|
// Address represents the FrostFS node
|
2020-07-24 13:54:03 +00:00
|
|
|
// network address.
|
2020-09-17 09:09:49 +00:00
|
|
|
type Address struct {
|
|
|
|
ma multiaddr.Multiaddr
|
|
|
|
}
|
|
|
|
|
2021-06-16 12:03:11 +00:00
|
|
|
// String returns multiaddr string.
|
2020-09-17 09:09:49 +00:00
|
|
|
func (a Address) String() string {
|
|
|
|
return a.ma.String()
|
|
|
|
}
|
|
|
|
|
2021-06-23 09:24:01 +00:00
|
|
|
// equal compares Address's.
|
|
|
|
func (a Address) equal(addr Address) bool {
|
2021-05-20 15:17:16 +00:00
|
|
|
return a.ma.Equal(addr.ma)
|
|
|
|
}
|
|
|
|
|
2022-05-04 12:34:26 +00:00
|
|
|
// URIAddr returns Address as a URI.
|
2021-06-16 11:08:07 +00:00
|
|
|
//
|
|
|
|
// Panics if host address cannot be fetched from Address.
|
2022-05-04 12:34:26 +00:00
|
|
|
//
|
|
|
|
// See also FromString.
|
|
|
|
func (a Address) URIAddr() string {
|
2021-05-17 11:59:57 +00:00
|
|
|
_, host, err := manet.DialArgs(a.ma)
|
|
|
|
if err != nil {
|
2021-06-16 11:08:07 +00:00
|
|
|
// 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))
|
2021-05-17 11:59:57 +00:00
|
|
|
}
|
|
|
|
|
2022-05-04 12:34:26 +00:00
|
|
|
if !a.isTLSEnabled() {
|
|
|
|
return host
|
|
|
|
}
|
|
|
|
|
|
|
|
return (&url.URL{
|
|
|
|
Scheme: "grpcs",
|
|
|
|
Host: host,
|
|
|
|
}).String()
|
2021-05-17 11:59:57 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 06:00:21 +00:00
|
|
|
// FromString restores Address from a string representation.
|
2021-06-16 11:00:49 +00:00
|
|
|
//
|
2021-06-24 12:25:11 +00:00
|
|
|
// Supports URIAddr, MultiAddr and HostAddr strings.
|
2021-06-18 06:00:21 +00:00
|
|
|
func (a *Address) FromString(s string) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
a.ma, err = multiaddr.NewMultiaddr(s)
|
2020-09-17 09:09:49 +00:00
|
|
|
if err != nil {
|
2021-10-07 15:43:09 +00:00
|
|
|
var (
|
|
|
|
host string
|
|
|
|
hasTLS bool
|
|
|
|
)
|
2022-03-14 09:15:50 +00:00
|
|
|
host, hasTLS, err = client.ParseURI(s)
|
2021-10-07 15:43:09 +00:00
|
|
|
if err != nil {
|
|
|
|
host = s
|
|
|
|
}
|
2021-06-24 12:25:11 +00:00
|
|
|
|
2021-10-07 15:43:09 +00:00
|
|
|
s, err = multiaddrStringFromHostAddr(host)
|
2021-06-18 06:00:21 +00:00
|
|
|
if err == nil {
|
|
|
|
a.ma, err = multiaddr.NewMultiaddr(s)
|
2021-10-07 15:43:09 +00:00
|
|
|
if err == nil && hasTLS {
|
2021-06-24 12:25:11 +00:00
|
|
|
a.ma = a.ma.Encapsulate(tls)
|
|
|
|
}
|
2020-09-24 08:27:14 +00:00
|
|
|
}
|
2020-09-17 09:09:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 06:00:21 +00:00
|
|
|
return err
|
2020-09-17 09:09:49 +00:00
|
|
|
}
|
2020-09-21 14:26:19 +00:00
|
|
|
|
2022-10-17 12:03:55 +00:00
|
|
|
// multiaddrStringFromHostAddr converts "localhost:8080" to "/dns4/localhost/tcp/8080".
|
2020-09-24 08:27:14 +00:00
|
|
|
func multiaddrStringFromHostAddr(host string) (string, error) {
|
|
|
|
endpoint, port, err := net.SplitHostPort(host)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-05-17 13:05:28 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-09-24 08:27:14 +00:00
|
|
|
var (
|
|
|
|
prefix = "/dns4"
|
|
|
|
addr = endpoint
|
|
|
|
)
|
|
|
|
|
|
|
|
if ip := net.ParseIP(endpoint); ip != nil {
|
|
|
|
addr = ip.String()
|
|
|
|
if ip.To4() == nil {
|
|
|
|
prefix = "/ip6"
|
|
|
|
} else {
|
|
|
|
prefix = "/ip4"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 10:21:24 +00:00
|
|
|
const l4Protocol = "tcp"
|
|
|
|
|
|
|
|
return strings.Join([]string{prefix, addr, l4Protocol, port}, "/"), nil
|
2020-09-24 08:27:14 +00:00
|
|
|
}
|