2022-03-25 22:35:36 +00:00
|
|
|
package ssh_test
|
|
|
|
|
|
|
|
import (
|
2022-04-21 00:36:19 +00:00
|
|
|
"errors"
|
2022-03-25 22:35:36 +00:00
|
|
|
"io"
|
2022-09-15 12:06:59 +00:00
|
|
|
"os"
|
2022-03-25 22:35:36 +00:00
|
|
|
|
|
|
|
"tailscale.com/tempfork/gliderlabs/ssh"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ExampleListenAndServe() {
|
|
|
|
ssh.ListenAndServe(":2222", func(s ssh.Session) {
|
|
|
|
io.WriteString(s, "Hello world\n")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExamplePasswordAuth() {
|
|
|
|
ssh.ListenAndServe(":2222", nil,
|
|
|
|
ssh.PasswordAuth(func(ctx ssh.Context, pass string) bool {
|
|
|
|
return pass == "secret"
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleNoPty() {
|
|
|
|
ssh.ListenAndServe(":2222", nil, ssh.NoPty())
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExamplePublicKeyAuth() {
|
|
|
|
ssh.ListenAndServe(":2222", nil,
|
2022-04-21 00:36:19 +00:00
|
|
|
ssh.PublicKeyAuth(func(ctx ssh.Context, key ssh.PublicKey) error {
|
2022-09-15 12:06:59 +00:00
|
|
|
data, err := os.ReadFile("/path/to/allowed/key.pub")
|
2022-04-21 00:36:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
allowed, _, _, _, err := ssh.ParseAuthorizedKey(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !ssh.KeysEqual(key, allowed) {
|
|
|
|
return errors.New("some error")
|
|
|
|
}
|
|
|
|
return nil
|
2022-03-25 22:35:36 +00:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleHostKeyFile() {
|
|
|
|
ssh.ListenAndServe(":2222", nil, ssh.HostKeyFile("/path/to/host/key"))
|
|
|
|
}
|