github.com/sagernet/sing-tun@v0.3.0-beta.5/stack_gvisor_err.go (about) 1 //go:build with_gvisor 2 3 package tun 4 5 import ( 6 "net" 7 8 "github.com/sagernet/gvisor/pkg/tcpip" 9 "github.com/sagernet/gvisor/pkg/tcpip/adapters/gonet" 10 E "github.com/sagernet/sing/common/exceptions" 11 ) 12 13 type gTCPConn struct { 14 *gonet.TCPConn 15 } 16 17 func (c *gTCPConn) Upstream() any { 18 return c.TCPConn 19 } 20 21 func (c *gTCPConn) Write(b []byte) (n int, err error) { 22 n, err = c.TCPConn.Write(b) 23 if err == nil { 24 return 25 } 26 err = wrapError(err) 27 return 28 } 29 30 func wrapStackError(err tcpip.Error) error { 31 switch err.(type) { 32 case *tcpip.ErrClosedForSend, 33 *tcpip.ErrClosedForReceive, 34 *tcpip.ErrAborted: 35 return net.ErrClosed 36 } 37 return E.New(err.String()) 38 } 39 40 func wrapError(err error) error { 41 if opErr, isOpErr := err.(*net.OpError); isOpErr { 42 switch opErr.Err.Error() { 43 case "endpoint is closed for send", 44 "endpoint is closed for receive", 45 "operation aborted": 46 return net.ErrClosed 47 } 48 } 49 return err 50 }