Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
getty
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
getty
Commits
169328c4
Commit
169328c4
authored
Aug 02, 2020
by
aliiohs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add tls test case
parent
a153e2e0
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
155 additions
and
29 deletions
+155
-29
client.go
client.go
+3
-3
ca.key
demo/hello/tls/certs/ca.key
+16
-0
ca.pem
demo/hello/tls/certs/ca.pem
+15
-0
client.key
demo/hello/tls/certs/client.key
+16
-0
client.pem
demo/hello/tls/certs/client.pem
+18
-0
server0.key
demo/hello/tls/certs/server0.key
+16
-0
server0.pem
demo/hello/tls/certs/server0.pem
+18
-0
client.go
demo/hello/tls/client/client.go
+6
-2
server.go
demo/hello/tls/server/server.go
+8
-3
options.go
options.go
+4
-4
server.go
server.go
+3
-3
server_test.go
server_test.go
+30
-12
tls.go
tls.go
+2
-2
No files found.
client.go
View file @
169328c4
...
@@ -125,7 +125,7 @@ func NewWSSClient(opts ...ClientOption) Client {
...
@@ -125,7 +125,7 @@ func NewWSSClient(opts ...ClientOption) Client {
c
:=
newClient
(
WSS_CLIENT
,
opts
...
)
c
:=
newClient
(
WSS_CLIENT
,
opts
...
)
if
c
.
cert
==
""
{
if
c
.
cert
==
""
{
panic
(
fmt
.
Sprintf
(
"@cert:%s"
,
c
.
cert
))
panic
(
fmt
.
Sprintf
(
"@cert
s
:%s"
,
c
.
cert
))
}
}
if
!
strings
.
HasPrefix
(
c
.
addr
,
"wss://"
)
{
if
!
strings
.
HasPrefix
(
c
.
addr
,
"wss://"
)
{
panic
(
fmt
.
Sprintf
(
"the prefix @serverAddr:%s is not wss://"
,
c
.
addr
))
panic
(
fmt
.
Sprintf
(
"the prefix @serverAddr:%s is not wss://"
,
c
.
addr
))
...
@@ -284,7 +284,7 @@ func (c *client) dialWSS() Session {
...
@@ -284,7 +284,7 @@ func (c *client) dialWSS() Session {
if
c
.
cert
!=
""
{
if
c
.
cert
!=
""
{
certPEMBlock
,
err
:=
ioutil
.
ReadFile
(
c
.
cert
)
certPEMBlock
,
err
:=
ioutil
.
ReadFile
(
c
.
cert
)
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
fmt
.
Sprintf
(
"ioutil.ReadFile(cert:%s) = error:%+v"
,
c
.
cert
,
perrors
.
WithStack
(
err
)))
panic
(
fmt
.
Sprintf
(
"ioutil.ReadFile(cert
s
:%s) = error:%+v"
,
c
.
cert
,
perrors
.
WithStack
(
err
)))
}
}
var
cert
tls
.
Certificate
var
cert
tls
.
Certificate
...
@@ -306,7 +306,7 @@ func (c *client) dialWSS() Session {
...
@@ -306,7 +306,7 @@ func (c *client) dialWSS() Session {
for
_
,
c
:=
range
config
.
Certificates
{
for
_
,
c
:=
range
config
.
Certificates
{
roots
,
err
=
x509
.
ParseCertificates
(
c
.
Certificate
[
len
(
c
.
Certificate
)
-
1
])
roots
,
err
=
x509
.
ParseCertificates
(
c
.
Certificate
[
len
(
c
.
Certificate
)
-
1
])
if
err
!=
nil
{
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 cert
s
: %+v
\n
"
,
perrors
.
WithStack
(
err
)))
}
}
for
_
,
root
=
range
roots
{
for
_
,
root
=
range
roots
{
certPool
.
AddCert
(
root
)
certPool
.
AddCert
(
root
)
...
...
demo/hello/tls/certs/ca.key
0 → 100644
View file @
169328c4
-----BEG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EAjAA
MAsGA1UdDwQEAwIF4DAdBgNVHQ4EFgQUbyZIbUvqmePzv40xa0mMaDxLToYwcAYD
VR0jBGkwZ6FapFgwVjELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUx
ITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEPMA0GA1UEAxMGdGVz
dGNhggkAkcYZHh0aKgcwCQYDVR0RBAIwADAJBgNVHRIEAjAAMA0GCSqGSIb3DQEB
CwUAA4GBAJ21MwMf4WwAjafPKn+8Ng7ordtdp6tlkjt+Xub4l4zMr6FCp6dc/Ceh
6Hj43zYcKpAe5I6eaVcMc9qcYfUb9i4NVX82dMQpAwpNHgqTzqYt6GYEjF3YhKA7
uOFdA0OvOFJa14SNdNRk9E1Cd/tElXnLnSE4DOguMNvXz8mRKfnD
-----END CERTIFICATE-----
demo/hello/tls/client/client.go
View file @
169328c4
...
@@ -19,6 +19,7 @@ package main
...
@@ -19,6 +19,7 @@ package main
import
(
import
(
"flag"
"flag"
"path/filepath"
)
)
import
(
import
(
...
@@ -61,9 +62,12 @@ func main() {
...
@@ -61,9 +62,12 @@ func main() {
gxsync
.
WithTaskPoolTaskPoolSize
(
*
taskPoolSize
),
gxsync
.
WithTaskPoolTaskPoolSize
(
*
taskPoolSize
),
)
)
}
}
keyPath
,
_
:=
filepath
.
Abs
(
"./demo/hello/tls/certs/ca.key"
)
caPemPath
,
_
:=
filepath
.
Abs
(
"./demo/hello/tls/certs/ca.pem"
)
config
:=
&
getty
.
ClientTlsConfigBuilder
{
config
:=
&
getty
.
ClientTlsConfigBuilder
{
ClientTrustCertCollectionPath
:
`E:\Projects\openSource\dubbo-samples\java\dubbo-samples-ssl\dubbo-samples-ssl-consumer\src\main\resources\certs\ca.pem`
,
ClientTrustCertCollectionPath
:
caPemPath
,
ClientPrivateKeyPath
:
`E:\Projects\openSource\dubbo-samples\java\dubbo-samples-ssl\dubbo-samples-ssl-provider\src\main\resources\certs\ca.key`
,
ClientPrivateKeyPath
:
keyPath
,
}
}
client
:=
getty
.
NewTCPClient
(
client
:=
getty
.
NewTCPClient
(
getty
.
WithServerAddress
(
*
ip
+
":8090"
),
getty
.
WithServerAddress
(
*
ip
+
":8090"
),
...
...
demo/hello/tls/server/server.go
View file @
169328c4
...
@@ -20,6 +20,7 @@ package main
...
@@ -20,6 +20,7 @@ package main
import
(
import
(
"flag"
"flag"
tls
"github.com/dubbogo/getty/demo/hello/tls"
tls
"github.com/dubbogo/getty/demo/hello/tls"
"path/filepath"
)
)
import
(
import
(
...
@@ -50,10 +51,14 @@ func main() {
...
@@ -50,10 +51,14 @@ func main() {
util
.
SetLimit
()
util
.
SetLimit
()
util
.
Profiling
(
*
pprofPort
)
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
{
c
:=
&
getty
.
ServerTlsConfigBuilder
{
ServerKeyCertChainPath
:
`E:\Projects\openSource\dubbo-samples\java\dubbo-samples-ssl\dubbo-samples-ssl-provider\src\main\resources\certs\server0.pem`
,
ServerKeyCertChainPath
:
serverPemPath
,
ServerPrivateKeyPath
:
`E:\Projects\openSource\dubbo-samples\java\dubbo-samples-ssl\dubbo-samples-ssl-provider\src\main\resources\certs\server0.key`
,
ServerPrivateKeyPath
:
serverKeyPath
,
ServerTrustCertCollectionPath
:
`E:\Projects\openSource\dubbo-samples\java\dubbo-samples-ssl\dubbo-samples-ssl-consumer\src\main\resources\certs\ca.pem`
,
ServerTrustCertCollectionPath
:
caPemPath
,
}
}
options
:=
[]
getty
.
ServerOption
{
getty
.
WithLocalAddress
(
":8090"
),
options
:=
[]
getty
.
ServerOption
{
getty
.
WithLocalAddress
(
":8090"
),
...
...
options.go
View file @
169328c4
...
@@ -49,7 +49,7 @@ func WithWebsocketServerPath(path string) ServerOption {
...
@@ -49,7 +49,7 @@ func WithWebsocketServerPath(path string) ServerOption {
}
}
}
}
// @cert: server certificate file
// @cert
s
: server certificate file
func
WithWebsocketServerCert
(
cert
string
)
ServerOption
{
func
WithWebsocketServerCert
(
cert
string
)
ServerOption
{
return
func
(
o
*
ServerOptions
)
{
return
func
(
o
*
ServerOptions
)
{
o
.
cert
=
cert
o
.
cert
=
cert
...
@@ -63,7 +63,7 @@ func WithWebsocketServerPrivateKey(key string) ServerOption {
...
@@ -63,7 +63,7 @@ func WithWebsocketServerPrivateKey(key string) ServerOption {
}
}
}
}
// @cert is the root certificate file to verify the legitimacy of server
// @cert
s
is the root certificate file to verify the legitimacy of server
func
WithWebsocketServerRootCert
(
cert
string
)
ServerOption
{
func
WithWebsocketServerRootCert
(
cert
string
)
ServerOption
{
return
func
(
o
*
ServerOptions
)
{
return
func
(
o
*
ServerOptions
)
{
o
.
caCert
=
cert
o
.
caCert
=
cert
...
@@ -99,7 +99,7 @@ type ClientOptions struct {
...
@@ -99,7 +99,7 @@ type ClientOptions struct {
sslEnabled
bool
sslEnabled
bool
tlsConfigBuilder
TlsConfigBuilder
tlsConfigBuilder
TlsConfigBuilder
// the cert file of wss server which may contain server domain, server ip, the starting effective date, effective
// the cert
s
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.
cert
string
cert
string
...
@@ -130,7 +130,7 @@ func WithConnectionNumber(num int) ClientOption {
...
@@ -130,7 +130,7 @@ func WithConnectionNumber(num int) ClientOption {
}
}
}
}
// @cert is client certificate file. it can be empty.
// @cert
s
is client certificate file. it can be empty.
func
WithRootCertificateFile
(
cert
string
)
ClientOption
{
func
WithRootCertificateFile
(
cert
string
)
ClientOption
{
return
func
(
o
*
ClientOptions
)
{
return
func
(
o
*
ClientOptions
)
{
o
.
cert
=
cert
o
.
cert
=
cert
...
...
server.go
View file @
169328c4
...
@@ -99,7 +99,7 @@ func NewWSSServer(opts ...ServerOption) Server {
...
@@ -99,7 +99,7 @@ func NewWSSServer(opts ...ServerOption) Server {
s
:=
newServer
(
WSS_SERVER
,
opts
...
)
s
:=
newServer
(
WSS_SERVER
,
opts
...
)
if
s
.
addr
==
""
||
s
.
cert
==
""
||
s
.
privateKey
==
""
{
if
s
.
addr
==
""
||
s
.
cert
==
""
||
s
.
privateKey
==
""
{
panic
(
fmt
.
Sprintf
(
"@addr:%s, @cert:%s, @privateKey:%s, @caCert:%s"
,
panic
(
fmt
.
Sprintf
(
"@addr:%s, @cert
s
:%s, @privateKey:%s, @caCert:%s"
,
s
.
addr
,
s
.
cert
,
s
.
privateKey
,
s
.
caCert
))
s
.
addr
,
s
.
cert
,
s
.
privateKey
,
s
.
caCert
))
}
}
...
@@ -414,12 +414,12 @@ func (s *server) runWSSEventLoop(newSession NewSessionCallback) {
...
@@ -414,12 +414,12 @@ func (s *server) runWSSEventLoop(newSession NewSessionCallback) {
defer
s
.
wg
.
Done
()
defer
s
.
wg
.
Done
()
if
certificate
,
err
=
tls
.
LoadX509KeyPair
(
s
.
cert
,
s
.
privateKey
);
err
!=
nil
{
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(cert
s
{%s}, privateKey{%s}) = err:%+v"
,
s
.
cert
,
s
.
privateKey
,
perrors
.
WithStack
(
err
)))
s
.
cert
,
s
.
privateKey
,
perrors
.
WithStack
(
err
)))
return
return
}
}
config
=
&
tls
.
Config
{
config
=
&
tls
.
Config
{
InsecureSkipVerify
:
true
,
// do not verify peer cert
InsecureSkipVerify
:
true
,
// do not verify peer cert
s
ClientAuth
:
tls
.
NoClientCert
,
ClientAuth
:
tls
.
NoClientCert
,
NextProtos
:
[]
string
{
"http/1.1"
},
NextProtos
:
[]
string
{
"http/1.1"
},
Certificates
:
[]
tls
.
Certificate
{
certificate
},
Certificates
:
[]
tls
.
Certificate
{
certificate
},
...
...
server_test.go
View file @
169328c4
...
@@ -18,6 +18,7 @@
...
@@ -18,6 +18,7 @@
package
getty
package
getty
import
(
import
(
"path/filepath"
"testing"
"testing"
"time"
"time"
)
)
...
@@ -80,14 +81,22 @@ func testTCPTlsServer(t *testing.T, address string) {
...
@@ -80,14 +81,22 @@ func testTCPTlsServer(t *testing.T, address string) {
server
*
server
server
*
server
serverMsgHandler
MessageHandler
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
()
{
func
()
{
server
=
newServer
(
server
=
newServer
(
TCP_SERVER
,
TCP_SERVER
,
WithLocalAddress
(
address
),
WithLocalAddress
(
address
),
WithServerSslEnabled
(
true
),
WithServerSslEnabled
(
true
),
WithServerKeyCertChainPath
(
`E:\Projects\openSource\dubbo-samples\java\dubbo-samples-ssl\dubbo-samples-ssl-provider\src\main\resources\certs\server0.pem`
),
WithServerTlsConfigBuilder
(
configBuilder
),
WithServerPrivateKeyPath
(
`E:\Projects\openSource\dubbo-samples\java\dubbo-samples-ssl\dubbo-samples-ssl-provider\src\main\resources\certs\server0.key`
),
)
)
newServerSession
:=
func
(
session
Session
)
error
{
newServerSession
:=
func
(
session
Session
)
error
{
return
newSessionCallback
(
session
,
&
serverMsgHandler
)
return
newSessionCallback
(
session
,
&
serverMsgHandler
)
...
@@ -101,10 +110,19 @@ func testTCPTlsServer(t *testing.T, address string) {
...
@@ -101,10 +110,19 @@ func testTCPTlsServer(t *testing.T, address string) {
addr
:=
server
.
streamListener
.
Addr
()
.
String
()
addr
:=
server
.
streamListener
.
Addr
()
.
String
()
t
.
Logf
(
"@address:%s, tcp server addr: %v"
,
address
,
addr
)
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
,
clt
:=
newClient
(
TCP_CLIENT
,
WithServerAddress
(
addr
),
WithServerAddress
(
addr
),
WithReconnectInterval
(
5e8
),
WithReconnectInterval
(
5e8
),
WithConnectionNumber
(
1
),
WithConnectionNumber
(
1
),
WithClientTlsConfigBuilder
(
clientConfig
),
)
)
assert
.
NotNil
(
t
,
clt
)
assert
.
NotNil
(
t
,
clt
)
assert
.
True
(
t
,
clt
.
ID
()
>
0
)
assert
.
True
(
t
,
clt
.
ID
()
>
0
)
...
@@ -155,16 +173,16 @@ func testUDPServer(t *testing.T, address string) {
...
@@ -155,16 +173,16 @@ func testUDPServer(t *testing.T, address string) {
func
TestServer
(
t
*
testing
.
T
)
{
func
TestServer
(
t
*
testing
.
T
)
{
var
addr
string
var
addr
string
//
testTCPServer(t, addr)
testTCPServer
(
t
,
addr
)
//
testUDPServer(t, addr)
testUDPServer
(
t
,
addr
)
//
//
addr = "127.0.0.1:0"
addr
=
"127.0.0.1:0"
//
testTCPServer(t, addr)
testTCPServer
(
t
,
addr
)
//
testUDPServer(t, addr)
testUDPServer
(
t
,
addr
)
//
//
addr = "127.0.0.1"
addr
=
"127.0.0.1"
//
testTCPServer(t, addr)
testTCPServer
(
t
,
addr
)
//
testUDPServer(t, addr)
testUDPServer
(
t
,
addr
)
addr
=
"127.0.0.9999"
addr
=
"127.0.0.9999"
testTCPTlsServer
(
t
,
addr
)
testTCPTlsServer
(
t
,
addr
)
}
}
tls.go
View file @
169328c4
...
@@ -47,12 +47,12 @@ func (s *ServerTlsConfigBuilder) BuildTlsConfig() (*tls.Config, error) {
...
@@ -47,12 +47,12 @@ func (s *ServerTlsConfigBuilder) BuildTlsConfig() (*tls.Config, error) {
config
*
tls
.
Config
config
*
tls
.
Config
)
)
if
certificate
,
err
=
tls
.
LoadX509KeyPair
(
s
.
ServerKeyCertChainPath
,
s
.
ServerPrivateKeyPath
);
err
!=
nil
{
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(cert
s
{%s}, privateKey{%s}) = err:%+v"
,
s
.
ServerKeyCertChainPath
,
s
.
ServerPrivateKeyPath
,
perrors
.
WithStack
(
err
)))
s
.
ServerKeyCertChainPath
,
s
.
ServerPrivateKeyPath
,
perrors
.
WithStack
(
err
)))
return
nil
,
err
return
nil
,
err
}
}
config
=
&
tls
.
Config
{
config
=
&
tls
.
Config
{
InsecureSkipVerify
:
true
,
// do not verify peer cert
InsecureSkipVerify
:
true
,
// do not verify peer cert
s
ClientAuth
:
tls
.
RequireAnyClientCert
,
ClientAuth
:
tls
.
RequireAnyClientCert
,
Certificates
:
[]
tls
.
Certificate
{
certificate
},
Certificates
:
[]
tls
.
Certificate
{
certificate
},
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment