Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support handshake data validator #69

Merged
merged 5 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}