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

Add TLS prefix into the server URL(s) when connection is not Websocket and it is Secured #1492

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion go.mod
Expand Up @@ -9,4 +9,7 @@ require (
golang.org/x/text v0.13.0
)

require golang.org/x/crypto v0.14.0 // indirect
require (
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/sys v0.13.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -6,5 +6,7 @@ github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
36 changes: 28 additions & 8 deletions nats.go
Expand Up @@ -264,7 +264,6 @@ type InProcessConnProvider interface {

// Options can be used to create a customized connection.
type Options struct {

// Url represents a single NATS server url to which the client
// will be connecting. If the Servers option is also set, it
// then becomes the first server in the Servers array.
Expand Down Expand Up @@ -1505,6 +1504,27 @@ func (o Options) Connect() (*Conn, error) {
nc.Opts.Secure = true
}

// Check whether secure mode is enabled or not. In case of having
// secure mode enabled we need to use tls:// as the protocol schema.
// valid prefixes for url are:
// - nats://
// - tls://
// - wss://
// - ws://
if nc.Opts.Secure {
for i := range nc.Opts.Servers {
switch {
case strings.HasPrefix(nc.Opts.Servers[i], "nats://"):
nc.Opts.Servers[i] = strings.Replace(nc.Opts.Servers[i], "nats://", "tls://", 1)
case strings.HasPrefix(nc.Opts.Servers[i], "tls://"):
case strings.HasPrefix(nc.Opts.Servers[i], "ws://"):
case strings.HasPrefix(nc.Opts.Servers[i], "wss://"):
default:
nc.Opts.Servers[i] = "tls://" + nc.Opts.Servers[i]
}
}
}

if err := nc.setupServerPool(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -2251,7 +2271,6 @@ func (nc *Conn) setup() {

// Process a connected connection and initialize properly.
func (nc *Conn) processConnectInit() error {

// Set our deadline for the whole connect process
nc.conn.SetDeadline(time.Now().Add(nc.Opts.Timeout))
defer nc.conn.SetDeadline(time.Time{})
Expand Down Expand Up @@ -2400,7 +2419,6 @@ func (nc *Conn) checkForSecure() error {
// processExpectedInfo will look for the expected first INFO message
// sent when a connection is established. The lock should be held entering.
func (nc *Conn) processExpectedInfo() error {

c := &control{}

// Read the protocol
Expand Down Expand Up @@ -2498,8 +2516,10 @@ func (nc *Conn) connectProto() (string, error) {

// If our server does not support headers then we can't do them or no responders.
hdrs := nc.info.Headers
cinfo := connectInfo{o.Verbose, o.Pedantic, ujwt, nkey, sig, user, pass, token,
o.Secure, o.Name, LangString, Version, clientProtoInfo, !o.NoEcho, hdrs, hdrs}
cinfo := connectInfo{
o.Verbose, o.Pedantic, ujwt, nkey, sig, user, pass, token,
o.Secure, o.Name, LangString, Version, clientProtoInfo, !o.NoEcho, hdrs, hdrs,
}

b, err := json.Marshal(cinfo)
if err != nil {
Expand Down Expand Up @@ -3109,7 +3129,7 @@ func (nc *Conn) processMsg(data []byte) {
// It's possible that we end-up not using the message, but that's ok.

// FIXME(dlc): Need to copy, should/can do COW?
var msgPayload = data
msgPayload := data
if !nc.ps.msgCopied {
msgPayload = make([]byte, len(data))
copy(msgPayload, data)
Expand Down Expand Up @@ -3793,7 +3813,7 @@ func (nc *Conn) publish(subj, reply string, hdr, data []byte) error {
// go 1.14 some values strconv faster, may be able to switch over.

var b [12]byte
var i = len(b)
i := len(b)

if hdr != nil {
if len(hdr) > 0 {
Expand Down Expand Up @@ -5304,7 +5324,7 @@ func (nc *Conn) IsDraining() bool {
// caller must lock
func (nc *Conn) getServers(implicitOnly bool) []string {
poolSize := len(nc.srvPool)
var servers = make([]string, 0)
servers := make([]string, 0)
for i := 0; i < poolSize; i++ {
if implicitOnly && !nc.srvPool[i].isImplicit {
continue
Expand Down