watchtower/wtserver/server: use CheckRemoteInit to validate Init msgs
This commit is contained in:
parent
4c7111256e
commit
b3a9650ff9
@ -71,8 +71,7 @@ type Server struct {
|
|||||||
clientMtx sync.RWMutex
|
clientMtx sync.RWMutex
|
||||||
clients map[wtdb.SessionID]Peer
|
clients map[wtdb.SessionID]Peer
|
||||||
|
|
||||||
globalFeatures *lnwire.RawFeatureVector
|
localInit *wtwire.Init
|
||||||
connFeatures *lnwire.RawFeatureVector
|
|
||||||
|
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
quit chan struct{}
|
quit chan struct{}
|
||||||
@ -82,16 +81,16 @@ type Server struct {
|
|||||||
// clients connecting to the listener addresses, and allows them to open
|
// clients connecting to the listener addresses, and allows them to open
|
||||||
// sessions and send state updates.
|
// sessions and send state updates.
|
||||||
func New(cfg *Config) (*Server, error) {
|
func New(cfg *Config) (*Server, error) {
|
||||||
connFeatures := lnwire.NewRawFeatureVector(
|
localInit := wtwire.NewInitMessage(
|
||||||
wtwire.WtSessionsOptional,
|
lnwire.NewRawFeatureVector(wtwire.WtSessionsOptional),
|
||||||
|
cfg.ChainHash,
|
||||||
)
|
)
|
||||||
|
|
||||||
s := &Server{
|
s := &Server{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
clients: make(map[wtdb.SessionID]Peer),
|
clients: make(map[wtdb.SessionID]Peer),
|
||||||
globalFeatures: lnwire.NewRawFeatureVector(),
|
localInit: localInit,
|
||||||
connFeatures: connFeatures,
|
quit: make(chan struct{}),
|
||||||
quit: make(chan struct{}),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
connMgr, err := connmgr.New(&connmgr.Config{
|
connMgr, err := connmgr.New(&connmgr.Config{
|
||||||
@ -209,17 +208,14 @@ func (s *Server) handleClient(peer Peer) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
localInit := wtwire.NewInitMessage(
|
err = s.sendMessage(peer, s.localInit)
|
||||||
s.connFeatures, s.cfg.ChainHash,
|
|
||||||
)
|
|
||||||
|
|
||||||
err = s.sendMessage(peer, localInit)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Unable to send Init msg to %s: %v", id, err)
|
log.Errorf("Unable to send Init msg to %s: %v", id, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = s.handleInit(localInit, remoteInit); err != nil {
|
err = s.localInit.CheckRemoteInit(remoteInit, wtwire.FeatureNames)
|
||||||
|
if err != nil {
|
||||||
log.Errorf("Cannot support client %s: %v", id, err)
|
log.Errorf("Cannot support client %s: %v", id, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -297,27 +293,6 @@ func (s *Server) handleClient(peer Peer) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleInit accepts the local and remote Init messages, and verifies that the
|
|
||||||
// client is not requesting any required features that are unknown to the tower.
|
|
||||||
func (s *Server) handleInit(localInit, remoteInit *wtwire.Init) error {
|
|
||||||
if localInit.ChainHash != remoteInit.ChainHash {
|
|
||||||
return fmt.Errorf("Peer chain hash unknown: %x",
|
|
||||||
remoteInit.ChainHash)
|
|
||||||
}
|
|
||||||
|
|
||||||
remoteConnFeatures := lnwire.NewFeatureVector(
|
|
||||||
remoteInit.ConnFeatures, wtwire.FeatureNames,
|
|
||||||
)
|
|
||||||
|
|
||||||
unknownLocalFeatures := remoteConnFeatures.UnknownRequiredFeatures()
|
|
||||||
if len(unknownLocalFeatures) > 0 {
|
|
||||||
return fmt.Errorf("Peer set unknown local feature bits: %v",
|
|
||||||
unknownLocalFeatures)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// handleCreateSession processes a CreateSession message from the peer, and returns
|
// handleCreateSession processes a CreateSession message from the peer, and returns
|
||||||
// a CreateSessionReply in response. This method will only succeed if no existing
|
// a CreateSessionReply in response. This method will only succeed if no existing
|
||||||
// session info is known about the session id. If an existing session is found,
|
// session info is known about the session id. If an existing session is found,
|
||||||
|
Loading…
Reference in New Issue
Block a user