diff options
author | Duco van Amstel <duco.vanamstel@gmail.com> | 2018-12-26 15:09:36 +0100 |
---|---|---|
committer | Wim <wim@42.be> | 2018-12-26 15:09:36 +0100 |
commit | 1b834c6858d6fdb8927f6a75fad2116d6d2bd394 (patch) | |
tree | c65300ae97ff6d708a5552a2804407231fe61fbc /bridge | |
parent | d82726cd1bafc4adbf4fd55b0200b853d5633e6d (diff) | |
download | matterbridge-msglm-1b834c6858d6fdb8927f6a75fad2116d6d2bd394.tar.gz matterbridge-msglm-1b834c6858d6fdb8927f6a75fad2116d6d2bd394.tar.bz2 matterbridge-msglm-1b834c6858d6fdb8927f6a75fad2116d6d2bd394.zip |
Fix sshchat connection logic (#661)
Diffstat (limited to 'bridge')
-rw-r--r-- | bridge/sshchat/sshchat.go | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/bridge/sshchat/sshchat.go b/bridge/sshchat/sshchat.go index 79ba9fb5..fc3145e6 100644 --- a/bridge/sshchat/sshchat.go +++ b/bridge/sshchat/sshchat.go @@ -23,22 +23,35 @@ func New(cfg *bridge.Config) bridge.Bridger { } func (b *Bsshchat) Connect() error { - var err error b.Log.Infof("Connecting %s", b.GetString("Server")) + + // 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.Scan() + b.w = w + if _, err := b.w.Write([]byte("/theme mono\r\n")); err != nil { + return err + } + close(connSignal) // Connection is established so we can signal the success. + return b.handleSSHChat() + } + go func() { - err = sshd.ConnectShell(b.GetString("Server"), b.GetString("Nick"), func(r io.Reader, w io.WriteCloser) error { - b.r = bufio.NewScanner(r) - b.w = w - b.r.Scan() - if _, handleErr := w.Write([]byte("/theme mono\r\n")); handleErr != nil { - return handleErr - } - return b.handleSSHChat() - }) + // 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) }() - if err != nil { - b.Log.Debugf("%#v", err) + + select { + case err := <-connErr: + b.Log.Error("Connection failed") return err + case <-connSignal: } b.Log.Info("Connection succeeded") return nil @@ -64,7 +77,9 @@ func (b *Bsshchat) Send(msg config.Message) (string, error) { b.Log.Errorf("Could not send extra message: %#v", err) } } - return b.handleUploadFile(&msg) + if len(msg.Extra["file"]) > 0 { + return b.handleUploadFile(&msg) + } } _, err := b.w.Write([]byte(msg.Username + msg.Text + "\r\n")) return "", err |