example_test.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package agent_test
  5. import (
  6. "log"
  7. "net"
  8. "os"
  9. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/crypto/ssh"
  10. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/crypto/ssh/agent"
  11. )
  12. func ExampleNewClient() {
  13. // ssh-agent(1) provides a UNIX socket at $SSH_AUTH_SOCK.
  14. socket := os.Getenv("SSH_AUTH_SOCK")
  15. conn, err := net.Dial("unix", socket)
  16. if err != nil {
  17. log.Fatalf("Failed to open SSH_AUTH_SOCK: %v", err)
  18. }
  19. agentClient := agent.NewClient(conn)
  20. config := &ssh.ClientConfig{
  21. User: "gopher",
  22. Auth: []ssh.AuthMethod{
  23. // Use a callback rather than PublicKeys so we only consult the
  24. // agent once the remote server wants it.
  25. ssh.PublicKeysCallback(agentClient.Signers),
  26. },
  27. HostKeyCallback: ssh.InsecureIgnoreHostKey(),
  28. }
  29. sshc, err := ssh.Dial("tcp", "localhost:22", config)
  30. if err != nil {
  31. log.Fatal(err)
  32. }
  33. // Use sshc...
  34. sshc.Close()
  35. }