sijms / go-ora

Pure go oracle client
MIT License
767 stars 168 forks source link

Is it possible to run with a ssh dialer ? #520

Closed lundin closed 4 months ago

lundin commented 4 months ago

Hi,

Trying to run go-ora over ssh due to currently the VM i am running this go program from is having blocked outgoing traffic (on specific port 1521). However i am able to ssh into another machine that has the ability to connect to Oracle on 1521. Of course the best is the add new rules to the gateway/ip route tables but until then i would like to use the db connection via a ssh connection similar to: https://github.com/jfcote87/sshdb

I tried something below which i am unsure is even correct, however get a

ssh: tcpChan: deadline not supported

type myDialer struct {
    client *ssh.Client
}

func (d myDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {

    sshConfig := &ssh.ClientConfig{
        User:            "userid",
        Auth:            []ssh.AuthMethod{ssh.Password("password")},
        HostKeyCallback: ssh.InsecureIgnoreHostKey(),
    }

    Client, err := ssh.Dial("tcp", "IP:22", sshConfig)
    if err != nil {
        panic(err)
    }

    d.client = Client

    cli, err := d.client.DialContext(ctx, "tcp", "IP:22")
    if err != nil {
        panic(err)
    }

    return cli, err

}
`
    var newDialer go_network.DialerContext = myDialer{}
    newDialer.DialContext(context.Background(), "test", "test")

    oc := new(go_ora.OracleDriver)
    connector, err := oc.OpenConnector(connStr)
    if err != nil {
        fmt.Println(err)
        return
    }
    oConnector, ok := connector.(*go_ora.OracleConnector)
    if !ok {
        fmt.Println(err)
        return
    }

    oConnector.Dialer(newDialer)

    // Get a database handle.
    db := sql.OpenDB(oConnector)

    // Confirm a successful connection.
    if err := db.Ping(); err != nil {
        log.Fatal(err)
    }`
lundin commented 4 months ago

I managed to get it working with the sshdb tunnel pkg, i will do a pull request to him for adding go-ora as an oracle option :)

lundin commented 4 months ago

A pull request in that repo with go-ora has now been added https://github.com/jfcote87/sshdb/

useful for me in this situation, however should not be needed in normal cases.