Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1a93d5d9f3
|
||
|
|
5dc944cf21
|
||
|
|
77a85a4f84
|
||
|
|
43d1ddfeb8
|
@@ -13,13 +13,14 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MaxTlsCipher uint16 = tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
|
MaxTlsCipher uint16 = tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
|
||||||
MaxCurveId tls.CurveID = tls.X25519 // 29
|
MaxCurveId tls.CurveID = tls.X25519 // 29
|
||||||
MinTlsVer uint16 = tls.VersionSSL30
|
MinTlsVer uint16 = tls.VersionSSL30
|
||||||
MaxTlsVer uint16 = tls.VersionTLS13
|
MaxTlsVer uint16 = tls.VersionTLS13
|
||||||
|
DefaultNetType string = "tcp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TlsUriParam* specifiy URL query parameters to parse a tls:// URI.
|
// TlsUriParam* specifiy URL query parameters to parse a tls:// URI, and are used by TlsUri methods.
|
||||||
const (
|
const (
|
||||||
/*
|
/*
|
||||||
TlsUriParamCa specifies a path to a CA certificate PEM-encded DER file.
|
TlsUriParamCa specifies a path to a CA certificate PEM-encded DER file.
|
||||||
@@ -110,6 +111,16 @@ const (
|
|||||||
Only the first defined instance is parsed.
|
Only the first defined instance is parsed.
|
||||||
*/
|
*/
|
||||||
TlsUriParamMaxTls string = "max_tls"
|
TlsUriParamMaxTls string = "max_tls"
|
||||||
|
/*
|
||||||
|
TlsUriParamNet is used by TlsUri.ToConn and TlsUri.ToTlsConn to explicitly specify a network.
|
||||||
|
|
||||||
|
The default is "tcp".
|
||||||
|
|
||||||
|
See net.Dial()'s "network" parameter for valid network types.
|
||||||
|
|
||||||
|
Only the first defined instance is parsed.
|
||||||
|
*/
|
||||||
|
TlsUriParamNet string = "net"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
@@ -3,11 +3,68 @@ package cryptparse
|
|||||||
import (
|
import (
|
||||||
`crypto`
|
`crypto`
|
||||||
`crypto/tls`
|
`crypto/tls`
|
||||||
|
`net`
|
||||||
`net/url`
|
`net/url`
|
||||||
`os`
|
`os`
|
||||||
`strings`
|
`strings`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
WithConn returns a (crypto/)tls.Conn from an existing/already dialed net.Conn.
|
||||||
|
|
||||||
|
underlying should be a "bare" net.Conn; behavior is undefined/unknown if the underlying conn is already a (crypto/)tls.Conn.
|
||||||
|
*/
|
||||||
|
func (t *TlsUri) WithConn(underlying net.Conn) (conn *tls.Conn, err error) {
|
||||||
|
|
||||||
|
var cfg *tls.Config
|
||||||
|
|
||||||
|
if cfg, err = t.ToTlsConfig(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
conn = tls.Client(underlying, cfg)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
ToConn returns a "bare" net.Conn (already dialed) from a TlsUri.
|
||||||
|
|
||||||
|
Note that this does NOT include the TLS configured or initialized; use TlsUri.ToTlsConn for that.
|
||||||
|
(A (crypto/)tls.Conn conforms to net.Conn.)
|
||||||
|
|
||||||
|
An error will be returned if no port is explicitly defined in the TlsUri.
|
||||||
|
*/
|
||||||
|
func (t *TlsUri) ToConn() (conn net.Conn, err error) {
|
||||||
|
|
||||||
|
var ok bool
|
||||||
|
var connHost string
|
||||||
|
var params map[string][]string
|
||||||
|
var netType string = DefaultNetType
|
||||||
|
|
||||||
|
params = t.Query()
|
||||||
|
|
||||||
|
if params != nil {
|
||||||
|
if _, ok = params[TlsUriParamNet]; ok {
|
||||||
|
netType = params[TlsUriParamNet][0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
netType = strings.ToLower(netType)
|
||||||
|
|
||||||
|
switch netType {
|
||||||
|
case "unix", "unixgram", "unixpacket":
|
||||||
|
connHost = t.Path
|
||||||
|
default:
|
||||||
|
connHost = t.Host
|
||||||
|
}
|
||||||
|
|
||||||
|
if conn, err = net.Dial(netType, connHost); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
ToTlsConfig returns a *tls.Config from a TlsUri.
|
ToTlsConfig returns a *tls.Config from a TlsUri.
|
||||||
|
|
||||||
@@ -22,6 +79,46 @@ func (t *TlsUri) ToTlsConfig() (cfg *tls.Config, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
ToTlsConn returns a (crypto/)tls.Conn (already dialed) from a TlsUri.
|
||||||
|
|
||||||
|
An error will be returned if no port is explicitly defined in the TlsUri.
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
|
||||||
|
if cfg, err = t.ToTlsConfig(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
params = t.Query()
|
||||||
|
|
||||||
|
if params != nil {
|
||||||
|
if _, ok = params[TlsUriParamNet]; ok {
|
||||||
|
netType = params[TlsUriParamNet][0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
netType = strings.ToLower(netType)
|
||||||
|
|
||||||
|
switch netType {
|
||||||
|
case "unix", "unixgram", "unixpacket":
|
||||||
|
connHost = t.Path
|
||||||
|
default:
|
||||||
|
connHost = t.Host
|
||||||
|
}
|
||||||
|
|
||||||
|
if conn, err = tls.Dial(netType, connHost, cfg); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// ToTlsFlat returns a *TlsFlat from a TlsUri.
|
// ToTlsFlat returns a *TlsFlat from a TlsUri.
|
||||||
func (t *TlsUri) ToTlsFlat() (tlsFlat *TlsFlat, err error) {
|
func (t *TlsUri) ToTlsFlat() (tlsFlat *TlsFlat, err error) {
|
||||||
|
|
||||||
@@ -146,7 +243,7 @@ func (t *TlsUri) ToTlsFlat() (tlsFlat *TlsFlat, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToURL returns the *url.URL representation of a TlsUri.
|
// ToURL returns the *url.URL representation of a TlsUri. Note that the params will remain, so remove them explicitly if needed.
|
||||||
func (t *TlsUri) ToURL() (u *url.URL) {
|
func (t *TlsUri) ToURL() (u *url.URL) {
|
||||||
|
|
||||||
if t == nil {
|
if t == nil {
|
||||||
|
|||||||
@@ -17,6 +17,34 @@ import (
|
|||||||
`r00t2.io/sysutils/paths`
|
`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.
|
// GetEnvMap returns a map of all environment variables. All values are strings.
|
||||||
func GetEnvMap() (envVars map[string]string) {
|
func GetEnvMap() (envVars map[string]string) {
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
package envs
|
package envs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
"golang.org/x/sys/windows/registry"
|
"golang.org/x/sys/windows/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user