Reverse SSH closes due to inactivity

Hi,

i am trying to connect a reverse ssh tunnel via go. This works so far. But the tunnel will be closed automatically after some time. As I already tried it (see code) it does not work. Is there anything that works the same way as ServerAliveInterval under the console? Does anyone know advice? Thanks!

‘’’
sshConfig := &ssh.ClientConfig{

    // SSH connection username

    User: "remoteuser",

    Auth: []ssh.AuthMethod{

        // put here your private key path

        publicKeyFile("C:/Users/user/.ssh/id_rsa"),

    },

    HostKeyCallback: ssh.InsecureIgnoreHostKey(),

    Timeout:         15 * time.Second,

}

// Connect to SSH remote server using serverEndpoint

serverConn, err := ssh.Dial("tcp", serverEndpoint.String(), sshConfig)

if err != nil {

    log.Fatalln(fmt.Printf("Dial INTO remote server error: %s", err))

}

// Listen on remote server port

listener, err := serverConn.Listen("tcp", remoteEndpoint.String())

if err != nil {

    log.Fatalln(fmt.Printf("Listen open port ON remote server error: %s", err))

}

defer listener.Close()

// handle incoming connections on reverse forwarded tunnel

for {

    // Open a (local) connection to localEndpoint whose content will be forwarded so serverEndpoint

    local, err := net.Dial("tcp", localEndpoint.String())

    if err != nil {

        log.Fatalln(fmt.Printf("Dial INTO local service error: %s", err))

    }       

    

    client, err := listener.Accept()

    if err != nil {

        log.Fatalln(err)

    }

    _, _, err = serverConn.SendRequest("keepalive", true, []byte("sup"))

    ticker := time.NewTicker(45 * time.Second)

    quit := make(chan struct{})

    go func() {

        for {

            select {

                case <- ticker.C:

                    _, _, err = serverConn.SendRequest("keepalive", true, []byte("ls"))

                    fmt.Println("WhatsUp, "+user.Name+"?")

                case <- quit:

                    ticker.Stop()

                return

    }

}

}()

    fmt.Println("OK")

    handleClient(client, local)

}

‘’’

package main

import (
	"log"
	"net"
	"net/http"
	"time"

	"golang.org/x/crypto/ssh"
)

func main() {
	client, err := ssh.Dial("tcp", "192.168.75.4:8089", &ssh.ClientConfig{
		User: "root",
		Auth: []ssh.AuthMethod{
			ssh.Password(""),
		},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
	})
	if err != nil {
		log.Panicln(err)
		return
	}

	ln, err := client.ListenTCP(&net.TCPAddr{
		IP:   net.ParseIP("0.0.0.0"),
		Port: 9091,
	})
	if err != nil {
		log.Panicln(err)
		return
	}
	defer ln.Close()

	go http.Serve(ln, http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
		resp.Header().Add("Message", "hello ssh")
	}))
	http.DefaultClient.Timeout = 3 * time.Second
	resp, err := http.Get("http://192.168.75.4:9091/ssh/forward-tcp")
	if err != nil {
		log.Println("direct read response error:", err)
		return
	}
	log.Printf("direct request http status: %s,headers: %v\n", resp.Status, resp.Header)
}

last is my forward test,neetd client send keepalive request.

github example keepalive

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.