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

How to customize the handshake authentication process #314

Open
haoyu234 opened this issue Jul 10, 2023 · 1 comment
Open

How to customize the handshake authentication process #314

haoyu234 opened this issue Jul 10, 2023 · 1 comment

Comments

@haoyu234
Copy link
Contributor

When we adapt the protobuf serialization format, we find that the handshake authentication process is fixed, how do I customize the process and change the json serialization to protobuf? We use different packet and route formats, so we want to be able to customize the entire HandlerService process

pitaya/service/handler.go

Lines 212 to 252 in 04e8c01

func (h *HandlerService) processPacket(a agent.Agent, p *packet.Packet) error {
switch p.Type {
case packet.Handshake:
logger.Log.Debug("Received handshake packet")
// Parse the json sent with the handshake by the client
handshakeData := &session.HandshakeData{}
if err := json.Unmarshal(p.Data, handshakeData); err != nil {
logger.Log.Errorf("Failed to unmarshal handshake data: %s", err.Error())
if serr := a.SendHandshakeErrorResponse(); serr != nil {
logger.Log.Errorf("Error sending handshake error response: %s", err.Error())
return err
}
return fmt.Errorf("invalid handshake data. Id=%d", a.GetSession().ID())
}
if err := a.GetSession().ValidateHandshake(handshakeData); err != nil {
logger.Log.Errorf("Handshake validation failed: %s", err.Error())
if serr := a.SendHandshakeErrorResponse(); serr != nil {
logger.Log.Errorf("Error sending handshake error response: %s", err.Error())
return err
}
return fmt.Errorf("handshake validation failed: %w. SessionId=%d", err, a.GetSession().ID())
}
if err := a.SendHandshakeResponse(); err != nil {
logger.Log.Errorf("Error sending handshake response: %s", err.Error())
return err
}
logger.Log.Debugf("Session handshake Id=%d, Remote=%s", a.GetSession().ID(), a.RemoteAddr())
a.GetSession().SetHandshakeData(handshakeData)
a.SetStatus(constants.StatusHandshake)
err := a.GetSession().Set(constants.IPVersionKey, a.IPVersion())
if err != nil {
logger.Log.Warnf("failed to save ip version on session: %q\n", err)
}
logger.Log.Debug("Successfully saved handshake data")

@sandy1219
Copy link

func (pool *sessionPoolImpl) AddHandshakeValidator(name string, f func(data *HandshakeData) error) {
pool.handshakeValidators[name] = f
}
builder.SessionPool.AddHandshakeValidator("versionValidator", func(data *session.HandshakeData) error {
if data.Sys.Version != "1.0.0" {
return errors.New("client version error")
}
return nil
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants