@@ -7,13 +7,15 @@ import (
7
7
"fmt"
8
8
"net"
9
9
"net/netip"
10
+ "runtime"
10
11
"strconv"
11
12
"time"
12
13
13
14
"github.com/metacubex/quic-go"
14
15
"github.com/metacubex/quic-go/congestion"
15
16
M "github.com/sagernet/sing/common/metadata"
16
17
18
+ CN "github.com/metacubex/mihomo/common/net"
17
19
"github.com/metacubex/mihomo/component/ca"
18
20
"github.com/metacubex/mihomo/component/dialer"
19
21
"github.com/metacubex/mihomo/component/proxydialer"
@@ -43,6 +45,8 @@ type Hysteria struct {
43
45
44
46
option * HysteriaOption
45
47
client * core.Client
48
+
49
+ closeCh chan struct {} // for test
46
50
}
47
51
48
52
func (h * Hysteria ) DialContext (ctx context.Context , metadata * C.Metadata , opts ... dialer.Option ) (C.Conn , error ) {
@@ -51,15 +55,15 @@ func (h *Hysteria) DialContext(ctx context.Context, metadata *C.Metadata, opts .
51
55
return nil , err
52
56
}
53
57
54
- return NewConn (tcpConn , h ), nil
58
+ return NewConn (CN . NewRefConn ( tcpConn , h ) , h ), nil
55
59
}
56
60
57
61
func (h * Hysteria ) ListenPacketContext (ctx context.Context , metadata * C.Metadata , opts ... dialer.Option ) (C.PacketConn , error ) {
58
62
udpConn , err := h .client .DialUDP (h .genHdc (ctx , opts ... ))
59
63
if err != nil {
60
64
return nil , err
61
65
}
62
- return newPacketConn (& hyPacketConn {udpConn }, h ), nil
66
+ return newPacketConn (CN . NewRefPacketConn ( & hyPacketConn {udpConn }, h ) , h ), nil
63
67
}
64
68
65
69
func (h * Hysteria ) genHdc (ctx context.Context , opts ... dialer.Option ) utils.PacketDialer {
@@ -218,7 +222,7 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
218
222
if err != nil {
219
223
return nil , fmt .Errorf ("hysteria %s create error: %w" , addr , err )
220
224
}
221
- return & Hysteria {
225
+ outbound := & Hysteria {
222
226
Base : & Base {
223
227
name : option .Name ,
224
228
addr : addr ,
@@ -231,7 +235,19 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
231
235
},
232
236
option : & option ,
233
237
client : client ,
234
- }, nil
238
+ }
239
+ runtime .SetFinalizer (outbound , closeHysteria )
240
+
241
+ return outbound , nil
242
+ }
243
+
244
+ func closeHysteria (h * Hysteria ) {
245
+ if h .client != nil {
246
+ _ = h .client .Close ()
247
+ }
248
+ if h .closeCh != nil {
249
+ close (h .closeCh )
250
+ }
235
251
}
236
252
237
253
type hyPacketConn struct {
0 commit comments