Skip to content

Commit

Permalink
xmpp: proxy ConnectionState through wrapped conns
Browse files Browse the repository at this point in the history
When we wrap a connection during stream negotiation (eg. to add
compression, or some other transformation), proxy through the
ConnectionState method. This way the TLS connection state isn't hidden
from the session which will need it to pass to SASL mechanisms that use
it.

See #45

Signed-off-by: Sam Whited <sam@samwhited.com>
  • Loading branch information
SamWhited committed May 7, 2020
1 parent 4161b93 commit ac109a4
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions conn.go
Expand Up @@ -5,15 +5,23 @@
package xmpp

import (
"crypto/tls"
"io"
"net"
"time"
)

type tlsConn interface {
ConnectionState() tls.ConnectionState
}

var _ tlsConn = (*conn)(nil)

// conn is a net.Conn created for the purpose of establishing an XMPP session.
type conn struct {
c net.Conn
rw io.ReadWriter
c net.Conn
rw io.ReadWriter
connState func() tls.ConnectionState
}

// newConn wraps an io.ReadWriter in a Conn.
Expand All @@ -25,10 +33,31 @@ func newConn(rw io.ReadWriter, prev net.Conn) net.Conn {
return c
}

nc := &conn{rw: rw, c: prev}
// Pull out a connection state function if possible.
tc, ok := rw.(tlsConn)
if !ok {
tc, _ = prev.(tlsConn)
}
var cs func() tls.ConnectionState
if tc != nil {
cs = tc.ConnectionState
}

nc := &conn{
rw: rw,
c: prev,
connState: cs,
}
return nc
}

func (c *conn) ConnectionState() tls.ConnectionState {
if c.connState == nil {
return tls.ConnectionState{}
}
return c.connState()
}

// Close closes the connection.
func (c *conn) Close() error {
if c.c != nil {
Expand Down

0 comments on commit ac109a4

Please sign in to comment.