Home > Backend Development > Golang > How to Create a Simple SSH Port Forward Using Golang?

How to Create a Simple SSH Port Forward Using Golang?

DDD
Release: 2024-11-04 09:01:02
Original
290 people have browsed it

How to Create a Simple SSH Port Forward Using Golang?

Simple SSH Port Forwarding in Golang

Problem:

Programmatically creating a basic TCP port forward over SSH in Golang for users familiar with Ruby.

Solution:

To tackle this challenge, it's essential to understand the crucial steps involved:

  1. Establishing a local listener (net.Listener) to accept connections on the local port.
  2. Upon a connection attempt, accepting it and acquiring local io.Reader and io.Writer objects.
  3. Establishing a remote connection to the server and remote port, obtaining its io.Reader and io.Writer objects.
  4. Continuously transferring bytes bidirectionally between the local and remote io.Readers and io.Writers.

Code:

<code class="go">package main

import (
    "io"
    "log"
    "net"
    "golang.org/x/crypto/ssh"
)

// Configuration parameters for SSH connection
var (
    username         = "root"
    password         = "password"
    serverAddrString = "192.168.1.100:22"
    localAddrString  = "localhost:9000"
    remoteAddrString = "localhost:9999"
)

func forward(localConn net.Conn, config *ssh.ClientConfig) {
    // Establish SSH connection to remote server
    sshClientConn, err := ssh.Dial("tcp", serverAddrString, config)
    if err != nil {
        log.Fatalf("ssh.Dial failed: %s", err)
    }

    // Connect to remote port
    sshConn, err := sshClientConn.Dial("tcp", remoteAddrString)
    if err != nil {
        log.Fatalf("sshConn.Dial failed: %s", err)
    }

    // Create goroutines for bidirectional data transfer
    go func() {
        _, err = io.Copy(sshConn, localConn)
        if err != nil {
            log.Fatalf("io.Copy failed: %v", err)
        }

    }()

    go func() {
        _, err = io.Copy(localConn, sshConn)
        if err != nil {
            log.Fatalf("io.Copy failed: %v", err)
        }

    }()
}

func main() {
    // Configure SSH client
    config := &ssh.ClientConfig{
        User: username,
        Auth: []ssh.AuthMethod{
            ssh.Password(password),
        },
    }

    // Create local listener
    localListener, err := net.Listen("tcp", localAddrString)
    if err != nil {
        log.Fatalf("net.Listen failed: %v", err)
    }

    // Monitor listener for new connections
    for {
        localConn, err := localListener.Accept()
        if err != nil {
            log.Fatalf("localListener.Accept failed: %v", err)
        }

        go forward(localConn, config)
    }
}</code>
Copy after login

The above is the detailed content of How to Create a Simple SSH Port Forward Using Golang?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template