2018-10-28 23:46:32 +08:00
|
|
|
package vmess
|
|
|
|
|
|
|
|
import (
|
2018-11-04 21:36:20 +08:00
|
|
|
"crypto/tls"
|
2018-10-28 23:46:32 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2018-12-11 00:25:05 +08:00
|
|
|
"net/http"
|
2018-11-04 21:36:20 +08:00
|
|
|
"net/url"
|
2018-10-28 23:46:32 +08:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
type websocketConn struct {
|
|
|
|
conn *websocket.Conn
|
|
|
|
reader io.Reader
|
|
|
|
remoteAddr net.Addr
|
|
|
|
}
|
|
|
|
|
2019-02-11 15:25:10 +08:00
|
|
|
type WebsocketConfig struct {
|
|
|
|
Host string
|
|
|
|
Path string
|
2019-07-31 11:13:49 +08:00
|
|
|
Headers http.Header
|
2019-02-11 15:25:10 +08:00
|
|
|
TLS bool
|
|
|
|
TLSConfig *tls.Config
|
2018-11-04 21:36:20 +08:00
|
|
|
}
|
|
|
|
|
2018-10-28 23:46:32 +08:00
|
|
|
// Read implements net.Conn.Read()
|
|
|
|
func (wsc *websocketConn) Read(b []byte) (int, error) {
|
|
|
|
for {
|
|
|
|
reader, err := wsc.getReader()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nBytes, err := reader.Read(b)
|
|
|
|
if err == io.EOF {
|
|
|
|
wsc.reader = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nBytes, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write implements io.Writer.
|
|
|
|
func (wsc *websocketConn) Write(b []byte) (int, error) {
|
|
|
|
if err := wsc.conn.WriteMessage(websocket.BinaryMessage, b); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsc *websocketConn) Close() error {
|
|
|
|
var errors []string
|
|
|
|
if err := wsc.conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second*5)); err != nil {
|
|
|
|
errors = append(errors, err.Error())
|
|
|
|
}
|
|
|
|
if err := wsc.conn.Close(); err != nil {
|
|
|
|
errors = append(errors, err.Error())
|
|
|
|
}
|
|
|
|
if len(errors) > 0 {
|
|
|
|
return fmt.Errorf("Failed to close connection: %s", strings.Join(errors, ","))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsc *websocketConn) getReader() (io.Reader, error) {
|
|
|
|
if wsc.reader != nil {
|
|
|
|
return wsc.reader, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, reader, err := wsc.conn.NextReader()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wsc.reader = reader
|
|
|
|
return reader, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsc *websocketConn) LocalAddr() net.Addr {
|
|
|
|
return wsc.conn.LocalAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsc *websocketConn) RemoteAddr() net.Addr {
|
|
|
|
return wsc.remoteAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsc *websocketConn) SetDeadline(t time.Time) error {
|
|
|
|
if err := wsc.SetReadDeadline(t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return wsc.SetWriteDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsc *websocketConn) SetReadDeadline(t time.Time) error {
|
|
|
|
return wsc.conn.SetReadDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsc *websocketConn) SetWriteDeadline(t time.Time) error {
|
|
|
|
return wsc.conn.SetWriteDeadline(t)
|
|
|
|
}
|
|
|
|
|
2019-02-11 15:25:10 +08:00
|
|
|
func NewWebsocketConn(conn net.Conn, c *WebsocketConfig) (net.Conn, error) {
|
2018-11-04 21:36:20 +08:00
|
|
|
dialer := &websocket.Dialer{
|
|
|
|
NetDial: func(network, addr string) (net.Conn, error) {
|
|
|
|
return conn, nil
|
|
|
|
},
|
|
|
|
ReadBufferSize: 4 * 1024,
|
|
|
|
WriteBufferSize: 4 * 1024,
|
|
|
|
HandshakeTimeout: time.Second * 8,
|
|
|
|
}
|
|
|
|
|
|
|
|
scheme := "ws"
|
2019-02-11 15:25:10 +08:00
|
|
|
if c.TLS {
|
2018-11-04 21:36:20 +08:00
|
|
|
scheme = "wss"
|
2019-02-11 15:25:10 +08:00
|
|
|
dialer.TLSClientConfig = c.TLSConfig
|
2018-11-04 21:36:20 +08:00
|
|
|
}
|
|
|
|
|
2019-02-11 15:25:10 +08:00
|
|
|
host, port, _ := net.SplitHostPort(c.Host)
|
2018-11-04 21:36:20 +08:00
|
|
|
if (scheme == "ws" && port != "80") || (scheme == "wss" && port != "443") {
|
2019-02-11 15:25:10 +08:00
|
|
|
host = c.Host
|
2018-10-28 23:46:32 +08:00
|
|
|
}
|
2018-11-04 21:36:20 +08:00
|
|
|
|
|
|
|
uri := url.URL{
|
|
|
|
Scheme: scheme,
|
|
|
|
Host: host,
|
2019-02-11 15:25:10 +08:00
|
|
|
Path: c.Path,
|
2018-11-04 21:36:20 +08:00
|
|
|
}
|
|
|
|
|
2018-12-11 00:25:05 +08:00
|
|
|
headers := http.Header{}
|
2019-02-11 15:25:10 +08:00
|
|
|
if c.Headers != nil {
|
2019-07-31 11:13:49 +08:00
|
|
|
for k := range c.Headers {
|
|
|
|
headers.Add(k, c.Headers.Get(k))
|
2018-12-11 00:25:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wsConn, resp, err := dialer.Dial(uri.String(), headers)
|
2018-11-04 21:36:20 +08:00
|
|
|
if err != nil {
|
2019-07-31 11:13:49 +08:00
|
|
|
reason := err.Error()
|
2018-11-04 21:36:20 +08:00
|
|
|
if resp != nil {
|
|
|
|
reason = resp.Status
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Dial %s error: %s", host, reason)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &websocketConn{
|
|
|
|
conn: wsConn,
|
|
|
|
remoteAddr: conn.RemoteAddr(),
|
|
|
|
}, nil
|
2018-10-28 23:46:32 +08:00
|
|
|
}
|