mirror of
https://github.com/fhmq/hmq.git
synced 2026-08-30 14:24:53 +00:00
force handshark
This commit is contained in:
+54
-24
@@ -63,16 +63,16 @@ func (b *Broker) Start() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if b.config.Port != "" {
|
if b.config.Port != "" {
|
||||||
go b.StartListening(CLIENT)
|
go b.StartClientListening(false)
|
||||||
}
|
}
|
||||||
if b.config.Cluster.Port != "" {
|
if b.config.Cluster.Port != "" {
|
||||||
go b.StartListening(ROUTER)
|
go b.StartClusterListening()
|
||||||
}
|
}
|
||||||
if b.config.WsPort != "" {
|
if b.config.WsPort != "" {
|
||||||
go b.StartWebsocketListening()
|
go b.StartWebsocketListening()
|
||||||
}
|
}
|
||||||
if b.config.TlsPort != "" {
|
if b.config.TlsPort != "" {
|
||||||
go b.StartTLSListening()
|
go b.StartClientListening(true)
|
||||||
}
|
}
|
||||||
if len(b.config.Cluster.Routes) > 0 {
|
if len(b.config.Cluster.Routes) > 0 {
|
||||||
b.ConnectToRouters()
|
b.ConnectToRouters()
|
||||||
@@ -96,11 +96,17 @@ func (b *Broker) wsHandler(ws *websocket.Conn) {
|
|||||||
go b.handleConnection(CLIENT, ws, b.cid)
|
go b.handleConnection(CLIENT, ws, b.cid)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Broker) StartTLSListening() {
|
func (b *Broker) StartClientListening(tls bool) {
|
||||||
hp := b.config.TlsHost + ":" + b.config.TlsPort
|
var hp string
|
||||||
log.Info("Start TLS Listening client on ", hp)
|
if tls {
|
||||||
|
hp = b.config.TlsHost + ":" + b.config.TlsPort
|
||||||
|
log.Info("Start TLS Listening client on ", hp)
|
||||||
|
} else {
|
||||||
|
hp := b.config.Host + ":" + b.config.Port
|
||||||
|
log.Info("Start Listening client on ", hp)
|
||||||
|
}
|
||||||
|
|
||||||
l, e := tls.Listen("tcp", hp, b.tlsConfig)
|
l, e := net.Listen("tcp", hp)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
log.Error("Error listening on ", e)
|
log.Error("Error listening on ", e)
|
||||||
return
|
return
|
||||||
@@ -123,20 +129,49 @@ func (b *Broker) StartTLSListening() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
tmpDelay = ACCEPT_MIN_SLEEP
|
tmpDelay = ACCEPT_MIN_SLEEP
|
||||||
|
if tls {
|
||||||
|
if !b.Handshake(conn) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
atomic.AddUint64(&b.cid, 1)
|
atomic.AddUint64(&b.cid, 1)
|
||||||
go b.handleConnection(CLIENT, conn, b.cid)
|
go b.handleConnection(CLIENT, conn, b.cid)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Broker) StartListening(typ int) {
|
func (b *Broker) Handshake(conn net.Conn) bool {
|
||||||
var hp string
|
|
||||||
if typ == CLIENT {
|
nc := tls.Server(conn, b.tlsConfig)
|
||||||
hp = b.config.Host + ":" + b.config.Port
|
time.AfterFunc(DEFAULT_TLS_TIMEOUT, func() { TlsTimeout(nc) })
|
||||||
log.Info("Start Listening client on ", hp)
|
nc.SetReadDeadline(time.Now().Add(DEFAULT_TLS_TIMEOUT))
|
||||||
} else if typ == ROUTER {
|
|
||||||
hp = b.config.Cluster.Host + ":" + b.config.Cluster.Port
|
// Force handshake
|
||||||
log.Info("Start Listening cluster on ", hp)
|
if err := nc.Handshake(); err != nil {
|
||||||
|
log.Error("TLS handshake error, ", err)
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
nc.SetReadDeadline(time.Time{})
|
||||||
|
return true
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TlsTimeout(conn *tls.Conn) {
|
||||||
|
nc := conn
|
||||||
|
// Check if already closed
|
||||||
|
if nc == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cs := nc.ConnectionState()
|
||||||
|
if !cs.HandshakeComplete {
|
||||||
|
log.Error("TLS handshake timeout")
|
||||||
|
nc.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Broker) StartClusterListening() {
|
||||||
|
var hp string = b.config.Cluster.Host + ":" + b.config.Cluster.Port
|
||||||
|
log.Info("Start Listening cluster on ", hp)
|
||||||
|
|
||||||
l, e := net.Listen("tcp", hp)
|
l, e := net.Listen("tcp", hp)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
@@ -164,16 +199,11 @@ func (b *Broker) StartListening(typ int) {
|
|||||||
}
|
}
|
||||||
tmpDelay = ACCEPT_MIN_SLEEP
|
tmpDelay = ACCEPT_MIN_SLEEP
|
||||||
|
|
||||||
if typ == CLIENT {
|
go b.handleConnection(ROUTER, conn, idx)
|
||||||
atomic.AddUint64(&b.cid, 1)
|
if idx == 1 {
|
||||||
go b.handleConnection(typ, conn, b.cid)
|
idx = 0
|
||||||
} else {
|
} else {
|
||||||
go b.handleConnection(typ, conn, idx)
|
idx = idx + 1
|
||||||
if idx == 1 {
|
|
||||||
idx = 0
|
|
||||||
} else {
|
|
||||||
idx = idx + 1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user