Commit b89c0bb5 authored by aliiohs's avatar aliiohs

add tls support

parent 4965ba8b
......@@ -69,6 +69,7 @@ type client struct {
newSession NewSessionCallback
ssMap map[Session]struct{}
sslConfig *tls.Config
sync.Once
done chan struct{}
......@@ -152,7 +153,12 @@ func (c *client) dialTCP() Session {
if c.IsClosed() {
return nil
}
conn, err = net.DialTimeout("tcp", c.addr, connectTimeout)
if c.sslConfig != nil {
d := &net.Dialer{Timeout: connectTimeout}
conn, err = tls.DialWithDialer(d, "tcp", c.addr, c.sslConfig)
} else {
conn, err = net.DialTimeout("tcp", c.addr, connectTimeout)
}
if err == nil && gxnet.IsSameAddr(conn.RemoteAddr(), conn.LocalAddr()) {
conn.Close()
err = errSelfConnect
......
......@@ -56,7 +56,7 @@ type server struct {
lock sync.Mutex // for server
endPointType EndPointType
server *http.Server // for ws or wss server
sslConfig *tls.Config
sync.Once
done chan struct{}
wg sync.WaitGroup
......@@ -80,7 +80,7 @@ func newServer(t EndPointType, opts ...ServerOption) *server {
return s
}
// NewTCServer builds a tcp server.
// NewTCPServer builds a tcp server.
func NewTCPServer(opts ...ServerOption) Server {
return newServer(TCP_SERVER, opts...)
}
......@@ -175,7 +175,11 @@ func (s *server) listenTCP() error {
return perrors.Wrapf(err, "gxnet.ListenOnTCPRandomPort(addr:%s)", s.addr)
}
} else {
streamListener, err = net.Listen("tcp", s.addr)
if s.sslConfig != nil {
streamListener, err = tls.Listen("tcp", s.addr, s.sslConfig)
} else {
streamListener, err = net.Listen("tcp", s.addr)
}
if err != nil {
return perrors.Wrapf(err, "net.Listen(tcp, addr:%s)", s.addr)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment