[#607] network: Support URI address strings

Make Address.FromString method to parse URI addresses and enable TLS for TLS
schemes.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-06-24 15:25:11 +03:00 committed by Leonard Lyubich
parent 43eff09944
commit 6f861b6489
3 changed files with 45 additions and 5 deletions

View file

@ -3,6 +3,7 @@ package network
import ( import (
"fmt" "fmt"
"net" "net"
"net/url"
"strings" "strings"
"github.com/multiformats/go-multiaddr" "github.com/multiformats/go-multiaddr"
@ -13,6 +14,7 @@ import (
HostAddr strings: "localhost:8080", ":8080", "192.168.0.1:8080" HostAddr strings: "localhost:8080", ":8080", "192.168.0.1:8080"
MultiAddr strings: "/dns4/localhost/tcp/8080", "/ip4/192.168.0.1/tcp/8080" MultiAddr strings: "/dns4/localhost/tcp/8080", "/ip4/192.168.0.1/tcp/8080"
IPAddr strings: "192.168.0.1:8080" IPAddr strings: "192.168.0.1:8080"
URIAddr strings: "<scheme://>127.0.0.1:8080"
*/ */
// Address represents the NeoFS node // Address represents the NeoFS node
@ -53,21 +55,54 @@ func (a Address) HostAddr() string {
// FromString restores Address from a string representation. // FromString restores Address from a string representation.
// //
// Supports MultiAddr and HostAddr strings. // Supports URIAddr, MultiAddr and HostAddr strings.
func (a *Address) FromString(s string) error { func (a *Address) FromString(s string) error {
var err error var err error
a.ma, err = multiaddr.NewMultiaddr(s) a.ma, err = multiaddr.NewMultiaddr(s)
if err != nil { if err != nil {
s, err = multiaddrStringFromHostAddr(s) var u uri
u.parse(s)
s, err = multiaddrStringFromHostAddr(u.host)
if err == nil { if err == nil {
a.ma, err = multiaddr.NewMultiaddr(s) a.ma, err = multiaddr.NewMultiaddr(s)
if err == nil && u.tls {
a.ma = a.ma.Encapsulate(tls)
}
} }
} }
return err return err
} }
type uri struct {
host string
tls bool
}
const grpcTLSScheme = "grpcs"
func (u *uri) parse(s string) {
// TODO: code is copy-pasted from client.WithURIAddress function.
// Would be nice to share the code.
uri, err := url.ParseRequestURI(s)
isURI := err == nil
if isURI && uri.Host != "" {
u.host = uri.Host
} else {
u.host = s
}
// check if passed string was parsed correctly
// URIs that do not start with a slash after the scheme are interpreted as:
// `scheme:opaque` => if `opaque` is not empty, then it is supposed that URI
// is in `host:port` format
u.tls = isURI && uri.Opaque == "" && uri.Scheme == grpcTLSScheme
}
// multiaddrStringFromHostAddr converts "localhost:8080" to "/dns4/localhost/tcp/8080" // multiaddrStringFromHostAddr converts "localhost:8080" to "/dns4/localhost/tcp/8080"
func multiaddrStringFromHostAddr(host string) (string, error) { func multiaddrStringFromHostAddr(host string) (string, error) {
endpoint, port, err := net.SplitHostPort(host) endpoint, port, err := net.SplitHostPort(host)

View file

@ -17,6 +17,8 @@ func TestAddressFromString(t *testing.T) {
{"example.com:7070", buildMultiaddr("/dns4/example.com/tcp/7070", t)}, {"example.com:7070", buildMultiaddr("/dns4/example.com/tcp/7070", t)},
{"213.44.87.1:32512", buildMultiaddr("/ip4/213.44.87.1/tcp/32512", t)}, {"213.44.87.1:32512", buildMultiaddr("/ip4/213.44.87.1/tcp/32512", t)},
{"[2004:eb1::1]:8080", buildMultiaddr("/ip6/2004:eb1::1/tcp/8080", t)}, {"[2004:eb1::1]:8080", buildMultiaddr("/ip6/2004:eb1::1/tcp/8080", t)},
{"grpc://example.com:7070", buildMultiaddr("/dns4/example.com/tcp/7070", t)},
{grpcTLSScheme + "://example.com:7070", buildMultiaddr("/dns4/example.com/tcp/7070/"+tlsProtocolName, t)},
} }
var addr Address var addr Address

View file

@ -14,12 +14,15 @@ func TestAddress_TLSEnabled(t *testing.T) {
{"/dns4/localhost/tcp/8080", false}, {"/dns4/localhost/tcp/8080", false},
{"/dns4/localhost/tcp/8080/tls", true}, {"/dns4/localhost/tcp/8080/tls", true},
{"/tls/dns4/localhost/tcp/8080", true}, {"/tls/dns4/localhost/tcp/8080", true},
{"grpc://localhost:8080", false},
{"grpcs://localhost:8080", true},
} }
var addr Address
for _, test := range testCases { for _, test := range testCases {
addr := Address{ err := addr.FromString(test.input)
ma: buildMultiaddr(test.input, t), require.NoError(t, err)
}
require.Equal(t, test.wantTLS, addr.TLSEnabled(), test.input) require.Equal(t, test.wantTLS, addr.TLSEnabled(), test.input)
} }