11package network
22
33import (
4+ "net"
5+
46 "github.com/sagernet/sing/common"
57 E "github.com/sagernet/sing/common/exceptions"
68)
@@ -13,17 +15,60 @@ type HandshakeSuccess interface {
1315 HandshakeSuccess () error
1416}
1517
16- func ReportHandshakeFailure (conn any , err error ) error {
17- if handshakeConn , isHandshakeConn := common.Cast [HandshakeFailure ](conn ); isHandshakeConn {
18+ type ConnHandshakeSuccess interface {
19+ ConnHandshakeSuccess (conn net.Conn ) error
20+ }
21+
22+ type PacketConnHandshakeSuccess interface {
23+ PacketConnHandshakeSuccess (conn net.PacketConn ) error
24+ }
25+
26+ func ReportHandshakeFailure (reporter any , err error ) error {
27+ if handshakeConn , isHandshakeConn := common.Cast [HandshakeFailure ](reporter ); isHandshakeConn {
1828 return E .Append (err , handshakeConn .HandshakeFailure (err ), func (err error ) error {
1929 return E .Cause (err , "write handshake failure" )
2030 })
2131 }
2232 return err
2333}
2434
25- func ReportHandshakeSuccess (conn any ) error {
26- if handshakeConn , isHandshakeConn := common.Cast [HandshakeSuccess ](conn ); isHandshakeConn {
35+ func CloseOnHandshakeFailure (reporter any , onClose CloseHandler , err error ) error {
36+ if handshakeConn , isHandshakeConn := common.Cast [HandshakeFailure ](reporter ); isHandshakeConn {
37+ err = E .Append (err , handshakeConn .HandshakeFailure (err ), func (err error ) error {
38+ return E .Cause (err , "write handshake failure" )
39+ })
40+ } else {
41+ common .Close (reporter )
42+ }
43+ if onClose != nil {
44+ onClose (err )
45+ }
46+ return err
47+ }
48+
49+ // Deprecated: use ReportConnHandshakeSuccess/ReportPacketConnHandshakeSuccess instead
50+ func ReportHandshakeSuccess (reporter any ) error {
51+ if handshakeConn , isHandshakeConn := common.Cast [HandshakeSuccess ](reporter ); isHandshakeConn {
52+ return handshakeConn .HandshakeSuccess ()
53+ }
54+ return nil
55+ }
56+
57+ func ReportConnHandshakeSuccess (reporter any , conn net.Conn ) error {
58+ if handshakeConn , isHandshakeConn := common.Cast [ConnHandshakeSuccess ](reporter ); isHandshakeConn {
59+ return handshakeConn .ConnHandshakeSuccess (conn )
60+ }
61+ if handshakeConn , isHandshakeConn := common.Cast [HandshakeSuccess ](reporter ); isHandshakeConn {
62+ return handshakeConn .HandshakeSuccess ()
63+ }
64+ return nil
65+ }
66+
67+ func ReportPacketConnHandshakeSuccess (reporter any , conn net.PacketConn ) error {
68+ if handshakeConn , isHandshakeConn := common.Cast [PacketConnHandshakeSuccess ](reporter ); isHandshakeConn {
69+ return handshakeConn .PacketConnHandshakeSuccess (conn )
70+ }
71+ if handshakeConn , isHandshakeConn := common.Cast [HandshakeSuccess ](reporter ); isHandshakeConn {
2772 return handshakeConn .HandshakeSuccess ()
2873 }
2974 return nil
0 commit comments