ing
This commit is contained in:
parent
0e01920fe9
commit
53a7e28c86
|
@ -1,6 +1,7 @@
|
||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
@ -31,20 +32,30 @@ func NewSocket(sb SocketBuilder, parentContext cuc.Context) (Socket, error) {
|
||||||
}
|
}
|
||||||
sh.Validate()
|
sh.Validate()
|
||||||
|
|
||||||
d := &net.Dialer{}
|
|
||||||
d.Timeout = sb.GetTimeout()
|
|
||||||
d.KeepAlive = sb.GetKeepAlive()
|
|
||||||
d.LocalAddr = sb.GetLocalAddress()
|
|
||||||
|
|
||||||
network := sb.GetNetwork()
|
network := sb.GetNetwork()
|
||||||
address := sb.GetAddress()
|
address := sb.GetAddress()
|
||||||
|
|
||||||
conn, err := sb.Dial(d, network, address)
|
conn, err := sb.Dial(network, address)
|
||||||
|
|
||||||
if nil != err {
|
if nil != err {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tlsConfig := sb.GetTLSConfig()
|
||||||
|
if nil != tlsConfig {
|
||||||
|
cfg := tlsConfig.Clone()
|
||||||
|
tlsConn := tls.Client(conn, cfg)
|
||||||
|
if err := tlsConn.Handshake(); err != nil {
|
||||||
|
tlsConn.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !cfg.InsecureSkipVerify {
|
||||||
|
if err := tlsConn.VerifyHostname(cfg.ServerName); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
conn = tlsConn
|
||||||
|
}
|
||||||
|
|
||||||
sh.OnConnect(sc, conn)
|
sh.OnConnect(sc, conn)
|
||||||
|
|
||||||
s := retainSocket()
|
s := retainSocket()
|
||||||
|
|
|
@ -12,14 +12,14 @@ type SocketBuilder interface {
|
||||||
SocketContext(parent cuc.Context) SocketContext
|
SocketContext(parent cuc.Context) SocketContext
|
||||||
SocketHandler() SocketHandler
|
SocketHandler() SocketHandler
|
||||||
|
|
||||||
Dial(dialer *net.Dialer, network, address string) (net.Conn, error)
|
Dial(network, address string) (net.Conn, error)
|
||||||
|
|
||||||
GetNetwork() string
|
GetNetwork() string
|
||||||
GetAddress() string
|
GetAddress() string
|
||||||
GetTLSConfig() *tls.Config
|
GetTLSConfig() *tls.Config
|
||||||
|
|
||||||
|
GetHandshakeTimeout() time.Duration
|
||||||
GetKeepAlive() time.Duration
|
GetKeepAlive() time.Duration
|
||||||
GetTimeout() time.Duration
|
|
||||||
GetLocalAddress() net.Addr
|
GetLocalAddress() net.Addr
|
||||||
|
|
||||||
// Validate is check handler value
|
// Validate is check handler value
|
||||||
|
|
|
@ -15,8 +15,8 @@ type SocketBuilders struct {
|
||||||
Address string
|
Address string
|
||||||
TLSConfig *tls.Config
|
TLSConfig *tls.Config
|
||||||
|
|
||||||
|
HandshakeTimeout time.Duration
|
||||||
KeepAlive time.Duration
|
KeepAlive time.Duration
|
||||||
Timeout time.Duration
|
|
||||||
|
|
||||||
LocalAddress net.Addr
|
LocalAddress net.Addr
|
||||||
}
|
}
|
||||||
|
@ -41,20 +41,27 @@ func (sb *SocketBuilders) GetTLSConfig() *tls.Config {
|
||||||
return sb.TLSConfig
|
return sb.TLSConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sb *SocketBuilders) Dial(dialer *net.Dialer, network, address string) (net.Conn, error) {
|
func (sb *SocketBuilders) Dial(network, address string) (net.Conn, error) {
|
||||||
if nil == sb.TLSConfig {
|
var deadline time.Time
|
||||||
return dialer.Dial(network, address)
|
if 0 != sb.HandshakeTimeout {
|
||||||
|
deadline = time.Now().Add(sb.HandshakeTimeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
return tls.DialWithDialer(dialer, network, address, sb.TLSConfig)
|
d := &net.Dialer{
|
||||||
|
KeepAlive: sb.KeepAlive,
|
||||||
|
Deadline: deadline,
|
||||||
|
LocalAddr: sb.LocalAddress,
|
||||||
|
}
|
||||||
|
|
||||||
|
return d.Dial(network, address)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sb *SocketBuilders) GetKeepAlive() time.Duration {
|
func (sb *SocketBuilders) GetKeepAlive() time.Duration {
|
||||||
return sb.KeepAlive
|
return sb.KeepAlive
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sb *SocketBuilders) GetTimeout() time.Duration {
|
func (sb *SocketBuilders) GetHandshakeTimeout() time.Duration {
|
||||||
return sb.Timeout
|
return sb.HandshakeTimeout
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sb *SocketBuilders) GetLocalAddress() net.Addr {
|
func (sb *SocketBuilders) GetLocalAddress() net.Addr {
|
||||||
|
@ -72,8 +79,8 @@ func (sb *SocketBuilders) Validate() {
|
||||||
if 0 >= sb.KeepAlive {
|
if 0 >= sb.KeepAlive {
|
||||||
sb.KeepAlive = cs.DefaultKeepAlive
|
sb.KeepAlive = cs.DefaultKeepAlive
|
||||||
}
|
}
|
||||||
if 0 >= sb.Timeout {
|
if 0 >= sb.HandshakeTimeout {
|
||||||
sb.Timeout = cs.DefaultConnectTimeout
|
sb.HandshakeTimeout = cs.DefaultHandshakeTimeout
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ const (
|
||||||
// DefaultWriteTimeout is default value of write timeout
|
// DefaultWriteTimeout is default value of write timeout
|
||||||
DefaultWriteTimeout = 0
|
DefaultWriteTimeout = 0
|
||||||
|
|
||||||
DefaultConnectTimeout = 0
|
DefaultHandshakeTimeout = 0
|
||||||
|
|
||||||
DefaultKeepAlive = 0
|
DefaultKeepAlive = 0
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user