-
-
Save pigscanflyyyy/83b62e24ead747e7de112a13dcf855dc to your computer and use it in GitHub Desktop.
golang dynamic port forward ssh socks5 tunnel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"net" | |
"os" | |
"os/signal" | |
"syscall" | |
"github.com/armon/go-socks5" | |
"golang.org/x/crypto/ssh" | |
) | |
func main() { | |
sshAddress := "10.0.0.11:22" | |
socks5Address := "localhost:1080" // open socks5 connection | |
sshConf := &ssh.ClientConfig{ | |
User: "root", | |
Auth: []ssh.AuthMethod{ssh.Password("SSH_PASSWORD_HERE")}, | |
HostKeyCallback: ssh.InsecureIgnoreHostKey(), | |
} | |
sshConn, err := ssh.Dial("tcp", sshAddress, sshConf) | |
if err != nil { | |
fmt.Println("error tunnel to server: ", err) | |
return | |
} | |
defer sshConn.Close() | |
fmt.Println("connected to ssh server") | |
go func() { | |
conf := &socks5.Config{ | |
Dial: func(ctx context.Context, network, addr string) (net.Conn, error) { | |
return sshConn.Dial(network, addr) | |
}, | |
} | |
serverSocks, err := socks5.New(conf) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
if err := serverSocks.ListenAndServe("tcp", socks5Address); err != nil { | |
fmt.Println("failed to create socks5 server", err) | |
} | |
}() | |
ch := make(chan os.Signal) | |
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) | |
<-ch | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment