-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathnet.go
More file actions
164 lines (145 loc) · 3.59 KB
/
net.go
File metadata and controls
164 lines (145 loc) · 3.59 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package connect
import (
"context"
"crypto/tls"
"fmt"
"net"
"time"
"golang.org/x/net/proxy"
"github.com/urnetwork/glog"
)
type DialContextFunction = func(ctx context.Context, network string, addr string) (net.Conn, error)
type DialTlsContextFunction = func(ctx context.Context, network string, addr string) (net.Conn, error)
func DefaultConnectSettings() *ConnectSettings {
tlsConfig, err := DefaultTlsConfig()
if err != nil {
panic(err)
}
return &ConnectSettings{
RequestTimeout: 15 * time.Second,
ConnectTimeout: 15 * time.Second,
TlsTimeout: 15 * time.Second,
HandshakeTimeout: 5 * time.Second,
IdleConnTimeout: 90 * time.Second,
KeepAliveTimeout: 5 * time.Second,
KeepAliveConfig: net.KeepAliveConfig{
Enable: true,
Idle: 5 * time.Second,
Interval: 5 * time.Second,
Count: 1,
},
TlsConfig: tlsConfig,
}
}
type ConnectSettings struct {
RequestTimeout time.Duration
ConnectTimeout time.Duration
TlsTimeout time.Duration
HandshakeTimeout time.Duration
IdleConnTimeout time.Duration
KeepAliveTimeout time.Duration
KeepAliveConfig net.KeepAliveConfig
TlsConfig *tls.Config
ProxySettings *ProxySettings
Resolver *net.Resolver
DialContextSettings *DialContextSettings
DisableIpv4 bool
DisableIpv6 bool
}
type DialContextSettings struct {
DialContext DialContextFunction
}
func (self *ConnectSettings) DialContext(ctx context.Context, network string, addr string) (net.Conn, error) {
if self.DisableIpv4 && self.DisableIpv6 {
return nil, fmt.Errorf("ipv4 and ipv6 are both disabled")
}
switch network {
case "tcp":
if self.DisableIpv4 {
network = "tcp6"
} else if self.DisableIpv6 {
network = "tcp4"
}
case "tcp4":
if self.DisableIpv4 {
return nil, fmt.Errorf("ipv4 is disabled")
}
case "tcp6":
if self.DisableIpv6 {
return nil, fmt.Errorf("ipv6 is disabled")
}
case "udp":
if self.DisableIpv4 {
network = "udp6"
} else if self.DisableIpv6 {
network = "udp4"
}
case "udp4":
if self.DisableIpv4 {
return nil, fmt.Errorf("ipv4 is disabled")
}
case "udp6":
if self.DisableIpv6 {
return nil, fmt.Errorf("ipv6 is disabled")
}
}
var dialContext DialContextFunction
if self.DialContextSettings != nil {
dialContext = self.DialContextSettings.DialContext
} else {
netDialer := self.NetDialer()
if self.ProxySettings != nil {
dialContext = self.ProxySettings.NewDialContext(
ctx,
netDialer,
)
} else {
dialContext = netDialer.DialContext
}
}
conn, err := dialContext(ctx, network, addr)
if glog.V(2) {
if err == nil {
glog.Infof("[net]dial %s %s success\n", network, addr)
} else {
glog.Infof("[net]dial %s %s err=%s\n", network, addr, err)
}
}
return conn, err
}
func (self *ConnectSettings) NetDialer() *net.Dialer {
return &net.Dialer{
Timeout: self.ConnectTimeout,
KeepAlive: self.KeepAliveTimeout,
KeepAliveConfig: self.KeepAliveConfig,
Resolver: self.Resolver,
}
}
type ProxySettings struct {
Network string
Address string
Auth *proxy.Auth
}
func (self *ProxySettings) NewDialContext(ctx context.Context, forward proxy.Dialer) DialContextFunction {
return func(ctx context.Context, network string, addr string) (net.Conn, error) {
proxyDialer, err := proxy.SOCKS5(
self.Network,
self.Address,
self.Auth,
forward,
)
if err != nil {
return nil, err
}
var conn net.Conn
if v, ok := proxyDialer.(proxy.ContextDialer); ok {
conn, err = v.DialContext(ctx, network, addr)
} else {
conn, err = proxyDialer.Dial(network, addr)
}
if err != nil {
return nil, err
}
return conn, nil
}
}