Skip wait for hysteria tcp handshake response

Co-authored-by: arm64v8a <48624112+arm64v8a@users.noreply.github.com>
This commit is contained in:
世界 2022-09-12 23:56:18 +08:00
parent 38088f28b0
commit 79b6bdfda1
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 26 additions and 17 deletions

View file

@ -273,7 +273,7 @@ func (h *Hysteria) acceptStream(ctx context.Context, conn quic.Connection, strea
return err return err
} }
h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination) 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 { } else {
h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination) h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
var id uint32 var id uint32

View file

@ -276,16 +276,7 @@ func (h *Hysteria) DialContext(ctx context.Context, network string, destination
stream.Close() stream.Close()
return nil, err return nil, err
} }
response, err := hysteria.ReadServerResponse(stream) return hysteria.NewConn(stream, destination, true), nil
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
case N.NetworkUDP: case N.NetworkUDP:
conn, err := h.ListenPacket(ctx, destination) conn, err := h.ListenPacket(ctx, destination)
if err != nil { if err != nil {

View file

@ -374,17 +374,35 @@ var _ net.Conn = (*Conn)(nil)
type Conn struct { type Conn struct {
quic.Stream quic.Stream
destination M.Socksaddr destination M.Socksaddr
responseWritten bool needReadResponse bool
} }
func NewConn(stream quic.Stream, destination M.Socksaddr) *Conn { func NewConn(stream quic.Stream, destination M.Socksaddr, isClient bool) *Conn {
return &Conn{ return &Conn{
Stream: stream, Stream: stream,
destination: destination, 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 { func (c *Conn) LocalAddr() net.Addr {
return nil return nil
} }
@ -394,7 +412,7 @@ func (c *Conn) RemoteAddr() net.Addr {
} }
func (c *Conn) ReaderReplaceable() bool { func (c *Conn) ReaderReplaceable() bool {
return true return !c.needReadResponse
} }
func (c *Conn) WriterReplaceable() bool { func (c *Conn) WriterReplaceable() bool {