2014-04-19 01:25:11 +04:00
|
|
|
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
2013-10-16 20:41:47 +04:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-12-14 09:30:22 +04:00
|
|
|
package websocket
|
2013-10-16 20:41:47 +04:00
|
|
|
|
|
|
|
import (
|
2017-12-01 04:43:01 +03:00
|
|
|
"bytes"
|
2013-12-14 09:30:22 +04:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2016-02-17 14:18:07 +03:00
|
|
|
"encoding/base64"
|
2017-12-01 04:43:01 +03:00
|
|
|
"encoding/binary"
|
2013-10-16 20:41:47 +04:00
|
|
|
"io"
|
2015-05-09 00:59:31 +03:00
|
|
|
"io/ioutil"
|
2017-12-01 04:43:01 +03:00
|
|
|
"net"
|
2013-10-16 20:41:47 +04:00
|
|
|
"net/http"
|
2016-10-17 21:19:52 +03:00
|
|
|
"net/http/cookiejar"
|
2013-10-16 20:41:47 +04:00
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2013-12-14 19:48:43 +04:00
|
|
|
"reflect"
|
2015-05-09 00:59:31 +03:00
|
|
|
"strings"
|
2013-10-16 20:41:47 +04:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-07-05 19:39:32 +04:00
|
|
|
var cstUpgrader = Upgrader{
|
2016-10-18 03:30:22 +03:00
|
|
|
Subprotocols: []string{"p0", "p1"},
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
|
|
|
EnableCompression: true,
|
2014-07-05 19:39:32 +04:00
|
|
|
Error: func(w http.ResponseWriter, r *http.Request, status int, reason error) {
|
|
|
|
http.Error(w, reason.Error(), status)
|
|
|
|
},
|
2014-04-20 23:08:24 +04:00
|
|
|
}
|
|
|
|
|
2014-07-05 19:39:32 +04:00
|
|
|
var cstDialer = Dialer{
|
2017-12-01 04:43:01 +03:00
|
|
|
Subprotocols: []string{"p1", "p2"},
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
|
|
|
HandshakeTimeout: 30 * time.Second,
|
2014-04-20 23:08:24 +04:00
|
|
|
}
|
|
|
|
|
2014-07-05 19:39:32 +04:00
|
|
|
type cstHandler struct{ *testing.T }
|
|
|
|
|
2018-02-02 07:48:44 +03:00
|
|
|
type cstContextTakeoverHandler struct{ *testing.T }
|
|
|
|
|
2015-05-09 00:59:31 +03:00
|
|
|
type cstServer struct {
|
2014-07-05 19:39:32 +04:00
|
|
|
*httptest.Server
|
|
|
|
URL string
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
2015-12-16 07:25:50 +03:00
|
|
|
const (
|
|
|
|
cstPath = "/a/b"
|
|
|
|
cstRawQuery = "x=y"
|
|
|
|
cstRequestURI = cstPath + "?" + cstRawQuery
|
|
|
|
)
|
|
|
|
|
2015-05-09 00:59:31 +03:00
|
|
|
func newServer(t *testing.T) *cstServer {
|
|
|
|
var s cstServer
|
2014-07-05 19:39:32 +04:00
|
|
|
s.Server = httptest.NewServer(cstHandler{t})
|
2015-12-16 07:25:50 +03:00
|
|
|
s.Server.URL += cstRequestURI
|
2015-05-09 00:59:31 +03:00
|
|
|
s.URL = makeWsProto(s.Server.URL)
|
2014-07-05 19:39:32 +04:00
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
2018-02-02 07:48:44 +03:00
|
|
|
func newContextTakeoverServer(t *testing.T) *cstServer {
|
|
|
|
var s cstServer
|
|
|
|
s.Server = httptest.NewServer(cstContextTakeoverHandler{t})
|
|
|
|
s.Server.URL += cstRequestURI
|
|
|
|
s.URL = makeWsProto(s.Server.URL)
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
2015-05-09 00:59:31 +03:00
|
|
|
func newTLSServer(t *testing.T) *cstServer {
|
|
|
|
var s cstServer
|
2014-07-05 19:39:32 +04:00
|
|
|
s.Server = httptest.NewTLSServer(cstHandler{t})
|
2015-12-16 07:25:50 +03:00
|
|
|
s.Server.URL += cstRequestURI
|
2015-05-09 00:59:31 +03:00
|
|
|
s.URL = makeWsProto(s.Server.URL)
|
2014-07-05 19:39:32 +04:00
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t cstHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2015-12-16 07:25:50 +03:00
|
|
|
if r.URL.Path != cstPath {
|
|
|
|
t.Logf("path=%v, want %v", r.URL.Path, cstPath)
|
2018-02-26 06:30:15 +03:00
|
|
|
http.Error(w, "bad path", http.StatusBadRequest)
|
2015-12-16 07:25:50 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.URL.RawQuery != cstRawQuery {
|
|
|
|
t.Logf("query=%v, want %v", r.URL.RawQuery, cstRawQuery)
|
2018-02-26 06:30:15 +03:00
|
|
|
http.Error(w, "bad path", http.StatusBadRequest)
|
2015-12-16 07:25:50 +03:00
|
|
|
return
|
|
|
|
}
|
2013-12-14 19:48:43 +04:00
|
|
|
subprotos := Subprotocols(r)
|
2014-07-05 19:39:32 +04:00
|
|
|
if !reflect.DeepEqual(subprotos, cstDialer.Subprotocols) {
|
|
|
|
t.Logf("subprotols=%v, want %v", subprotos, cstDialer.Subprotocols)
|
2018-02-26 06:30:15 +03:00
|
|
|
http.Error(w, "bad protocol", http.StatusBadRequest)
|
2013-12-14 19:48:43 +04:00
|
|
|
return
|
|
|
|
}
|
2014-07-05 19:39:32 +04:00
|
|
|
ws, err := cstUpgrader.Upgrade(w, r, http.Header{"Set-Cookie": {"sessionID=1234"}})
|
2014-04-20 23:08:24 +04:00
|
|
|
if err != nil {
|
2014-07-05 19:39:32 +04:00
|
|
|
t.Logf("Upgrade: %v", err)
|
2013-10-16 20:41:47 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer ws.Close()
|
2013-12-14 19:48:43 +04:00
|
|
|
|
2014-04-20 23:08:24 +04:00
|
|
|
if ws.Subprotocol() != "p1" {
|
2014-07-05 19:39:32 +04:00
|
|
|
t.Logf("Subprotocol() = %s, want p1", ws.Subprotocol())
|
|
|
|
ws.Close()
|
2013-12-14 19:48:43 +04:00
|
|
|
return
|
|
|
|
}
|
2014-07-05 19:39:32 +04:00
|
|
|
op, rd, err := ws.NextReader()
|
2013-10-16 20:41:47 +04:00
|
|
|
if err != nil {
|
2014-07-05 19:39:32 +04:00
|
|
|
t.Logf("NextReader: %v", err)
|
|
|
|
return
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
2014-07-05 19:39:32 +04:00
|
|
|
wr, err := ws.NextWriter(op)
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("NextWriter: %v", err)
|
|
|
|
return
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
2014-07-05 19:39:32 +04:00
|
|
|
if _, err = io.Copy(wr, rd); err != nil {
|
|
|
|
t.Logf("NextWriter: %v", err)
|
|
|
|
return
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
2014-07-05 19:39:32 +04:00
|
|
|
if err := wr.Close(); err != nil {
|
|
|
|
t.Logf("Close: %v", err)
|
|
|
|
return
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
}
|
2013-12-14 09:30:22 +04:00
|
|
|
|
2018-02-02 07:48:44 +03:00
|
|
|
func (t cstContextTakeoverHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path != cstPath {
|
|
|
|
t.Logf("path=%v, want %v", r.URL.Path, cstPath)
|
|
|
|
http.Error(w, "bad path", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.URL.RawQuery != cstRawQuery {
|
|
|
|
t.Logf("query=%v, want %v", r.URL.RawQuery, cstRawQuery)
|
|
|
|
http.Error(w, "bad path", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
subprotos := Subprotocols(r)
|
|
|
|
if !reflect.DeepEqual(subprotos, cstDialer.Subprotocols) {
|
|
|
|
t.Logf("subprotols=%v, want %v", subprotos, cstDialer.Subprotocols)
|
|
|
|
http.Error(w, "bad protocol", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cu := cstUpgrader
|
|
|
|
cu.CompressionLevel = defaultCompressionLevel
|
|
|
|
cu.EnableContextTakeover = true
|
|
|
|
ws, err := cu.Upgrade(w, r, http.Header{"Set-Cookie": {"sessionID=1234"}})
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("Upgrade: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
if ws.Subprotocol() != "p1" {
|
|
|
|
t.Logf("Subprotocol() = %s, want p1", ws.Subprotocol())
|
|
|
|
ws.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// first message
|
|
|
|
op, rd, err := ws.NextReader()
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("NextReader: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wr, err := ws.NextWriter(op)
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("NextWriter: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = io.Copy(wr, rd); err != nil {
|
|
|
|
t.Logf("NextWriter: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := wr.Close(); err != nil {
|
|
|
|
t.Logf("Close: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// second message
|
|
|
|
op, rd, err = ws.NextReader()
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("NextReader: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wr, err = ws.NextWriter(op)
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("NextWriter: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = io.Copy(wr, rd); err != nil {
|
|
|
|
t.Logf("NextWriter: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := wr.Close(); err != nil {
|
|
|
|
t.Logf("Close: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-09 00:59:31 +03:00
|
|
|
func makeWsProto(s string) string {
|
|
|
|
return "ws" + strings.TrimPrefix(s, "http")
|
|
|
|
}
|
|
|
|
|
2014-07-05 19:39:32 +04:00
|
|
|
func sendRecv(t *testing.T, ws *Conn) {
|
|
|
|
const message = "Hello World!"
|
|
|
|
if err := ws.SetWriteDeadline(time.Now().Add(time.Second)); err != nil {
|
|
|
|
t.Fatalf("SetWriteDeadline: %v", err)
|
|
|
|
}
|
|
|
|
if err := ws.WriteMessage(TextMessage, []byte(message)); err != nil {
|
|
|
|
t.Fatalf("WriteMessage: %v", err)
|
|
|
|
}
|
|
|
|
if err := ws.SetReadDeadline(time.Now().Add(time.Second)); err != nil {
|
|
|
|
t.Fatalf("SetReadDeadline: %v", err)
|
|
|
|
}
|
|
|
|
_, p, err := ws.ReadMessage()
|
2014-04-20 23:08:24 +04:00
|
|
|
if err != nil {
|
2014-07-05 19:39:32 +04:00
|
|
|
t.Fatalf("ReadMessage: %v", err)
|
2013-12-14 09:30:22 +04:00
|
|
|
}
|
2014-07-05 19:39:32 +04:00
|
|
|
if string(p) != message {
|
|
|
|
t.Fatalf("message=%s, want %s", p, message)
|
2013-12-14 09:30:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 11:52:23 +03:00
|
|
|
func multipleSendRecv(t *testing.T, ws *Conn) {
|
|
|
|
message := "Hello World!"
|
|
|
|
if err := ws.SetWriteDeadline(time.Now().Add(time.Second)); err != nil {
|
|
|
|
t.Fatalf("SetWriteDeadline: %v", err)
|
|
|
|
}
|
|
|
|
if err := ws.WriteMessage(TextMessage, []byte(message)); err != nil {
|
|
|
|
t.Fatalf("WriteMessage: %v", err)
|
|
|
|
}
|
|
|
|
if err := ws.SetReadDeadline(time.Now().Add(time.Second)); err != nil {
|
|
|
|
t.Fatalf("SetReadDeadline: %v", err)
|
|
|
|
}
|
|
|
|
_, p, err := ws.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ReadMessage: %v", err)
|
|
|
|
}
|
|
|
|
if string(p) != message {
|
|
|
|
t.Fatalf("message=%s, want %s", p, message)
|
|
|
|
}
|
|
|
|
|
2018-02-02 07:48:44 +03:00
|
|
|
nextMessage := "Can you read message?"
|
2018-01-26 11:52:23 +03:00
|
|
|
if err := ws.SetWriteDeadline(time.Now().Add(time.Second)); err != nil {
|
|
|
|
t.Fatalf("SetWriteDeadline: %v", err)
|
|
|
|
}
|
2018-02-02 07:48:44 +03:00
|
|
|
if err := ws.WriteMessage(TextMessage, []byte(nextMessage)); err != nil {
|
2018-01-26 11:52:23 +03:00
|
|
|
t.Fatalf("_WriteMessage: %v", err)
|
|
|
|
}
|
|
|
|
if err := ws.SetReadDeadline(time.Now().Add(time.Second)); err != nil {
|
|
|
|
t.Fatalf("_SetReadDeadline: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, p, err = ws.ReadMessage()
|
|
|
|
if err != nil {
|
2018-02-02 07:48:44 +03:00
|
|
|
t.Fatalf("_ReadMessage: %v", err)
|
2018-01-26 11:52:23 +03:00
|
|
|
}
|
2018-02-02 07:48:44 +03:00
|
|
|
if string(p) != nextMessage {
|
|
|
|
t.Fatalf("_message=%s, want %s", p, nextMessage)
|
2018-01-26 11:52:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 12:29:17 +03:00
|
|
|
func TestProxyDial(t *testing.T) {
|
|
|
|
|
|
|
|
s := newServer(t)
|
|
|
|
defer s.Close()
|
|
|
|
|
2017-12-01 04:43:01 +03:00
|
|
|
surl, _ := url.Parse(s.Server.URL)
|
2015-10-20 12:29:17 +03:00
|
|
|
|
2017-12-01 04:43:01 +03:00
|
|
|
cstDialer := cstDialer // make local copy for modification on next line.
|
2015-10-20 12:29:17 +03:00
|
|
|
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) {
|
|
|
|
if r.Method == "CONNECT" {
|
|
|
|
connect = true
|
2018-02-26 06:30:15 +03:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2015-10-20 12:29:17 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !connect {
|
2017-11-02 17:16:55 +03:00
|
|
|
t.Log("connect not received")
|
2018-02-26 06:30:15 +03:00
|
|
|
http.Error(w, "connect not received", http.StatusMethodNotAllowed)
|
2015-10-20 12:29:17 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-02-17 14:18:07 +03:00
|
|
|
func TestProxyAuthorizationDial(t *testing.T) {
|
|
|
|
s := newServer(t)
|
|
|
|
defer s.Close()
|
|
|
|
|
2017-12-01 04:43:01 +03:00
|
|
|
surl, _ := url.Parse(s.Server.URL)
|
2016-02-17 14:18:07 +03:00
|
|
|
surl.User = url.UserPassword("username", "password")
|
2017-12-01 04:43:01 +03:00
|
|
|
|
|
|
|
cstDialer := cstDialer // make local copy for modification on next line.
|
2016-02-17 14:18:07 +03:00
|
|
|
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
|
2018-02-26 06:30:15 +03:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2016-02-17 14:18:07 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !connect {
|
2017-11-02 17:16:55 +03:00
|
|
|
t.Log("connect with proxy authorization not received")
|
2018-02-26 06:30:15 +03:00
|
|
|
http.Error(w, "connect with proxy authorization not received", http.StatusMethodNotAllowed)
|
2016-02-17 14:18:07 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2013-12-14 09:30:22 +04:00
|
|
|
func TestDial(t *testing.T) {
|
2014-07-05 19:39:32 +04:00
|
|
|
s := newServer(t)
|
2013-12-14 09:30:22 +04:00
|
|
|
defer s.Close()
|
2014-07-05 19:39:32 +04:00
|
|
|
|
|
|
|
ws, _, err := cstDialer.Dial(s.URL, nil)
|
2013-12-14 09:30:22 +04:00
|
|
|
if err != nil {
|
2014-07-05 19:39:32 +04:00
|
|
|
t.Fatalf("Dial: %v", err)
|
2013-12-14 09:30:22 +04:00
|
|
|
}
|
|
|
|
defer ws.Close()
|
|
|
|
sendRecv(t, ws)
|
|
|
|
}
|
|
|
|
|
2016-10-17 21:19:52 +03:00
|
|
|
func TestDialCookieJar(t *testing.T) {
|
|
|
|
s := newServer(t)
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
jar, _ := cookiejar.New(nil)
|
|
|
|
d := cstDialer
|
|
|
|
d.Jar = jar
|
|
|
|
|
2017-10-11 19:07:11 +03:00
|
|
|
u, _ := url.Parse(s.URL)
|
2016-10-17 21:19:52 +03:00
|
|
|
|
|
|
|
switch u.Scheme {
|
|
|
|
case "ws":
|
|
|
|
u.Scheme = "http"
|
|
|
|
case "wss":
|
|
|
|
u.Scheme = "https"
|
|
|
|
}
|
|
|
|
|
2017-11-02 17:16:55 +03:00
|
|
|
cookies := []*http.Cookie{{Name: "gorilla", Value: "ws", Path: "/"}}
|
2016-10-17 21:19:52 +03:00
|
|
|
d.Jar.SetCookies(u, cookies)
|
|
|
|
|
|
|
|
ws, _, err := d.Dial(s.URL, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Dial: %v", err)
|
|
|
|
}
|
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
var gorilla string
|
|
|
|
var sessionID string
|
|
|
|
for _, c := range d.Jar.Cookies(u) {
|
|
|
|
if c.Name == "gorilla" {
|
|
|
|
gorilla = c.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Name == "sessionID" {
|
|
|
|
sessionID = c.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if gorilla != "ws" {
|
|
|
|
t.Error("Cookie not present in jar.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if sessionID != "1234" {
|
|
|
|
t.Error("Set-Cookie not received from the server.")
|
|
|
|
}
|
|
|
|
|
|
|
|
sendRecv(t, ws)
|
|
|
|
}
|
|
|
|
|
2013-12-14 09:30:22 +04:00
|
|
|
func TestDialTLS(t *testing.T) {
|
2014-07-05 19:39:32 +04:00
|
|
|
s := newTLSServer(t)
|
2013-12-14 09:30:22 +04:00
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
certs := x509.NewCertPool()
|
|
|
|
for _, c := range s.TLS.Certificates {
|
|
|
|
roots, err := x509.ParseCertificates(c.Certificate[len(c.Certificate)-1])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error parsing server's root cert: %v", err)
|
|
|
|
}
|
|
|
|
for _, root := range roots {
|
|
|
|
certs.AddCert(root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-05 19:39:32 +04:00
|
|
|
d := cstDialer
|
|
|
|
d.TLSClientConfig = &tls.Config{RootCAs: certs}
|
2016-09-12 18:30:41 +03:00
|
|
|
ws, _, err := d.Dial(s.URL, nil)
|
2013-12-14 09:30:22 +04:00
|
|
|
if err != nil {
|
2014-07-05 19:39:32 +04:00
|
|
|
t.Fatalf("Dial: %v", err)
|
2013-12-14 09:30:22 +04:00
|
|
|
}
|
|
|
|
defer ws.Close()
|
|
|
|
sendRecv(t, ws)
|
|
|
|
}
|
|
|
|
|
2014-07-05 19:39:32 +04:00
|
|
|
func xTestDialTLSBadCert(t *testing.T) {
|
2015-05-09 00:59:31 +03:00
|
|
|
// This test is deactivated because of noisy logging from the net/http package.
|
2014-07-05 19:39:32 +04:00
|
|
|
s := newTLSServer(t)
|
2013-12-14 09:30:22 +04:00
|
|
|
defer s.Close()
|
2014-07-05 19:39:32 +04:00
|
|
|
|
|
|
|
ws, _, err := cstDialer.Dial(s.URL, nil)
|
2013-12-14 09:30:22 +04:00
|
|
|
if err == nil {
|
2014-07-05 19:39:32 +04:00
|
|
|
ws.Close()
|
|
|
|
t.Fatalf("Dial: nil")
|
2013-12-14 09:30:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 18:30:41 +03:00
|
|
|
func TestDialTLSNoVerify(t *testing.T) {
|
2014-07-05 19:39:32 +04:00
|
|
|
s := newTLSServer(t)
|
2013-12-14 09:30:22 +04:00
|
|
|
defer s.Close()
|
2014-07-05 19:39:32 +04:00
|
|
|
|
|
|
|
d := cstDialer
|
|
|
|
d.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
|
|
|
ws, _, err := d.Dial(s.URL, nil)
|
2013-12-14 09:30:22 +04:00
|
|
|
if err != nil {
|
2014-07-05 19:39:32 +04:00
|
|
|
t.Fatalf("Dial: %v", err)
|
2013-12-14 09:30:22 +04:00
|
|
|
}
|
|
|
|
defer ws.Close()
|
|
|
|
sendRecv(t, ws)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDialTimeout(t *testing.T) {
|
2014-07-05 19:39:32 +04:00
|
|
|
s := newServer(t)
|
2013-12-14 09:30:22 +04:00
|
|
|
defer s.Close()
|
2014-07-05 19:39:32 +04:00
|
|
|
|
|
|
|
d := cstDialer
|
|
|
|
d.HandshakeTimeout = -1
|
|
|
|
ws, _, err := d.Dial(s.URL, nil)
|
2013-12-14 09:30:22 +04:00
|
|
|
if err == nil {
|
2014-07-05 19:39:32 +04:00
|
|
|
ws.Close()
|
|
|
|
t.Fatalf("Dial: nil")
|
2013-12-14 09:30:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDialBadScheme(t *testing.T) {
|
2014-07-05 19:39:32 +04:00
|
|
|
s := newServer(t)
|
2013-12-14 09:30:22 +04:00
|
|
|
defer s.Close()
|
2014-07-05 19:39:32 +04:00
|
|
|
|
|
|
|
ws, _, err := cstDialer.Dial(s.Server.URL, nil)
|
2013-12-14 09:30:22 +04:00
|
|
|
if err == nil {
|
2014-07-05 19:39:32 +04:00
|
|
|
ws.Close()
|
|
|
|
t.Fatalf("Dial: nil")
|
2013-12-14 09:30:22 +04:00
|
|
|
}
|
|
|
|
}
|
2014-07-01 01:57:20 +04:00
|
|
|
|
|
|
|
func TestDialBadOrigin(t *testing.T) {
|
2014-07-05 19:39:32 +04:00
|
|
|
s := newServer(t)
|
2014-07-01 01:57:20 +04:00
|
|
|
defer s.Close()
|
2014-07-05 19:39:32 +04:00
|
|
|
|
|
|
|
ws, resp, err := cstDialer.Dial(s.URL, http.Header{"Origin": {"bad"}})
|
2014-07-01 01:57:20 +04:00
|
|
|
if err == nil {
|
2014-07-05 19:39:32 +04:00
|
|
|
ws.Close()
|
|
|
|
t.Fatalf("Dial: nil")
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatalf("resp=nil, err=%v", err)
|
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusForbidden {
|
|
|
|
t.Fatalf("status=%d, want %d", resp.StatusCode, http.StatusForbidden)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-02 19:39:02 +03:00
|
|
|
func TestDialBadHeader(t *testing.T) {
|
|
|
|
s := newServer(t)
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
for _, k := range []string{"Upgrade",
|
|
|
|
"Connection",
|
|
|
|
"Sec-Websocket-Key",
|
|
|
|
"Sec-Websocket-Version",
|
|
|
|
"Sec-Websocket-Protocol"} {
|
|
|
|
h := http.Header{}
|
|
|
|
h.Set(k, "bad")
|
|
|
|
ws, _, err := cstDialer.Dial(s.URL, http.Header{"Origin": {"bad"}})
|
|
|
|
if err == nil {
|
|
|
|
ws.Close()
|
|
|
|
t.Errorf("Dial with header %s returned nil", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-02 20:08:11 +03:00
|
|
|
func TestBadMethod(t *testing.T) {
|
|
|
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ws, err := cstUpgrader.Upgrade(w, r, nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("handshake succeeded, expect fail")
|
|
|
|
ws.Close()
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
defer s.Close()
|
|
|
|
|
2017-12-01 03:45:44 +03:00
|
|
|
req, err := http.NewRequest("POST", s.URL, strings.NewReader(""))
|
2015-11-02 20:08:11 +03:00
|
|
|
if err != nil {
|
2017-12-01 03:45:44 +03:00
|
|
|
t.Fatalf("NewRequest returned error %v", err)
|
|
|
|
}
|
|
|
|
req.Header.Set("Connection", "upgrade")
|
|
|
|
req.Header.Set("Upgrade", "websocket")
|
|
|
|
req.Header.Set("Sec-Websocket-Version", "13")
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Do returned error %v", err)
|
2015-11-02 20:08:11 +03:00
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusMethodNotAllowed {
|
|
|
|
t.Errorf("Status = %d, want %d", resp.StatusCode, http.StatusMethodNotAllowed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-05 19:39:32 +04:00
|
|
|
func TestHandshake(t *testing.T) {
|
|
|
|
s := newServer(t)
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
ws, resp, err := cstDialer.Dial(s.URL, http.Header{"Origin": {s.URL}})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Dial: %v", err)
|
|
|
|
}
|
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
var sessionID string
|
|
|
|
for _, c := range resp.Cookies() {
|
|
|
|
if c.Name == "sessionID" {
|
|
|
|
sessionID = c.Value
|
|
|
|
}
|
2014-07-01 01:57:20 +04:00
|
|
|
}
|
2014-07-05 19:39:32 +04:00
|
|
|
if sessionID != "1234" {
|
|
|
|
t.Error("Set-Cookie not received from the server.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ws.Subprotocol() != "p1" {
|
|
|
|
t.Errorf("ws.Subprotocol() = %s, want p1", ws.Subprotocol())
|
|
|
|
}
|
|
|
|
sendRecv(t, ws)
|
2014-07-01 01:57:20 +04:00
|
|
|
}
|
2015-05-09 00:59:31 +03:00
|
|
|
|
|
|
|
func TestRespOnBadHandshake(t *testing.T) {
|
|
|
|
const expectedStatus = http.StatusGone
|
|
|
|
const expectedBody = "This is the response body."
|
|
|
|
|
|
|
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(expectedStatus)
|
|
|
|
io.WriteString(w, expectedBody)
|
|
|
|
}))
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
ws, resp, err := cstDialer.Dial(makeWsProto(s.URL), nil)
|
|
|
|
if err == nil {
|
|
|
|
ws.Close()
|
|
|
|
t.Fatalf("Dial: nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatalf("resp=nil, err=%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != expectedStatus {
|
|
|
|
t.Errorf("resp.StatusCode=%d, want %d", resp.StatusCode, expectedStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ReadFull(resp.Body) returned error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(p) != expectedBody {
|
|
|
|
t.Errorf("resp.Body=%s, want %s", p, expectedBody)
|
|
|
|
}
|
|
|
|
}
|
2015-05-08 20:33:57 +03:00
|
|
|
|
2015-10-20 20:44:58 +03:00
|
|
|
// TestHostHeader confirms that the host header provided in the call to Dial is
|
|
|
|
// sent to the server.
|
2015-05-08 20:33:57 +03:00
|
|
|
func TestHostHeader(t *testing.T) {
|
|
|
|
s := newServer(t)
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
specifiedHost := make(chan string, 1)
|
|
|
|
origHandler := s.Server.Config.Handler
|
|
|
|
|
|
|
|
// Capture the request Host header.
|
|
|
|
s.Server.Config.Handler = http.HandlerFunc(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
specifiedHost <- r.Host
|
|
|
|
origHandler.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
|
2015-10-20 20:44:58 +03:00
|
|
|
ws, _, err := cstDialer.Dial(s.URL, http.Header{"Host": {"testhost"}})
|
2015-05-08 20:33:57 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Dial: %v", err)
|
|
|
|
}
|
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
if gotHost := <-specifiedHost; gotHost != "testhost" {
|
|
|
|
t.Fatalf("gotHost = %q, want \"testhost\"", gotHost)
|
|
|
|
}
|
|
|
|
|
|
|
|
sendRecv(t, ws)
|
|
|
|
}
|
2016-10-18 03:30:22 +03:00
|
|
|
|
|
|
|
func TestDialCompression(t *testing.T) {
|
|
|
|
s := newServer(t)
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
dialer := cstDialer
|
|
|
|
dialer.EnableCompression = true
|
|
|
|
ws, _, err := dialer.Dial(s.URL, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Dial: %v", err)
|
|
|
|
}
|
|
|
|
defer ws.Close()
|
|
|
|
sendRecv(t, ws)
|
|
|
|
}
|
2017-12-01 04:43:01 +03:00
|
|
|
|
2018-01-26 11:52:23 +03:00
|
|
|
func TestDialCompressionOfContextTakeover(t *testing.T) {
|
2018-02-02 07:48:44 +03:00
|
|
|
s := newContextTakeoverServer(t)
|
2018-01-26 11:52:23 +03:00
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
dialer := cstDialer
|
|
|
|
dialer.EnableCompression = true
|
|
|
|
dialer.EnableContextTakeover = true
|
2018-02-02 07:48:44 +03:00
|
|
|
dialer.CompressionLevel = 2
|
2018-01-26 11:52:23 +03:00
|
|
|
ws, _, err := dialer.Dial(s.URL, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Dial: %v", err)
|
|
|
|
}
|
|
|
|
defer ws.Close()
|
|
|
|
|
2018-02-02 07:48:44 +03:00
|
|
|
multipleSendRecv(t, ws)
|
2018-01-26 11:52:23 +03:00
|
|
|
}
|
|
|
|
|
2017-12-01 04:43:01 +03:00
|
|
|
func TestSocksProxyDial(t *testing.T) {
|
|
|
|
s := newServer(t)
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
proxyListener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("listen failed: %v", err)
|
|
|
|
}
|
|
|
|
defer proxyListener.Close()
|
|
|
|
go func() {
|
|
|
|
c1, err := proxyListener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("proxy accept failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer c1.Close()
|
|
|
|
|
|
|
|
c1.SetDeadline(time.Now().Add(30 * time.Second))
|
|
|
|
|
|
|
|
buf := make([]byte, 32)
|
|
|
|
if _, err := io.ReadFull(c1, buf[:3]); err != nil {
|
|
|
|
t.Errorf("read failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if want := []byte{5, 1, 0}; !bytes.Equal(want, buf[:len(want)]) {
|
|
|
|
t.Errorf("read %x, want %x", buf[:len(want)], want)
|
|
|
|
}
|
|
|
|
if _, err := c1.Write([]byte{5, 0}); err != nil {
|
|
|
|
t.Errorf("write failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err := io.ReadFull(c1, buf[:10]); err != nil {
|
|
|
|
t.Errorf("read failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if want := []byte{5, 1, 0, 1}; !bytes.Equal(want, buf[:len(want)]) {
|
|
|
|
t.Errorf("read %x, want %x", buf[:len(want)], want)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
buf[1] = 0
|
|
|
|
if _, err := c1.Write(buf[:10]); err != nil {
|
|
|
|
t.Errorf("write failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := net.IP(buf[4:8])
|
|
|
|
port := binary.BigEndian.Uint16(buf[8:10])
|
|
|
|
|
|
|
|
c2, err := net.DialTCP("tcp", nil, &net.TCPAddr{IP: ip, Port: int(port)})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("dial failed; %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer c2.Close()
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
io.Copy(c1, c2)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
io.Copy(c2, c1)
|
|
|
|
<-done
|
|
|
|
}()
|
|
|
|
|
|
|
|
purl, err := url.Parse("socks5://" + proxyListener.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("parse failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cstDialer := cstDialer // make local copy for modification on next line.
|
|
|
|
cstDialer.Proxy = http.ProxyURL(purl)
|
|
|
|
|
|
|
|
ws, _, err := cstDialer.Dial(s.URL, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Dial: %v", err)
|
|
|
|
}
|
|
|
|
defer ws.Close()
|
|
|
|
sendRecv(t, ws)
|
|
|
|
}
|