chisel
chisel copied to clipboard
Be able to wait until client SSH tunnel is established
I would like to use chisel client and block or get notified when the SSH tunnel is ready to send/receive data.
I would like to be able to do something like this:
import (
"context"
"time"
chclient "github.com/jpillora/chisel/client"
)
func main(){
config := chclient.Config(
KeepAlive: time.Second,
MaxRetryInterval: time.Second,
Server: "my-chisel-server",
Remotes: []string{"1234:localhost:1234"}
}
established := make(chan bool)
c, err := chclient.NewClient(&config)
if err != nil {
log.Fatal(err)
}
go func() {
ctx, cancel = context.WithCancel(context.Background())
defer cancel()
if err := c.Start(ctx); err != nil {
log.Fatal(err)
}
go func(ctx context.Context) {
<-ctx.Done()
// connection failed
log.Fatal(ctx.Error())
}(ctx)
go func() {
// GetTunnelSSHConn bloquea hasta que está establecida la conexión
sshCon := c.GetTunnelSSHConn(ctx)
if (sshCon != nil) {
log.Printf("Connection established")
// NOW WE CAN SEND/RECEIVE DATA
}
}()
c.Wait() // <-- this waits until client is closed or context is cancelled
}()
// wait until connection established
if (<- established) {
// now I am sure connection has been established and can use tunnel
}
}
To be able to do this, we would need to expose the internal tunnel inside Client, and use the (private) function getSSH that blocks until connection is established.