-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.go
More file actions
59 lines (47 loc) · 1.51 KB
/
Copy pathproxy.go
File metadata and controls
59 lines (47 loc) · 1.51 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package singproxy
import (
"context"
"net"
"time"
)
// Proxy is a parsed proxy. It can dial connections to arbitrary targets
// through the remote proxy server, and must be closed when no longer
// needed to release the underlying transport resources.
type Proxy interface {
String() string
Addr() net.IP
DialContext(ctx context.Context, network string, addr string) (net.Conn, error)
DialContextAddr(ctx context.Context, network string, addr *net.TCPAddr) (net.Conn, error)
// Close releases all resources held by the proxy. It is safe to call
// multiple times. Proxies that never allocate transport resources
// (for example direct connections) may implement a no-op Close.
Close() error
}
type directProxy struct {
timeout time.Duration
}
func newDirectProxy(cfg Config) *directProxy {
return &directProxy{timeout: cfg.withDefaults().DirectTimeout}
}
func (d directProxy) String() string {
return "direct"
}
func (d directProxy) Addr() net.IP {
return net.IPv4zero
}
func (directProxy) Close() error {
return nil
}
func (d directProxy) dialer() *net.Dialer {
return &net.Dialer{
Timeout: d.timeout,
KeepAlive: d.timeout,
}
}
func (d directProxy) DialContext(ctx context.Context, network string, addr string) (net.Conn, error) {
return d.dialer().DialContext(ctx, network, addr)
}
func (d directProxy) DialContextAddr(ctx context.Context, network string, addr *net.TCPAddr) (net.Conn, error) {
return d.dialer().DialContext(ctx, network, addr.String())
}
var Direct Proxy = newDirectProxy(DefaultConfig())