Home > Software design >  Connection fails (timeout) to SFTP server with correct private key (Golang)
Connection fails (timeout) to SFTP server with correct private key (Golang)

Time:10-18

I'm trying to write an sftp client in Golang. The owners of the server require connection using public/private keys. I have already sent them a public key (4096 bit) and got a username. To test the connection I user FileZilla with my private key - it worked fine. But my golang client does not work: ssh.Dial hangs forever or just aborts on ClientConfig timeout... Does anyone has any ideas what can be wrong with it (with my code or around it)? My code is bellow:

keyFile := filepath.Join(dir, "id_rsa")
pkBytes, err := os.ReadFile(keyFile)
if err != nil {
    return err
}
signer, err := ssh.ParsePrivateKeyWithPassphrase(pkBytes, []byte("mysecret"))
if err != nil {
    return err
}

username := "my_username"
sftpDir := "/home/upload/"

keyFile = filepath.Join(dir, "id_rsa.pub")
pkBytes, err = os.ReadFile(keyFile)
if err != nil {
    return err
}

hostKey, _, _, _, err := ssh.ParseAuthorizedKey(pkBytes)
if err != nil {
    return err
}

clientConfig := &ssh.ClientConfig{
    User:            username,
    Auth:            []ssh.AuthMethod{ssh.PublicKeys(signer)},
    HostKeyCallback: ssh.FixedHostKey(hostKey),
    Timeout:         10 * time.Second,
}

conn, err := ssh.Dial("tcp", "sftp.trustyou.com:22", clientConfig)
// here we stop or hang forever.... :-(
if err != nil {
    log.Fatalf("SSH DIAL FAILED:%v", err)
}
defer conn.Close()

CodePudding user response:

I can finally answer my own question: the code is Ok. The problem happens only on our customer' server which is located behind a firewall. So it is network settings that are responsible for this. To make sure I changed the ClientConfig settings to ssh.InsecureIgnoreHostKey and ran it on my office computer. It connected successfully.

clientConfig := &ssh.ClientConfig{
    User:            h.UserName,
    Auth:            []ssh.AuthMethod{ssh.PublicKeys(t.signer)},
    HostKeyCallback: ssh.InsecureIgnoreHostKey(), // ssh.FixedHostKey(hostKey),
    Timeout:         15 * time.Second,
}

P.S. I know that my answer (and the question itself) now looks stupid comparing to some really creative issues here... Sorry about that. But this is how it works sometimes...

CodePudding user response:

package main

import (
    "github.com/alessiosavi/GoGPUtils/helper"
    stringutils "github.com/alessiosavi/GoGPUtils/string"
    "github.com/alessiosavi/GoSFTPtoS3"
    "io/ioutil"
    "log"
    "strings"
)

func main() {
    log.SetFlags(log.LstdFlags | log.Lshortfile | log.Lmicroseconds)

    var sftpConf = &GoSFTPtoS3.SFTPConf{
        Host:     "localhost", // FIXME: Change host and user
        User:     "alessiosavi",
        Password: "",
        Port:     22,
        Bucket:   "bucket-ftp",
        Timeout:  50,
        PrivKey:  "",
    }
    // FIXME: Use your key
    file, err := ioutil.ReadFile("/home/alessiosavi/.ssh/mykey.pem")
    if err != nil {
        panic(err)
    }
    sftpConf.PrivKey = string(file)

    conn, err := sftpConf.NewConn()
    defer conn.Close()
    list, err := conn.List("/tmp")
    if err != nil {
        panic(err)
    }

    log.Println(helper.MarshalIndent(list))

}
  • Related