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

ability to disable certificate check #10

Merged
merged 2 commits into from
Jan 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions recws.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package recws

import (
"crypto/tls"
"errors"
"log"
"math/rand"
Expand Down Expand Up @@ -36,6 +37,8 @@ type RecConn struct {
// Proxy specifies the proxy function for the dialer
// defaults to ProxyFromEnvironment
Proxy func(*http.Request) (*url.URL, error)
// Client TLS config to use on reconnect
TLSClientConfig *tls.Config
// SubscribeHandler fires after the connection successfully establish.
SubscribeHandler func() error
// KeepAliveTimeout is an interval for sending ping/pong messages
Expand Down Expand Up @@ -243,13 +246,14 @@ func (rc *RecConn) setDefaultProxy() {
}
}

func (rc *RecConn) setDefaultDialer(handshakeTimeout time.Duration) {
func (rc *RecConn) setDefaultDialer(tlsClientConfig *tls.Config, handshakeTimeout time.Duration) {
rc.mu.Lock()
defer rc.mu.Unlock()

rc.dialer = &websocket.Dialer{
HandshakeTimeout: handshakeTimeout,
Proxy: rc.Proxy,
TLSClientConfig: tlsClientConfig,
}
}

Expand All @@ -260,6 +264,20 @@ func (rc *RecConn) getHandshakeTimeout() time.Duration {
return rc.HandshakeTimeout
}

func (rc *RecConn) getTLSClientConfig() *tls.Config {
rc.mu.RLock()
defer rc.mu.RUnlock()

return rc.TLSClientConfig
}

func (rc *RecConn) SetTLSClientConfig(tlsClientConfig *tls.Config) {
rc.mu.Lock()
defer rc.mu.Unlock()

rc.TLSClientConfig = tlsClientConfig
}

// Dial creates a new client connection.
// The URL url specifies the host and request URI. Use requestHeader to specify
// the origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies
Expand All @@ -280,7 +298,7 @@ func (rc *RecConn) Dial(urlStr string, reqHeader http.Header) {
rc.setDefaultRecIntvlFactor()
rc.setDefaultHandshakeTimeout()
rc.setDefaultProxy()
rc.setDefaultDialer(rc.getHandshakeTimeout())
rc.setDefaultDialer(rc.getTLSClientConfig(), rc.getHandshakeTimeout())

// Connect
go rc.connect()
Expand Down