-
Notifications
You must be signed in to change notification settings - Fork 2
/
dialer.go
45 lines (39 loc) · 946 Bytes
/
dialer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package manager
import (
"context"
"github.com/daeuniverse/softwind/netproxy"
"golang.org/x/net/proxy"
)
// Dialer is used to create connection.
type Dialer interface {
// DialContext connects to the given address
Dial(network, addr string) (c netproxy.Conn, err error)
DialContext(ctx context.Context, network, addr string) (c netproxy.Conn, err error)
}
type DialerConverter struct {
Dialer proxy.Dialer
}
func (d *DialerConverter) DialContext(ctx context.Context, network, addr string) (c netproxy.Conn, err error) {
var done = make(chan struct{})
go func() {
c, err = d.Dialer.Dial(network, addr)
if err != nil {
return
}
select {
case <-ctx.Done():
_ = c.Close()
default:
close(done)
}
}()
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-done:
return c, err
}
}
func (d *DialerConverter) Dial(network, addr string) (c netproxy.Conn, err error) {
return d.Dialer.Dial(network, addr)
}