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
92f8562c
Commit
92f8562c
authored
Apr 21, 2017
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix wss client bug
parent
75b0c121
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
71 additions
and
46 deletions
+71
-46
change_log.md
change_log.md
+6
-0
client.go
client.go
+26
-11
server.go
server.go
+25
-24
session.go
session.go
+11
-8
version.go
version.go
+3
-3
No files found.
change_log.md
View file @
92f8562c
...
...
@@ -11,6 +11,12 @@
## develop history ##
---
-
2017/04/21
> 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
> version: 0.7.02
-
2017/02/08
> improvement
>
...
...
client.go
View file @
92f8562c
...
...
@@ -173,6 +173,8 @@ func (c *Client) dialWSS() Session {
var
(
err
error
certPem
[]
byte
root
*
x509
.
Certificate
roots
[]
*
x509
.
Certificate
certPool
*
x509
.
CertPool
config
*
tls
.
Config
dialer
websocket
.
Dialer
...
...
@@ -186,26 +188,36 @@ func (c *Client) dialWSS() Session {
InsecureSkipVerify
:
true
,
}
if
c
.
cert
!=
""
&&
c
.
privateKey
!=
""
{
config
.
Certificates
=
make
([]
tls
.
Certificate
,
1
)
if
config
.
Certificates
[
0
],
err
=
tls
.
LoadX509KeyPair
(
c
.
cert
,
c
.
privateKey
);
err
!=
nil
{
panic
(
fmt
.
Sprintf
(
"tls.LoadX509KeyPair(cert{%s}, privateKey{%s}) = err{%#v}"
,
c
.
cert
,
c
.
privateKey
,
err
))
}
}
certPool
=
x509
.
NewCertPool
()
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
"
,
err
))
}
for
_
,
root
=
range
roots
{
certPool
.
AddCert
(
root
)
}
}
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
))
}
certPool
=
x509
.
NewCertPool
()
if
ok
:=
certPool
.
AppendCertsFromPEM
(
certPem
);
!
ok
{
panic
(
"failed to parse root certificate file."
)
}
config
.
RootCAs
=
certPool
config
.
InsecureSkipVerify
=
false
}
if
c
.
cert
!=
""
&&
c
.
privateKey
!=
""
{
config
.
Certificates
=
make
([]
tls
.
Certificate
,
1
)
if
config
.
Certificates
[
0
],
err
=
tls
.
LoadX509KeyPair
(
c
.
cert
,
c
.
privateKey
);
err
!=
nil
{
panic
(
fmt
.
Sprintf
(
"tls.LoadX509KeyPair(cert{%s}, privateKey{%s}) = err{%#v}"
,
c
.
cert
,
c
.
privateKey
,
err
))
}
}
config
.
RootCAs
=
certPool
// dialer.EnableCompression = true
dialer
.
TLSClientConfig
=
config
...
...
@@ -222,6 +234,7 @@ func (c *Client) dialWSS() Session {
if
ss
.
(
*
session
)
.
maxMsgLen
>
0
{
conn
.
SetReadLimit
(
int64
(
ss
.
(
*
session
)
.
maxMsgLen
))
}
ss
.
SetName
(
defaultWSSSessionName
)
return
ss
}
...
...
@@ -233,10 +246,12 @@ func (c *Client) dialWSS() Session {
}
func
(
c
*
Client
)
dial
()
Session
{
if
strings
.
HasPrefix
(
c
.
addr
,
"wss"
)
{
return
c
.
dialWSS
()
}
if
strings
.
HasPrefix
(
c
.
addr
,
"ws"
)
{
return
c
.
dialWS
()
}
else
if
strings
.
HasPrefix
(
c
.
addr
,
"wss"
)
{
return
c
.
dialWSS
()
}
return
c
.
dialTCP
()
...
...
server.go
View file @
92f8562c
...
...
@@ -10,7 +10,7 @@
package
getty
import
(
//
"context"
"context"
"crypto/tls"
"crypto/x509"
"errors"
...
...
@@ -23,7 +23,6 @@ import (
)
import
(
"github.com/AlexStocks/goext/log"
"github.com/AlexStocks/goext/net"
"github.com/AlexStocks/goext/sync"
"github.com/AlexStocks/goext/time"
...
...
@@ -54,8 +53,8 @@ func NewServer() *Server {
func
(
s
*
Server
)
stop
()
{
var
(
//
err error
//
ctx context.Context
err
error
ctx
context
.
Context
)
select
{
case
<-
s
.
done
:
...
...
@@ -65,12 +64,12 @@ func (s *Server) stop() {
close
(
s
.
done
)
s
.
lock
.
Lock
()
if
s
.
server
!=
nil
{
//
ctx, _ = context.WithTimeout(context.Background(), serverFastFailTimeout)
//
if err = s.server.Shutdown(ctx); err != nil {
//
// 如果下面内容输出为:server shutdown ctx: context deadline exceeded,
//
// 则说明有未处理完的active connections。
//
log.Error("server shutdown ctx:%#v", err)
//
}
ctx
,
_
=
context
.
WithTimeout
(
context
.
Background
(),
serverFastFailTimeout
)
if
err
=
s
.
server
.
Shutdown
(
ctx
);
err
!=
nil
{
// 如果下面内容输出为:server shutdown ctx: context deadline exceeded,
// 则说明有未处理完的active connections。
log
.
Error
(
"server shutdown ctx:%#v"
,
err
)
}
}
s
.
lock
.
Unlock
()
// 把listener.Close放在这里,既能防止多次关闭调用,
...
...
@@ -252,30 +251,32 @@ func (s *Server) RunWSSEventLoop(
path
string
,
cert
string
,
privateKey
string
,
caCert
string
)
{
caCert
string
,
)
{
s
.
wg
.
Add
(
1
)
go
func
()
{
defer
s
.
wg
.
Done
()
var
(
err
error
certPem
[]
byte
certPool
*
x509
.
CertPool
config
*
tls
.
Config
handler
*
wsHandler
server
*
http
.
Server
err
error
certPem
[]
byte
certificate
tls
.
Certificate
certPool
*
x509
.
CertPool
config
*
tls
.
Config
handler
*
wsHandler
server
*
http
.
Server
)
config
=
&
tls
.
Config
{
InsecureSkipVerify
:
true
,
ClientAuth
:
tls
.
NoClientCert
,
}
config
.
Certificates
=
make
([]
tls
.
Certificate
,
1
)
gxlog
.
CInfo
(
"server cert:%s, key:%s, caCert:%s"
,
cert
,
privateKey
,
caCert
)
if
config
.
Certificates
[
0
],
err
=
tls
.
LoadX509KeyPair
(
cert
,
privateKey
);
err
!=
nil
{
if
certificate
,
err
=
tls
.
LoadX509KeyPair
(
cert
,
privateKey
);
err
!=
nil
{
panic
(
fmt
.
Sprintf
(
"tls.LoadX509KeyPair(cert{%s}, privateKey{%s}) = err{%#v}"
,
cert
,
privateKey
,
err
))
return
}
config
=
&
tls
.
Config
{
InsecureSkipVerify
:
true
,
// 不对对端的证书进行校验
ClientAuth
:
tls
.
NoClientCert
,
NextProtos
:
[]
string
{
"http/1.1"
},
Certificates
:
[]
tls
.
Certificate
{
certificate
},
}
if
caCert
!=
""
{
certPem
,
err
=
ioutil
.
ReadFile
(
caCert
)
...
...
session.go
View file @
92f8562c
...
...
@@ -28,12 +28,15 @@ import (
)
const
(
maxReadBufLen
=
4
*
1024
netIOTimeout
=
1e9
// 1s
period
=
60
*
1e9
// 1 minute
pendingDuration
=
3e9
defaultSessionName
=
"session"
outputFormat
=
"session %s, Read Count: %d, Write Count: %d, Read Pkg Count: %d, Write Pkg Count: %d"
maxReadBufLen
=
4
*
1024
netIOTimeout
=
1e9
// 1s
period
=
60
*
1e9
// 1 minute
pendingDuration
=
3e9
defaultSessionName
=
"session"
defaultTCPSessionName
=
"tcp-session"
defaultWSSessionName
=
"ws-session"
defaultWSSSessionName
=
"wss-session"
outputFormat
=
"session %s, Read Count: %d, Write Count: %d, Read Pkg Count: %d, Write Pkg Count: %d"
)
/////////////////////////////////////////
...
...
@@ -121,7 +124,7 @@ func NewSession() Session {
func
NewTCPSession
(
conn
net
.
Conn
)
Session
{
session
:=
&
session
{
name
:
defaultSessionName
,
name
:
default
TCP
SessionName
,
Connection
:
newGettyTCPConn
(
conn
),
done
:
make
(
chan
gxsync
.
Empty
),
period
:
period
,
...
...
@@ -137,7 +140,7 @@ func NewTCPSession(conn net.Conn) Session {
func
NewWSSession
(
conn
*
websocket
.
Conn
)
Session
{
session
:=
&
session
{
name
:
defaultSessionName
,
name
:
default
WS
SessionName
,
Connection
:
newGettyWSConn
(
conn
),
done
:
make
(
chan
gxsync
.
Empty
),
period
:
period
,
...
...
version.go
View file @
92f8562c
...
...
@@ -10,9 +10,9 @@
package
getty
const
(
Version
=
"0.7.0
1
"
DATE
=
"2017/0
2/08
"
Version
=
"0.7.0
2
"
DATE
=
"2017/0
4/21
"
GETTY_MAJOR
=
0
GETTY_MINOR
=
7
GETTY_BUILD
=
1
GETTY_BUILD
=
2
)
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