Merge from remote branch
This commit is contained in:
@@ -211,6 +211,8 @@ func StreamGunWithTransport(transport *http2.Transport, cfg *Config) (net.Conn,
|
||||
Scheme: "https",
|
||||
Host: cfg.Host,
|
||||
Path: fmt.Sprintf("/%s/Tun", serviceName),
|
||||
// for unescape path
|
||||
Opaque: fmt.Sprintf("//%s/%s/Tun", cfg.Host, serviceName),
|
||||
},
|
||||
Proto: "HTTP/2",
|
||||
ProtoMajor: 2,
|
||||
|
||||
@@ -152,7 +152,7 @@ func (a *authAES128) Encode(buf *bytes.Buffer, b []byte) error {
|
||||
}
|
||||
|
||||
func (a *authAES128) DecodePacket(b []byte) ([]byte, error) {
|
||||
if !bytes.Equal(a.hmac(a.Key, b[:len(b)-4])[:4], b[len(b)-4:]) {
|
||||
if !bytes.Equal(a.hmac(a.userKey, b[:len(b)-4])[:4], b[len(b)-4:]) {
|
||||
return nil, errAuthAES128ChksumError
|
||||
}
|
||||
return b[:len(b)-4], nil
|
||||
|
||||
@@ -59,12 +59,12 @@ func (vc *Conn) Read(b []byte) (int, error) {
|
||||
func (vc *Conn) sendRequest() error {
|
||||
timestamp := time.Now()
|
||||
|
||||
mbuf := &bytes.Buffer{}
|
||||
|
||||
if !vc.isAead {
|
||||
h := hmac.New(md5.New, vc.id.UUID.Bytes())
|
||||
binary.Write(h, binary.BigEndian, uint64(timestamp.Unix()))
|
||||
if _, err := vc.Conn.Write(h.Sum(nil)); err != nil {
|
||||
return err
|
||||
}
|
||||
mbuf.Write(h.Sum(nil))
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
@@ -110,7 +110,8 @@ func (vc *Conn) sendRequest() error {
|
||||
|
||||
stream := cipher.NewCFBEncrypter(block, hashTimestamp(timestamp))
|
||||
stream.XORKeyStream(buf.Bytes(), buf.Bytes())
|
||||
_, err = vc.Conn.Write(buf.Bytes())
|
||||
mbuf.Write(buf.Bytes())
|
||||
_, err = vc.Conn.Write(mbuf.Bytes())
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
package vmess
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -23,15 +28,26 @@ type websocketConn struct {
|
||||
rMux sync.Mutex
|
||||
wMux sync.Mutex
|
||||
}
|
||||
type websocketWithEarlyDataConn struct {
|
||||
net.Conn
|
||||
underlay net.Conn
|
||||
closed bool
|
||||
dialed chan bool
|
||||
cancel context.CancelFunc
|
||||
ctx context.Context
|
||||
config *WebsocketConfig
|
||||
}
|
||||
|
||||
type WebsocketConfig struct {
|
||||
Host string
|
||||
Port string
|
||||
Path string
|
||||
Headers http.Header
|
||||
TLS bool
|
||||
SkipCertVerify bool
|
||||
ServerName string
|
||||
Host string
|
||||
Port string
|
||||
Path string
|
||||
Headers http.Header
|
||||
TLS bool
|
||||
SkipCertVerify bool
|
||||
ServerName string
|
||||
MaxEarlyData int
|
||||
EarlyDataHeaderName string
|
||||
}
|
||||
|
||||
// Read implements net.Conn.Read()
|
||||
@@ -113,7 +129,118 @@ func (wsc *websocketConn) SetWriteDeadline(t time.Time) error {
|
||||
return wsc.conn.SetWriteDeadline(t)
|
||||
}
|
||||
|
||||
func StreamWebsocketConn(conn net.Conn, c *WebsocketConfig) (net.Conn, error) {
|
||||
func (wsedc *websocketWithEarlyDataConn) Dial(earlyData []byte) error {
|
||||
base64DataBuf := &bytes.Buffer{}
|
||||
base64EarlyDataEncoder := base64.NewEncoder(base64.RawURLEncoding, base64DataBuf)
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
if errc := base64EarlyDataEncoder.Close(); errc != nil {
|
||||
return errors.New("failed to encode early data tail: " + errc.Error())
|
||||
}
|
||||
|
||||
var err error
|
||||
if wsedc.Conn, err = streamWebsocketConn(wsedc.underlay, wsedc.config, base64DataBuf); err != nil {
|
||||
wsedc.Close()
|
||||
return errors.New("failed to dial WebSocket: " + err.Error())
|
||||
}
|
||||
|
||||
wsedc.dialed <- true
|
||||
if earlyDataBuf.Len() != 0 {
|
||||
_, err = wsedc.Conn.Write(earlyDataBuf.Bytes())
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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) {
|
||||
dialer := &websocket.Dialer{
|
||||
NetDial: func(network, addr string) (net.Conn, error) {
|
||||
return conn, nil
|
||||
@@ -152,6 +279,14 @@ func StreamWebsocketConn(conn net.Conn, c *WebsocketConfig) (net.Conn, error) {
|
||||
}
|
||||
}
|
||||
|
||||
if earlyData != nil {
|
||||
if c.EarlyDataHeaderName == "" {
|
||||
uri.Path += earlyData.String()
|
||||
} else {
|
||||
headers.Set(c.EarlyDataHeaderName, earlyData.String())
|
||||
}
|
||||
}
|
||||
|
||||
wsConn, resp, err := dialer.Dial(uri.String(), headers)
|
||||
if err != nil {
|
||||
reason := err.Error()
|
||||
@@ -166,3 +301,23 @@ func StreamWebsocketConn(conn net.Conn, c *WebsocketConfig) (net.Conn, error) {
|
||||
remoteAddr: conn.RemoteAddr(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func StreamWebsocketConn(conn net.Conn, c *WebsocketConfig) (net.Conn, error) {
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if c.MaxEarlyData > 0 {
|
||||
return streamWebsocketWithEarlyDataConn(conn, c)
|
||||
}
|
||||
|
||||
return streamWebsocketConn(conn, c, nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user