watchtower/wtserver: add DeleteSession handler

This commit is contained in:
Conner Fromknecht 2019-03-19 19:38:46 -07:00
parent 8e4a20e1a5
commit 26adf735c4
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7
2 changed files with 64 additions and 0 deletions

@ -0,0 +1,57 @@
package wtserver
import (
"github.com/lightningnetwork/lnd/watchtower/wtdb"
"github.com/lightningnetwork/lnd/watchtower/wtwire"
)
// handleDeleteSession processes a DeleteSession request for a client with given
// SessionID. The id is assumed to have been previously authenticated by the
// brontide connection.
func (s *Server) handleDeleteSession(peer Peer, id *wtdb.SessionID) error {
var failCode wtwire.DeleteSessionCode
// Delete all session data associated with id.
err := s.cfg.DB.DeleteSession(*id)
switch {
case err == nil:
failCode = wtwire.CodeOK
log.Debugf("Session %s deleted", id)
case err == wtdb.ErrSessionNotFound:
failCode = wtwire.DeleteSessionCodeNotFound
default:
failCode = wtwire.CodeTemporaryFailure
}
return s.replyDeleteSession(peer, id, failCode)
}
// replyDeleteSession sends a DeleteSessionReply back to the peer containing the
// error code resulting from processes a DeleteSession request.
func (s *Server) replyDeleteSession(peer Peer, id *wtdb.SessionID,
code wtwire.DeleteSessionCode) error {
msg := &wtwire.DeleteSessionReply{
Code: code,
}
err := s.sendMessage(peer, msg)
if err != nil {
log.Errorf("Unable to send DeleteSessionReply to %s", id)
}
// Return the write error if the request succeeded.
if code == wtwire.CodeOK {
return err
}
// Otherwise the request failed, return a connection failure to
// disconnect the client.
return &connFailure{
ID: *id,
Code: uint16(code),
}
}

@ -259,6 +259,13 @@ func (s *Server) handleClient(peer Peer) {
"from %s: %v", id, err) "from %s: %v", id, err)
} }
case *wtwire.DeleteSession:
err = s.handleDeleteSession(peer, &id)
if err != nil {
log.Errorf("Unable to handle DeleteSession "+
"from %s: %v", id, err)
}
case *wtwire.StateUpdate: case *wtwire.StateUpdate:
err = s.handleStateUpdates(peer, &id, msg) err = s.handleStateUpdates(peer, &id, msg)
if err != nil { if err != nil {