Commit 169328c4 authored by aliiohs's avatar aliiohs

add tls test case

parent a153e2e0
......@@ -125,7 +125,7 @@ func NewWSSClient(opts ...ClientOption) Client {
c := newClient(WSS_CLIENT, opts...)
if c.cert == "" {
panic(fmt.Sprintf("@cert:%s", c.cert))
panic(fmt.Sprintf("@certs:%s", c.cert))
}
if !strings.HasPrefix(c.addr, "wss://") {
panic(fmt.Sprintf("the prefix @serverAddr:%s is not wss://", c.addr))
......@@ -284,7 +284,7 @@ func (c *client) dialWSS() Session {
if c.cert != "" {
certPEMBlock, err := ioutil.ReadFile(c.cert)
if err != nil {
panic(fmt.Sprintf("ioutil.ReadFile(cert:%s) = error:%+v", c.cert, perrors.WithStack(err)))
panic(fmt.Sprintf("ioutil.ReadFile(certs:%s) = error:%+v", c.cert, perrors.WithStack(err)))
}
var cert tls.Certificate
......@@ -306,7 +306,7 @@ func (c *client) dialWSS() Session {
for _, c := range config.Certificates {
roots, err = x509.ParseCertificates(c.Certificate[len(c.Certificate)-1])
if err != nil {
panic(fmt.Sprintf("error parsing server's root cert: %+v\n", perrors.WithStack(err)))
panic(fmt.Sprintf("error parsing server's root certs: %+v\n", perrors.WithStack(err)))
}
for _, root = range roots {
certPool.AddCert(root)
......
-----BEGIN PRIVATE KEY-----
MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAMBA3wVeTGHZR1Ry
e/i+J8a2cu5gXwFV6TnObzGM7bLFCO5i9v4mLo4iFzPsHmWDUxKS3Y8iXbu0eYBl
LoNY0lSvxDx33O+DuwMmVN+DzSD+Eod9zfvwOWHsazYCZT2PhNxnVWIuJXViY4JA
HUGodjx+QAi6yCAurUZGvYXGgZSBAgMBAAECgYAxRi8i9BlFlufGSBVoGmydbJOm
bwLKl9dP3o33ODSP9hok5y6A0w5plWk3AJSF1hPLleK9VcSKYGYnt0clmPVHF35g
bx2rVK8dOT0mn7rz9Zr70jcSz1ETA2QonHZ+Y+niLmcic9At6hRtWiewblUmyFQm
GwggIzi7L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q8OOHD2rQy4H
s8BXaVIzw4YcaCGAMS0iDX052Sy7e2JhP8Noxvo=
-----END CERTIFICATE-----
-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANOmffupIGC8YDau
rOF4eKnHwPszgpkkhWzKsVxhNDBxCVYx4TEjG0XWIO0iyRXupZbUC+7N/8HnEVNa
8F1jYhng14Iiq99cNQbbnuHHhIztmpocrJTxmnhGzoAnRa1Tb+GnAuRoIHRA/V2c
VUE9tbikQugFx/SPgXAw6tfWB+YvAgMBAAECgYEAoEq9qzUBgoHoVEGiSPiWWe8g
5p6yUA1qx2QTQyWTAwT4z0DjjfVKmG99bFsl8+hTnJFnoCp/gnjflEOROwkjp5kG
m0drqOPx1jeipJjpXYTBu49h+WpZ1PF+KhVtxsIm3OOCvh67iWaKyyOVb5Og8aiR
jl6dn/TdG/dlGD8AfUECQQDuNMle6p0oU8amC6O9wIMBroxx2nFstzE6O35PLEzG
/tj0kxxn9Jp2TS9mGaLCzSuXmpjlF4+NOWiBPkrLC2TfAkEA43Xg7uEUkaJAz2/W
m1lIBTLt+4rIQY/2emh33bDcA+rv8rwwrMMIv17/xPx7bs49YqGG5xufD+Rwl6TL
qFXYsQJAPrOwagax1aKvwJeBw3oAQhoTKAkLIEXcdGqipe6QSzVcIIz0xjxxyEAr
AOIwoLxnBCISqwMXq2H4K0UdZPMb2wJAdhdYLY1L6YRMk6XjzImg25oidisKZweA
FvMv8DgHMj2CUAqmVrt3SivfLH1M9C09L3zfFhOAFHcsgX58gav4MQJBANSBnrHj
tIq4l8z79CPUIuu3QyeEh+XwY8s5qE5CNTck0U59lzp9NvENHbkx3KO896TTerko
+8bXHMLkJkHPXms=
-----END PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
MIIC8zCCAlygAwIBAgIBCzANBgkqhkiG9w0BAQsFADBWMQswCQYDVQQGEwJBVTET
MBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQ
dHkgTHRkMQ8wDQYDVQQDEwZ0ZXN0Y2EwHhcNMTUxMTEwMDExNDU1WhcNMjUxMTA3
MDExNDU1WjBkMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8G
A1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMR0wGwYDVQQDDBQqLnRlc3Qu
Z29vZ2xlLmNvbS5hdTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA06Z9+6kg
YLxgNq6s4Xh4qcfA+zOCmSSFbMqxXGE0MHEJVjHhMSMbRdYg7SLJFe6lltQL7s3/
wecRU1rwXWNiGeDXgiKr31w1Btue4ceEjO2amhyslPGaeEbOgCdFrVNv4acC5Ggg
dED9XZxVQT21uKRC6AXH9I+BcDDq19YH5i8CAwEAAaOBwjCBvzAJBgNVHRMEAjAA
MAsGA1UdDwQEAwIF4DAdBgNVHQ4EFgQUbyZIbUvqmePzv40xa0mMaDxLToYwcAYD
VR0jBGkwZ6FapFgwVjELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUx
ITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEPMA0GA1UEAxMGdGVz
dGNhggkAkcYZHh0aKgcwCQYDVR0RBAIwADAJBgNVHRIEAjAAMA0GCSqGSIb3DQEB
CwUAA4GBAJ21MwMf4WwAjafPKn+8Ng7ordtdp6tlkjt+Xub4l4zMr6FCp6dc/Ceh
6Hj43zYcKpAe5I6eaVcMc9qcYfUb9i4NVX82dMQpAwpNHgqTzqYt6GYEjF3YhKA7
uOFdA0OvOFJa14SNdNRk9E1Cd/tElXnLnSE4DOguMNvXz8mRKfnD
-----END CERTIFICATE-----
......@@ -19,6 +19,7 @@ package main
import (
"flag"
"path/filepath"
)
import (
......@@ -61,9 +62,12 @@ func main() {
gxsync.WithTaskPoolTaskPoolSize(*taskPoolSize),
)
}
keyPath, _ := filepath.Abs("./demo/hello/tls/certs/ca.key")
caPemPath, _ := filepath.Abs("./demo/hello/tls/certs/ca.pem")
config := &getty.ClientTlsConfigBuilder{
ClientTrustCertCollectionPath: `E:\Projects\openSource\dubbo-samples\java\dubbo-samples-ssl\dubbo-samples-ssl-consumer\src\main\resources\certs\ca.pem`,
ClientPrivateKeyPath: `E:\Projects\openSource\dubbo-samples\java\dubbo-samples-ssl\dubbo-samples-ssl-provider\src\main\resources\certs\ca.key`,
ClientTrustCertCollectionPath: caPemPath,
ClientPrivateKeyPath: keyPath,
}
client := getty.NewTCPClient(
getty.WithServerAddress(*ip+":8090"),
......
......@@ -20,6 +20,7 @@ package main
import (
"flag"
tls "github.com/dubbogo/getty/demo/hello/tls"
"path/filepath"
)
import (
......@@ -50,10 +51,14 @@ func main() {
util.SetLimit()
util.Profiling(*pprofPort)
serverPemPath, _ := filepath.Abs("./demo/hello/tls/certs/server0.pem")
serverKeyPath, _ := filepath.Abs("./demo/hello/tls/certs/server0.key")
caPemPath, _ := filepath.Abs("./demo/hello/tls/certs/ca.pem")
c := &getty.ServerTlsConfigBuilder{
ServerKeyCertChainPath: `E:\Projects\openSource\dubbo-samples\java\dubbo-samples-ssl\dubbo-samples-ssl-provider\src\main\resources\certs\server0.pem`,
ServerPrivateKeyPath: `E:\Projects\openSource\dubbo-samples\java\dubbo-samples-ssl\dubbo-samples-ssl-provider\src\main\resources\certs\server0.key`,
ServerTrustCertCollectionPath: `E:\Projects\openSource\dubbo-samples\java\dubbo-samples-ssl\dubbo-samples-ssl-consumer\src\main\resources\certs\ca.pem`,
ServerKeyCertChainPath: serverPemPath,
ServerPrivateKeyPath: serverKeyPath,
ServerTrustCertCollectionPath: caPemPath,
}
options := []getty.ServerOption{getty.WithLocalAddress(":8090"),
......
......@@ -49,7 +49,7 @@ func WithWebsocketServerPath(path string) ServerOption {
}
}
// @cert: server certificate file
// @certs: server certificate file
func WithWebsocketServerCert(cert string) ServerOption {
return func(o *ServerOptions) {
o.cert = cert
......@@ -63,7 +63,7 @@ func WithWebsocketServerPrivateKey(key string) ServerOption {
}
}
// @cert is the root certificate file to verify the legitimacy of server
// @certs is the root certificate file to verify the legitimacy of server
func WithWebsocketServerRootCert(cert string) ServerOption {
return func(o *ServerOptions) {
o.caCert = cert
......@@ -99,7 +99,7 @@ type ClientOptions struct {
sslEnabled bool
tlsConfigBuilder TlsConfigBuilder
// the cert file of wss server which may contain server domain, server ip, the starting effective date, effective
// the certs 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.
// wss client will use it.
cert string
......@@ -130,7 +130,7 @@ func WithConnectionNumber(num int) ClientOption {
}
}
// @cert is client certificate file. it can be empty.
// @certs is client certificate file. it can be empty.
func WithRootCertificateFile(cert string) ClientOption {
return func(o *ClientOptions) {
o.cert = cert
......
......@@ -99,7 +99,7 @@ func NewWSSServer(opts ...ServerOption) Server {
s := newServer(WSS_SERVER, opts...)
if s.addr == "" || s.cert == "" || s.privateKey == "" {
panic(fmt.Sprintf("@addr:%s, @cert:%s, @privateKey:%s, @caCert:%s",
panic(fmt.Sprintf("@addr:%s, @certs:%s, @privateKey:%s, @caCert:%s",
s.addr, s.cert, s.privateKey, s.caCert))
}
......@@ -414,12 +414,12 @@ func (s *server) runWSSEventLoop(newSession NewSessionCallback) {
defer s.wg.Done()
if certificate, err = tls.LoadX509KeyPair(s.cert, s.privateKey); err != nil {
panic(fmt.Sprintf("tls.LoadX509KeyPair(cert{%s}, privateKey{%s}) = err:%+v",
panic(fmt.Sprintf("tls.LoadX509KeyPair(certs{%s}, privateKey{%s}) = err:%+v",
s.cert, s.privateKey, perrors.WithStack(err)))
return
}
config = &tls.Config{
InsecureSkipVerify: true, // do not verify peer cert
InsecureSkipVerify: true, // do not verify peer certs
ClientAuth: tls.NoClientCert,
NextProtos: []string{"http/1.1"},
Certificates: []tls.Certificate{certificate},
......
......@@ -18,6 +18,7 @@
package getty
import (
"path/filepath"
"testing"
"time"
)
......@@ -80,14 +81,22 @@ func testTCPTlsServer(t *testing.T, address string) {
server *server
serverMsgHandler MessageHandler
)
serverPemPath, _ := filepath.Abs("./demo/hello/tls/certs/server0.pem")
serverKeyPath, _ := filepath.Abs("./demo/hello/tls/certs/server0.key")
caPemPath, _ := filepath.Abs("./demo/hello/tls/certs/ca.pem")
configBuilder := &ServerTlsConfigBuilder{
ServerKeyCertChainPath: serverPemPath,
ServerPrivateKeyPath: serverKeyPath,
ServerTrustCertCollectionPath: caPemPath,
}
func() {
server = newServer(
TCP_SERVER,
WithLocalAddress(address),
WithServerSslEnabled(true),
WithServerKeyCertChainPath(`E:\Projects\openSource\dubbo-samples\java\dubbo-samples-ssl\dubbo-samples-ssl-provider\src\main\resources\certs\server0.pem`),
WithServerPrivateKeyPath(`E:\Projects\openSource\dubbo-samples\java\dubbo-samples-ssl\dubbo-samples-ssl-provider\src\main\resources\certs\server0.key`),
WithServerTlsConfigBuilder(configBuilder),
)
newServerSession := func(session Session) error {
return newSessionCallback(session, &serverMsgHandler)
......@@ -101,10 +110,19 @@ func testTCPTlsServer(t *testing.T, address string) {
addr := server.streamListener.Addr().String()
t.Logf("@address:%s, tcp server addr: %v", address, addr)
keyPath, _ := filepath.Abs("./demo/hello/tls/certs/ca.key")
clientCaPemPath, _ := filepath.Abs("./demo/hello/tls/certs/ca.pem")
clientConfig := &ClientTlsConfigBuilder{
ClientTrustCertCollectionPath: clientCaPemPath,
ClientPrivateKeyPath: keyPath,
}
clt := newClient(TCP_CLIENT,
WithServerAddress(addr),
WithReconnectInterval(5e8),
WithConnectionNumber(1),
WithClientTlsConfigBuilder(clientConfig),
)
assert.NotNil(t, clt)
assert.True(t, clt.ID() > 0)
......@@ -155,16 +173,16 @@ func testUDPServer(t *testing.T, address string) {
func TestServer(t *testing.T) {
var addr string
//testTCPServer(t, addr)
//testUDPServer(t, addr)
//
//addr = "127.0.0.1:0"
//testTCPServer(t, addr)
//testUDPServer(t, addr)
//
//addr = "127.0.0.1"
//testTCPServer(t, addr)
//testUDPServer(t, addr)
testTCPServer(t, addr)
testUDPServer(t, addr)
addr = "127.0.0.1:0"
testTCPServer(t, addr)
testUDPServer(t, addr)
addr = "127.0.0.1"
testTCPServer(t, addr)
testUDPServer(t, addr)
addr = "127.0.0.9999"
testTCPTlsServer(t, addr)
}
......@@ -47,12 +47,12 @@ func (s *ServerTlsConfigBuilder) BuildTlsConfig() (*tls.Config, error) {
config *tls.Config
)
if certificate, err = tls.LoadX509KeyPair(s.ServerKeyCertChainPath, s.ServerPrivateKeyPath); err != nil {
log.Error(fmt.Sprintf("tls.LoadX509KeyPair(cert{%s}, privateKey{%s}) = err:%+v",
log.Error(fmt.Sprintf("tls.LoadX509KeyPair(certs{%s}, privateKey{%s}) = err:%+v",
s.ServerKeyCertChainPath, s.ServerPrivateKeyPath, perrors.WithStack(err)))
return nil, err
}
config = &tls.Config{
InsecureSkipVerify: true, // do not verify peer cert
InsecureSkipVerify: true, // do not verify peer certs
ClientAuth: tls.RequireAnyClientCert,
Certificates: []tls.Certificate{certificate},
}
......
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