mirror of
https://github.com/daeuniverse/dae.git
synced 2025-07-14 17:59:57 +07:00
feat: support independent tcp4, tcp6, udp4, udp6 connectivity check
This commit is contained in:
39
common/netutils/context_dialer.go
Normal file
39
common/netutils/context_dialer.go
Normal file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (c) since 2023, mzz2017 <mzz@tuta.io>
|
||||
*/
|
||||
|
||||
package netutils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"golang.org/x/net/proxy"
|
||||
"net"
|
||||
)
|
||||
|
||||
type ContextDialer struct {
|
||||
Dialer proxy.Dialer
|
||||
}
|
||||
|
||||
func (d *ContextDialer) DialContext(ctx context.Context, network, addr string) (c net.Conn, err error) {
|
||||
var done = make(chan struct{})
|
||||
go func() {
|
||||
c, err = d.Dialer.Dial(network, addr)
|
||||
if err != nil {
|
||||
close(done)
|
||||
return
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
_ = c.Close()
|
||||
default:
|
||||
close(done)
|
||||
}
|
||||
}()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
case <-done:
|
||||
return c, err
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user