-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
67 lines (49 loc) · 1.63 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package easyssh_test
import (
"fmt"
"io/ioutil"
"dev.justinjudd.org/justin/easyssh"
"golang.org/x/crypto/ssh"
)
func ExampleServer_ListenAndServe() {
s := easyssh.Server{Addr: ":2022"}
privateBytes, err := ioutil.ReadFile("id_rsa")
if err != nil {
// Failed to load private key (./id_rsa)
}
private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
// Failed to parse private 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)
s.Config = config
handler := easyssh.NewStandardSSHServerHandler()
channelHandler := easyssh.NewChannelsMux()
channelHandler.HandleChannel(easyssh.SessionRequest, easyssh.SessionHandler())
handler.MultipleChannelsHandler = channelHandler
s.Handler = handler
s.ListenAndServe()
}
func ExampleListenAndServe() {
config := &ssh.ServerConfig{}
easyssh.HandleChannel(easyssh.SessionRequest, easyssh.SessionHandler())
easyssh.HandleChannel(easyssh.DirectForwardRequest, easyssh.DirectPortForwardHandler())
easyssh.HandleRequestFunc(easyssh.RemoteForwardRequest, easyssh.TCPIPForwardRequest)
easyssh.ListenAndServe(":2022", config, nil)
}
func ExampleChannelsMux_HandleChannelFunc() {
handler := easyssh.NewChannelsMux()
testHandler := func(newChannel ssh.NewChannel, channel ssh.Channel, reqs <-chan *ssh.Request, sshConn ssh.Conn) {
defer channel.Close()
ssh.DiscardRequests(reqs)
}
handler.HandleChannelFunc("test", testHandler)
}