项目作者: wanyvic

项目描述 :
go-libp2p-ssh
高级语言: Go
项目地址: git://github.com/wanyvic/go-libp2p-ssh.git
创建时间: 2019-09-06T11:04:09Z
项目社区:https://github.com/wanyvic/go-libp2p-ssh

开源协议:MIT License

下载


go-libp2p-ssh

a implementation ssh client server for libp2p

server

server will check password used by reading /etc/shadow, please running with sudo!

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/libp2p/go-libp2p"
  6. lssh "github.com/wanyvic/go-libp2p-ssh"
  7. )
  8. func main() {
  9. listenAddrs := libp2p.ListenAddrStrings(
  10. "/ip4/0.0.0.0/tcp/9001",
  11. )
  12. host, err := libp2p.New(context.Background(), listenAddrs)
  13. if err != nil {
  14. fmt.Println(err)
  15. }
  16. fmt.Printf("Your PeerID is :%s\nListen:%s\n", host.ID().String(), host.Addrs())
  17. /*Your PeerID is :QmZ8zzzFhZAxWHzWecrj6x1r4UH9TnD35f6hBom3TbRGpu
  18. Listen:[/ip4/127.0.0.1/tcp/9000 /ip4/192.168.3.131/tcp/9000 /ip4/192.168.0.133/tcp/9000 /ip4/172.17.0.1/tcp/9000]*/
  19. lssh.NewSSHService(host)
  20. select {} //hold on
  21. }

client

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "time"
  8. "github.com/libp2p/go-libp2p"
  9. "github.com/libp2p/go-libp2p-core/peer"
  10. ma "github.com/multiformats/go-multiaddr"
  11. lssh "github.com/wanyvic/go-libp2p-ssh"
  12. "github.com/wanyvic/ssh"
  13. )
  14. func main() {
  15. host, err := libp2p.New(context.Background())
  16. if err != nil {
  17. fmt.Println(err)
  18. }
  19. fmt.Printf("Your PeerID is :%s\nListen:%s\n", host.ID().String(), host.Addrs())
  20. //pid: /ip4/127.0.0.1/tcp/9000/p2p/QmZ8zzzFhZAxWHzWecrj6x1r4UH9TnD35f6hBom3TbRGpu
  21. maddr, err := ma.NewMultiaddr(pid)
  22. if err != nil {
  23. fmt.Println(err)
  24. }
  25. peerinfo, _ := peer.AddrInfoFromP2pAddr(maddr)
  26. if err := host.Connect(context.Background(), *peerinfo); err != nil {
  27. fmt.Println(err)
  28. }
  29. //auth
  30. auth := make([]ssh.AuthMethod, 0)
  31. // password authentication
  32. auth = append(auth, ssh.Password("xxxx")) //your os password
  33. // public key authentication
  34. home := os.Getenv("HOME")
  35. privateBytes, err := ioutil.ReadFile(home + "/.ssh/id_rsa")
  36. if err != nil {
  37. fmt.Println(err)
  38. }
  39. Signer, err := ssh.ParsePrivateKey(privateBytes)
  40. if err != nil {
  41. fmt.Println(err)
  42. }
  43. auth = append(auth, ssh.PublicKeys(Signer))
  44. //create clientConfig
  45. clientConfig := &ssh.ClientConfig{
  46. User: "wany", // username which you want to login with
  47. Auth: auth,
  48. Timeout: 30 * time.Second,
  49. HostKeyCallback: func(hostname string, remote ma.Multiaddr, key ssh.PublicKey) error {
  50. return nil
  51. },
  52. }
  53. clients := lssh.NewSSHClientWithConfig(host, *clientConfig)
  54. //bind reader writer
  55. clients.Stdout = os.Stdout
  56. clients.Stderr = os.Stderr
  57. clients.Stdin = os.Stdin
  58. clients.Connect(peerinfo.ID)
  59. }

more GoDoc