Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client/core: assume TLS #2513

Merged
merged 1 commit into from Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 15 additions & 17 deletions client/comms/wsconn.go
Expand Up @@ -165,28 +165,26 @@ func NewWsConn(cfg *WsCfg) (WsConn, error) {
return nil, fmt.Errorf("ping wait cannot be negative")
}

var tlsConfig *tls.Config
if len(cfg.Cert) > 0 {

uri, err := url.Parse(cfg.URL)
if err != nil {
return nil, fmt.Errorf("error parsing URL: %w", err)
}
uri, err := url.Parse(cfg.URL)
if err != nil {
return nil, fmt.Errorf("error parsing URL: %w", err)
}

rootCAs, _ := x509.SystemCertPool()
if rootCAs == nil {
rootCAs = x509.NewCertPool()
}
rootCAs, _ := x509.SystemCertPool()
if rootCAs == nil {
rootCAs = x509.NewCertPool()
}

if len(cfg.Cert) > 0 {
if ok := rootCAs.AppendCertsFromPEM(cfg.Cert); !ok {
return nil, ErrInvalidCert
}
}

tlsConfig = &tls.Config{
RootCAs: rootCAs,
MinVersion: tls.VersionTLS12,
ServerName: uri.Hostname(),
}
tlsConfig := &tls.Config{
RootCAs: rootCAs,
MinVersion: tls.VersionTLS12,
ServerName: uri.Hostname(),
}

return &wsConn{
Expand Down Expand Up @@ -229,7 +227,7 @@ func (conn *wsConn) connect(ctx context.Context) error {
if err != nil {
if isErrorInvalidCert(err) {
conn.setConnectionStatus(InvalidCert)
if conn.tlsCfg == nil {
if len(conn.cfg.Cert) == 0 {
return dex.NewError(ErrCertRequired, err.Error())
}
return dex.NewError(ErrInvalidCert, err.Error())
Expand Down
15 changes: 4 additions & 11 deletions client/core/core.go
Expand Up @@ -8144,15 +8144,9 @@ func (c *Core) newDEXConnection(acctInfo *db.AccountInfo, flag connectDEXFlag) (
if err != nil {
return nil, newError(addressParseErr, "error parsing address: %v", err)
}
// The scheme switches gorilla/websocket to use the tls.Config or not.
scheme := "wss"
if len(acctInfo.Cert) == 0 {
scheme = "ws" // only supported for .onion hosts, but could allow private IP too
}
wsAddr := scheme + "://" + host + "/ws"
wsURL, err := url.Parse(wsAddr)
wsURL, err := url.Parse("wss://" + host + "/ws")
if err != nil {
return nil, newError(addressParseErr, "error parsing ws address %s: %w", wsAddr, err)
return nil, newError(addressParseErr, "error parsing ws address from host %s: %w", host, err)
}

listen := flag&connectDEXFlagTemporary == 0
Expand Down Expand Up @@ -8199,9 +8193,8 @@ func (c *Core) newDEXConnection(acctInfo *db.AccountInfo, flag connectDEXFlag) (
TorIsolation: c.cfg.TorIsolation, // need socks.NewPool with isolation???
}
wsCfg.NetDialContext = proxy.DialContext
}
if scheme == "ws" && !isOnionHost {
return nil, errors.New("a TLS connection is required when not using a hidden service")
wsURL.Scheme = "ws"
wsCfg.URL = wsURL.String()
}

wsCfg.ConnectEventFunc = func(status comms.ConnectionStatus) {
Expand Down