fix wss bug

This commit is contained in:
rui.zheng 2016-09-15 12:28:07 +08:00
parent f7c8c40081
commit 986b27475f
2 changed files with 13 additions and 19 deletions

View File

@ -440,13 +440,13 @@ func forward(conn net.Conn, arg Args) (net.Conn, error) {
switch arg.Transport {
case "ws": // websocket connection
conn, err = wsClient(conn, arg.Addr)
conn, err = wsClient("ws", conn, arg.Addr)
if err != nil {
return nil, err
}
case "wss": // websocket security
tlsUsed = true
conn, err = wssClient(conn, arg.Addr)
conn, err = wsClient("wss", conn, arg.Addr)
if err != nil {
return nil, err
}

28
ws.go
View File

@ -17,24 +17,18 @@ type wsConn struct {
rb []byte
}
func wsClient(conn net.Conn, host string) (*wsConn, error) {
c, resp, err := websocket.NewClient(conn, &url.URL{Scheme: "ws", Host: host, Path: "/ws"}, nil, 4096, 4096)
if err != nil {
return nil, err
func wsClient(scheme string, conn net.Conn, host string) (*wsConn, error) {
dialer := websocket.Dialer{
ReadBufferSize: 4096,
WriteBufferSize: 4096,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
HandshakeTimeout: time.Second * 90,
NetDial: func(net, addr string) (net.Conn, error) {
return conn, nil
},
}
resp.Body.Close()
return &wsConn{conn: c}, nil
}
func wssClient(conn net.Conn, host string) (*wsConn, error) {
tlsConn := tls.Client(conn, &tls.Config{InsecureSkipVerify: true})
if err := tlsConn.Handshake(); err != nil {
return nil, err
}
conn = tlsConn
c, resp, err := websocket.NewClient(conn, &url.URL{Scheme: "wss", Host: host, Path: "/ws"}, nil, 4096, 4096)
u := url.URL{Scheme: scheme, Host: host, Path: "/ws"}
c, resp, err := dialer.Dial(u.String(), nil)
if err != nil {
return nil, err
}