delay close client

This commit is contained in:
keakon 2024-01-15 18:22:41 +08:00
parent 29ac82e320
commit df8d1c0161

View file

@ -77,7 +77,7 @@ func (h *Hysteria2) DialContext(ctx context.Context, metadata *C.Metadata, opts
client, err := hysteria2.NewClient(*h.clientOptions)
if err == nil {
h.client.CloseWithError(closedForPortHopping)
runtime.SetFinalizer(client, closeHysteria2Client)
h.client = client
h.addr = net.JoinHostPort(h.option.Server, strconv.Itoa(int(h.option.Port)))
log.Infoln("[%s] hopped to port %d", h.name, h.option.Port)
@ -115,6 +115,12 @@ func closeHysteria2(h *Hysteria2) {
}
}
func closeHysteria2Client(c *hysteria2.Client) {
if c != nil {
c.CloseWithError(closedForPortHopping)
}
}
func NewHysteria2(option Hysteria2Option) (*Hysteria2, error) {
var ports []uint16
if option.Ports != "" {