Skip to content

Instantly share code, notes, and snippets.

@kevinpostal
Created November 22, 2016 07:30
Show Gist options
  • Save kevinpostal/0228941c2b594504ed1f39914a3bf1ca to your computer and use it in GitHub Desktop.
Save kevinpostal/0228941c2b594504ed1f39914a3bf1ca to your computer and use it in GitHub Desktop.
SSH Remote Port Fowarding
package main
import (
"log"
"dev.justinjudd.org/justin/easyssh"
"golang.org/x/crypto/ssh"
)
func main() {
config := &ssh.ClientConfig{
User: "test",
Auth: []ssh.AuthMethod{
ssh.Password("test"),
},
}
hostString := "ip:port"
conn, err := easyssh.Dial("tcp", hostString, config)
if err != nil {
log.Fatalf("unable to connect: %s", err)
}
defer conn.Close()
err = conn.RemoteForward("0.0.0.0:8000", "localhost:80")
if err != nil {
log.Fatalf("unable to forward local port: %s", err)
}
}
package main
import (
"fmt"
"net"
"dev.justinjudd.org/justin/easyssh"
"golang.org/x/crypto/ssh"
)
var sshListener net.Listener
func main() {
host_string := "0.0.0.0:6667"
privateBytes := []byte(`-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA3iFZY9GjMkGznXCGIutlgFJxfaq2qmx5vW7Hld7mT8Bxn5GP
TX1oe/AZbfGovYTxD8i0hew2j3qmG2ZkgWPOWVZd+Ky7gZe5pJylTL79YpYs/3Xq
xtuNpwtcdqd4QHuon01WODFq4RQhFwq/s3tLjNBuWgQm/6uiDrgw3aUJPqwXX8cr
lBQRPX5CdtkbGEHp1cUWz9EF35Hwu9FQ+Ki3snspf8Dz3Nmsr+8COlMp3CWrGCt8
gG7IQH1YP6CNz4xp5OUEm6VYhtVoY90N4My6ZFR1KFL/vgHlFx8dp/OugQZEqZTU
g8ExDmuNtE8nGUMExHgASz7UIR+90kOBx7rnpQIDAQABAoIBAQCV5+OvnZaJIBXb
NkkP39SMGRdGO6MyuCogUVaYKek8AR+tFEqlfPC6U3qV2/SIkeMtR8Gsb5ESmy7m
Zt+FApoLaGHapJeWCZLbKGvVsz7E/Zw4epeKxDiAdwZbfQGGYu2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OVsLFtqOOBYJgUlm+tzbiIFaXzY26j8Q7A==
-----END RSA PRIVATE KEY-----\n`)
private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
fmt.Println("Error with Key")
}
config := &ssh.ServerConfig{
PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
if c.User() == "test" && string(pass) == "test" {
return nil, nil
}
return nil, fmt.Errorf("password rejected for %s", c.User())
},
}
config.AddHostKey(private)
easyssh.HandleChannel(easyssh.SessionRequest, easyssh.SessionHandler())
easyssh.HandleChannel(easyssh.DirectForwardRequest, easyssh.DirectPortForwardHandler())
easyssh.HandleRequestFunc(easyssh.RemoteForwardRequest, easyssh.TCPIPForwardRequest)
//easyssh.ListenAndServe(":2022", config, nil)
sshListener, err := net.Listen("tcp", host_string)
if err != nil {
fmt.Println("SSH Error")
return
}
easyssh.Serve(sshListener,config, nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment