add example
This commit is contained in:
parent
d29a484fe3
commit
5b98dd6f4d
2 changed files with 57 additions and 1 deletions
|
|
@ -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
|
||||
|
|
|
|||
56
ssh/sshpiper_test.go
Normal file
56
ssh/sshpiper_test.go
Normal file
|
|
@ -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()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue