2018-10-28 23:46:32 +08:00
|
|
|
package vmess
|
|
|
|
|
|
|
|
import (
|
2021-08-22 00:25:29 +08:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2018-11-04 21:36:20 +08:00
|
|
|
"crypto/tls"
|
2021-08-22 00:25:29 +08:00
|
|
|
"encoding/base64"
|
2023-01-16 09:42:03 +08:00
|
|
|
"encoding/binary"
|
2021-08-22 00:25:29 +08:00
|
|
|
"errors"
|
2018-10-28 23:46:32 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-01-16 09:42:03 +08:00
|
|
|
"math/rand"
|
2018-10-28 23:46:32 +08:00
|
|
|
"net"
|
2018-12-11 00:25:05 +08:00
|
|
|
"net/http"
|
2018-11-04 21:36:20 +08:00
|
|
|
"net/url"
|
2021-08-22 16:03:46 +08:00
|
|
|
"strconv"
|
2018-10-28 23:46:32 +08:00
|
|
|
"strings"
|
2019-12-05 14:12:29 +08:00
|
|
|
"sync"
|
2018-10-28 23:46:32 +08:00
|
|
|
"time"
|
2023-01-16 09:42:03 +08:00
|
|
|
_ "unsafe"
|
2018-10-28 23:46:32 +08:00
|
|
|
|
2023-01-16 10:50:31 +08:00
|
|
|
"github.com/Dreamacro/clash/common/buf"
|
|
|
|
N "github.com/Dreamacro/clash/common/net"
|
|
|
|
|
2018-10-28 23:46:32 +08:00
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
2023-01-16 09:42:03 +08:00
|
|
|
//go:linkname maskBytes github.com/gorilla/websocket.maskBytes
|
|
|
|
func maskBytes(key [4]byte, pos int, b []byte) int
|
|
|
|
|
2018-10-28 23:46:32 +08:00
|
|
|
type websocketConn struct {
|
|
|
|
conn *websocket.Conn
|
|
|
|
reader io.Reader
|
|
|
|
remoteAddr net.Addr
|
2019-12-05 14:12:29 +08:00
|
|
|
|
2023-01-16 10:50:31 +08:00
|
|
|
rawWriter N.ExtendedWriter
|
2023-01-16 09:42:03 +08:00
|
|
|
|
2019-12-05 14:12:29 +08:00
|
|
|
// https://godoc.org/github.com/gorilla/websocket#hdr-Concurrency
|
|
|
|
rMux sync.Mutex
|
|
|
|
wMux sync.Mutex
|
2018-10-28 23:46:32 +08:00
|
|
|
}
|
2021-10-10 23:44:09 +08:00
|
|
|
|
2021-08-22 00:25:29 +08:00
|
|
|
type websocketWithEarlyDataConn struct {
|
|
|
|
net.Conn
|
2023-01-16 10:50:31 +08:00
|
|
|
wsWriter N.ExtendedWriter
|
2021-08-22 00:25:29 +08:00
|
|
|
underlay net.Conn
|
|
|
|
closed bool
|
|
|
|
dialed chan bool
|
|
|
|
cancel context.CancelFunc
|
|
|
|
ctx context.Context
|
|
|
|
config *WebsocketConfig
|
|
|
|
}
|
2018-10-28 23:46:32 +08:00
|
|
|
|
2019-02-11 15:25:10 +08:00
|
|
|
type WebsocketConfig struct {
|
2021-08-22 00:25:29 +08:00
|
|
|
Host string
|
|
|
|
Port string
|
|
|
|
Path string
|
|
|
|
Headers http.Header
|
|
|
|
TLS bool
|
2021-10-16 20:19:59 +08:00
|
|
|
TLSConfig *tls.Config
|
2021-08-22 00:25:29 +08:00
|
|
|
MaxEarlyData int
|
|
|
|
EarlyDataHeaderName string
|
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) {
|
2019-12-05 14:12:29 +08:00
|
|
|
wsc.rMux.Lock()
|
|
|
|
defer wsc.rMux.Unlock()
|
2018-10-28 23:46:32 +08:00
|
|
|
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) {
|
2019-12-05 14:12:29 +08:00
|
|
|
wsc.wMux.Lock()
|
|
|
|
defer wsc.wMux.Unlock()
|
2018-10-28 23:46:32 +08:00
|
|
|
if err := wsc.conn.WriteMessage(websocket.BinaryMessage, b); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
2023-01-16 09:42:03 +08:00
|
|
|
func (wsc *websocketConn) WriteBuffer(buffer *buf.Buffer) error {
|
|
|
|
var payloadBitLength int
|
|
|
|
dataLen := buffer.Len()
|
|
|
|
data := buffer.Bytes()
|
|
|
|
if dataLen < 126 {
|
|
|
|
payloadBitLength = 1
|
|
|
|
} else if dataLen < 65536 {
|
|
|
|
payloadBitLength = 3
|
|
|
|
} else {
|
|
|
|
payloadBitLength = 9
|
|
|
|
}
|
|
|
|
|
|
|
|
var headerLen int
|
|
|
|
headerLen += 1 // FIN / RSV / OPCODE
|
|
|
|
headerLen += payloadBitLength
|
|
|
|
headerLen += 4 // MASK KEY
|
|
|
|
|
|
|
|
header := buffer.ExtendHeader(headerLen)
|
|
|
|
header[0] = websocket.BinaryMessage | 1<<7
|
|
|
|
header[1] = 1 << 7
|
|
|
|
|
|
|
|
if dataLen < 126 {
|
|
|
|
header[1] |= byte(dataLen)
|
|
|
|
} else if dataLen < 65536 {
|
|
|
|
header[1] |= 126
|
|
|
|
binary.BigEndian.PutUint16(header[2:], uint16(dataLen))
|
|
|
|
} else {
|
|
|
|
header[1] |= 127
|
|
|
|
binary.BigEndian.PutUint64(header[2:], uint64(dataLen))
|
|
|
|
}
|
|
|
|
|
|
|
|
maskKey := rand.Uint32()
|
2023-01-16 11:42:10 +08:00
|
|
|
binary.LittleEndian.PutUint32(header[1+payloadBitLength:], maskKey)
|
|
|
|
N.MaskWebSocket(maskKey, data)
|
2023-01-16 09:42:03 +08:00
|
|
|
|
|
|
|
wsc.wMux.Lock()
|
|
|
|
defer wsc.wMux.Unlock()
|
|
|
|
return wsc.rawWriter.WriteBuffer(buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsc *websocketConn) FrontHeadroom() int {
|
|
|
|
return 14
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsc *websocketConn) Upstream() any {
|
|
|
|
return wsc.conn.UnderlyingConn()
|
|
|
|
}
|
|
|
|
|
2018-10-28 23:46:32 +08:00
|
|
|
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 {
|
2020-08-25 22:19:59 +08:00
|
|
|
return fmt.Errorf("failed to close connection: %s", strings.Join(errors, ","))
|
2018-10-28 23:46:32 +08:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-08-22 00:25:29 +08:00
|
|
|
func (wsedc *websocketWithEarlyDataConn) Dial(earlyData []byte) error {
|
2021-08-22 16:16:45 +08:00
|
|
|
base64DataBuf := &bytes.Buffer{}
|
|
|
|
base64EarlyDataEncoder := base64.NewEncoder(base64.RawURLEncoding, base64DataBuf)
|
2021-08-22 00:25:29 +08:00
|
|
|
|
2021-08-22 16:16:45 +08:00
|
|
|
earlyDataBuf := bytes.NewBuffer(earlyData)
|
|
|
|
if _, err := base64EarlyDataEncoder.Write(earlyDataBuf.Next(wsedc.config.MaxEarlyData)); err != nil {
|
|
|
|
return errors.New("failed to encode early data: " + err.Error())
|
2021-08-22 00:25:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if errc := base64EarlyDataEncoder.Close(); errc != nil {
|
|
|
|
return errors.New("failed to encode early data tail: " + errc.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2021-08-22 16:16:45 +08:00
|
|
|
if wsedc.Conn, err = streamWebsocketConn(wsedc.underlay, wsedc.config, base64DataBuf); err != nil {
|
2021-08-22 00:25:29 +08:00
|
|
|
wsedc.Close()
|
|
|
|
return errors.New("failed to dial WebSocket: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
wsedc.dialed <- true
|
2023-01-16 10:50:31 +08:00
|
|
|
wsedc.wsWriter = N.NewExtendedWriter(wsedc.Conn)
|
2021-08-22 16:16:45 +08:00
|
|
|
if earlyDataBuf.Len() != 0 {
|
|
|
|
_, err = wsedc.Conn.Write(earlyDataBuf.Bytes())
|
2021-08-22 00:25:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsedc *websocketWithEarlyDataConn) Write(b []byte) (int, error) {
|
|
|
|
if wsedc.closed {
|
|
|
|
return 0, io.ErrClosedPipe
|
|
|
|
}
|
|
|
|
if wsedc.Conn == nil {
|
|
|
|
if err := wsedc.Dial(b); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return wsedc.Conn.Write(b)
|
|
|
|
}
|
|
|
|
|
2023-01-16 09:42:03 +08:00
|
|
|
func (wsedc *websocketWithEarlyDataConn) WriteBuffer(buffer *buf.Buffer) error {
|
|
|
|
if wsedc.closed {
|
|
|
|
return io.ErrClosedPipe
|
|
|
|
}
|
|
|
|
if wsedc.Conn == nil {
|
|
|
|
if err := wsedc.Dial(buffer.Bytes()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return wsedc.wsWriter.WriteBuffer(buffer)
|
|
|
|
}
|
|
|
|
|
2021-08-22 00:25:29 +08:00
|
|
|
func (wsedc *websocketWithEarlyDataConn) Read(b []byte) (int, error) {
|
|
|
|
if wsedc.closed {
|
|
|
|
return 0, io.ErrClosedPipe
|
|
|
|
}
|
|
|
|
if wsedc.Conn == nil {
|
|
|
|
select {
|
|
|
|
case <-wsedc.ctx.Done():
|
|
|
|
return 0, io.ErrUnexpectedEOF
|
|
|
|
case <-wsedc.dialed:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wsedc.Conn.Read(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsedc *websocketWithEarlyDataConn) Close() error {
|
|
|
|
wsedc.closed = true
|
|
|
|
wsedc.cancel()
|
|
|
|
if wsedc.Conn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return wsedc.Conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsedc *websocketWithEarlyDataConn) LocalAddr() net.Addr {
|
|
|
|
if wsedc.Conn == nil {
|
|
|
|
return wsedc.underlay.LocalAddr()
|
|
|
|
}
|
|
|
|
return wsedc.Conn.LocalAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsedc *websocketWithEarlyDataConn) RemoteAddr() net.Addr {
|
|
|
|
if wsedc.Conn == nil {
|
|
|
|
return wsedc.underlay.RemoteAddr()
|
|
|
|
}
|
|
|
|
return wsedc.Conn.RemoteAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsedc *websocketWithEarlyDataConn) SetDeadline(t time.Time) error {
|
|
|
|
if err := wsedc.SetReadDeadline(t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return wsedc.SetWriteDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsedc *websocketWithEarlyDataConn) SetReadDeadline(t time.Time) error {
|
|
|
|
if wsedc.Conn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return wsedc.Conn.SetReadDeadline(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsedc *websocketWithEarlyDataConn) SetWriteDeadline(t time.Time) error {
|
|
|
|
if wsedc.Conn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return wsedc.Conn.SetWriteDeadline(t)
|
|
|
|
}
|
|
|
|
|
2023-01-16 09:42:03 +08:00
|
|
|
func (wsedc *websocketWithEarlyDataConn) Upstream() any {
|
|
|
|
return wsedc.Conn
|
|
|
|
}
|
|
|
|
|
2021-08-22 00:25:29 +08:00
|
|
|
func streamWebsocketWithEarlyDataConn(conn net.Conn, c *WebsocketConfig) (net.Conn, error) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
conn = &websocketWithEarlyDataConn{
|
|
|
|
dialed: make(chan bool, 1),
|
|
|
|
cancel: cancel,
|
|
|
|
ctx: ctx,
|
|
|
|
underlay: conn,
|
|
|
|
config: c,
|
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func streamWebsocketConn(conn net.Conn, c *WebsocketConfig, earlyData *bytes.Buffer) (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"
|
2021-10-16 20:19:59 +08:00
|
|
|
dialer.TLSClientConfig = c.TLSConfig
|
2018-10-28 23:46:32 +08:00
|
|
|
}
|
2018-11-04 21:36:20 +08:00
|
|
|
|
2022-07-10 14:56:34 +08:00
|
|
|
u, err := url.Parse(c.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parse url %s error: %w", c.Path, err)
|
|
|
|
}
|
|
|
|
|
2018-11-04 21:36:20 +08:00
|
|
|
uri := url.URL{
|
2022-07-10 14:56:34 +08:00
|
|
|
Scheme: scheme,
|
|
|
|
Host: net.JoinHostPort(c.Host, c.Port),
|
|
|
|
Path: u.Path,
|
|
|
|
RawQuery: u.RawQuery,
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-22 00:25:29 +08:00
|
|
|
if earlyData != nil {
|
|
|
|
if c.EarlyDataHeaderName == "" {
|
|
|
|
uri.Path += earlyData.String()
|
|
|
|
} else {
|
|
|
|
headers.Set(c.EarlyDataHeaderName, earlyData.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2020-08-25 22:19:59 +08:00
|
|
|
return nil, fmt.Errorf("dial %s error: %s", uri.Host, reason)
|
2018-11-04 21:36:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return &websocketConn{
|
|
|
|
conn: wsConn,
|
2023-01-16 10:50:31 +08:00
|
|
|
rawWriter: N.NewExtendedWriter(wsConn.UnderlyingConn()),
|
2018-11-04 21:36:20 +08:00
|
|
|
remoteAddr: conn.RemoteAddr(),
|
|
|
|
}, nil
|
2018-10-28 23:46:32 +08:00
|
|
|
}
|
2021-08-22 00:25:29 +08:00
|
|
|
|
|
|
|
func StreamWebsocketConn(conn net.Conn, c *WebsocketConfig) (net.Conn, error) {
|
2021-08-22 16:03:46 +08:00
|
|
|
if u, err := url.Parse(c.Path); err == nil {
|
|
|
|
if q := u.Query(); q.Get("ed") != "" {
|
|
|
|
if ed, err := strconv.Atoi(q.Get("ed")); err == nil {
|
|
|
|
c.MaxEarlyData = ed
|
|
|
|
c.EarlyDataHeaderName = "Sec-WebSocket-Protocol"
|
|
|
|
q.Del("ed")
|
|
|
|
u.RawQuery = q.Encode()
|
|
|
|
c.Path = u.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-22 00:25:29 +08:00
|
|
|
if c.MaxEarlyData > 0 {
|
|
|
|
return streamWebsocketWithEarlyDataConn(conn, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return streamWebsocketConn(conn, c, nil)
|
|
|
|
}
|