5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-22 17:30:26 +00:00

Fix sshchat connection logic (#661)

This commit is contained in:
Duco van Amstel 2018-12-26 15:09:36 +01:00 committed by Wim
parent d82726cd1b
commit 1b834c6858

View File

@ -23,23 +23,36 @@ func New(cfg *bridge.Config) bridge.Bridger {
} }
func (b *Bsshchat) Connect() error { func (b *Bsshchat) Connect() error {
var err error
b.Log.Infof("Connecting %s", b.GetString("Server")) b.Log.Infof("Connecting %s", b.GetString("Server"))
go func() {
err = sshd.ConnectShell(b.GetString("Server"), b.GetString("Nick"), func(r io.Reader, w io.WriteCloser) error { // connHandler will be called by 'sshd.ConnectShell()' below
// once the connection is established in order to handle it.
connErr := make(chan error, 1) // Needs to be buffered.
connSignal := make(chan struct{})
connHandler := func(r io.Reader, w io.WriteCloser) error {
b.r = bufio.NewScanner(r) b.r = bufio.NewScanner(r)
b.w = w
b.r.Scan() b.r.Scan()
if _, handleErr := w.Write([]byte("/theme mono\r\n")); handleErr != nil { b.w = w
return handleErr if _, err := b.w.Write([]byte("/theme mono\r\n")); err != nil {
}
return b.handleSSHChat()
})
}()
if err != nil {
b.Log.Debugf("%#v", err)
return err return err
} }
close(connSignal) // Connection is established so we can signal the success.
return b.handleSSHChat()
}
go func() {
// As a successful connection will result in this returning after the Connection
// method has already returned point we NEED to have a buffered channel to still
// be able to write.
connErr <- sshd.ConnectShell(b.GetString("Server"), b.GetString("Nick"), connHandler)
}()
select {
case err := <-connErr:
b.Log.Error("Connection failed")
return err
case <-connSignal:
}
b.Log.Info("Connection succeeded") b.Log.Info("Connection succeeded")
return nil return nil
} }
@ -64,8 +77,10 @@ func (b *Bsshchat) Send(msg config.Message) (string, error) {
b.Log.Errorf("Could not send extra message: %#v", err) b.Log.Errorf("Could not send extra message: %#v", err)
} }
} }
if len(msg.Extra["file"]) > 0 {
return b.handleUploadFile(&msg) return b.handleUploadFile(&msg)
} }
}
_, err := b.w.Write([]byte(msg.Username + msg.Text + "\r\n")) _, err := b.w.Write([]byte(msg.Username + msg.Text + "\r\n"))
return "", err return "", err
} }