Add HandshakeSuccess interface

This commit is contained in:
世界 2023-09-08 11:22:32 +08:00
parent 03c21c0a12
commit b0849c43a6
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 22 additions and 11 deletions

View file

@ -98,7 +98,7 @@ func CopyExtendedBuffer(originSource io.Writer, destination N.ExtendedWriter, so
err = destination.WriteBuffer(buffer)
if err != nil {
if !notFirstTime {
err = N.HandshakeFailure(originSource, err)
err = N.ReportHandshakeFailure(originSource, err)
}
return
}
@ -130,7 +130,7 @@ func CopyExtendedWithSrcBuffer(originSource io.Reader, destination N.ExtendedWri
if err != nil {
buffer.Release()
if !notFirstTime {
err = N.HandshakeFailure(originSource, err)
err = N.ReportHandshakeFailure(originSource, err)
}
return
}
@ -175,7 +175,7 @@ func CopyExtendedWithPool(originSource io.Reader, destination N.ExtendedWriter,
if err != nil {
buffer.Release()
if !notFirstTime {
err = N.HandshakeFailure(originSource, err)
err = N.ReportHandshakeFailure(originSource, err)
}
return
}
@ -304,7 +304,7 @@ func CopyPacketWithSrcBuffer(originSource N.PacketReader, destinationConn N.Pack
if err != nil {
buffer.Release()
if !notFirstTime {
err = N.HandshakeFailure(originSource, err)
err = N.ReportHandshakeFailure(originSource, err)
}
return
}
@ -345,7 +345,7 @@ func CopyPacketWithPool(originSource N.PacketReader, destinationConn N.PacketWri
if err != nil {
buffer.Release()
if !notFirstTime {
err = N.HandshakeFailure(originSource, err)
err = N.ReportHandshakeFailure(originSource, err)
}
return
}
@ -381,7 +381,7 @@ func WritePacketWithPool(originSource N.PacketReader, destinationConn N.PacketWr
if err != nil {
buffer.Release()
if !notFirstTime {
err = N.HandshakeFailure(originSource, err)
err = N.ReportHandshakeFailure(originSource, err)
}
return
}

View file

@ -53,7 +53,7 @@ func copyWaitWithPool(originSource io.Reader, destination N.ExtendedWriter, sour
if err != nil {
buffer.Release()
if !notFirstTime {
err = N.HandshakeFailure(originSource, err)
err = N.ReportHandshakeFailure(originSource, err)
}
return
}
@ -102,7 +102,7 @@ func copyPacketWaitWithPool(originSource N.PacketReader, destinationConn N.Packe
if err != nil {
buffer.Release()
if !notFirstTime {
err = N.HandshakeFailure(originSource, err)
err = N.ReportHandshakeFailure(originSource, err)
}
return
}

View file

@ -5,15 +5,26 @@ import (
E "github.com/sagernet/sing/common/exceptions"
)
type HandshakeConn interface {
type HandshakeFailure interface {
HandshakeFailure(err error) error
}
func HandshakeFailure(conn any, err error) error {
if handshakeConn, isHandshakeConn := common.Cast[HandshakeConn](conn); isHandshakeConn {
type HandshakeSuccess interface {
HandshakeSuccess() error
}
func ReportHandshakeFailure(conn any, err error) error {
if handshakeConn, isHandshakeConn := common.Cast[HandshakeFailure](conn); isHandshakeConn {
return E.Append(err, handshakeConn.HandshakeFailure(err), func(err error) error {
return E.Cause(err, "write handshake failure")
})
}
return err
}
func ReportHandshakeSuccess(conn any) error {
if handshakeConn, isHandshakeConn := common.Cast[HandshakeSuccess](conn); isHandshakeConn {
return handshakeConn.HandshakeSuccess()
}
return nil
}