fix websocket config
This commit is contained in:
parent
2a2deb02a8
commit
791bd63dd5
41
conn.go
41
conn.go
@ -6,10 +6,6 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/ginuerzh/gosocks4"
|
|
||||||
"github.com/ginuerzh/gosocks5"
|
|
||||||
"github.com/golang/glog"
|
|
||||||
ss "github.com/shadowsocks/shadowsocks-go/shadowsocks"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
@ -18,6 +14,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/ginuerzh/gosocks4"
|
||||||
|
"github.com/ginuerzh/gosocks5"
|
||||||
|
"github.com/golang/glog"
|
||||||
|
ss "github.com/shadowsocks/shadowsocks-go/shadowsocks"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ProxyConn struct {
|
type ProxyConn struct {
|
||||||
@ -57,20 +58,40 @@ func (c *ProxyConn) handshake() error {
|
|||||||
|
|
||||||
switch c.Node.Transport {
|
switch c.Node.Transport {
|
||||||
case "ws": // websocket connection
|
case "ws": // websocket connection
|
||||||
|
rbuf, _ := strconv.Atoi(c.Node.Get("rbuf"))
|
||||||
|
wbuf, _ := strconv.Atoi(c.Node.Get("wbuf"))
|
||||||
|
comp := c.Node.getBool("compression")
|
||||||
|
opt := WSOptions{
|
||||||
|
ReadBufferSize: rbuf,
|
||||||
|
WriteBufferSize: wbuf,
|
||||||
|
HandshakeTimeout: DialTimeout,
|
||||||
|
EnableCompression: comp,
|
||||||
|
}
|
||||||
u := url.URL{Scheme: "ws", Host: c.Node.Addr, Path: "/ws"}
|
u := url.URL{Scheme: "ws", Host: c.Node.Addr, Path: "/ws"}
|
||||||
conn, err := WebsocketClientConn(u.String(), c.conn, nil)
|
conn, err := WebsocketClientConn(u.String(), c.conn, &opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
c.conn = conn
|
c.conn = conn
|
||||||
case "wss": // websocket security
|
case "wss": // websocket security
|
||||||
tlsUsed = true
|
tlsUsed = true
|
||||||
u := url.URL{Scheme: "wss", Host: c.Node.Addr, Path: "/ws"}
|
|
||||||
config := &tls.Config{
|
rbuf, _ := strconv.Atoi(c.Node.Get("rbuf"))
|
||||||
InsecureSkipVerify: c.Node.insecureSkipVerify(),
|
wbuf, _ := strconv.Atoi(c.Node.Get("wbuf"))
|
||||||
ServerName: c.Node.serverName,
|
comp := c.Node.getBool("compression")
|
||||||
|
opt := WSOptions{
|
||||||
|
ReadBufferSize: rbuf,
|
||||||
|
WriteBufferSize: wbuf,
|
||||||
|
HandshakeTimeout: DialTimeout,
|
||||||
|
EnableCompression: comp,
|
||||||
|
TLSConfig: &tls.Config{
|
||||||
|
InsecureSkipVerify: c.Node.insecureSkipVerify(),
|
||||||
|
ServerName: c.Node.serverName,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
conn, err := WebsocketClientConn(u.String(), c.conn, config)
|
|
||||||
|
u := url.URL{Scheme: "wss", Host: c.Node.Addr, Path: "/ws"}
|
||||||
|
conn, err := WebsocketClientConn(u.String(), c.conn, &opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
39
ws.go
39
ws.go
@ -2,12 +2,14 @@ package gost
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"github.com/golang/glog"
|
|
||||||
"gopkg.in/gorilla/websocket.v1"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/golang/glog"
|
||||||
|
"gopkg.in/gorilla/websocket.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WebsocketServer struct {
|
type WebsocketServer struct {
|
||||||
@ -18,14 +20,18 @@ type WebsocketServer struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewWebsocketServer(base *ProxyServer) *WebsocketServer {
|
func NewWebsocketServer(base *ProxyServer) *WebsocketServer {
|
||||||
|
rbuf, _ := strconv.Atoi(base.Node.Get("rbuf"))
|
||||||
|
wbuf, _ := strconv.Atoi(base.Node.Get("wbuf"))
|
||||||
|
comp := base.Node.getBool("compression")
|
||||||
|
|
||||||
return &WebsocketServer{
|
return &WebsocketServer{
|
||||||
Addr: base.Node.Addr,
|
Addr: base.Node.Addr,
|
||||||
Base: base,
|
Base: base,
|
||||||
upgrader: websocket.Upgrader{
|
upgrader: websocket.Upgrader{
|
||||||
ReadBufferSize: 1024,
|
ReadBufferSize: rbuf,
|
||||||
WriteBufferSize: 1024,
|
WriteBufferSize: wbuf,
|
||||||
CheckOrigin: func(r *http.Request) bool { return true },
|
CheckOrigin: func(r *http.Request) bool { return true },
|
||||||
EnableCompression: true,
|
EnableCompression: comp,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -68,18 +74,29 @@ func (s *WebsocketServer) ListenAndServeTLS(config *tls.Config) error {
|
|||||||
return server.ListenAndServeTLS("", "")
|
return server.ListenAndServeTLS("", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type WSOptions struct {
|
||||||
|
ReadBufferSize int
|
||||||
|
WriteBufferSize int
|
||||||
|
HandshakeTimeout time.Duration
|
||||||
|
EnableCompression bool
|
||||||
|
TLSConfig *tls.Config
|
||||||
|
}
|
||||||
|
|
||||||
type WebsocketConn struct {
|
type WebsocketConn struct {
|
||||||
conn *websocket.Conn
|
conn *websocket.Conn
|
||||||
rb []byte
|
rb []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func WebsocketClientConn(url string, conn net.Conn, config *tls.Config) (*WebsocketConn, error) {
|
func WebsocketClientConn(url string, conn net.Conn, options *WSOptions) (*WebsocketConn, error) {
|
||||||
|
if options == nil {
|
||||||
|
options = &WSOptions{}
|
||||||
|
}
|
||||||
dialer := websocket.Dialer{
|
dialer := websocket.Dialer{
|
||||||
ReadBufferSize: 1024,
|
ReadBufferSize: options.ReadBufferSize,
|
||||||
WriteBufferSize: 1024,
|
WriteBufferSize: options.WriteBufferSize,
|
||||||
TLSClientConfig: config,
|
TLSClientConfig: options.TLSConfig,
|
||||||
HandshakeTimeout: DialTimeout,
|
HandshakeTimeout: options.HandshakeTimeout,
|
||||||
EnableCompression: true,
|
EnableCompression: options.EnableCompression,
|
||||||
NetDial: func(net, addr string) (net.Conn, error) {
|
NetDial: func(net, addr string) (net.Conn, error) {
|
||||||
return conn, nil
|
return conn, nil
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user