Commit 94d35a38 authored by aliiohs's avatar aliiohs

add tls support

parent b89c0bb5
...@@ -153,7 +153,7 @@ func (c *client) dialTCP() Session { ...@@ -153,7 +153,7 @@ func (c *client) dialTCP() Session {
if c.IsClosed() { if c.IsClosed() {
return nil return nil
} }
if c.sslConfig != nil { if c.sslEnabled && c.sslConfig != nil {
d := &net.Dialer{Timeout: connectTimeout} d := &net.Dialer{Timeout: connectTimeout}
conn, err = tls.DialWithDialer(d, "tcp", c.addr, c.sslConfig) conn, err = tls.DialWithDialer(d, "tcp", c.addr, c.sslConfig)
} else { } else {
......
module github.com/dubbogo/getty module github.com/dubbogo/getty
go 1.14
require ( require (
github.com/dubbogo/gost v1.9.0 github.com/dubbogo/gost v1.9.0
github.com/golang/snappy v0.0.1 github.com/golang/snappy v0.0.1
...@@ -8,5 +10,3 @@ require ( ...@@ -8,5 +10,3 @@ require (
github.com/stretchr/testify v1.5.1 github.com/stretchr/testify v1.5.1
go.uber.org/zap v1.15.0 go.uber.org/zap v1.15.0
) )
go 1.13
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
package getty package getty
import "crypto/tls"
///////////////////////////////////////// /////////////////////////////////////////
// Server Options // Server Options
///////////////////////////////////////// /////////////////////////////////////////
...@@ -25,6 +27,9 @@ type ServerOption func(*ServerOptions) ...@@ -25,6 +27,9 @@ type ServerOption func(*ServerOptions)
type ServerOptions struct { type ServerOptions struct {
addr string addr string
//tls
sslEnabled bool
sslConfig *tls.Config
// websocket // websocket
path string path string
...@@ -79,6 +84,10 @@ type ClientOptions struct { ...@@ -79,6 +84,10 @@ type ClientOptions struct {
number int number int
reconnectInterval int // reConnect Interval reconnectInterval int // reConnect Interval
//tls
sslEnabled bool
sslConfig *tls.Config
// the cert file of wss server which may contain server domain, server ip, the starting effective date, effective // the cert file of wss server which may contain server domain, server ip, the starting effective date, effective
// duration, the hash alg, the len of the private key. // duration, the hash alg, the len of the private key.
// wss client will use it. // wss client will use it.
......
...@@ -56,7 +56,6 @@ type server struct { ...@@ -56,7 +56,6 @@ type server struct {
lock sync.Mutex // for server lock sync.Mutex // for server
endPointType EndPointType endPointType EndPointType
server *http.Server // for ws or wss server server *http.Server // for ws or wss server
sslConfig *tls.Config
sync.Once sync.Once
done chan struct{} done chan struct{}
wg sync.WaitGroup wg sync.WaitGroup
...@@ -175,7 +174,7 @@ func (s *server) listenTCP() error { ...@@ -175,7 +174,7 @@ func (s *server) listenTCP() error {
return perrors.Wrapf(err, "gxnet.ListenOnTCPRandomPort(addr:%s)", s.addr) return perrors.Wrapf(err, "gxnet.ListenOnTCPRandomPort(addr:%s)", s.addr)
} }
} else { } else {
if s.sslConfig != nil { if s.sslEnabled && s.sslConfig != nil {
streamListener, err = tls.Listen("tcp", s.addr, s.sslConfig) streamListener, err = tls.Listen("tcp", s.addr, s.sslConfig)
} else { } else {
streamListener, err = net.Listen("tcp", s.addr) streamListener, err = net.Listen("tcp", 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