Merge pull request #111 from seikichi/support_proxy_auth

Support proxy authorization for websocket client
This commit is contained in:
Gary Burd 2016-02-17 09:43:51 -08:00
commit 4935ba31a2
2 changed files with 51 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"crypto/tls" "crypto/tls"
"encoding/base64"
"errors" "errors"
"io" "io"
"io/ioutil" "io/ioutil"
@ -265,11 +266,19 @@ func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Re
} }
if proxyURL != nil { if proxyURL != nil {
connectHeader := make(http.Header)
if user := proxyURL.User; user != nil {
proxyUser := user.Username()
if proxyPassword, passwordSet := user.Password(); passwordSet {
credential := base64.StdEncoding.EncodeToString([]byte(proxyUser + ":" + proxyPassword))
connectHeader.Set("Proxy-Authorization", "Basic "+credential)
}
}
connectReq := &http.Request{ connectReq := &http.Request{
Method: "CONNECT", Method: "CONNECT",
URL: &url.URL{Opaque: hostPort}, URL: &url.URL{Opaque: hostPort},
Host: hostPort, Host: hostPort,
Header: make(http.Header), Header: connectHeader,
} }
connectReq.Write(netConn) connectReq.Write(netConn)

View File

@ -7,6 +7,7 @@ package websocket
import ( import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"encoding/base64"
"io" "io"
"io/ioutil" "io/ioutil"
"net" "net"
@ -175,6 +176,46 @@ func TestProxyDial(t *testing.T) {
cstDialer.Proxy = http.ProxyFromEnvironment cstDialer.Proxy = http.ProxyFromEnvironment
} }
func TestProxyAuthorizationDial(t *testing.T) {
s := newServer(t)
defer s.Close()
surl, _ := url.Parse(s.URL)
surl.User = url.UserPassword("username", "password")
cstDialer.Proxy = http.ProxyURL(surl)
connect := false
origHandler := s.Server.Config.Handler
// Capture the request Host header.
s.Server.Config.Handler = http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
proxyAuth := r.Header.Get("Proxy-Authorization")
expectedProxyAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte("username:password"))
if r.Method == "CONNECT" && proxyAuth == expectedProxyAuth {
connect = true
w.WriteHeader(200)
return
}
if !connect {
t.Log("connect with proxy authorization not recieved")
http.Error(w, "connect with proxy authorization not recieved", 405)
return
}
origHandler.ServeHTTP(w, r)
})
ws, _, err := cstDialer.Dial(s.URL, nil)
if err != nil {
t.Fatalf("Dial: %v", err)
}
defer ws.Close()
sendRecv(t, ws)
cstDialer.Proxy = http.ProxyFromEnvironment
}
func TestDial(t *testing.T) { func TestDial(t *testing.T) {
s := newServer(t) s := newServer(t)
defer s.Close() defer s.Close()