Skip to content

Commit

Permalink
Overwrite TransportCredentials ServerName.
Browse files Browse the repository at this point in the history
This ensures the transport credentials are properly configured to use the host to which the connection was made for verification purposes.
  • Loading branch information
johanbrandhorst committed Feb 5, 2018
1 parent 6099f35 commit fe0e4a9
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions wsproxy/wsproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wsproxy

import (
"context"
"crypto/tls"
"encoding/binary"
"errors"
"io"
Expand Down Expand Up @@ -45,6 +46,7 @@ func WrapServer(h http.Handler, opts ...Option) http.Handler {
p := &proxy{
h: h,
logger: noopLogger{},
creds: credentials.NewTLS(&tls.Config{InsecureSkipVerify: true}),
}

for _, opt := range opts {
Expand Down Expand Up @@ -104,6 +106,14 @@ func (p *proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

// Override TLS config ServerName in case
// it hasn't been set explicitly already
err = p.creds.OverrideServerName(stripPort(r.Host))
if err != nil {
p.logger.Warnln("Failed to set TLS Server Name:", err)
return
}

defer func() {
err = conn.Close()
if err != nil {
Expand Down Expand Up @@ -296,3 +306,15 @@ func withPort(host string) string {
}
return host
}

// stripPort removes a port, if any, from the input
func stripPort(hostport string) string {
colon := strings.IndexByte(hostport, ':')
if colon == -1 {
return hostport
}
if i := strings.IndexByte(hostport, ']'); i != -1 {
return strings.TrimPrefix(hostport[:i], "[")
}
return hostport[:colon]
}

0 comments on commit fe0e4a9

Please sign in to comment.