From 5b98dd6f4db8c8abce1e95bfa79bc4a68324b51a Mon Sep 17 00:00:00 2001 From: tgic Date: Wed, 17 Dec 2014 16:59:55 +0800 Subject: [PATCH] add example --- ssh/doc.go | 2 +- ssh/sshpiper_test.go | 56 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 ssh/sshpiper_test.go diff --git a/ssh/doc.go b/ssh/doc.go index de143f80..405449c6 100644 --- a/ssh/doc.go +++ b/ssh/doc.go @@ -6,7 +6,7 @@ Package ssh in sshpiper is compatible with golang.org/x/crypto/ssh. All func and datatype left unchanged. You can use it like golang.org/x/crypto/ssh. -Also, sshpiper provide additional APIs that allows build a piped ssh connection. +Also, sshpiper provide additional APIs that allows you to build a piped ssh connection. SSH is a transport security protocol, an authentication protocol and a family of application protocols. The most typical application level diff --git a/ssh/sshpiper_test.go b/ssh/sshpiper_test.go new file mode 100644 index 00000000..3b0ba9b7 --- /dev/null +++ b/ssh/sshpiper_test.go @@ -0,0 +1,56 @@ +package ssh + +import ( + "io/ioutil" + "net" +) + +func ExampleNewSSHPiperConn() { + + // upstream addr + const serverAddr = "127.0.0.1:22" + + piper := &SSHPiperConfig{ + // return conn dial to serverAddr + FindUpstream: func(conn ConnMetadata) (net.Conn, error) { + c, err := net.Dial("tcp", serverAddr) + if err != nil { + return nil, err + } + + return c, nil + }, + } + + // add private key + privateBytes, err := ioutil.ReadFile("id_rsa") + if err != nil { + panic("Failed to load private key") + } + + private, err := ParsePrivateKey(privateBytes) + if err != nil { + panic("Failed to parse private key") + } + + piper.AddHostKey(private) + + // serve at a address + listener, err := net.Listen("tcp", "0.0.0.0:2022") + if err != nil { + panic("failed to listen for connection") + } + nConn, err := listener.Accept() + if err != nil { + panic("failed to accept incoming connection") + } + + // accept nConn and build a SSHPipe + p, err := NewSSHPiperConn(nConn, piper) + if err != nil { + panic("failed to establish piped connection") + } + + // wait util either side shutdown + p.Wait() +}