Browse Source

Skip wait for hysteria tcp handshake response

Co-authored-by: arm64v8a <[email protected]>
世界 3 years ago
parent
commit
79b6bdfda1
3 changed files with 26 additions and 17 deletions
  1. 1 1
      inbound/hysteria.go
  2. 1 10
      outbound/hysteria.go
  3. 24 6
      transport/hysteria/protocol.go

+ 1 - 1
inbound/hysteria.go

@@ -273,7 +273,7 @@ func (h *Hysteria) acceptStream(ctx context.Context, conn quic.Connection, strea
 			return err
 		}
 		h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
-		return h.router.RouteConnection(ctx, hysteria.NewConn(stream, metadata.Destination), metadata)
+		return h.router.RouteConnection(ctx, hysteria.NewConn(stream, metadata.Destination, false), metadata)
 	} else {
 		h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
 		var id uint32

+ 1 - 10
outbound/hysteria.go

@@ -276,16 +276,7 @@ func (h *Hysteria) DialContext(ctx context.Context, network string, destination
 			stream.Close()
 			return nil, err
 		}
-		response, err := hysteria.ReadServerResponse(stream)
-		if err != nil {
-			stream.Close()
-			return nil, err
-		}
-		if !response.OK {
-			stream.Close()
-			return nil, E.New("remote error: ", response.Message)
-		}
-		return hysteria.NewConn(stream, destination), nil
+		return hysteria.NewConn(stream, destination, true), nil
 	case N.NetworkUDP:
 		conn, err := h.ListenPacket(ctx, destination)
 		if err != nil {

+ 24 - 6
transport/hysteria/protocol.go

@@ -374,15 +374,33 @@ var _ net.Conn = (*Conn)(nil)
 
 type Conn struct {
 	quic.Stream
-	destination     M.Socksaddr
-	responseWritten bool
+	destination      M.Socksaddr
+	needReadResponse bool
 }
 
-func NewConn(stream quic.Stream, destination M.Socksaddr) *Conn {
+func NewConn(stream quic.Stream, destination M.Socksaddr, isClient bool) *Conn {
 	return &Conn{
-		Stream:      stream,
-		destination: destination,
+		Stream:           stream,
+		destination:      destination,
+		needReadResponse: isClient,
+	}
+}
+
+func (c *Conn) Read(p []byte) (n int, err error) {
+	if c.needReadResponse {
+		var response *ServerResponse
+		response, err = ReadServerResponse(c.Stream)
+		if err != nil {
+			c.Close()
+			return
+		}
+		if !response.OK {
+			c.Close()
+			return 0, E.New("remote error: ", response.Message)
+		}
+		c.needReadResponse = false
 	}
+	return c.Stream.Read(p)
 }
 
 func (c *Conn) LocalAddr() net.Addr {
@@ -394,7 +412,7 @@ func (c *Conn) RemoteAddr() net.Addr {
 }
 
 func (c *Conn) ReaderReplaceable() bool {
-	return true
+	return !c.needReadResponse
 }
 
 func (c *Conn) WriterReplaceable() bool {