From 287ad5bc531f6fe222a1469278e3d7fcb0c27b56 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Fri, 15 Feb 2019 21:55:15 +0800 Subject: [PATCH] Fix: vmess handshake block (#117) --- component/vmess/conn.go | 18 ++++++------------ component/vmess/vmess.go | 2 +- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/component/vmess/conn.go b/component/vmess/conn.go index 8efe7ffc..15b1e05c 100644 --- a/component/vmess/conn.go +++ b/component/vmess/conn.go @@ -35,20 +35,10 @@ type Conn struct { respV byte security byte - sent bool received bool } func (vc *Conn) Write(b []byte) (int, error) { - if vc.sent { - return vc.writer.Write(b) - } - - if err := vc.sendRequest(); err != nil { - return 0, err - } - - vc.sent = true return vc.writer.Write(b) } @@ -153,7 +143,7 @@ func hashTimestamp(t time.Time) []byte { } // newConn return a Conn instance -func newConn(conn net.Conn, id *ID, dst *DstAddr, security Security) *Conn { +func newConn(conn net.Conn, id *ID, dst *DstAddr, security Security) (*Conn, error) { randBytes := make([]byte, 33) rand.Read(randBytes) reqBodyIV := make([]byte, 16) @@ -196,7 +186,7 @@ func newConn(conn net.Conn, id *ID, dst *DstAddr, security Security) *Conn { reader = newAEADReader(conn, aead, respBodyIV[:]) } - return &Conn{ + c := &Conn{ Conn: conn, id: id, dst: dst, @@ -209,4 +199,8 @@ func newConn(conn net.Conn, id *ID, dst *DstAddr, security Security) *Conn { writer: writer, security: security, } + if err := c.sendRequest(); err != nil { + return nil, err + } + return c, nil } diff --git a/component/vmess/vmess.go b/component/vmess/vmess.go index e0a7a24f..71b30c15 100644 --- a/component/vmess/vmess.go +++ b/component/vmess/vmess.go @@ -100,7 +100,7 @@ func (c *Client) New(conn net.Conn, dst *DstAddr) (net.Conn, error) { } else if c.tls { conn = tls.Client(conn, c.tlsConfig) } - return newConn(conn, c.user[r], dst, c.security), nil + return newConn(conn, c.user[r], dst, c.security) } // NewClient return Client instance