restapi client: support talking to unix sockets
Setting APIAddr to a Unix socket multiaddress will just work.
This commit is contained in:
parent
d628558aeb
commit
71217ac5d1
|
@ -292,6 +292,8 @@ func (c *defaultClient) setupHTTPClient() error {
|
||||||
switch {
|
switch {
|
||||||
case IsPeerAddress(c.config.APIAddr):
|
case IsPeerAddress(c.config.APIAddr):
|
||||||
err = c.enableLibp2p()
|
err = c.enableLibp2p()
|
||||||
|
case isUnixSocketAddress(c.config.APIAddr):
|
||||||
|
err = c.enableUnix()
|
||||||
case c.config.SSL:
|
case c.config.SSL:
|
||||||
err = c.enableTLS()
|
err = c.enableTLS()
|
||||||
default:
|
default:
|
||||||
|
@ -318,13 +320,15 @@ func (c *defaultClient) setupHTTPClient() error {
|
||||||
func (c *defaultClient) setupHostname() error {
|
func (c *defaultClient) setupHostname() error {
|
||||||
// Extract host:port form APIAddr or use Host:Port.
|
// Extract host:port form APIAddr or use Host:Port.
|
||||||
// For libp2p, hostname is set in enableLibp2p()
|
// For libp2p, hostname is set in enableLibp2p()
|
||||||
if IsPeerAddress(c.config.APIAddr) {
|
// For unix sockets, hostname set in enableUnix()
|
||||||
|
if IsPeerAddress(c.config.APIAddr) || isUnixSocketAddress(c.config.APIAddr) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
_, hostname, err := manet.DialArgs(c.config.APIAddr)
|
_, hostname, err := manet.DialArgs(c.config.APIAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
c.hostname = hostname
|
c.hostname = hostname
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -361,3 +365,13 @@ func IsPeerAddress(addr ma.Multiaddr) bool {
|
||||||
dnsaddr, err2 := addr.ValueForProtocol(madns.DnsaddrProtocol.Code)
|
dnsaddr, err2 := addr.ValueForProtocol(madns.DnsaddrProtocol.Code)
|
||||||
return (pid != "" && err == nil) || (dnsaddr != "" && err2 == nil)
|
return (pid != "" && err == nil) || (dnsaddr != "" && err2 == nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isUnixSocketAddress returns if the given address corresponds to a
|
||||||
|
// unix socket.
|
||||||
|
func isUnixSocketAddress(addr ma.Multiaddr) bool {
|
||||||
|
if addr == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
value, err := addr.ValueForProtocol(ma.P_UNIX)
|
||||||
|
return (value != "" && err == nil)
|
||||||
|
}
|
||||||
|
|
|
@ -18,6 +18,8 @@ import (
|
||||||
secio "github.com/libp2p/go-libp2p-secio"
|
secio "github.com/libp2p/go-libp2p-secio"
|
||||||
libp2ptls "github.com/libp2p/go-libp2p-tls"
|
libp2ptls "github.com/libp2p/go-libp2p-tls"
|
||||||
madns "github.com/multiformats/go-multiaddr-dns"
|
madns "github.com/multiformats/go-multiaddr-dns"
|
||||||
|
manet "github.com/multiformats/go-multiaddr-net"
|
||||||
|
"github.com/tv42/httpunix"
|
||||||
)
|
)
|
||||||
|
|
||||||
// This is essentially a http.DefaultTransport. We should not mess
|
// This is essentially a http.DefaultTransport. We should not mess
|
||||||
|
@ -110,3 +112,19 @@ func (c *defaultClient) enableTLS() error {
|
||||||
c.net = "https"
|
c.net = "https"
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *defaultClient) enableUnix() error {
|
||||||
|
c.defaultTransport()
|
||||||
|
unixTransport := &httpunix.Transport{
|
||||||
|
DialTimeout: time.Second,
|
||||||
|
}
|
||||||
|
_, addr, err := manet.DialArgs(c.config.APIAddr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
unixTransport.RegisterLocation("restapi", addr)
|
||||||
|
c.transport.RegisterProtocol(httpunix.Scheme, unixTransport)
|
||||||
|
c.net = httpunix.Scheme
|
||||||
|
c.hostname = "restapi"
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -70,6 +70,7 @@ require (
|
||||||
github.com/pkg/errors v0.8.1
|
github.com/pkg/errors v0.8.1
|
||||||
github.com/prometheus/client_golang v1.1.0
|
github.com/prometheus/client_golang v1.1.0
|
||||||
github.com/rs/cors v1.7.0
|
github.com/rs/cors v1.7.0
|
||||||
|
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926
|
||||||
github.com/ugorji/go/codec v1.1.7
|
github.com/ugorji/go/codec v1.1.7
|
||||||
github.com/urfave/cli v1.22.1
|
github.com/urfave/cli v1.22.1
|
||||||
go.opencensus.io v0.22.1
|
go.opencensus.io v0.22.1
|
||||||
|
|
1
go.sum
1
go.sum
|
@ -825,6 +825,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
|
||||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
|
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
|
||||||
|
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926 h1:G3dpKMzFDjgEh2q1Z7zUUtKa8ViPtH+ocF0bE0g00O8=
|
||||||
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
|
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
|
||||||
github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw=
|
github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw=
|
||||||
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
|
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
|
||||||
|
|
Loading…
Reference in New Issue
Block a user