fix: hysteria dial use external context
This commit is contained in:
parent
2ba933d16a
commit
f4b9f2965f
1 changed files with 28 additions and 14 deletions
|
@ -42,23 +42,31 @@ type Hysteria struct {
|
|||
*Base
|
||||
|
||||
client *core.Client
|
||||
clientTransport *transport.ClientTransport
|
||||
}
|
||||
|
||||
func (h *Hysteria) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
||||
tcpConn, err := h.client.DialTCP(metadata.RemoteAddress(), hyDialer(func() (net.PacketConn, error) {
|
||||
hdc := hyDialerWithContext{
|
||||
ctx: ctx,
|
||||
hyDialer: func() (net.PacketConn, error) {
|
||||
return dialer.ListenPacket(ctx, "udp", "", h.Base.DialOptions(opts...)...)
|
||||
}))
|
||||
},
|
||||
}
|
||||
tcpConn, err := h.client.DialTCP(metadata.RemoteAddress(), &hdc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewConn(tcpConn, h), nil
|
||||
}
|
||||
|
||||
func (h *Hysteria) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||
udpConn, err := h.client.DialUDP(hyDialer(func() (net.PacketConn, error) {
|
||||
hdc := hyDialerWithContext{
|
||||
ctx: ctx,
|
||||
hyDialer: func() (net.PacketConn, error) {
|
||||
return dialer.ListenPacket(ctx, "udp", "", h.Base.DialOptions(opts...)...)
|
||||
}))
|
||||
},
|
||||
}
|
||||
udpConn, err := h.client.DialUDP(&hdc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -192,7 +200,6 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
|
|||
rmark: option.RoutingMark,
|
||||
},
|
||||
client: client,
|
||||
clientTransport: clientTransport,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -255,8 +262,15 @@ func (c *hyPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
type hyDialer func() (net.PacketConn, error)
|
||||
|
||||
func (h hyDialer) ListenPacket() (net.PacketConn, error) {
|
||||
return h()
|
||||
type hyDialerWithContext struct {
|
||||
hyDialer func() (net.PacketConn, error)
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func (h *hyDialerWithContext) ListenPacket() (net.PacketConn, error) {
|
||||
return h.hyDialer()
|
||||
}
|
||||
|
||||
func (h *hyDialerWithContext) Context() context.Context {
|
||||
return h.ctx
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue