Skip to content

Commit

Permalink
Support handshake data validator (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasChan1025 authored Dec 10, 2020
1 parent c56bea9 commit e51e7f3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
4 changes: 4 additions & 0 deletions cluster/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ func (h *LocalHandler) handle(conn net.Conn) {
func (h *LocalHandler) processPacket(agent *agent, p *packet.Packet) error {
switch p.Type {
case packet.Handshake:
if err := env.HandshakeValidator(p.Data); err != nil {
return err
}

if _, err := agent.conn.Write(hrd); err != nil {
return err
}
Expand Down
14 changes: 8 additions & 6 deletions internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ import (
)

var (
Wd string // working path
Die chan bool // wait for end application
Heartbeat time.Duration // Heartbeat internal
CheckOrigin func(*http.Request) bool // check origin when websocket enabled
Debug bool // enable Debug
WSPath string // WebSocket path(eg: ws://127.0.0.1/WSPath)
Wd string // working path
Die chan bool // wait for end application
Heartbeat time.Duration // Heartbeat internal
CheckOrigin func(*http.Request) bool // check origin when websocket enabled
Debug bool // enable Debug
WSPath string // WebSocket path(eg: ws://127.0.0.1/WSPath)
HandshakeValidator func([]byte) error // When you need to verify the custom data of the handshake request

// timerPrecision indicates the precision of timer, default is time.Second
TimerPrecision = time.Second
Expand All @@ -56,5 +57,6 @@ func init() {
Heartbeat = 30 * time.Second
Debug = false
CheckOrigin = func(_ *http.Request) bool { return true }
HandshakeValidator = func(_ []byte) error { return nil }
Serializer = protobuf.NewSerializer()
}
7 changes: 7 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,10 @@ func WithLogger(l log.Logger) Option {
log.SetLogger(l)
}
}

// WithHandshakeValidator sets the function that Verify `handshake` data
func WithHandshakeValidator(fn func([]byte) error) Option {
return func(opt *cluster.Options) {
env.HandshakeValidator = fn
}
}

0 comments on commit e51e7f3

Please sign in to comment.