I created a remote tunnel that connects to my server and forwards port 8080 to my local web server. However, it only forward localhost, or 127.0.0.1, but doesn’t work using the server’s IP address. Any ideas why myserver’s IP address doesn’t work, but localhost does? Here are some screen shots:
Connecting using localhost on the server gets forwarded to myclient just fine.
smith@myserver:~$ curl localhost:8080
Hello world!
smith@myserver:~$ curl 127.0.0.1:8080
Hello world!
smith@myserver:~$
Connecting using myserver IP address on myserver or on myclient both fail.
smith@myserver:~$ curl 192.168.1.10:8080
curl: (7) Failed to connect to 192.168.1.10 port 8080 after 0 ms: Connection refused
smith@myclient:~$ curl 192.168.1.10:8080
curl: (7) Failed to connect to 192.168.1.10 port 8080 after 0 ms: Connection refused
Here’s my code:
package main
import (
"fmt"
"log"
"net/http"
"os"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/knownhosts"
)
func main() {
key, err := os.ReadFile("/home/smith/.ssh/id_rsa")
if err != nil {
log.Fatalf("unable to read SSH private key: %v", err)
}
// Create the Signer for this private key.
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
log.Fatalf("unable to parse SSH private key: %v", err)
}
// Configure known hosts
hostKeyCallback, err := knownhosts.New("known_hosts")
if err != nil {
log.Fatal(err)
}
// Create SSH client config using SSH keys
config := &ssh.ClientConfig{
User: "smith",
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: hostKeyCallback,
}
conn, err := ssh.Dial("tcp", "192.168.1.10:22", config) // myserver
if err != nil {
log.Fatal("Failed to dial: ", err)
}
defer conn.Close()
// Request the remote side to open port 8080 on all interfaces.
l, err := conn.Listen("tcp", "192.168.1.10:8080")
if err != nil {
log.Fatal("unable to register tcp forward: ", err)
}
defer l.Close()
// Serve HTTP with your SSH server acting as a reverse proxy.
http.Serve(l, http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
fmt.Fprintf(resp, "Hello world!n")
}))
}