@ -21,6 +21,7 @@ const (
protoWSS = "wss"
protoWSS = "wss"
protoWS = "ws"
protoWS = "ws"
protoTCP = "tcp"
protoTCP = "tcp"
protoUNIX = "unix"
)
)
//-------------------------------------------------------------
//-------------------------------------------------------------
@ -28,6 +29,8 @@ const (
// Parsed URL structure
// Parsed URL structure
type parsedURL struct {
type parsedURL struct {
url . URL
url . URL
isUnixSocket bool
}
}
// Parse URL and set defaults
// Parse URL and set defaults
@ -42,7 +45,16 @@ func newParsedURL(remoteAddr string) (*parsedURL, error) {
u . Scheme = protoTCP
u . Scheme = protoTCP
}
}
return & parsedURL { * u } , nil
pu := & parsedURL {
URL : * u ,
isUnixSocket : false ,
}
if u . Scheme == protoUNIX {
pu . isUnixSocket = true
}
return pu , nil
}
}
// Change protocol to HTTP for unknown protocols and TCP protocol - useful for RPC connections
// Change protocol to HTTP for unknown protocols and TCP protocol - useful for RPC connections
@ -65,10 +77,26 @@ func (u parsedURL) GetHostWithPath() string {
// Get a trimmed address - useful for WS connections
// Get a trimmed address - useful for WS connections
func ( u parsedURL ) GetTrimmedHostWithPath ( ) string {
func ( u parsedURL ) GetTrimmedHostWithPath ( ) string {
// replace / with . for http requests (kvstore domain)
// if it's not an unix socket we return the normal URL
if ! u . isUnixSocket {
return u . GetHostWithPath ( )
}
// if it's a unix socket we replace the host slashes with a period
// this is because otherwise the http.Client would think that the
// domain is invalid.
return strings . ReplaceAll ( u . GetHostWithPath ( ) , "/" , "." )
return strings . ReplaceAll ( u . GetHostWithPath ( ) , "/" , "." )
}
}
// GetDialAddress returns the endpoint to dial for the parsed URL
func ( u parsedURL ) GetDialAddress ( ) string {
// if it's not a unix socket we return the host, example: localhost:443
if ! u . isUnixSocket {
return u . Host
}
// otherwise we return the path of the unix socket, ex /tmp/socket
return u . GetHostWithPath ( )
}
// Get a trimmed address with protocol - useful as address in RPC connections
// Get a trimmed address with protocol - useful as address in RPC connections
func ( u parsedURL ) GetTrimmedURL ( ) string {
func ( u parsedURL ) GetTrimmedURL ( ) string {
return u . Scheme + "://" + u . GetTrimmedHostWithPath ( )
return u . Scheme + "://" + u . GetTrimmedHostWithPath ( )
@ -350,7 +378,7 @@ func makeHTTPDialer(remoteAddr string) (func(string, string) (net.Conn, error),
}
}
dialFn := func ( proto , addr string ) ( net . Conn , error ) {
dialFn := func ( proto , addr string ) ( net . Conn , error ) {
return net . Dial ( protocol , u . GetHostWithPath ( ) )
return net . Dial ( protocol , u . GetDialAddress ( ) )
}
}
return dialFn , nil
return dialFn , nil