Commit 03f1fd31 authored by AlexStocks's avatar AlexStocks

client just use cert file when build wss connection

parent 92f8562c
...@@ -11,11 +11,17 @@ ...@@ -11,11 +11,17 @@
## develop history ## ## develop history ##
--- ---
- 2017/04/27
> bug fix
* 1 client connect wss server just using the cert file.
> version: 0.7.03
- 2017/04/21 - 2017/04/21
> bug fix > bug fix
* 1 client can not connect wss server because of getty does not verify whether cert&key is nil or not in client.go:dialWSS * 1 client can not connect wss server because of getty does not verify whether cert&key is nil or not in client.go:dialWSS
> version: 0.7.02 > version: 0.7.02
- 2017/02/08 - 2017/02/08
> improvement > improvement
......
...@@ -21,7 +21,7 @@ import ( ...@@ -21,7 +21,7 @@ import (
) )
import ( import (
"github.com/AlexStocks/goext/log" "encoding/pem"
"github.com/AlexStocks/goext/sync" "github.com/AlexStocks/goext/sync"
log "github.com/AlexStocks/log4go" log "github.com/AlexStocks/log4go"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
...@@ -51,9 +51,7 @@ type Client struct { ...@@ -51,9 +51,7 @@ type Client struct {
wg sync.WaitGroup wg sync.WaitGroup
// for wss client // for wss client
cert string // 客户端的证书 cert string // 服务端的证书文件(包含了公钥以及服务端其他一些验证信息:服务端域名、服务端ip、起始有效日期、有效时长、hash算法、秘钥长度等)
privateKey string // 客户端的私钥(包含了它的public key)
caCert string // 用于验证服务端的合法性
} }
// NewClient function builds a tcp & ws client. // NewClient function builds a tcp & ws client.
...@@ -89,8 +87,6 @@ func NewWSSClient( ...@@ -89,8 +87,6 @@ func NewWSSClient(
connInterval time.Duration, connInterval time.Duration,
serverAddr string, serverAddr string,
cert string, cert string,
privateKey string,
caCert string,
) *Client { ) *Client {
if connNum < 0 { if connNum < 0 {
...@@ -101,14 +97,12 @@ func NewWSSClient( ...@@ -101,14 +97,12 @@ func NewWSSClient(
} }
return &Client{ return &Client{
number: connNum, number: connNum,
interval: connInterval, interval: connInterval,
addr: serverAddr, addr: serverAddr,
ssMap: make(map[Session]gxsync.Empty, connNum), ssMap: make(map[Session]gxsync.Empty, connNum),
done: make(chan gxsync.Empty), done: make(chan gxsync.Empty),
caCert: caCert, cert: cert,
cert: cert,
privateKey: privateKey,
} }
} }
...@@ -172,7 +166,6 @@ func (c *Client) dialWS() Session { ...@@ -172,7 +166,6 @@ func (c *Client) dialWS() Session {
func (c *Client) dialWSS() Session { func (c *Client) dialWSS() Session {
var ( var (
err error err error
certPem []byte
root *x509.Certificate root *x509.Certificate
roots []*x509.Certificate roots []*x509.Certificate
certPool *x509.CertPool certPool *x509.CertPool
...@@ -188,11 +181,25 @@ func (c *Client) dialWSS() Session { ...@@ -188,11 +181,25 @@ func (c *Client) dialWSS() Session {
InsecureSkipVerify: true, InsecureSkipVerify: true,
} }
if c.cert != "" && c.privateKey != "" { if c.cert != "" {
config.Certificates = make([]tls.Certificate, 1) certPEMBlock, err := ioutil.ReadFile(c.cert)
if config.Certificates[0], err = tls.LoadX509KeyPair(c.cert, c.privateKey); err != nil { if err != nil {
panic(fmt.Sprintf("tls.LoadX509KeyPair(cert{%s}, privateKey{%s}) = err{%#v}", c.cert, c.privateKey, err)) panic(fmt.Sprintf("ioutil.ReadFile(cert:%s) = error{%#v}", c.cert, err))
} }
var cert tls.Certificate
for {
var certDERBlock *pem.Block
certDERBlock, certPEMBlock = pem.Decode(certPEMBlock)
if certDERBlock == nil {
break
}
if certDERBlock.Type == "CERTIFICATE" {
cert.Certificate = append(cert.Certificate, certDERBlock.Bytes)
}
}
config.Certificates = make([]tls.Certificate, 1)
config.Certificates[0] = cert
} }
certPool = x509.NewCertPool() certPool = x509.NewCertPool()
...@@ -205,18 +212,7 @@ func (c *Client) dialWSS() Session { ...@@ -205,18 +212,7 @@ func (c *Client) dialWSS() Session {
certPool.AddCert(root) certPool.AddCert(root)
} }
} }
config.InsecureSkipVerify = true
gxlog.CInfo("client cert:%s, key:%s, caCert:%s", c.cert, c.privateKey, c.caCert)
if c.caCert != "" {
certPem, err = ioutil.ReadFile(c.caCert)
if err != nil {
panic(fmt.Errorf("ioutil.ReadFile(caCert{%s}) = err{%#v}", c.caCert, err))
}
if ok := certPool.AppendCertsFromPEM(certPem); !ok {
panic("failed to parse root certificate file.")
}
config.InsecureSkipVerify = false
}
config.RootCAs = certPool config.RootCAs = certPool
// dialer.EnableCompression = true // dialer.EnableCompression = true
......
...@@ -12,6 +12,7 @@ package getty ...@@ -12,6 +12,7 @@ package getty
import ( import (
// "errors" // "errors"
"compress/flate" "compress/flate"
"crypto/tls"
"fmt" "fmt"
"io" "io"
"net" "net"
...@@ -21,7 +22,6 @@ import ( ...@@ -21,7 +22,6 @@ import (
import ( import (
log "github.com/AlexStocks/log4go" log "github.com/AlexStocks/log4go"
"github.com/golang/snappy" "github.com/golang/snappy"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
...@@ -386,6 +386,11 @@ func (w *gettyWSConn) writePing() error { ...@@ -386,6 +386,11 @@ func (w *gettyWSConn) writePing() error {
// close websocket connection // close websocket connection
func (w *gettyWSConn) close(waitSec int) { func (w *gettyWSConn) close(waitSec int) {
w.conn.WriteMessage(websocket.CloseMessage, []byte("bye-bye!!!")) w.conn.WriteMessage(websocket.CloseMessage, []byte("bye-bye!!!"))
w.conn.UnderlyingConn().(*net.TCPConn).SetLinger(waitSec) conn := w.conn.UnderlyingConn()
if tcpConn, ok := conn.(*net.TCPConn); ok {
tcpConn.SetLinger(waitSec)
} else if wsConn, ok := conn.(*tls.Conn); ok {
wsConn.CloseWrite()
}
w.conn.Close() w.conn.Close()
} }
...@@ -10,9 +10,9 @@ ...@@ -10,9 +10,9 @@
package getty package getty
const ( const (
Version = "0.7.02" Version = "0.7.03"
DATE = "2017/04/21" DATE = "2017/04/27"
GETTY_MAJOR = 0 GETTY_MAJOR = 0
GETTY_MINOR = 7 GETTY_MINOR = 7
GETTY_BUILD = 2 GETTY_BUILD = 3
) )
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