2 Commits

Author SHA1 Message Date
brent saner
1a93d5d9f3 v1.6.0
ADDED:
- env/DefEnv(), env/DefEnvBlank()
2024-09-06 12:50:23 -04:00
brent saner
5dc944cf21 v1.5.1
FIXES:
* cryptparse.TlsUri.ToConn and cryptparse.TlsUri.ToTlsConn would
  previously use incorrect "host" parameter during dial for UDS/IPC
  sockets.
2024-08-12 15:59:38 -04:00
2 changed files with 48 additions and 2 deletions

View File

@@ -38,6 +38,7 @@ func (t *TlsUri) WithConn(underlying net.Conn) (conn *tls.Conn, err error) {
func (t *TlsUri) ToConn() (conn net.Conn, err error) {
var ok bool
var connHost string
var params map[string][]string
var netType string = DefaultNetType
@@ -48,8 +49,16 @@ func (t *TlsUri) ToConn() (conn net.Conn, err error) {
netType = params[TlsUriParamNet][0]
}
}
netType = strings.ToLower(netType)
if conn, err = net.Dial(netType, t.Host); err != nil {
switch netType {
case "unix", "unixgram", "unixpacket":
connHost = t.Path
default:
connHost = t.Host
}
if conn, err = net.Dial(netType, connHost); err != nil {
return
}
@@ -79,6 +88,7 @@ func (t *TlsUri) ToTlsConn() (conn *tls.Conn, err error) {
var ok bool
var cfg *tls.Config
var connHost string
var params map[string][]string
var netType string = DefaultNetType
@@ -93,8 +103,16 @@ func (t *TlsUri) ToTlsConn() (conn *tls.Conn, err error) {
netType = params[TlsUriParamNet][0]
}
}
netType = strings.ToLower(netType)
if conn, err = tls.Dial(netType, t.Host, cfg); err != nil {
switch netType {
case "unix", "unixgram", "unixpacket":
connHost = t.Path
default:
connHost = t.Host
}
if conn, err = tls.Dial(netType, connHost, cfg); err != nil {
return
}

View File

@@ -17,6 +17,34 @@ import (
`r00t2.io/sysutils/paths`
)
/*
DefEnv operates like Python's .get() method on dicts (maps);
if the environment variable specified by key does not exist/is not specified,
then the value specified by fallback will be returned instead
otherwise key's value is returned.
*/
func DefEnv(key, fallback string) (value string) {
var exists bool
if value, exists = os.LookupEnv(key); !exists {
value = fallback
}
return
}
// DefEnvBlank is like DefEnv but will ADDITIONALLY/ALSO apply fallback if key is *defined/exists but is an empty string*.
func DefEnvBlank(key, fallback string) (value string) {
value = DefEnv(key, fallback)
if value == "" {
value = fallback
}
return
}
// GetEnvMap returns a map of all environment variables. All values are strings.
func GetEnvMap() (envVars map[string]string) {